jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef STORAGE_LEVELDB_DB_DB_IMPL_H_ |
| 6 | #define STORAGE_LEVELDB_DB_DB_IMPL_H_ |
| 7 | |
Sanjay Ghemawat | d79762e | 2012-03-08 16:23:21 -0800 | [diff] [blame] | 8 | #include <deque> |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 9 | #include <set> |
| 10 | #include "db/dbformat.h" |
| 11 | #include "db/log_writer.h" |
| 12 | #include "db/snapshot.h" |
jorlow@chromium.org | 4671a69 | 2011-03-30 18:35:40 +0000 | [diff] [blame] | 13 | #include "leveldb/db.h" |
| 14 | #include "leveldb/env.h" |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 15 | #include "port/port.h" |
David Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 16 | #include "port/thread_annotations.h" |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 17 | |
| 18 | namespace leveldb { |
| 19 | |
| 20 | class MemTable; |
| 21 | class TableCache; |
| 22 | class Version; |
| 23 | class VersionEdit; |
| 24 | class VersionSet; |
| 25 | |
| 26 | class 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.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 41 | virtual bool GetProperty(const Slice& property, std::string* value); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 42 | virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes); |
Gabor Cselle | 299cced | 2011-10-05 16:30:28 -0700 | [diff] [blame] | 43 | virtual void CompactRange(const Slice* begin, const Slice* end); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 44 | |
| 45 | // Extra methods (for testing) that are not in the public DB interface |
| 46 | |
Gabor Cselle | 299cced | 2011-10-05 16:30:28 -0700 | [diff] [blame] | 47 | // 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.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 49 | |
| 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.org | 13b72af | 2011-03-22 18:32:49 +0000 | [diff] [blame] | 58 | // Return the maximum overlapping data (in bytes) at next level for any |
| 59 | // file at a level >= 1. |
jorlow@chromium.org | 8303bb1 | 2011-03-22 23:24:02 +0000 | [diff] [blame] | 60 | int64_t TEST_MaxNextLevelOverlappingBytes(); |
jorlow@chromium.org | 13b72af | 2011-03-22 18:32:49 +0000 | [diff] [blame] | 61 | |
David Grogan | 748539c | 2013-08-21 11:12:47 -0700 | [diff] [blame] | 62 | // 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.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 67 | private: |
| 68 | friend class DB; |
Sanjay Ghemawat | d79762e | 2012-03-08 16:23:21 -0800 | [diff] [blame] | 69 | struct CompactionState; |
| 70 | struct Writer; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 71 | |
| 72 | Iterator* NewInternalIterator(const ReadOptions&, |
David Grogan | 748539c | 2013-08-21 11:12:47 -0700 | [diff] [blame] | 73 | SequenceNumber* latest_snapshot, |
| 74 | uint32_t* seed); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 75 | |
| 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 Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 81 | Status Recover(VersionEdit* edit) EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 82 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 83 | void MaybeIgnoreError(Status* s) const; |
| 84 | |
| 85 | // Delete any unneeded files and stale in-memory entries. |
| 86 | void DeleteObsoleteFiles(); |
| 87 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 88 | // Compact the in-memory write buffer to disk. Switches to a new |
| 89 | // log-file/memtable and writes a new descriptor iff successful. |
David Grogan | 0cfb990 | 2013-12-10 10:36:31 -0800 | [diff] [blame] | 90 | // Errors are recorded in bg_error_. |
| 91 | void CompactMemTable() EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 92 | |
| 93 | Status RecoverLogFile(uint64_t log_number, |
| 94 | VersionEdit* edit, |
David Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 95 | SequenceNumber* max_sequence) |
| 96 | EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 97 | |
David Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 98 | Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base) |
| 99 | EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 100 | |
David Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 101 | Status MakeRoomForWrite(bool force /* compact even if there is room? */) |
| 102 | EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
Sanjay Ghemawat | d79762e | 2012-03-08 16:23:21 -0800 | [diff] [blame] | 103 | WriteBatch* BuildBatchGroup(Writer** last_writer); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 104 | |
David Grogan | 0cfb990 | 2013-12-10 10:36:31 -0800 | [diff] [blame] | 105 | void RecordBackgroundError(const Status& s); |
| 106 | |
David Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 107 | void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 108 | static void BGWork(void* db); |
| 109 | void BackgroundCall(); |
David Grogan | 0cfb990 | 2013-12-10 10:36:31 -0800 | [diff] [blame] | 110 | void BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
David Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 111 | void CleanupCompaction(CompactionState* compact) |
| 112 | EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 113 | Status DoCompactionWork(CompactionState* compact) |
| 114 | EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 115 | |
| 116 | Status OpenCompactionOutputFile(CompactionState* compact); |
| 117 | Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input); |
David Grogan | 946e5b5 | 2012-10-12 11:53:12 -0700 | [diff] [blame] | 118 | Status InstallCompactionResults(CompactionState* compact) |
| 119 | EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 120 | |
| 121 | // Constant after construction |
| 122 | Env* const env_; |
| 123 | const InternalKeyComparator internal_comparator_; |
Sanjay Ghemawat | 85584d4 | 2012-04-17 08:36:46 -0700 | [diff] [blame] | 124 | const InternalFilterPolicy internal_filter_policy_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 125 | const Options options_; // options_.comparator == &internal_comparator_ |
| 126 | bool owns_info_log_; |
dgrogan@chromium.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 127 | bool owns_cache_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 128 | const std::string dbname_; |
| 129 | |
| 130 | // table_cache_ provides its own synchronization |
| 131 | TableCache* table_cache_; |
| 132 | |
| 133 | // Lock over the persistent DB state. Non-NULL iff successfully acquired. |
| 134 | FileLock* db_lock_; |
| 135 | |
| 136 | // State below is protected by mutex_ |
| 137 | port::Mutex mutex_; |
| 138 | port::AtomicPointer shutting_down_; |
hans@chromium.org | 80e5b0d | 2011-06-07 14:40:26 +0000 | [diff] [blame] | 139 | port::CondVar bg_cv_; // Signalled when background work finishes |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 140 | MemTable* mem_; |
dgrogan@chromium.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 141 | MemTable* imm_; // Memtable being compacted |
| 142 | port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_ |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 143 | WritableFile* logfile_; |
gabor@google.com | ccf0fcd | 2011-06-22 02:36:45 +0000 | [diff] [blame] | 144 | uint64_t logfile_number_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 145 | log::Writer* log_; |
David Grogan | 748539c | 2013-08-21 11:12:47 -0700 | [diff] [blame] | 146 | uint32_t seed_; // For sampling. |
Sanjay Ghemawat | d79762e | 2012-03-08 16:23:21 -0800 | [diff] [blame] | 147 | |
| 148 | // Queue of writers. |
| 149 | std::deque<Writer*> writers_; |
| 150 | WriteBatch* tmp_batch_; |
| 151 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 152 | SnapshotList snapshots_; |
| 153 | |
| 154 | // Set of table files to protect from deletion because they are |
| 155 | // part of ongoing compactions. |
| 156 | std::set<uint64_t> pending_outputs_; |
| 157 | |
| 158 | // Has a background compaction been scheduled or is running? |
| 159 | bool bg_compaction_scheduled_; |
| 160 | |
hans@chromium.org | 80e5b0d | 2011-06-07 14:40:26 +0000 | [diff] [blame] | 161 | // Information for a manual compaction |
| 162 | struct ManualCompaction { |
| 163 | int level; |
Gabor Cselle | 299cced | 2011-10-05 16:30:28 -0700 | [diff] [blame] | 164 | bool done; |
| 165 | const InternalKey* begin; // NULL means beginning of key range |
| 166 | const InternalKey* end; // NULL means end of key range |
| 167 | InternalKey tmp_storage; // Used to keep track of compaction progress |
hans@chromium.org | 80e5b0d | 2011-06-07 14:40:26 +0000 | [diff] [blame] | 168 | }; |
| 169 | ManualCompaction* manual_compaction_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 170 | |
| 171 | VersionSet* versions_; |
| 172 | |
| 173 | // Have we encountered a background error in paranoid mode? |
| 174 | Status bg_error_; |
| 175 | |
dgrogan@chromium.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 176 | // Per level compaction stats. stats_[level] stores the stats for |
| 177 | // compactions that produced data for the specified "level". |
| 178 | struct CompactionStats { |
| 179 | int64_t micros; |
| 180 | int64_t bytes_read; |
| 181 | int64_t bytes_written; |
| 182 | |
| 183 | CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { } |
| 184 | |
| 185 | void Add(const CompactionStats& c) { |
| 186 | this->micros += c.micros; |
| 187 | this->bytes_read += c.bytes_read; |
| 188 | this->bytes_written += c.bytes_written; |
| 189 | } |
| 190 | }; |
| 191 | CompactionStats stats_[config::kNumLevels]; |
| 192 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 193 | // No copying allowed |
| 194 | DBImpl(const DBImpl&); |
| 195 | void operator=(const DBImpl&); |
| 196 | |
| 197 | const Comparator* user_comparator() const { |
| 198 | return internal_comparator_.user_comparator(); |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | // Sanitize db options. The caller should delete result.info_log if |
| 203 | // it is not equal to src.info_log. |
| 204 | extern Options SanitizeOptions(const std::string& db, |
| 205 | const InternalKeyComparator* icmp, |
Sanjay Ghemawat | 85584d4 | 2012-04-17 08:36:46 -0700 | [diff] [blame] | 206 | const InternalFilterPolicy* ipolicy, |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 207 | const Options& src); |
| 208 | |
Hans Wennborg | 36a5f8e | 2011-10-31 17:22:06 +0000 | [diff] [blame] | 209 | } // namespace leveldb |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 210 | |
| 211 | #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_ |