作者: 谢瑞阳 10225101483 徐翔宇 10225101535
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

207 行
6.9 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. // Extra methods (for testing) that are not in the public DB interface
  36. // Compact any files in the named level that overlap [begin,end]
  37. void TEST_CompactRange(
  38. int level,
  39. const std::string& begin,
  40. const std::string& 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. private:
  51. friend class DB;
  52. Iterator* NewInternalIterator(const ReadOptions&,
  53. SequenceNumber* latest_snapshot);
  54. Status NewDB();
  55. // Recover the descriptor from persistent storage. May do a significant
  56. // amount of work to recover recently logged updates. Any changes to
  57. // be made to the descriptor are added to *edit.
  58. Status Recover(VersionEdit* edit);
  59. void MaybeIgnoreError(Status* s) const;
  60. // Delete any unneeded files and stale in-memory entries.
  61. void DeleteObsoleteFiles();
  62. // Called when an iterator over a particular version of the
  63. // descriptor goes away.
  64. static void Unref(void* arg1, void* arg2);
  65. // Compact the in-memory write buffer to disk. Switches to a new
  66. // log-file/memtable and writes a new descriptor iff successful.
  67. Status CompactMemTable();
  68. Status RecoverLogFile(uint64_t log_number,
  69. VersionEdit* edit,
  70. SequenceNumber* max_sequence);
  71. Status WriteLevel0Table(MemTable* mem, VersionEdit* edit);
  72. Status MakeRoomForWrite(bool force /* compact even if there is room? */);
  73. bool HasLargeValues(const WriteBatch& batch) const;
  74. // Process data in "*updates" and return a status. "assigned_seq"
  75. // is the sequence number assigned to the first mod in "*updates".
  76. // If no large values are encountered, "*final" is set to "updates".
  77. // If large values were encountered, registers the references of the
  78. // large values with the VersionSet, writes the large values to
  79. // files (if appropriate), and allocates a new WriteBatch with the
  80. // large values replaced with indirect references and stores a
  81. // pointer to the new WriteBatch in *final. If *final != updates on
  82. // return, then the client should delete *final when no longer
  83. // needed. Returns OK on success, and an appropriate error
  84. // otherwise.
  85. Status HandleLargeValues(SequenceNumber assigned_seq,
  86. WriteBatch* updates,
  87. WriteBatch** final);
  88. // Helper routine for HandleLargeValues
  89. void MaybeCompressLargeValue(
  90. const Slice& raw_value,
  91. Slice* file_bytes,
  92. std::string* scratch,
  93. LargeValueRef* ref);
  94. struct CompactionState;
  95. void MaybeScheduleCompaction();
  96. static void BGWork(void* db);
  97. void BackgroundCall();
  98. void BackgroundCompaction();
  99. void CleanupCompaction(CompactionState* compact);
  100. Status DoCompactionWork(CompactionState* compact);
  101. Status OpenCompactionOutputFile(CompactionState* compact);
  102. Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
  103. Status InstallCompactionResults(CompactionState* compact);
  104. // Constant after construction
  105. Env* const env_;
  106. const InternalKeyComparator internal_comparator_;
  107. const Options options_; // options_.comparator == &internal_comparator_
  108. bool owns_info_log_;
  109. bool owns_cache_;
  110. const std::string dbname_;
  111. // table_cache_ provides its own synchronization
  112. TableCache* table_cache_;
  113. // Lock over the persistent DB state. Non-NULL iff successfully acquired.
  114. FileLock* db_lock_;
  115. // State below is protected by mutex_
  116. port::Mutex mutex_;
  117. port::AtomicPointer shutting_down_;
  118. port::CondVar bg_cv_; // Signalled when !bg_compaction_scheduled_
  119. port::CondVar compacting_cv_; // Signalled when !compacting_
  120. MemTable* mem_;
  121. MemTable* imm_; // Memtable being compacted
  122. port::AtomicPointer has_imm_; // So bg thread can detect non-NULL imm_
  123. WritableFile* logfile_;
  124. log::Writer* log_;
  125. SnapshotList snapshots_;
  126. // Set of table files to protect from deletion because they are
  127. // part of ongoing compactions.
  128. std::set<uint64_t> pending_outputs_;
  129. // Has a background compaction been scheduled or is running?
  130. bool bg_compaction_scheduled_;
  131. // Is there a compaction running?
  132. bool compacting_;
  133. VersionSet* versions_;
  134. // Have we encountered a background error in paranoid mode?
  135. Status bg_error_;
  136. // Per level compaction stats. stats_[level] stores the stats for
  137. // compactions that produced data for the specified "level".
  138. struct CompactionStats {
  139. int64_t micros;
  140. int64_t bytes_read;
  141. int64_t bytes_written;
  142. CompactionStats() : micros(0), bytes_read(0), bytes_written(0) { }
  143. void Add(const CompactionStats& c) {
  144. this->micros += c.micros;
  145. this->bytes_read += c.bytes_read;
  146. this->bytes_written += c.bytes_written;
  147. }
  148. };
  149. CompactionStats stats_[config::kNumLevels];
  150. // No copying allowed
  151. DBImpl(const DBImpl&);
  152. void operator=(const DBImpl&);
  153. const Comparator* user_comparator() const {
  154. return internal_comparator_.user_comparator();
  155. }
  156. };
  157. // Sanitize db options. The caller should delete result.info_log if
  158. // it is not equal to src.info_log.
  159. extern Options SanitizeOptions(const std::string& db,
  160. const InternalKeyComparator* icmp,
  161. const Options& src);
  162. }
  163. #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_