提供基本的ttl测试用例
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.

194 lines
6.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. 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. Status 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 InternalFilterPolicy internal_filter_policy_;
  85. const Options options_; // options_.comparator == &internal_comparator_
  86. bool owns_info_log_;
  87. bool owns_cache_;
  88. const std::string dbname_;
  89. // table_cache_ provides its own synchronization
  90. TableCache* table_cache_;
  91. // Lock over the persistent DB state. Non-NULL iff successfully acquired.
  92. FileLock* db_lock_;
  93. // State below is protected by mutex_
  94. port::Mutex mutex_;
  95. port::AtomicPointer shutting_down_;
  96. port::CondVar bg_cv_; // Signalled when background work finishes
  97. MemTable* mem_;
  98. MemTable* imm_; // Memtable being compacted
  99. port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_
  100. WritableFile* logfile_;
  101. uint64_t logfile_number_;
  102. log::Writer* log_;
  103. // Queue of writers.
  104. std::deque<Writer*> writers_;
  105. WriteBatch* tmp_batch_;
  106. SnapshotList snapshots_;
  107. // Set of table files to protect from deletion because they are
  108. // part of ongoing compactions.
  109. std::set<uint64_t> pending_outputs_;
  110. // Has a background compaction been scheduled or is running?
  111. bool bg_compaction_scheduled_;
  112. // Information for a manual compaction
  113. struct ManualCompaction {
  114. int level;
  115. bool done;
  116. const InternalKey* begin; // NULL means beginning of key range
  117. const InternalKey* end; // NULL means end of key range
  118. InternalKey tmp_storage; // Used to keep track of compaction progress
  119. };
  120. ManualCompaction* manual_compaction_;
  121. VersionSet* versions_;
  122. // Have we encountered a background error in paranoid mode?
  123. Status bg_error_;
  124. // Per level compaction stats. stats_[level] stores the stats for
  125. // compactions that produced data for the specified "level".
  126. struct CompactionStats {
  127. int64_t micros;
  128. int64_t bytes_read;
  129. int64_t bytes_written;
  130. CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
  131. void Add(const CompactionStats& c) {
  132. this->micros += c.micros;
  133. this->bytes_read += c.bytes_read;
  134. this->bytes_written += c.bytes_written;
  135. }
  136. };
  137. CompactionStats stats_[config::kNumLevels];
  138. // No copying allowed
  139. DBImpl(const DBImpl&);
  140. void operator=(const DBImpl&);
  141. const Comparator* user_comparator() const {
  142. return internal_comparator_.user_comparator();
  143. }
  144. };
  145. // Sanitize db options. The caller should delete result.info_log if
  146. // it is not equal to src.info_log.
  147. extern Options SanitizeOptions(const std::string& db,
  148. const InternalKeyComparator* icmp,
  149. const InternalFilterPolicy* ipolicy,
  150. const Options& src);
  151. } // namespace leveldb
  152. #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_