10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.2 KiB

  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. #ifndef STORAGE_LEVELDB_DB_DB_IMPL_H_
  5. #define STORAGE_LEVELDB_DB_DB_IMPL_H_
  6. #include <deque>
  7. #include <set>
  8. #include "db/dbformat.h"
  9. #include "db/log_writer.h"
  10. #include "db/snapshot.h"
  11. #include "leveldb/db.h"
  12. #include "leveldb/env.h"
  13. #include "port/port.h"
  14. #include "port/thread_annotations.h"
  15. namespace leveldb {
  16. class MemTable;
  17. class TableCache;
  18. class Version;
  19. class VersionEdit;
  20. class VersionSet;
  21. class DBImpl : public DB {
  22. public:
  23. DBImpl(const Options& options, const std::string& dbname);
  24. virtual ~DBImpl();
  25. // Implementations of the DB interface
  26. virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value);
  27. virtual Status Delete(const WriteOptions&, const Slice& key);
  28. virtual Status Write(const WriteOptions& options, WriteBatch* updates);
  29. virtual Status Get(const ReadOptions& options,
  30. const Slice& key,
  31. std::string* value);
  32. virtual Iterator* NewIterator(const ReadOptions&);
  33. virtual const Snapshot* GetSnapshot();
  34. virtual void ReleaseSnapshot(const Snapshot* snapshot);
  35. virtual bool GetProperty(const Slice& property, std::string* value);
  36. virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
  37. virtual void CompactRange(const Slice* begin, const Slice* end);
  38. // Extra methods (for testing) that are not in the public DB interface
  39. // Compact any files in the named level that overlap [*begin,*end]
  40. void TEST_CompactRange(int level, const Slice* begin, const Slice* end);
  41. // Force current memtable contents to be compacted.
  42. Status TEST_CompactMemTable();
  43. // Return an internal iterator over the current state of the database.
  44. // The keys of this iterator are internal keys (see format.h).
  45. // The returned iterator should be deleted when no longer needed.
  46. Iterator* TEST_NewInternalIterator();
  47. // Return the maximum overlapping data (in bytes) at next level for any
  48. // file at a level >= 1.
  49. int64_t TEST_MaxNextLevelOverlappingBytes();
  50. // Record a sample of bytes read at the specified internal key.
  51. // Samples are taken approximately once every config::kReadBytesPeriod
  52. // bytes.
  53. void RecordReadSample(Slice key);
  54. private:
  55. friend class DB;
  56. struct CompactionState;
  57. struct Writer;
  58. Iterator* NewInternalIterator(const ReadOptions&,
  59. SequenceNumber* latest_snapshot,
  60. uint32_t* seed);
  61. Status NewDB();
  62. // Recover the descriptor from persistent storage. May do a significant
  63. // amount of work to recover recently logged updates. Any changes to
  64. // be made to the descriptor are added to *edit.
  65. Status Recover(VersionEdit* edit, bool* save_manifest)
  66. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  67. void MaybeIgnoreError(Status* s) const;
  68. // Delete any unneeded files and stale in-memory entries.
  69. void DeleteObsoleteFiles();
  70. // Compact the in-memory write buffer to disk. Switches to a new
  71. // log-file/memtable and writes a new descriptor iff successful.
  72. // Errors are recorded in bg_error_.
  73. void CompactMemTable() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  74. Status RecoverLogFile(uint64_t log_number, bool last_log, bool* save_manifest,
  75. VersionEdit* edit, SequenceNumber* max_sequence)
  76. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  77. Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base)
  78. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  79. Status MakeRoomForWrite(bool force /* compact even if there is room? */)
  80. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  81. WriteBatch* BuildBatchGroup(Writer** last_writer);
  82. void RecordBackgroundError(const Status& s);
  83. void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  84. static void BGWork(void* db);
  85. void BackgroundCall();
  86. void BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  87. void CleanupCompaction(CompactionState* compact)
  88. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  89. Status DoCompactionWork(CompactionState* compact)
  90. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  91. Status OpenCompactionOutputFile(CompactionState* compact);
  92. Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
  93. Status InstallCompactionResults(CompactionState* compact)
  94. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  95. // Constant after construction
  96. Env* const env_;
  97. const InternalKeyComparator internal_comparator_;
  98. const InternalFilterPolicy internal_filter_policy_;
  99. const Options options_; // options_.comparator == &internal_comparator_
  100. bool owns_info_log_;
  101. bool owns_cache_;
  102. const std::string dbname_;
  103. // table_cache_ provides its own synchronization
  104. TableCache* table_cache_;
  105. // Lock over the persistent DB state. Non-NULL iff successfully acquired.
  106. FileLock* db_lock_;
  107. // State below is protected by mutex_
  108. port::Mutex mutex_;
  109. port::AtomicPointer shutting_down_;
  110. port::CondVar bg_cv_; // Signalled when background work finishes
  111. MemTable* mem_;
  112. MemTable* imm_; // Memtable being compacted
  113. port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_
  114. WritableFile* logfile_;
  115. uint64_t logfile_number_;
  116. log::Writer* log_;
  117. uint32_t seed_; // For sampling.
  118. // Queue of writers.
  119. std::deque<Writer*> writers_;
  120. WriteBatch* tmp_batch_;
  121. SnapshotList snapshots_;
  122. // Set of table files to protect from deletion because they are
  123. // part of ongoing compactions.
  124. std::set<uint64_t> pending_outputs_;
  125. // Has a background compaction been scheduled or is running?
  126. bool bg_compaction_scheduled_;
  127. // Information for a manual compaction
  128. struct ManualCompaction {
  129. int level;
  130. bool done;
  131. const InternalKey* begin; // NULL means beginning of key range
  132. const InternalKey* end; // NULL means end of key range
  133. InternalKey tmp_storage; // Used to keep track of compaction progress
  134. };
  135. ManualCompaction* manual_compaction_;
  136. VersionSet* versions_;
  137. // Have we encountered a background error in paranoid mode?
  138. Status bg_error_;
  139. // Per level compaction stats. stats_[level] stores the stats for
  140. // compactions that produced data for the specified "level".
  141. struct CompactionStats {
  142. int64_t micros;
  143. int64_t bytes_read;
  144. int64_t bytes_written;
  145. CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
  146. void Add(const CompactionStats& c) {
  147. this->micros += c.micros;
  148. this->bytes_read += c.bytes_read;
  149. this->bytes_written += c.bytes_written;
  150. }
  151. };
  152. CompactionStats stats_[config::kNumLevels];
  153. // No copying allowed
  154. DBImpl(const DBImpl&);
  155. void operator=(const DBImpl&);
  156. const Comparator* user_comparator() const {
  157. return internal_comparator_.user_comparator();
  158. }
  159. };
  160. // Sanitize db options. The caller should delete result.info_log if
  161. // it is not equal to src.info_log.
  162. extern Options SanitizeOptions(const std::string& db,
  163. const InternalKeyComparator* icmp,
  164. const InternalFilterPolicy* ipolicy,
  165. const Options& src);
  166. } // namespace leveldb
  167. #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_