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