作者: 韩晨旭 10225101440 李畅 10225102463
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

192 行
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 <set>
  7. #include "db/dbformat.h"
  8. #include "db/log_writer.h"
  9. #include "db/snapshot.h"
  10. #include "leveldb/db.h"
  11. #include "leveldb/env.h"
  12. #include "port/port.h"
  13. namespace leveldb {
  14. class MemTable;
  15. class TableCache;
  16. class Version;
  17. class VersionEdit;
  18. class VersionSet;
  19. class DBImpl : public DB {
  20. public:
  21. DBImpl(const Options& options, const std::string& dbname);
  22. virtual ~DBImpl();
  23. // Implementations of the DB interface
  24. virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value);
  25. virtual Status Delete(const WriteOptions&, const Slice& key);
  26. virtual Status Write(const WriteOptions& options, WriteBatch* updates);
  27. virtual Status Get(const ReadOptions& options,
  28. const Slice& key,
  29. std::string* value);
  30. virtual Iterator* NewIterator(const ReadOptions&);
  31. virtual const Snapshot* GetSnapshot();
  32. virtual void ReleaseSnapshot(const Snapshot* snapshot);
  33. virtual bool GetProperty(const Slice& property, std::string* value);
  34. virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
  35. virtual void CompactRange(const Slice* begin, const Slice* end);
  36. // Extra methods (for testing) that are not in the public DB interface
  37. // Compact any files in the named level that overlap [*begin,*end]
  38. void TEST_CompactRange(int level, const Slice* begin, const Slice* end);
  39. // Force current memtable contents to be compacted.
  40. Status TEST_CompactMemTable();
  41. // Return an internal iterator over the current state of the database.
  42. // The keys of this iterator are internal keys (see format.h).
  43. // The returned iterator should be deleted when no longer needed.
  44. Iterator* TEST_NewInternalIterator();
  45. // Return the maximum overlapping data (in bytes) at next level for any
  46. // file at a level >= 1.
  47. int64_t TEST_MaxNextLevelOverlappingBytes();
  48. private:
  49. friend class DB;
  50. Iterator* NewInternalIterator(const ReadOptions&,
  51. SequenceNumber* latest_snapshot);
  52. Status NewDB();
  53. // Recover the descriptor from persistent storage. May do a significant
  54. // amount of work to recover recently logged updates. Any changes to
  55. // be made to the descriptor are added to *edit.
  56. Status Recover(VersionEdit* edit);
  57. void MaybeIgnoreError(Status* s) const;
  58. // Delete any unneeded files and stale in-memory entries.
  59. void DeleteObsoleteFiles();
  60. // Compact the in-memory write buffer to disk. Switches to a new
  61. // log-file/memtable and writes a new descriptor iff successful.
  62. Status CompactMemTable();
  63. Status RecoverLogFile(uint64_t log_number,
  64. VersionEdit* edit,
  65. SequenceNumber* max_sequence);
  66. Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base);
  67. // Only thread is allowed to log at a time.
  68. struct LoggerId { }; // Opaque identifier for logging thread
  69. void AcquireLoggingResponsibility(LoggerId* self);
  70. void ReleaseLoggingResponsibility(LoggerId* self);
  71. Status MakeRoomForWrite(bool force /* compact even if there is room? */);
  72. struct CompactionState;
  73. void MaybeScheduleCompaction();
  74. static void BGWork(void* db);
  75. void BackgroundCall();
  76. void BackgroundCompaction();
  77. void CleanupCompaction(CompactionState* compact);
  78. Status DoCompactionWork(CompactionState* compact);
  79. Status OpenCompactionOutputFile(CompactionState* compact);
  80. Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
  81. Status InstallCompactionResults(CompactionState* compact);
  82. // Constant after construction
  83. Env* const env_;
  84. const InternalKeyComparator internal_comparator_;
  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. LoggerId* logger_; // NULL, or the id of the current logging thread
  104. port::CondVar logger_cv_; // For threads waiting to log
  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_