| 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 | |||
| 8 | #include <set> | ||
| 9 | #include "db/dbformat.h" | ||
| 10 | #include "db/log_writer.h" | ||
| 11 | #include "db/snapshot.h" | ||
| 12 | #include "leveldb/db.h" | ||
| 13 | #include "leveldb/env.h" | ||
| 14 | #include "port/port.h" | ||
| 15 | |||
| 16 | namespace leveldb { | ||
| 17 | |||
| 18 | class MemTable; | ||
| 19 | class TableCache; | ||
| 20 | class Version; | ||
| 21 | class VersionEdit; | ||
| 22 | class VersionSet; | ||
| 23 | |||
| 24 | class DBImpl : public DB { | ||
| 25 | public: | ||
| 26 | DBImpl(const Options& options, const std::string& dbname); | ||
| 27 | virtual ~DBImpl(); | ||
| 28 | |||
| 29 | // Implementations of the DB interface | ||
| 30 | virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value); | ||
| 31 | virtual Status Delete(const WriteOptions&, const Slice& key); | ||
| 32 | virtual Status Write(const WriteOptions& options, WriteBatch* updates); | ||
| 33 | virtual Status Get(const ReadOptions& options, | ||
| 34 | const Slice& key, | ||
| 35 | std::string* value); | ||
| 36 | virtual Iterator* NewIterator(const ReadOptions&); | ||
| 37 | virtual const Snapshot* GetSnapshot(); | ||
| 38 | virtual void ReleaseSnapshot(const Snapshot* snapshot); | ||
| 39 | virtual bool GetProperty(const Slice& property, std::string* value); | ||
| 40 | virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes); | ||
| 41 | |||
| 42 | // Extra methods (for testing) that are not in the public DB interface | ||
| 43 | |||
| 44 | // Compact any files in the named level that overlap [begin,end] | ||
| 45 | void TEST_CompactRange( | ||
| 46 | int level, | ||
| 47 | const std::string& begin, | ||
| 48 | const std::string& end); | ||
| 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 | |||
| 58 | // Return the maximum overlapping data (in bytes) at next level for any | ||
| 59 | // file at a level >= 1. | ||
| 60 | int64_t TEST_MaxNextLevelOverlappingBytes(); | ||
| 61 | |||
| 62 | private: | ||
| 63 | friend class DB; | ||
| 64 | |||
| 65 | Iterator* NewInternalIterator(const ReadOptions&, | ||
| 66 | SequenceNumber* latest_snapshot); | ||
| 67 | |||
| 68 | Status NewDB(); | ||
| 69 | |||
| 70 | // Recover the descriptor from persistent storage. May do a significant | ||
| 71 | // amount of work to recover recently logged updates. Any changes to | ||
| 72 | // be made to the descriptor are added to *edit. | ||
| 73 | Status Recover(VersionEdit* edit); | ||
| 74 | |||
| 75 | void MaybeIgnoreError(Status* s) const; | ||
| 76 | |||
| 77 | // Delete any unneeded files and stale in-memory entries. | ||
| 78 | void DeleteObsoleteFiles(); | ||
| 79 | |||
| 80 | // Compact the in-memory write buffer to disk. Switches to a new | ||
| 81 | // log-file/memtable and writes a new descriptor iff successful. | ||
| 82 | Status CompactMemTable(); | ||
| 83 | |||
| 84 | Status RecoverLogFile(uint64_t log_number, | ||
| 85 | VersionEdit* edit, | ||
| 86 | SequenceNumber* max_sequence); | ||
| 87 | |||
| 88 | Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base); | ||
| 89 | |||
| 90 | // Only thread is allowed to log at a time. | ||
| 91 | struct LoggerId { }; // Opaque identifier for logging thread | ||
| 92 | void AcquireLoggingResponsibility(LoggerId* self); | ||
| 93 | void ReleaseLoggingResponsibility(LoggerId* self); | ||
| 94 | |||
| 95 | Status MakeRoomForWrite(bool force /* compact even if there is room? */); | ||
| 96 | |||
| 97 | struct CompactionState; | ||
| 98 | |||
| 99 | void MaybeScheduleCompaction(); | ||
| 100 | static void BGWork(void* db); | ||
| 101 | void BackgroundCall(); | ||
| 102 | void BackgroundCompaction(); | ||
| 103 | void CleanupCompaction(CompactionState* compact); | ||
| 104 | Status DoCompactionWork(CompactionState* compact); | ||
| 105 | |||
| 106 | Status OpenCompactionOutputFile(CompactionState* compact); | ||
| 107 | Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input); | ||
| 108 | Status InstallCompactionResults(CompactionState* compact); | ||
| 109 | |||
| 110 | // Constant after construction | ||
| 111 | Env* const env_; | ||
| 112 | const InternalKeyComparator internal_comparator_; | ||
| 113 | const Options options_; // options_.comparator == &internal_comparator_ | ||
| 114 | bool owns_info_log_; | ||
| 115 | bool owns_cache_; | ||
| 116 | const std::string dbname_; | ||
| 117 | |||
| 118 | // table_cache_ provides its own synchronization | ||
| 119 | TableCache* table_cache_; | ||
| 120 | |||
| 121 | // Lock over the persistent DB state. Non-NULL iff successfully acquired. | ||
| 122 | FileLock* db_lock_; | ||
| 123 | |||
| 124 | // State below is protected by mutex_ | ||
| 125 | port::Mutex mutex_; | ||
| 126 | port::AtomicPointer shutting_down_; | ||
| 127 | port::CondVar bg_cv_; // Signalled when background work finishes | ||
| 128 | MemTable* mem_; | ||
| 129 | MemTable* imm_; // Memtable being compacted | ||
| 130 | port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_ | ||
| 131 | WritableFile* logfile_; | ||
| 132 | uint64_t logfile_number_; | ||
| 133 | log::Writer* log_; | ||
| 134 | LoggerId* logger_; // NULL, or the id of the current logging thread | ||
| 135 | port::CondVar logger_cv_; // For threads waiting to log | ||
| 136 | SnapshotList snapshots_; | ||
| 137 | |||
| 138 | // Set of table files to protect from deletion because they are | ||
| 139 | // part of ongoing compactions. | ||
| 140 | std::set<uint64_t> pending_outputs_; | ||
| 141 | |||
| 142 | // Has a background compaction been scheduled or is running? | ||
| 143 | bool bg_compaction_scheduled_; | ||
| 144 | |||
| 145 | // Information for a manual compaction | ||
| 146 | struct ManualCompaction { | ||
| 147 | int level; | ||
| 148 | std::string begin; | ||
| 149 | std::string end; | ||
| 150 | }; | ||
| 151 | ManualCompaction* manual_compaction_; | ||
| 152 | |||
| 153 | VersionSet* versions_; | ||
| 154 | |||
| 155 | // Have we encountered a background error in paranoid mode? | ||
| 156 | Status bg_error_; | ||
| 157 | |||
| 158 | // 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 | 29 | CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { } | |
| 166 | |||
| 167 | 1 | void Add(const CompactionStats& c) { | |
| 168 | 1 | this->micros += c.micros; | |
| 169 | 1 | this->bytes_read += c.bytes_read; | |
| 170 | 1 | this->bytes_written += c.bytes_written; | |
| 171 | 1 | } | |
| 172 | }; | ||
| 173 | CompactionStats stats_[config::kNumLevels]; | ||
| 174 | |||
| 175 | // No copying allowed | ||
| 176 | DBImpl(const DBImpl&); | ||
| 177 | void operator=(const DBImpl&); | ||
| 178 | |||
| 179 | 16 | const Comparator* user_comparator() const { | |
| 180 | 16 | 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. | ||
| 186 | extern Options SanitizeOptions(const std::string& db, | ||
| 187 | const InternalKeyComparator* icmp, | ||
| 188 | const Options& src); | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_ |