blob: 2f8b52358866eb657d9a530dab9257c326ed23e9 [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"
16
17namespace leveldb {
18
19class MemTable;
20class TableCache;
21class Version;
22class VersionEdit;
23class VersionSet;
24
25class DBImpl : public DB {
26 public:
27 DBImpl(const Options& options, const std::string& dbname);
28 virtual ~DBImpl();
29
30 // Implementations of the DB interface
31 virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value);
32 virtual Status Delete(const WriteOptions&, const Slice& key);
33 virtual Status Write(const WriteOptions& options, WriteBatch* updates);
34 virtual Status Get(const ReadOptions& options,
35 const Slice& key,
36 std::string* value);
37 virtual Iterator* NewIterator(const ReadOptions&);
38 virtual const Snapshot* GetSnapshot();
39 virtual void ReleaseSnapshot(const Snapshot* snapshot);
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +000040 virtual bool GetProperty(const Slice& property, std::string* value);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000041 virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
Gabor Cselle299cced2011-10-05 16:30:28 -070042 virtual void CompactRange(const Slice* begin, const Slice* end);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000043
44 // Extra methods (for testing) that are not in the public DB interface
45
Gabor Cselle299cced2011-10-05 16:30:28 -070046 // Compact any files in the named level that overlap [*begin,*end]
47 void TEST_CompactRange(int level, const Slice* begin, const Slice* end);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000048
49 // Force current memtable contents to be compacted.
50 Status TEST_CompactMemTable();
51
52 // Return an internal iterator over the current state of the database.
53 // The keys of this iterator are internal keys (see format.h).
54 // The returned iterator should be deleted when no longer needed.
55 Iterator* TEST_NewInternalIterator();
56
jorlow@chromium.org13b72af2011-03-22 18:32:49 +000057 // Return the maximum overlapping data (in bytes) at next level for any
58 // file at a level >= 1.
jorlow@chromium.org8303bb12011-03-22 23:24:02 +000059 int64_t TEST_MaxNextLevelOverlappingBytes();
jorlow@chromium.org13b72af2011-03-22 18:32:49 +000060
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000061 private:
62 friend class DB;
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080063 struct CompactionState;
64 struct Writer;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000065
66 Iterator* NewInternalIterator(const ReadOptions&,
67 SequenceNumber* latest_snapshot);
68
69 Status NewDB();
70
71 // Recover the descriptor from persistent storage. May do a significant
72 // amount of work to recover recently logged updates. Any changes to
73 // be made to the descriptor are added to *edit.
74 Status Recover(VersionEdit* edit);
75
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000076 void MaybeIgnoreError(Status* s) const;
77
78 // Delete any unneeded files and stale in-memory entries.
79 void DeleteObsoleteFiles();
80
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000081 // Compact the in-memory write buffer to disk. Switches to a new
82 // log-file/memtable and writes a new descriptor iff successful.
83 Status CompactMemTable();
84
85 Status RecoverLogFile(uint64_t log_number,
86 VersionEdit* edit,
87 SequenceNumber* max_sequence);
88
gabor@google.comccf0fcd2011-06-22 02:36:45 +000089 Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000090
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +000091 Status MakeRoomForWrite(bool force /* compact even if there is room? */);
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080092 WriteBatch* BuildBatchGroup(Writer** last_writer);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000093
94 void MaybeScheduleCompaction();
95 static void BGWork(void* db);
96 void BackgroundCall();
97 void BackgroundCompaction();
98 void CleanupCompaction(CompactionState* compact);
99 Status DoCompactionWork(CompactionState* compact);
100
101 Status OpenCompactionOutputFile(CompactionState* compact);
102 Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
103 Status InstallCompactionResults(CompactionState* compact);
104
105 // Constant after construction
106 Env* const env_;
107 const InternalKeyComparator internal_comparator_;
Sanjay Ghemawat85584d42012-04-17 08:36:46 -0700108 const InternalFilterPolicy internal_filter_policy_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000109 const Options options_; // options_.comparator == &internal_comparator_
110 bool owns_info_log_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000111 bool owns_cache_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000112 const std::string dbname_;
113
114 // table_cache_ provides its own synchronization
115 TableCache* table_cache_;
116
117 // Lock over the persistent DB state. Non-NULL iff successfully acquired.
118 FileLock* db_lock_;
119
120 // State below is protected by mutex_
121 port::Mutex mutex_;
122 port::AtomicPointer shutting_down_;
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000123 port::CondVar bg_cv_; // Signalled when background work finishes
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000124 MemTable* mem_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000125 MemTable* imm_; // Memtable being compacted
126 port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000127 WritableFile* logfile_;
gabor@google.comccf0fcd2011-06-22 02:36:45 +0000128 uint64_t logfile_number_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000129 log::Writer* log_;
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -0800130
131 // Queue of writers.
132 std::deque<Writer*> writers_;
133 WriteBatch* tmp_batch_;
134
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000135 SnapshotList snapshots_;
136
137 // Set of table files to protect from deletion because they are
138 // part of ongoing compactions.
139 std::set<uint64_t> pending_outputs_;
140
141 // Has a background compaction been scheduled or is running?
142 bool bg_compaction_scheduled_;
143
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000144 // Information for a manual compaction
145 struct ManualCompaction {
146 int level;
Gabor Cselle299cced2011-10-05 16:30:28 -0700147 bool done;
148 const InternalKey* begin; // NULL means beginning of key range
149 const InternalKey* end; // NULL means end of key range
150 InternalKey tmp_storage; // Used to keep track of compaction progress
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000151 };
152 ManualCompaction* manual_compaction_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000153
154 VersionSet* versions_;
155
156 // Have we encountered a background error in paranoid mode?
157 Status bg_error_;
158
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000159 // Per level compaction stats. stats_[level] stores the stats for
160 // compactions that produced data for the specified "level".
161 struct CompactionStats {
162 int64_t micros;
163 int64_t bytes_read;
164 int64_t bytes_written;
165
166 CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
167
168 void Add(const CompactionStats& c) {
169 this->micros += c.micros;
170 this->bytes_read += c.bytes_read;
171 this->bytes_written += c.bytes_written;
172 }
173 };
174 CompactionStats stats_[config::kNumLevels];
175
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000176 // No copying allowed
177 DBImpl(const DBImpl&);
178 void operator=(const DBImpl&);
179
180 const Comparator* user_comparator() const {
181 return internal_comparator_.user_comparator();
182 }
183};
184
185// Sanitize db options. The caller should delete result.info_log if
186// it is not equal to src.info_log.
187extern Options SanitizeOptions(const std::string& db,
188 const InternalKeyComparator* icmp,
Sanjay Ghemawat85584d42012-04-17 08:36:46 -0700189 const InternalFilterPolicy* ipolicy,
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000190 const Options& src);
191
Hans Wennborg36a5f8e2011-10-31 17:22:06 +0000192} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000193
194#endif // STORAGE_LEVELDB_DB_DB_IMPL_H_