Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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