10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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