小组成员:姚凯文(kevinyao0901),姜嘉琪
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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