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.

212 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 <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() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  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. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  83. void RecordBackgroundError(const Status& s);
  84. void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  85. static void BGWork(void* db);
  86. void BackgroundCall();
  87. void BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  88. void CleanupCompaction(CompactionState* compact)
  89. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  90. Status DoCompactionWork(CompactionState* compact)
  91. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  92. Status OpenCompactionOutputFile(CompactionState* compact);
  93. Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
  94. Status InstallCompactionResults(CompactionState* compact)
  95. EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  96. // Constant after construction
  97. Env* const env_;
  98. const InternalKeyComparator internal_comparator_;
  99. const InternalFilterPolicy internal_filter_policy_;
  100. const Options options_; // options_.comparator == &internal_comparator_
  101. const bool owns_info_log_;
  102. const bool owns_cache_;
  103. const std::string dbname_;
  104. // table_cache_ provides its own synchronization
  105. TableCache* const table_cache_;
  106. // Lock over the persistent DB state. Non-null iff successfully acquired.
  107. FileLock* db_lock_;
  108. // State below is protected by mutex_
  109. port::Mutex mutex_;
  110. port::AtomicPointer shutting_down_;
  111. port::CondVar background_work_finished_signal_ GUARDED_BY(mutex_);
  112. MemTable* mem_;
  113. MemTable* imm_ GUARDED_BY(mutex_); // Memtable being compacted
  114. port::AtomicPointer has_imm_; // So bg thread can detect non-null imm_
  115. WritableFile* logfile_;
  116. uint64_t logfile_number_ GUARDED_BY(mutex_);
  117. log::Writer* log_;
  118. uint32_t seed_ GUARDED_BY(mutex_); // For sampling.
  119. // Queue of writers.
  120. std::deque<Writer*> writers_ GUARDED_BY(mutex_);
  121. WriteBatch* tmp_batch_ GUARDED_BY(mutex_);
  122. SnapshotList snapshots_ GUARDED_BY(mutex_);
  123. // Set of table files to protect from deletion because they are
  124. // part of ongoing compactions.
  125. std::set<uint64_t> pending_outputs_ GUARDED_BY(mutex_);
  126. // Has a background compaction been scheduled or is running?
  127. bool background_compaction_scheduled_ GUARDED_BY(mutex_);
  128. // Information for a manual compaction
  129. struct ManualCompaction {
  130. int level;
  131. bool done;
  132. const InternalKey* begin; // null means beginning of key range
  133. const InternalKey* end; // null means end of key range
  134. InternalKey tmp_storage; // Used to keep track of compaction progress
  135. };
  136. ManualCompaction* manual_compaction_ GUARDED_BY(mutex_);
  137. VersionSet* const versions_;
  138. // Have we encountered a background error in paranoid mode?
  139. Status bg_error_ GUARDED_BY(mutex_);
  140. // Per level compaction stats. stats_[level] stores the stats for
  141. // compactions that produced data for the specified "level".
  142. struct CompactionStats {
  143. int64_t micros;
  144. int64_t bytes_read;
  145. int64_t bytes_written;
  146. CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
  147. void Add(const CompactionStats& c) {
  148. this->micros += c.micros;
  149. this->bytes_read += c.bytes_read;
  150. this->bytes_written += c.bytes_written;
  151. }
  152. };
  153. CompactionStats stats_[config::kNumLevels] GUARDED_BY(mutex_);
  154. // No copying allowed
  155. DBImpl(const DBImpl&);
  156. void operator=(const DBImpl&);
  157. const Comparator* user_comparator() const {
  158. return internal_comparator_.user_comparator();
  159. }
  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_