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