LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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 regels
3.4 KiB

1 maand geleden
1 maand geleden
1 maand geleden
1 maand geleden
  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. FileMetaData() : refs(0), allowed_seeks(1 << 30), file_size(0),oldest_ts(UINT64_MAX),newer_ts(0) {}
  14. int refs;
  15. int allowed_seeks; // Seeks allowed until compaction
  16. uint64_t number;
  17. uint64_t file_size; // File size in bytes
  18. InternalKey smallest; // Smallest internal key served by table
  19. InternalKey largest; // Largest internal key served by table
  20. TIMESTAMP oldest_ts;
  21. TIMESTAMP newer_ts;
  22. };
  23. class VersionEdit {
  24. public:
  25. VersionEdit() { Clear(); }
  26. ~VersionEdit() = default;
  27. void Clear();
  28. void SetComparatorName(const Slice& name) {
  29. has_comparator_ = true;
  30. comparator_ = name.ToString();
  31. }
  32. void SetLogNumber(uint64_t num) {
  33. has_log_number_ = true;
  34. log_number_ = num;
  35. }
  36. void SetPrevLogNumber(uint64_t num) {
  37. has_prev_log_number_ = true;
  38. prev_log_number_ = num;
  39. }
  40. void SetNextFile(uint64_t num) {
  41. has_next_file_number_ = true;
  42. next_file_number_ = num;
  43. }
  44. void SetLastSequence(SequenceNumber seq) {
  45. has_last_sequence_ = true;
  46. last_sequence_ = seq;
  47. }
  48. void SetCompactPointer(int level, const InternalKey& key) {
  49. compact_pointers_.push_back(std::make_pair(level, key));
  50. }
  51. // Add the specified file at the specified number.
  52. // REQUIRES: This version has not been saved (see VersionSet::SaveTo)
  53. // REQUIRES: "smallest" and "largest" are smallest and largest keys in file
  54. void AddFile(int level, uint64_t file, uint64_t file_size,
  55. const InternalKey& smallest, const InternalKey& largest) {
  56. FileMetaData f;
  57. f.number = file;
  58. f.file_size = file_size;
  59. f.smallest = smallest;
  60. f.largest = largest;
  61. new_files_.push_back(std::make_pair(level, f));
  62. }
  63. void AddFile(int level, uint64_t file, uint64_t file_size,
  64. const InternalKey& smallest, const InternalKey& largest,const TIMESTAMP old_ts,const TIMESTAMP new_ts) {
  65. FileMetaData f;
  66. f.number = file;
  67. f.file_size = file_size;
  68. f.smallest = smallest;
  69. f.largest = largest;
  70. f.newer_ts = new_ts;
  71. f.oldest_ts = old_ts;
  72. new_files_.push_back(std::make_pair(level, f));
  73. }
  74. // Delete the specified "file" from the specified "level".
  75. void RemoveFile(int level, uint64_t file) {
  76. deleted_files_.insert(std::make_pair(level, file));
  77. }
  78. void EncodeTo(std::string* dst) const;
  79. Status DecodeFrom(const Slice& src);
  80. std::string DebugString() const;
  81. private:
  82. friend class VersionSet;
  83. typedef std::set<std::pair<int, uint64_t>> DeletedFileSet;
  84. std::string comparator_;
  85. uint64_t log_number_;
  86. uint64_t prev_log_number_;
  87. uint64_t next_file_number_;
  88. SequenceNumber last_sequence_;
  89. bool has_comparator_;
  90. bool has_log_number_;
  91. bool has_prev_log_number_;
  92. bool has_next_file_number_;
  93. bool has_last_sequence_;
  94. std::vector<std::pair<int, InternalKey>> compact_pointers_;
  95. DeletedFileSet deleted_files_;
  96. std::vector<std::pair<int, FileMetaData>> new_files_;
  97. };
  98. } // namespace leveldb
  99. #endif // STORAGE_LEVELDB_DB_VERSION_EDIT_H_