blob: e665c0ec2d63e43e60447257746efb6c26f755dd [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_;
108 const Options options_; // options_.comparator == &internal_comparator_
109 bool owns_info_log_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000110 bool owns_cache_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000111 const std::string dbname_;
112
113 // table_cache_ provides its own synchronization
114 TableCache* table_cache_;
115
116 // Lock over the persistent DB state. Non-NULL iff successfully acquired.
117 FileLock* db_lock_;
118
119 // State below is protected by mutex_
120 port::Mutex mutex_;
121 port::AtomicPointer shutting_down_;
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000122 port::CondVar bg_cv_; // Signalled when background work finishes
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000123 MemTable* mem_;
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000124 MemTable* imm_; // Memtable being compacted
125 port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000126 WritableFile* logfile_;
gabor@google.comccf0fcd2011-06-22 02:36:45 +0000127 uint64_t logfile_number_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000128 log::Writer* log_;
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -0800129
130 // Queue of writers.
131 std::deque<Writer*> writers_;
132 WriteBatch* tmp_batch_;
133
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000134 SnapshotList snapshots_;
135
136 // Set of table files to protect from deletion because they are
137 // part of ongoing compactions.
138 std::set<uint64_t> pending_outputs_;
139
140 // Has a background compaction been scheduled or is running?
141 bool bg_compaction_scheduled_;
142
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000143 // Information for a manual compaction
144 struct ManualCompaction {
145 int level;
Gabor Cselle299cced2011-10-05 16:30:28 -0700146 bool done;
147 const InternalKey* begin; // NULL means beginning of key range
148 const InternalKey* end; // NULL means end of key range
149 InternalKey tmp_storage; // Used to keep track of compaction progress
hans@chromium.org80e5b0d2011-06-07 14:40:26 +0000150 };
151 ManualCompaction* manual_compaction_;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000152
153 VersionSet* versions_;
154
155 // Have we encountered a background error in paranoid mode?
156 Status bg_error_;
157
dgrogan@chromium.orgf779e7a2011-04-12 19:38:58 +0000158 // Per level compaction stats. stats_[level] stores the stats for
159 // compactions that produced data for the specified "level".
160 struct CompactionStats {
161 int64_t micros;
162 int64_t bytes_read;
163 int64_t bytes_written;
164
165 CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
166
167 void Add(const CompactionStats& c) {
168 this->micros += c.micros;
169 this->bytes_read += c.bytes_read;
170 this->bytes_written += c.bytes_written;
171 }
172 };
173 CompactionStats stats_[config::kNumLevels];
174
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000175 // No copying allowed
176 DBImpl(const DBImpl&);
177 void operator=(const DBImpl&);
178
179 const Comparator* user_comparator() const {
180 return internal_comparator_.user_comparator();
181 }
182};
183
184// Sanitize db options. The caller should delete result.info_log if
185// it is not equal to src.info_log.
186extern Options SanitizeOptions(const std::string& db,
187 const InternalKeyComparator* icmp,
188 const Options& src);
189
Hans Wennborg36a5f8e2011-10-31 17:22:06 +0000190} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000191
192#endif // STORAGE_LEVELDB_DB_DB_IMPL_H_