作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

91 lines
2.4 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. TableBuilder* builder = new TableBuilder(options, file);
  26. meta->smallest.DecodeFrom(iter->key());
  27. Slice key=iter->key();
  28. // 小合并时检查ttl
  29. time_t now = time(nullptr); // 获得当前时间
  30. uint64_t now_time=static_cast<uint64_t>(now);
  31. for (; iter->Valid(); iter->Next()) {
  32. uint64_t ttl=*(uint64_t*)(iter->value().data()+iter->value().size()-sizeof(uint64_t)); // 将 TTL 从 new_data 的末尾取出
  33. // 如果 TTL 超过当前时间,说明数据已经过期
  34. if(ttl < static_cast<uint64_t>(now_time)){
  35. continue;
  36. }
  37. key = iter->key();
  38. builder->Add(key, iter->value());
  39. }
  40. if (!key.empty()) {
  41. meta->largest.DecodeFrom(key);
  42. }
  43. // Finish and check for builder errors
  44. s = builder->Finish();
  45. if (s.ok()) {
  46. meta->file_size = builder->FileSize();
  47. assert(meta->file_size > 0);
  48. }
  49. delete builder;
  50. // Finish and check for file errors
  51. if (s.ok()) {
  52. s = file->Sync();
  53. }
  54. if (s.ok()) {
  55. s = file->Close();
  56. }
  57. delete file;
  58. file = nullptr;
  59. if (s.ok()) {
  60. // Verify that the table is usable
  61. Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,
  62. meta->file_size);
  63. s = it->status();
  64. delete it;
  65. }
  66. }
  67. // Check for input iterator errors
  68. if (!iter->status().ok()) {
  69. s = iter->status();
  70. }
  71. if (s.ok() && meta->file_size > 0) {
  72. // Keep it
  73. } else {
  74. env->RemoveFile(fname);
  75. }
  76. return s;
  77. }
  78. } // namespace leveldb