blob: 9d084fdb7d00574c627291b1a48e059a8093d5b4 [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// The representation of a DBImpl consists of a set of Versions. The
6// newest version is called "current". Older versions may be kept
7// around to provide a consistent view to live iterators.
8//
9// Each Version keeps track of a set of Table files per level. The
10// entire set of versions is maintained in a VersionSet.
11//
12// Version,VersionSet are thread-compatible, but require external
13// synchronization on all accesses.
14
15#ifndef STORAGE_LEVELDB_DB_VERSION_SET_H_
16#define STORAGE_LEVELDB_DB_VERSION_SET_H_
17
18#include <map>
19#include <set>
20#include <vector>
21#include "db/dbformat.h"
22#include "db/version_edit.h"
23#include "port/port.h"
David Grogan946e5b52012-10-12 11:53:12 -070024#include "port/thread_annotations.h"
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000025
26namespace leveldb {
27
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000028namespace log { class Writer; }
29
30class Compaction;
31class Iterator;
32class MemTable;
33class TableBuilder;
34class TableCache;
35class Version;
36class VersionSet;
37class WritableFile;
38
gabor@google.comccf0fcd2011-06-22 02:36:45 +000039// Return the smallest index i such that files[i]->largest >= key.
40// Return files.size() if there is no such file.
41// REQUIRES: "files" contains a sorted list of non-overlapping files.
42extern int FindFile(const InternalKeyComparator& icmp,
43 const std::vector<FileMetaData*>& files,
44 const Slice& key);
45
gabor@google.com6699c7e2011-07-15 00:20:57 +000046// Returns true iff some file in "files" overlaps the user key range
Gabor Cselle299cced2011-10-05 16:30:28 -070047// [*smallest,*largest].
48// smallest==NULL represents a key smaller than all keys in the DB.
49// largest==NULL represents a key largest than all keys in the DB.
50// REQUIRES: If disjoint_sorted_files, files[] contains disjoint ranges
51// in sorted order.
gabor@google.comccf0fcd2011-06-22 02:36:45 +000052extern bool SomeFileOverlapsRange(
53 const InternalKeyComparator& icmp,
Gabor Cselle299cced2011-10-05 16:30:28 -070054 bool disjoint_sorted_files,
gabor@google.comccf0fcd2011-06-22 02:36:45 +000055 const std::vector<FileMetaData*>& files,
Gabor Cselle299cced2011-10-05 16:30:28 -070056 const Slice* smallest_user_key,
57 const Slice* largest_user_key);
gabor@google.comccf0fcd2011-06-22 02:36:45 +000058
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000059class Version {
60 public:
61 // Append to *iters a sequence of iterators that will
62 // yield the contents of this Version when merged together.
63 // REQUIRES: This version has been saved (see VersionSet::SaveTo)
64 void AddIterators(const ReadOptions&, std::vector<Iterator*>* iters);
65
gabor@google.comccf0fcd2011-06-22 02:36:45 +000066 // Lookup the value for key. If found, store it in *val and
67 // return OK. Else return a non-OK status. Fills *stats.
68 // REQUIRES: lock is not held
69 struct GetStats {
70 FileMetaData* seek_file;
71 int seek_file_level;
72 };
73 Status Get(const ReadOptions&, const LookupKey& key, std::string* val,
74 GetStats* stats);
75
76 // Adds "stats" into the current state. Returns true if a new
77 // compaction may need to be triggered, false otherwise.
78 // REQUIRES: lock is held
79 bool UpdateStats(const GetStats& stats);
80
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000081 // Reference count management (so Versions do not disappear out from
82 // under live iterators)
83 void Ref();
84 void Unref();
85
Gabor Cselle299cced2011-10-05 16:30:28 -070086 void GetOverlappingInputs(
87 int level,
88 const InternalKey* begin, // NULL means before all keys
89 const InternalKey* end, // NULL means after all keys
90 std::vector<FileMetaData*>* inputs);
91
gabor@google.comccf0fcd2011-06-22 02:36:45 +000092 // Returns true iff some file in the specified level overlaps
Gabor Cselle299cced2011-10-05 16:30:28 -070093 // some part of [*smallest_user_key,*largest_user_key].
94 // smallest_user_key==NULL represents a key smaller than all keys in the DB.
95 // largest_user_key==NULL represents a key largest than all keys in the DB.
gabor@google.comccf0fcd2011-06-22 02:36:45 +000096 bool OverlapInLevel(int level,
Gabor Cselle299cced2011-10-05 16:30:28 -070097 const Slice* smallest_user_key,
98 const Slice* largest_user_key);
99
100 // Return the level at which we should place a new memtable compaction
101 // result that covers the range [smallest_user_key,largest_user_key].
102 int PickLevelForMemTableOutput(const Slice& smallest_user_key,
103 const Slice& largest_user_key);
gabor@google.comccf0fcd2011-06-22 02:36:45 +0000104
105 int NumFiles(int level) const { return files_[level].size(); }
106
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000107 // Return a human readable string that describes this version's contents.
108 std::string DebugString() const;
109
110 private:
111 friend class Compaction;
112 friend class VersionSet;
113
114 class LevelFileNumIterator;
115 Iterator* NewConcatenatingIterator(const ReadOptions&, int level) const;
116
117 VersionSet* vset_; // VersionSet to which this Version belongs
118 Version* next_; // Next version in linked list
dgrogan@chromium.orgda799092011-05-21 02:17:43 +0000119 Version* prev_; // Previous version in linked list
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000120 int refs_; // Number of live refs to this version
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000121
122 // List of files per level
123 std::vector<FileMetaData*> files_[config::kNumLevels];
124
gabor@google.comccf0fcd2011-06-22 02:36:45 +0000125 // Next file to compact based on seek stats.
126 FileMetaData* file_to_compact_;
127 int file_to_compact_level_;
128
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000129 // Level that should be compacted next and its compaction score.
130 // Score < 1 means compaction is not strictly needed. These fields
131 // are initialized by Finalize().
132 double compaction_score_;
133 int compaction_level_;
134
135 explicit Version(VersionSet* vset)
dgrogan@chromium.orgda799092011-05-21 02:17:43 +0000136 : vset_(vset), next_(this), prev_(this), refs_(0),
gabor@google.comccf0fcd2011-06-22 02:36:45 +0000137 file_to_compact_(NULL),
138 file_to_compact_level_(-1),
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000139 compaction_score_(-1),
140 compaction_level_(-1) {
141 }
142
143 ~Version();
144
145 // No copying allowed
146 Version(const Version&);
147 void operator=(const Version&);
148};
149
150class VersionSet {
151 public:
152 VersionSet(const std::string& dbname,
153 const Options* options,
154 TableCache* table_cache,
155 const InternalKeyComparator*);
156 ~VersionSet();
157
158 // Apply *edit to the current version to form a new descriptor that
159 // is both saved to persistent state and installed as the new
gabor@google.com72630232011-09-01 19:08:02 +0000160 // current version. Will release *mu while actually writing to the file.
161 // REQUIRES: *mu is held on entry.
162 // REQUIRES: no other thread concurrently calls LogAndApply()
David Grogan946e5b52012-10-12 11:53:12 -0700163 Status LogAndApply(VersionEdit* edit, port::Mutex* mu)
164 EXCLUSIVE_LOCKS_REQUIRED(mu);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000165
166 // Recover the last saved descriptor from persistent storage.
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000167 Status Recover();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000168
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000169 // Return the current version.
170 Version* current() const { return current_; }
171
172 // Return the current manifest file number
173 uint64_t ManifestFileNumber() const { return manifest_file_number_; }
174
175 // Allocate and return a new file number
176 uint64_t NewFileNumber() { return next_file_number_++; }
177
Sanjay Ghemawat075a35a2012-05-30 09:45:46 -0700178 // Arrange to reuse "file_number" unless a newer file number has
179 // already been allocated.
180 // REQUIRES: "file_number" was returned by a call to NewFileNumber().
181 void ReuseFileNumber(uint64_t file_number) {
182 if (next_file_number_ == file_number + 1) {
183 next_file_number_ = file_number;
184 }
185 }
186
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000187 // Return the number of Table files at the specified level.
188 int NumLevelFiles(int level) const;
189
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000190 // Return the combined file size of all files at the specified level.
191 int64_t NumLevelBytes(int level) const;
192
193 // Return the last sequence number.
194 uint64_t LastSequence() const { return last_sequence_; }
195
196 // Set the last sequence number to s.
197 void SetLastSequence(uint64_t s) {
198 assert(s >= last_sequence_);
199 last_sequence_ = s;
200 }
201
gabor@google.com72630232011-09-01 19:08:02 +0000202 // Mark the specified file number as used.
203 void MarkFileNumberUsed(uint64_t number);
204
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000205 // Return the current log file number.
206 uint64_t LogNumber() const { return log_number_; }
207
208 // Return the log file number for the log file that is currently
209 // being compacted, or zero if there is no such log file.
210 uint64_t PrevLogNumber() const { return prev_log_number_; }
211
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000212 // Pick level and inputs for a new compaction.
213 // Returns NULL if there is no compaction to be done.
214 // Otherwise returns a pointer to a heap-allocated object that
215 // describes the compaction. Caller should delete the result.
216 Compaction* PickCompaction();
217
218 // Return a compaction object for compacting the range [begin,end] in
219 // the specified level. Returns NULL if there is nothing in that
220 // level that overlaps the specified range. Caller should delete
221 // the result.
222 Compaction* CompactRange(
223 int level,
Gabor Cselle299cced2011-10-05 16:30:28 -0700224 const InternalKey* begin,
225 const InternalKey* end);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000226
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000227 // Return the maximum overlapping data (in bytes) at next level for any
228 // file at a level >= 1.
jorlow@chromium.org8303bb12011-03-22 23:24:02 +0000229 int64_t MaxNextLevelOverlappingBytes();
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000230
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000231 // Create an iterator that reads over the compaction inputs for "*c".
232 // The caller should delete the iterator when no longer needed.
233 Iterator* MakeInputIterator(Compaction* c);
234
235 // Returns true iff some level needs a compaction.
gabor@google.comccf0fcd2011-06-22 02:36:45 +0000236 bool NeedsCompaction() const {
237 Version* v = current_;
238 return (v->compaction_score_ >= 1) || (v->file_to_compact_ != NULL);
239 }
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000240
241 // Add all files listed in any live version to *live.
242 // May also mutate some internal state.
243 void AddLiveFiles(std::set<uint64_t>* live);
244
245 // Return the approximate offset in the database of the data for
246 // "key" as of version "v".
247 uint64_t ApproximateOffsetOf(Version* v, const InternalKey& key);
248
dgrogan@chromium.orgda799092011-05-21 02:17:43 +0000249 // Return a human-readable short (single-line) summary of the number
250 // of files per level. Uses *scratch as backing store.
251 struct LevelSummaryStorage {
252 char buffer[100];
253 };
254 const char* LevelSummary(LevelSummaryStorage* scratch) const;
255
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000256 private:
257 class Builder;
258
259 friend class Compaction;
260 friend class Version;
261
dgrogan@chromium.orgda799092011-05-21 02:17:43 +0000262 void Finalize(Version* v);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000263
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000264 void GetRange(const std::vector<FileMetaData*>& inputs,
265 InternalKey* smallest,
266 InternalKey* largest);
267
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000268 void GetRange2(const std::vector<FileMetaData*>& inputs1,
269 const std::vector<FileMetaData*>& inputs2,
270 InternalKey* smallest,
271 InternalKey* largest);
272
273 void SetupOtherInputs(Compaction* c);
274
gabor@google.com72630232011-09-01 19:08:02 +0000275 // Save current contents to *log
276 Status WriteSnapshot(log::Writer* log);
277
dgrogan@chromium.orgda799092011-05-21 02:17:43 +0000278 void AppendVersion(Version* v);
279
David Grogand84c8252013-01-07 13:17:43 -0800280 bool ManifestContains(const std::string& record) const;
281
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000282 Env* const env_;
283 const std::string dbname_;
284 const Options* const options_;
285 TableCache* const table_cache_;
286 const InternalKeyComparator icmp_;
287 uint64_t next_file_number_;
288 uint64_t manifest_file_number_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000289 uint64_t last_sequence_;
290 uint64_t log_number_;
291 uint64_t prev_log_number_; // 0 or backing store for memtable being compacted
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000292
293 // Opened lazily
294 WritableFile* descriptor_file_;
295 log::Writer* descriptor_log_;
dgrogan@chromium.orgda799092011-05-21 02:17:43 +0000296 Version dummy_versions_; // Head of circular doubly-linked list of versions.
297 Version* current_; // == dummy_versions_.prev_
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000298
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000299 // Per-level key at which the next compaction at that level should start.
300 // Either an empty string, or a valid InternalKey.
301 std::string compact_pointer_[config::kNumLevels];
302
303 // No copying allowed
304 VersionSet(const VersionSet&);
305 void operator=(const VersionSet&);
306};
307
308// A Compaction encapsulates information about a compaction.
309class Compaction {
310 public:
311 ~Compaction();
312
313 // Return the level that is being compacted. Inputs from "level"
314 // and "level+1" will be merged to produce a set of "level+1" files.
315 int level() const { return level_; }
316
317 // Return the object that holds the edits to the descriptor done
318 // by this compaction.
319 VersionEdit* edit() { return &edit_; }
320
321 // "which" must be either 0 or 1
322 int num_input_files(int which) const { return inputs_[which].size(); }
323
324 // Return the ith input file at "level()+which" ("which" must be 0 or 1).
325 FileMetaData* input(int which, int i) const { return inputs_[which][i]; }
326
327 // Maximum size of files to build during this compaction.
328 uint64_t MaxOutputFileSize() const { return max_output_file_size_; }
329
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000330 // Is this a trivial compaction that can be implemented by just
331 // moving a single input file to the next level (no merging or splitting)
332 bool IsTrivialMove() const;
333
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000334 // Add all inputs to this compaction as delete operations to *edit.
335 void AddInputDeletions(VersionEdit* edit);
336
337 // Returns true if the information we have available guarantees that
338 // the compaction is producing data in "level+1" for which no data exists
339 // in levels greater than "level+1".
340 bool IsBaseLevelForKey(const Slice& user_key);
341
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000342 // Returns true iff we should stop building the current output
dgrogan@chromium.orgda799092011-05-21 02:17:43 +0000343 // before processing "internal_key".
344 bool ShouldStopBefore(const Slice& internal_key);
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000345
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000346 // Release the input version for the compaction, once the compaction
347 // is successful.
348 void ReleaseInputs();
349
350 private:
351 friend class Version;
352 friend class VersionSet;
353
354 explicit Compaction(int level);
355
356 int level_;
357 uint64_t max_output_file_size_;
358 Version* input_version_;
359 VersionEdit edit_;
360
361 // Each compaction reads inputs from "level_" and "level_+1"
362 std::vector<FileMetaData*> inputs_[2]; // The two sets of inputs
363
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000364 // State used to check for number of of overlapping grandparent files
365 // (parent == level_ + 1, grandparent == level_ + 2)
366 std::vector<FileMetaData*> grandparents_;
dgrogan@chromium.orgba6dac02011-04-20 22:48:11 +0000367 size_t grandparent_index_; // Index in grandparent_starts_
jorlow@chromium.org8303bb12011-03-22 23:24:02 +0000368 bool seen_key_; // Some output key has been seen
369 int64_t overlapped_bytes_; // Bytes of overlap between current output
370 // and grandparent files
jorlow@chromium.org13b72af2011-03-22 18:32:49 +0000371
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000372 // State for implementing IsBaseLevelForKey
373
374 // level_ptrs_ holds indices into input_version_->levels_: our state
375 // is that we are positioned at one of the file ranges for each
376 // higher level than the ones involved in this compaction (i.e. for
377 // all L >= level_ + 2).
dgrogan@chromium.orgba6dac02011-04-20 22:48:11 +0000378 size_t level_ptrs_[config::kNumLevels];
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000379};
380
Hans Wennborg36a5f8e2011-10-31 17:22:06 +0000381} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000382
383#endif // STORAGE_LEVELDB_DB_VERSION_SET_H_