10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

102 line
2.8 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 年之前
  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. time_t nowTime;
  26. time(&nowTime);
  27. assert(nowTime > 0);
  28. TableBuilder* builder = new TableBuilder(options, file);
  29. meta->smallest.DecodeFrom(iter->key());
  30. Slice key;
  31. ParsedInternalKey parsed;
  32. meta->smallest_deadtime = UINT64_MAX;
  33. meta->largest_deadtime = 0;
  34. for (; iter->Valid(); iter->Next()) {
  35. key = iter->key();
  36. builder->Add(key, iter->value());
  37. //在构建sstable文件的时候,记录当前文件的生存期最大和最小值,
  38. //这里要注意internalkey和metadata中对于没有生存期的表示的转换
  39. ParseInternalKey(key,&parsed);
  40. if(parsed.deadTime == 0) parsed.deadTime = UINT64_MAX;
  41. if(parsed.deadTime < nowTime) {
  42. static int count = 0;
  43. if(count % 1000 == 0) {
  44. std::cout<<"count "<<count++<<" drop dead in L0: "<<parsed.user_key.ToString()<<" "<<parsed.deadTime<<std::endl;
  45. }
  46. continue;
  47. }
  48. meta->smallest_deadtime = std::min(meta->smallest_deadtime,parsed.deadTime);
  49. meta->largest_deadtime = std::max(meta->largest_deadtime,parsed.deadTime);
  50. }
  51. if (!key.empty()) {
  52. meta->largest.DecodeFrom(key);
  53. }
  54. // Finish and check for builder errors
  55. s = builder->Finish();
  56. if (s.ok()) {
  57. meta->file_size = builder->FileSize();
  58. assert(meta->file_size > 0);
  59. }
  60. delete builder;
  61. // Finish and check for file errors
  62. if (s.ok()) {
  63. s = file->Sync();
  64. }
  65. if (s.ok()) {
  66. s = file->Close();
  67. }
  68. delete file;
  69. file = nullptr;
  70. if (s.ok()) {
  71. // Verify that the table is usable
  72. Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,
  73. meta->file_size);
  74. s = it->status();
  75. delete it;
  76. }
  77. }
  78. // Check for input iterator errors
  79. if (!iter->status().ok()) {
  80. s = iter->status();
  81. }
  82. if (s.ok() && meta->file_size > 0) {
  83. // Keep it
  84. } else {
  85. env->RemoveFile(fname);
  86. }
  87. return s;
  88. }
  89. } // namespace leveldb