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" |
| 16 | |
| 17 | namespace leveldb { |
| 18 | |
| 19 | class MemTable; |
| 20 | class TableCache; |
| 21 | class Version; |
| 22 | class VersionEdit; |
| 23 | class VersionSet; |
| 24 | |
| 25 | class 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.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 40 | virtual bool GetProperty(const Slice& property, std::string* value); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 41 | virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes); |
Gabor Cselle | 299cced | 2011-10-05 16:30:28 -0700 | [diff] [blame] | 42 | virtual void CompactRange(const Slice* begin, const Slice* end); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 43 | |
| 44 | // Extra methods (for testing) that are not in the public DB interface |
| 45 | |
Gabor Cselle | 299cced | 2011-10-05 16:30:28 -0700 | [diff] [blame] | 46 | // 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.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 48 | |
| 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.org | 13b72af | 2011-03-22 18:32:49 +0000 | [diff] [blame] | 57 | // Return the maximum overlapping data (in bytes) at next level for any |
| 58 | // file at a level >= 1. |
jorlow@chromium.org | 8303bb1 | 2011-03-22 23:24:02 +0000 | [diff] [blame] | 59 | int64_t TEST_MaxNextLevelOverlappingBytes(); |
jorlow@chromium.org | 13b72af | 2011-03-22 18:32:49 +0000 | [diff] [blame] | 60 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 61 | private: |
| 62 | friend class DB; |
Sanjay Ghemawat | d79762e | 2012-03-08 16:23:21 -0800 | [diff] [blame] | 63 | struct CompactionState; |
| 64 | struct Writer; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 65 | |
| 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.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 76 | void MaybeIgnoreError(Status* s) const; |
| 77 | |
| 78 | // Delete any unneeded files and stale in-memory entries. |
| 79 | void DeleteObsoleteFiles(); |
| 80 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 81 | // 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.com | ccf0fcd | 2011-06-22 02:36:45 +0000 | [diff] [blame] | 89 | Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 90 | |
dgrogan@chromium.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 91 | Status MakeRoomForWrite(bool force /* compact even if there is room? */); |
Sanjay Ghemawat | d79762e | 2012-03-08 16:23:21 -0800 | [diff] [blame] | 92 | WriteBatch* BuildBatchGroup(Writer** last_writer); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 93 | |
| 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 Ghemawat | 85584d4 | 2012-04-17 08:36:46 -0700 | [diff] [blame^] | 108 | const InternalFilterPolicy internal_filter_policy_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 109 | const Options options_; // options_.comparator == &internal_comparator_ |
| 110 | bool owns_info_log_; |
dgrogan@chromium.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 111 | bool owns_cache_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 112 | 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.org | 80e5b0d | 2011-06-07 14:40:26 +0000 | [diff] [blame] | 123 | port::CondVar bg_cv_; // Signalled when background work finishes |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 124 | MemTable* mem_; |
dgrogan@chromium.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 125 | MemTable* imm_; // Memtable being compacted |
| 126 | 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] | 127 | WritableFile* logfile_; |
gabor@google.com | ccf0fcd | 2011-06-22 02:36:45 +0000 | [diff] [blame] | 128 | uint64_t logfile_number_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 129 | log::Writer* log_; |
Sanjay Ghemawat | d79762e | 2012-03-08 16:23:21 -0800 | [diff] [blame] | 130 | |
| 131 | // Queue of writers. |
| 132 | std::deque<Writer*> writers_; |
| 133 | WriteBatch* tmp_batch_; |
| 134 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 135 | 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.org | 80e5b0d | 2011-06-07 14:40:26 +0000 | [diff] [blame] | 144 | // Information for a manual compaction |
| 145 | struct ManualCompaction { |
| 146 | int level; |
Gabor Cselle | 299cced | 2011-10-05 16:30:28 -0700 | [diff] [blame] | 147 | 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.org | 80e5b0d | 2011-06-07 14:40:26 +0000 | [diff] [blame] | 151 | }; |
| 152 | ManualCompaction* manual_compaction_; |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 153 | |
| 154 | VersionSet* versions_; |
| 155 | |
| 156 | // Have we encountered a background error in paranoid mode? |
| 157 | Status bg_error_; |
| 158 | |
dgrogan@chromium.org | f779e7a | 2011-04-12 19:38:58 +0000 | [diff] [blame] | 159 | // 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.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 176 | // 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. |
| 187 | extern Options SanitizeOptions(const std::string& db, |
| 188 | const InternalKeyComparator* icmp, |
Sanjay Ghemawat | 85584d4 | 2012-04-17 08:36:46 -0700 | [diff] [blame^] | 189 | const InternalFilterPolicy* ipolicy, |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 190 | const Options& src); |
| 191 | |
Hans Wennborg | 36a5f8e | 2011-10-31 17:22:06 +0000 | [diff] [blame] | 192 | } // namespace leveldb |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 193 | |
| 194 | #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_ |