作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.3 KiB

Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
5 years ago
  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. #include "table/vtable_builder.h"
  13. namespace leveldb {
  14. Status BuildTable(const std::string& dbname, Env* env, const Options& options,
  15. TableCache* table_cache, Iterator* iter, FileMetaData* meta) {
  16. Status s;
  17. meta->file_size = 0;
  18. iter->SeekToFirst();
  19. std::string fname = TableFileName(dbname, meta->number);
  20. std::string vtb_name = VTableFileName(dbname, meta->number);
  21. if (iter->Valid()) {
  22. WritableFile* file;
  23. s = env->NewWritableFile(fname, &file);
  24. if (!s.ok()) {
  25. return s;
  26. }
  27. WritableFile* vtb_file;
  28. s = env->NewWritableFile(vtb_name, &vtb_file);
  29. if (!s.ok()) {
  30. return s;
  31. }
  32. TableBuilder* builder = new TableBuilder(options, file);
  33. VTableBuilder* vtb_builder = new VTableBuilder(options, vtb_file);
  34. meta->smallest.DecodeFrom(iter->key());
  35. Slice key;
  36. for (; iter->Valid(); iter->Next()) {
  37. key = iter->key();
  38. Slice value = iter->value();
  39. if (value.size() < options.kv_sep_size) {
  40. // No need to separate key and value
  41. builder->Add(key, value);
  42. }
  43. else {
  44. // Separate key value
  45. ParsedInternalKey parsed;
  46. if (!ParseInternalKey(key, &parsed)) {
  47. s = Status::Corruption("Fatal. Memtable Key Error");
  48. builder->Abandon();
  49. vtb_builder->Abandon();
  50. return s;
  51. }
  52. value.remove_prefix(1);
  53. VTableRecord record {parsed.user_key, value};
  54. VTableHandle handle;
  55. VTableIndex index;
  56. std::string value_index;
  57. vtb_builder->Add(record, &handle);
  58. index.file_number = meta->number;
  59. index.vtable_handle = handle;
  60. index.Encode(&value_index);
  61. builder->Add(key, Slice(value_index));
  62. }
  63. }
  64. if (!key.empty()) {
  65. meta->largest.DecodeFrom(key);
  66. }
  67. // Finish and check for builder errors
  68. s = builder->Finish();
  69. if (s.ok()) {
  70. meta->file_size = builder->FileSize();
  71. assert(meta->file_size > 0);
  72. }
  73. delete builder;
  74. // Finish and check for file errors
  75. if (s.ok()) {
  76. s = file->Sync();
  77. }
  78. if (s.ok()) {
  79. s = file->Close();
  80. }
  81. delete file;
  82. file = nullptr;
  83. if (s.ok()) {
  84. s = vtb_builder->Finish();
  85. }
  86. delete vtb_builder;
  87. if (s.ok()) {
  88. s = vtb_file->Sync();
  89. }
  90. if (s.ok()) {
  91. s = vtb_file->Close();
  92. }
  93. delete vtb_file;
  94. vtb_file = nullptr;
  95. if (s.ok()) {
  96. // Verify that the table is usable
  97. Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,
  98. meta->file_size);
  99. s = it->status();
  100. delete it;
  101. }
  102. }
  103. // Check for input iterator errors
  104. if (!iter->status().ok()) {
  105. s = iter->status();
  106. }
  107. if (s.ok() && meta->file_size > 0) {
  108. // Keep it
  109. } else {
  110. env->RemoveFile(fname);
  111. }
  112. return s;
  113. }
  114. } // namespace leveldb