blob: 75fd30abe9a56bd663537baa20b2104b349f645f [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_DB_IMPL_H_
6#define STORAGE_LEVELDB_DB_DB_IMPL_H_
7
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -08008#include <deque>
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +00009#include <set>
10#include "db/dbformat.h"
11#include "db/log_writer.h"
12#include "db/snapshot.h"
jorlow@chromium.org4671a692011-03-30 18:35:40 +000013#include "leveldb/db.h"
14#include "leveldb/env.h"
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000015#include "port/port.h"
David Grogan946e5b52012-10-12 11:53:12 -070016#include "port/thread_annotations.h"
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000017
18namespace leveldb {
19
20class MemTable;
21class TableCache;
22class Version;
23class VersionEdit;
24class VersionSet;
25
26class DBImpl : public DB {
27 public:
28 DBImpl(const Options& options, const std::string& dbname);
29 virtual ~DBImpl();
30
31 // Implementations of the DB interface
32 virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value);
33 virtual Status Delete(const WriteOptions&, const Slice& key);
34 virtual Status Write(const WriteOptions& options, WriteBatch* updates);
35 virtual Status Get(const ReadOptions& options,
36 const Slice& key,
37 std::string* value);
38 virtual Iterator* NewIterator(const ReadOptions&);
39 virtual const Snapshot* GetSnapshot();
40 virtual void ReleaseSnapshot(const Snapshot* snapshot);
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +000041 virtual bool GetProperty(const Slice& property, std::string* value);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000042 virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
Gabor Cselle299cced2011-10-05 16:30:28 -070043 virtual void CompactRange(const Slice* begin, const Slice* end);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000044
45 // Extra methods (for testing) that are not in the public DB interface
46
Gabor Cselle299cced2011-10-05 16:30:28 -070047 // Compact any files in the named level that overlap [*begin,*end]
48 void TEST_CompactRange(int level, const Slice* begin, const Slice* end);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000049
50 // Force current memtable contents to be compacted.
51 Status TEST_CompactMemTable();
52
53 // Return an internal iterator over the current state of the database.
54 // The keys of this iterator are internal keys (see format.h).
55 // The returned iterator should be deleted when no longer needed.
56 Iterator* TEST_NewInternalIterator();
57
jorlow@chromium.org13b72af2011-03-22 18:32:49 +000058 // Return the maximum overlapping data (in bytes) at next level for any
59 // file at a level >= 1.
jorlow@chromium.org8303bb12011-03-22 23:24:02 +000060 int64_t TEST_MaxNextLevelOverlappingBytes();
jorlow@chromium.org13b72af2011-03-22 18:32:49 +000061
David Grogan748539c2013-08-21 11:12:47 -070062 // Record a sample of bytes read at the specified internal key.
63 // Samples are taken approximately once every config::kReadBytesPeriod
64 // bytes.
65 void RecordReadSample(Slice key);
66
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000067 private:
68 friend class DB;
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080069 struct CompactionState;
70 struct Writer;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000071
72 Iterator* NewInternalIterator(const ReadOptions&,
David Grogan748539c2013-08-21 11:12:47 -070073 SequenceNumber* latest_snapshot,
74 uint32_t* seed);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000075
76 Status NewDB();
77
78 // Recover the descriptor from persistent storage. May do a significant
79 // amount of work to recover recently logged updates. Any changes to
80 // be made to the descriptor are added to *edit.
David Grogan946e5b52012-10-12 11:53:12 -070081 Status Recover(VersionEdit* edit) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000082
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000083 void MaybeIgnoreError(Status* s) const;
84
85 // Delete any unneeded files and stale in-memory entries.
86 void DeleteObsoleteFiles();
87
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000088 // Compact the in-memory write buffer to disk. Switches to a new
89 // log-file/memtable and writes a new descriptor iff successful.
David Grogan946e5b52012-10-12 11:53:12 -070090 Status CompactMemTable()
91 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000092
93 Status RecoverLogFile(uint64_t log_number,
94 VersionEdit* edit,
David Grogan946e5b52012-10-12 11:53:12 -070095 SequenceNumber* max_sequence)
96 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000097
David Grogan946e5b52012-10-12 11:53:12 -070098 Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base)
99 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000100
David Grogan946e5b52012-10-12 11:53:12 -0700101 Status MakeRoomForWrite(bool force /* compact even if there is room? */)
102 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -0800103 WriteBatch* BuildBatchGroup(Writer** last_writer);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000104
David Grogan946e5b52012-10-12 11:53:12 -0700105 void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000106 static void BGWork(void* db);
107 void BackgroundCall();
David Grogan946e5b52012-10-12 11:53:12 -0700108 Status BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
109 void CleanupCompaction(CompactionState* compact)
110 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
111 Status DoCompactionWork(CompactionState* compact)
112 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000113
114 Status OpenCompactionOutputFile(CompactionState* compact);
115 Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
David Grogan946e5b52012-10-12 11:53:12 -0700116 Status InstallCompactionResults(CompactionState* compact)
117 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000118
119 // Constant after construction
120 Env* const env_;
121 const InternalKeyComparator internal_comparator_;
Sanjay Ghemawat85584d42012-04-17 08:36:46 -0700122 const InternalFilterPolicy internal_filter_policy_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000123 const Options options_; // options_.comparator == &internal_comparator_
124 bool owns_info_log_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000125 bool owns_cache_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000126 const std::string dbname_;
127
128 // table_cache_ provides its own synchronization
129 TableCache* table_cache_;
130
131 // Lock over the persistent DB state. Non-NULL iff successfully acquired.
132 FileLock* db_lock_;
133
134 // State below is protected by mutex_
135 port::Mutex mutex_;
136 port::AtomicPointer shutting_down_;
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000137 port::CondVar bg_cv_; // Signalled when background work finishes
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000138 MemTable* mem_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000139 MemTable* imm_; // Memtable being compacted
140 port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000141 WritableFile* logfile_;
gabor@google.comccf0fcd2011-06-22 02:36:45 +0000142 uint64_t logfile_number_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000143 log::Writer* log_;
David Grogan748539c2013-08-21 11:12:47 -0700144 uint32_t seed_; // For sampling.
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -0800145
146 // Queue of writers.
147 std::deque<Writer*> writers_;
148 WriteBatch* tmp_batch_;
149
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000150 SnapshotList snapshots_;
151
152 // Set of table files to protect from deletion because they are
153 // part of ongoing compactions.
154 std::set<uint64_t> pending_outputs_;
155
156 // Has a background compaction been scheduled or is running?
157 bool bg_compaction_scheduled_;
158
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000159 // Information for a manual compaction
160 struct ManualCompaction {
161 int level;
Gabor Cselle299cced2011-10-05 16:30:28 -0700162 bool done;
163 const InternalKey* begin; // NULL means beginning of key range
164 const InternalKey* end; // NULL means end of key range
165 InternalKey tmp_storage; // Used to keep track of compaction progress
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000166 };
167 ManualCompaction* manual_compaction_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000168
169 VersionSet* versions_;
170
171 // Have we encountered a background error in paranoid mode?
172 Status bg_error_;
David Grogan7b094f12013-06-13 16:14:06 -0700173 int consecutive_compaction_errors_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000174
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000175 // Per level compaction stats. stats_[level] stores the stats for
176 // compactions that produced data for the specified "level".
177 struct CompactionStats {
178 int64_t micros;
179 int64_t bytes_read;
180 int64_t bytes_written;
181
182 CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
183
184 void Add(const CompactionStats& c) {
185 this->micros += c.micros;
186 this->bytes_read += c.bytes_read;
187 this->bytes_written += c.bytes_written;
188 }
189 };
190 CompactionStats stats_[config::kNumLevels];
191
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000192 // No copying allowed
193 DBImpl(const DBImpl&);
194 void operator=(const DBImpl&);
195
196 const Comparator* user_comparator() const {
197 return internal_comparator_.user_comparator();
198 }
199};
200
201// Sanitize db options. The caller should delete result.info_log if
202// it is not equal to src.info_log.
203extern Options SanitizeOptions(const std::string& db,
204 const InternalKeyComparator* icmp,
Sanjay Ghemawat85584d42012-04-17 08:36:46 -0700205 const InternalFilterPolicy* ipolicy,
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000206 const Options& src);
207
Hans Wennborg36a5f8e2011-10-31 17:22:06 +0000208} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000209
210#endif // STORAGE_LEVELDB_DB_DB_IMPL_H_