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.
5 #ifndef STORAGE_LEVELDB_DB_DB_IMPL_H_
6 #define STORAGE_LEVELDB_DB_DB_IMPL_H_
10 #include "db/dbformat.h"
11 #include "db/log_writer.h"
12 #include "db/snapshot.h"
13 #include "leveldb/db.h"
14 #include "leveldb/env.h"
15 #include "port/port.h"
16 #include "port/thread_annotations.h"
26 class DBImpl : public DB {
28 DBImpl(const Options& options, const std::string& dbname);
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,
38 virtual Iterator* NewIterator(const ReadOptions&);
39 virtual const Snapshot* GetSnapshot();
40 virtual void ReleaseSnapshot(const Snapshot* snapshot);
41 virtual bool GetProperty(const Slice& property, std::string* value);
42 virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
43 virtual void CompactRange(const Slice* begin, const Slice* end);
45 // Extra methods (for testing) that are not in the public DB interface
47 // Compact any files in the named level that overlap [*begin,*end]
48 void TEST_CompactRange(int level, const Slice* begin, const Slice* end);
50 // Force current memtable contents to be compacted.
51 Status TEST_CompactMemTable();
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();
58 // Return the maximum overlapping data (in bytes) at next level for any
59 // file at a level >= 1.
60 int64_t TEST_MaxNextLevelOverlappingBytes();
64 struct CompactionState;
67 Iterator* NewInternalIterator(const ReadOptions&,
68 SequenceNumber* latest_snapshot);
72 // Recover the descriptor from persistent storage. May do a significant
73 // amount of work to recover recently logged updates. Any changes to
74 // be made to the descriptor are added to *edit.
75 Status Recover(VersionEdit* edit) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
77 void MaybeIgnoreError(Status* s) const;
79 // Delete any unneeded files and stale in-memory entries.
80 void DeleteObsoleteFiles();
82 // Compact the in-memory write buffer to disk. Switches to a new
83 // log-file/memtable and writes a new descriptor iff successful.
84 Status CompactMemTable()
85 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
87 Status RecoverLogFile(uint64_t log_number,
89 SequenceNumber* max_sequence)
90 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
92 Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base)
93 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
95 Status MakeRoomForWrite(bool force /* compact even if there is room? */)
96 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
97 WriteBatch* BuildBatchGroup(Writer** last_writer);
99 void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
100 static void BGWork(void* db);
101 void BackgroundCall();
102 Status BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
103 void CleanupCompaction(CompactionState* compact)
104 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
105 Status DoCompactionWork(CompactionState* compact)
106 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
108 Status OpenCompactionOutputFile(CompactionState* compact);
109 Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
110 Status InstallCompactionResults(CompactionState* compact)
111 EXCLUSIVE_LOCKS_REQUIRED(mutex_);
113 // Constant after construction
115 const InternalKeyComparator internal_comparator_;
116 const InternalFilterPolicy internal_filter_policy_;
117 const Options options_; // options_.comparator == &internal_comparator_
120 const std::string dbname_;
122 // table_cache_ provides its own synchronization
123 TableCache* table_cache_;
125 // Lock over the persistent DB state. Non-NULL iff successfully acquired.
128 // State below is protected by mutex_
130 port::AtomicPointer shutting_down_;
131 port::CondVar bg_cv_; // Signalled when background work finishes
133 MemTable* imm_; // Memtable being compacted
134 port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_
135 WritableFile* logfile_;
136 uint64_t logfile_number_;
140 std::deque<Writer*> writers_;
141 WriteBatch* tmp_batch_;
143 SnapshotList snapshots_;
145 // Set of table files to protect from deletion because they are
146 // part of ongoing compactions.
147 std::set<uint64_t> pending_outputs_;
149 // Has a background compaction been scheduled or is running?
150 bool bg_compaction_scheduled_;
152 // Information for a manual compaction
153 struct ManualCompaction {
156 const InternalKey* begin; // NULL means beginning of key range
157 const InternalKey* end; // NULL means end of key range
158 InternalKey tmp_storage; // Used to keep track of compaction progress
160 ManualCompaction* manual_compaction_;
162 VersionSet* versions_;
164 // Have we encountered a background error in paranoid mode?
166 int consecutive_compaction_errors_;
168 // Per level compaction stats. stats_[level] stores the stats for
169 // compactions that produced data for the specified "level".
170 struct CompactionStats {
173 int64_t bytes_written;
175 CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
177 void Add(const CompactionStats& c) {
178 this->micros += c.micros;
179 this->bytes_read += c.bytes_read;
180 this->bytes_written += c.bytes_written;
183 CompactionStats stats_[config::kNumLevels];
185 // No copying allowed
186 DBImpl(const DBImpl&);
187 void operator=(const DBImpl&);
189 const Comparator* user_comparator() const {
190 return internal_comparator_.user_comparator();
194 // Sanitize db options. The caller should delete result.info_log if
195 // it is not equal to src.info_log.
196 extern Options SanitizeOptions(const std::string& db,
197 const InternalKeyComparator* icmp,
198 const InternalFilterPolicy* ipolicy,
201 } // namespace leveldb
203 #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_