blob: eaef77b327c64a3756a5f2512bc1786530907cbd [file] [log] [blame]
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +00001// 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
13namespace leveldb {
14
15class VersionSet;
16
17struct FileMetaData {
18 int refs;
gabor@google.comccf0fcd2011-06-22 02:36:45 +000019 int allowed_seeks; // Seeks allowed until compaction
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000020 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
gabor@google.comccf0fcd2011-06-22 02:36:45 +000025 FileMetaData() : refs(0), allowed_seeks(1 << 30), file_size(0) { }
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000026};
27
28class VersionEdit {
29 public:
30 VersionEdit() { Clear(); }
31 ~VersionEdit() { }
32
33 void Clear();
34
35 void SetComparatorName(const Slice& name) {
36 has_comparator_ = true;
37 comparator_ = name.ToString();
38 }
39 void SetLogNumber(uint64_t num) {
40 has_log_number_ = true;
41 log_number_ = num;
42 }
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +000043 void SetPrevLogNumber(uint64_t num) {
44 has_prev_log_number_ = true;
45 prev_log_number_ = num;
46 }
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000047 void SetNextFile(uint64_t num) {
48 has_next_file_number_ = true;
49 next_file_number_ = num;
50 }
51 void SetLastSequence(SequenceNumber seq) {
52 has_last_sequence_ = true;
53 last_sequence_ = seq;
54 }
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
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".
75 void DeleteFile(int level, uint64_t file) {
76 deleted_files_.insert(std::make_pair(level, file));
77 }
78
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000079 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_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +000091 uint64_t prev_log_number_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000092 uint64_t next_file_number_;
93 SequenceNumber last_sequence_;
94 bool has_comparator_;
95 bool has_log_number_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +000096 bool has_prev_log_number_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000097 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_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000103};
104
Hans Wennborg36a5f8e2011-10-31 17:22:06 +0000105} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000106
107#endif // STORAGE_LEVELDB_DB_VERSION_EDIT_H_