小组成员:姚凯文(kevinyao0901),姜嘉琪
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

79 wiersze
1.9 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "db/builder.h"
  5. #include "db/dbformat.h"
  6. #include "db/filename.h"
  7. #include "db/table_cache.h"
  8. #include "db/version_edit.h"
  9. #include "leveldb/db.h"
  10. #include "leveldb/env.h"
  11. #include "leveldb/iterator.h"
  12. namespace leveldb {
  13. Status BuildTable(const std::string& dbname, Env* env, const Options& options,
  14. TableCache* table_cache, Iterator* iter, FileMetaData* meta) {
  15. Status s;
  16. meta->file_size = 0;
  17. iter->SeekToFirst();
  18. std::string fname = TableFileName(dbname, meta->number);
  19. if (iter->Valid()) {
  20. WritableFile* file;
  21. s = env->NewWritableFile(fname, &file);
  22. if (!s.ok()) {
  23. return s;
  24. }
  25. TableBuilder* builder = new TableBuilder(options, file);
  26. meta->smallest.DecodeFrom(iter->key());
  27. for (; iter->Valid(); iter->Next()) {
  28. Slice key = iter->key();
  29. meta->largest.DecodeFrom(key);
  30. builder->Add(key, iter->value());
  31. }
  32. // Finish and check for builder errors
  33. s = builder->Finish();
  34. if (s.ok()) {
  35. meta->file_size = builder->FileSize();
  36. assert(meta->file_size > 0);
  37. }
  38. delete builder;
  39. // Finish and check for file errors
  40. if (s.ok()) {
  41. s = file->Sync();
  42. }
  43. if (s.ok()) {
  44. s = file->Close();
  45. }
  46. delete file;
  47. file = nullptr;
  48. if (s.ok()) {
  49. // Verify that the table is usable
  50. Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,
  51. meta->file_size);
  52. s = it->status();
  53. delete it;
  54. }
  55. }
  56. // Check for input iterator errors
  57. if (!iter->status().ok()) {
  58. s = iter->status();
  59. }
  60. if (s.ok() && meta->file_size > 0) {
  61. // Keep it
  62. } else {
  63. env->DeleteFile(fname);
  64. }
  65. return s;
  66. }
  67. } // namespace leveldb