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.

105 lines
3.0 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
4 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. 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 % 10 == 0) {
  44. std::cout<<"count "<<count++<<" drop dead in L0: "<<parsed.user_key.ToString()<<" "<<parsed.deadTime<<std::endl;
  45. }
  46. continue;
  47. }
  48. if(parsed.user_key.ToString() == "16636") {
  49. printf("specific insert %s deadtime %lu file num %lu\n",parsed.user_key.ToString().c_str(),parsed.deadTime,meta->number);
  50. }
  51. meta->smallest_deadtime = std::min(meta->smallest_deadtime,parsed.deadTime);
  52. meta->largest_deadtime = std::max(meta->largest_deadtime,parsed.deadTime);
  53. }
  54. if (!key.empty()) {
  55. meta->largest.DecodeFrom(key);
  56. }
  57. // Finish and check for builder errors
  58. s = builder->Finish();
  59. if (s.ok()) {
  60. meta->file_size = builder->FileSize();
  61. assert(meta->file_size > 0);
  62. }
  63. delete builder;
  64. // Finish and check for file errors
  65. if (s.ok()) {
  66. s = file->Sync();
  67. }
  68. if (s.ok()) {
  69. s = file->Close();
  70. }
  71. delete file;
  72. file = nullptr;
  73. if (s.ok()) {
  74. // Verify that the table is usable
  75. Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,
  76. meta->file_size);
  77. s = it->status();
  78. delete it;
  79. }
  80. }
  81. // Check for input iterator errors
  82. if (!iter->status().ok()) {
  83. s = iter->status();
  84. }
  85. if (s.ok() && meta->file_size > 0) {
  86. // Keep it
  87. } else {
  88. env->RemoveFile(fname);
  89. }
  90. return s;
  91. }
  92. } // namespace leveldb