作者: 谢瑞阳 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.

124 line
3.4 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. #ifndef STORAGE_LEVELDB_DB_VERSION_EDIT_H_
  5. #define STORAGE_LEVELDB_DB_VERSION_EDIT_H_
  6. #include <set>
  7. #include <utility>
  8. #include <vector>
  9. #include "db/dbformat.h"
  10. namespace leveldb {
  11. class VersionSet;
  12. struct FileMetaData {
  13. int refs;
  14. uint64_t number;
  15. uint64_t file_size; // File size in bytes
  16. InternalKey smallest; // Smallest internal key served by table
  17. InternalKey largest; // Largest internal key served by table
  18. FileMetaData() : refs(0), file_size(0) { }
  19. };
  20. class VersionEdit {
  21. public:
  22. VersionEdit() { Clear(); }
  23. ~VersionEdit() { }
  24. void Clear();
  25. void SetComparatorName(const Slice& name) {
  26. has_comparator_ = true;
  27. comparator_ = name.ToString();
  28. }
  29. void SetLogNumber(uint64_t num) {
  30. has_log_number_ = true;
  31. log_number_ = num;
  32. }
  33. void SetPrevLogNumber(uint64_t num) {
  34. has_prev_log_number_ = true;
  35. prev_log_number_ = num;
  36. }
  37. void SetNextFile(uint64_t num) {
  38. has_next_file_number_ = true;
  39. next_file_number_ = num;
  40. }
  41. void SetLastSequence(SequenceNumber seq) {
  42. has_last_sequence_ = true;
  43. last_sequence_ = seq;
  44. }
  45. void SetCompactPointer(int level, const InternalKey& key) {
  46. compact_pointers_.push_back(std::make_pair(level, key));
  47. }
  48. // Add the specified file at the specified number.
  49. // REQUIRES: This version has not been saved (see VersionSet::SaveTo)
  50. // REQUIRES: "smallest" and "largest" are smallest and largest keys in file
  51. void AddFile(int level, uint64_t file,
  52. uint64_t file_size,
  53. const InternalKey& smallest,
  54. const InternalKey& largest) {
  55. FileMetaData f;
  56. f.number = file;
  57. f.file_size = file_size;
  58. f.smallest = smallest;
  59. f.largest = largest;
  60. new_files_.push_back(std::make_pair(level, f));
  61. }
  62. // Delete the specified "file" from the specified "level".
  63. void DeleteFile(int level, uint64_t file) {
  64. deleted_files_.insert(std::make_pair(level, file));
  65. }
  66. // Record that a large value with the specified large_ref was
  67. // written to the output file numbered "fnum"
  68. void AddLargeValueRef(const LargeValueRef& large_ref,
  69. uint64_t fnum,
  70. const Slice& internal_key) {
  71. large_refs_added_.resize(large_refs_added_.size() + 1);
  72. Large* large = &(large_refs_added_.back());
  73. large->large_ref = large_ref;
  74. large->fnum = fnum;
  75. large->internal_key.DecodeFrom(internal_key);
  76. }
  77. void EncodeTo(std::string* dst) const;
  78. Status DecodeFrom(const Slice& src);
  79. std::string DebugString() const;
  80. private:
  81. friend class VersionSet;
  82. typedef std::set< std::pair<int, uint64_t> > DeletedFileSet;
  83. std::string comparator_;
  84. uint64_t log_number_;
  85. uint64_t prev_log_number_;
  86. uint64_t next_file_number_;
  87. SequenceNumber last_sequence_;
  88. bool has_comparator_;
  89. bool has_log_number_;
  90. bool has_prev_log_number_;
  91. bool has_next_file_number_;
  92. bool has_last_sequence_;
  93. std::vector< std::pair<int, InternalKey> > compact_pointers_;
  94. DeletedFileSet deleted_files_;
  95. std::vector< std::pair<int, FileMetaData> > new_files_;
  96. struct Large {
  97. LargeValueRef large_ref;
  98. uint64_t fnum;
  99. InternalKey internal_key;
  100. };
  101. std::vector<Large> large_refs_added_;
  102. };
  103. }
  104. #endif // STORAGE_LEVELDB_DB_VERSION_EDIT_H_