作者: 韩晨旭 10225101440 李畅 10225102463
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

118 рядки
3.3 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 SetNextFile(uint64_t num) {
  34. has_next_file_number_ = true;
  35. next_file_number_ = num;
  36. }
  37. void SetLastSequence(SequenceNumber seq) {
  38. has_last_sequence_ = true;
  39. last_sequence_ = seq;
  40. }
  41. void SetCompactPointer(int level, const InternalKey& key) {
  42. compact_pointers_.push_back(std::make_pair(level, key));
  43. }
  44. // Add the specified file at the specified number.
  45. // REQUIRES: This version has not been saved (see VersionSet::SaveTo)
  46. // REQUIRES: "smallest" and "largest" are smallest and largest keys in file
  47. void AddFile(int level, uint64_t file,
  48. uint64_t file_size,
  49. const InternalKey& smallest,
  50. const InternalKey& largest) {
  51. FileMetaData f;
  52. f.number = file;
  53. f.file_size = file_size;
  54. f.smallest = smallest;
  55. f.largest = largest;
  56. new_files_.push_back(std::make_pair(level, f));
  57. }
  58. // Delete the specified "file" from the specified "level".
  59. void DeleteFile(int level, uint64_t file) {
  60. deleted_files_.insert(std::make_pair(level, file));
  61. }
  62. // Record that a large value with the specified large_ref was
  63. // written to the output file numbered "fnum"
  64. void AddLargeValueRef(const LargeValueRef& large_ref,
  65. uint64_t fnum,
  66. const Slice& internal_key) {
  67. large_refs_added_.resize(large_refs_added_.size() + 1);
  68. Large* large = &(large_refs_added_.back());
  69. large->large_ref = large_ref;
  70. large->fnum = fnum;
  71. large->internal_key.DecodeFrom(internal_key);
  72. }
  73. void EncodeTo(std::string* dst) const;
  74. Status DecodeFrom(const Slice& src);
  75. std::string DebugString() const;
  76. private:
  77. friend class VersionSet;
  78. typedef std::set< std::pair<int, uint64_t> > DeletedFileSet;
  79. std::string comparator_;
  80. uint64_t log_number_;
  81. uint64_t next_file_number_;
  82. SequenceNumber last_sequence_;
  83. bool has_comparator_;
  84. bool has_log_number_;
  85. bool has_next_file_number_;
  86. bool has_last_sequence_;
  87. std::vector< std::pair<int, InternalKey> > compact_pointers_;
  88. DeletedFileSet deleted_files_;
  89. std::vector< std::pair<int, FileMetaData> > new_files_;
  90. struct Large {
  91. LargeValueRef large_ref;
  92. uint64_t fnum;
  93. InternalKey internal_key;
  94. };
  95. std::vector<Large> large_refs_added_;
  96. };
  97. }
  98. #endif // STORAGE_LEVELDB_DB_VERSION_EDIT_H_