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

332 行
11 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. //
  5. // The representation of a DBImpl consists of a set of Versions. The
  6. // newest version is called "current". Older versions may be kept
  7. // around to provide a consistent view to live iterators.
  8. //
  9. // Each Version keeps track of a set of Table files per level. The
  10. // entire set of versions is maintained in a VersionSet.
  11. //
  12. // Version,VersionSet are thread-compatible, but require external
  13. // synchronization on all accesses.
  14. #ifndef STORAGE_LEVELDB_DB_VERSION_SET_H_
  15. #define STORAGE_LEVELDB_DB_VERSION_SET_H_
  16. #include <map>
  17. #include <set>
  18. #include <vector>
  19. #include "db/dbformat.h"
  20. #include "db/version_edit.h"
  21. #include "port/port.h"
  22. namespace leveldb {
  23. namespace log { class Writer; }
  24. class Compaction;
  25. class Iterator;
  26. class MemTable;
  27. class TableBuilder;
  28. class TableCache;
  29. class Version;
  30. class VersionSet;
  31. class WritableFile;
  32. class Version {
  33. public:
  34. // Append to *iters a sequence of iterators that will
  35. // yield the contents of this Version when merged together.
  36. // REQUIRES: This version has been saved (see VersionSet::SaveTo)
  37. void AddIterators(const ReadOptions&, std::vector<Iterator*>* iters);
  38. // Reference count management (so Versions do not disappear out from
  39. // under live iterators)
  40. void Ref();
  41. void Unref();
  42. // Return a human readable string that describes this version's contents.
  43. std::string DebugString() const;
  44. private:
  45. friend class Compaction;
  46. friend class VersionSet;
  47. class LevelFileNumIterator;
  48. Iterator* NewConcatenatingIterator(const ReadOptions&, int level) const;
  49. VersionSet* vset_; // VersionSet to which this Version belongs
  50. Version* next_; // Next version in linked list
  51. int refs_; // Number of live refs to this version
  52. MemTable* cleanup_mem_; // NULL, or table to delete when version dropped
  53. // List of files per level
  54. std::vector<FileMetaData*> files_[config::kNumLevels];
  55. // Level that should be compacted next and its compaction score.
  56. // Score < 1 means compaction is not strictly needed. These fields
  57. // are initialized by Finalize().
  58. double compaction_score_;
  59. int compaction_level_;
  60. explicit Version(VersionSet* vset)
  61. : vset_(vset), next_(NULL), refs_(0),
  62. cleanup_mem_(NULL),
  63. compaction_score_(-1),
  64. compaction_level_(-1) {
  65. }
  66. ~Version();
  67. // No copying allowed
  68. Version(const Version&);
  69. void operator=(const Version&);
  70. };
  71. class VersionSet {
  72. public:
  73. VersionSet(const std::string& dbname,
  74. const Options* options,
  75. TableCache* table_cache,
  76. const InternalKeyComparator*);
  77. ~VersionSet();
  78. // Apply *edit to the current version to form a new descriptor that
  79. // is both saved to persistent state and installed as the new
  80. // current version. Iff Apply() returns OK, arrange to delete
  81. // cleanup_mem (if cleanup_mem != NULL) when it is no longer needed
  82. // by older versions.
  83. Status LogAndApply(VersionEdit* edit, MemTable* cleanup_mem);
  84. // Recover the last saved descriptor from persistent storage.
  85. Status Recover();
  86. // Save current contents to *log
  87. Status WriteSnapshot(log::Writer* log);
  88. // Return the current version.
  89. Version* current() const { return current_; }
  90. // Return the current manifest file number
  91. uint64_t ManifestFileNumber() const { return manifest_file_number_; }
  92. // Allocate and return a new file number
  93. uint64_t NewFileNumber() { return next_file_number_++; }
  94. // Return the number of Table files at the specified level.
  95. int NumLevelFiles(int level) const;
  96. // Return the combined file size of all files at the specified level.
  97. int64_t NumLevelBytes(int level) const;
  98. // Return the last sequence number.
  99. uint64_t LastSequence() const { return last_sequence_; }
  100. // Set the last sequence number to s.
  101. void SetLastSequence(uint64_t s) {
  102. assert(s >= last_sequence_);
  103. last_sequence_ = s;
  104. }
  105. // Return the current log file number.
  106. uint64_t LogNumber() const { return log_number_; }
  107. // Return the log file number for the log file that is currently
  108. // being compacted, or zero if there is no such log file.
  109. uint64_t PrevLogNumber() const { return prev_log_number_; }
  110. // Pick level and inputs for a new compaction.
  111. // Returns NULL if there is no compaction to be done.
  112. // Otherwise returns a pointer to a heap-allocated object that
  113. // describes the compaction. Caller should delete the result.
  114. Compaction* PickCompaction();
  115. // Return a compaction object for compacting the range [begin,end] in
  116. // the specified level. Returns NULL if there is nothing in that
  117. // level that overlaps the specified range. Caller should delete
  118. // the result.
  119. Compaction* CompactRange(
  120. int level,
  121. const InternalKey& begin,
  122. const InternalKey& end);
  123. // Return the maximum overlapping data (in bytes) at next level for any
  124. // file at a level >= 1.
  125. int64_t MaxNextLevelOverlappingBytes();
  126. // Create an iterator that reads over the compaction inputs for "*c".
  127. // The caller should delete the iterator when no longer needed.
  128. Iterator* MakeInputIterator(Compaction* c);
  129. // Returns true iff some level needs a compaction.
  130. bool NeedsCompaction() const { return current_->compaction_score_ >= 1; }
  131. // Add all files listed in any live version to *live.
  132. // May also mutate some internal state.
  133. void AddLiveFiles(std::set<uint64_t>* live);
  134. // Return the approximate offset in the database of the data for
  135. // "key" as of version "v".
  136. uint64_t ApproximateOffsetOf(Version* v, const InternalKey& key);
  137. // Register a reference to a large value with the specified
  138. // large_ref from the specified file number. Returns "true" if this
  139. // is the first recorded reference to the "large_ref" value in the
  140. // database, and false otherwise.
  141. bool RegisterLargeValueRef(const LargeValueRef& large_ref,
  142. uint64_t filenum,
  143. const InternalKey& internal_key);
  144. // Cleanup the large value reference state by eliminating any
  145. // references from files that are not includes in either "live_tables"
  146. // or the current log.
  147. void CleanupLargeValueRefs(const std::set<uint64_t>& live_tables);
  148. // Returns true if a large value with the given reference is live.
  149. bool LargeValueIsLive(const LargeValueRef& large_ref);
  150. private:
  151. class Builder;
  152. friend class Compaction;
  153. friend class Version;
  154. Status Finalize(Version* v);
  155. // Delete any old versions that are no longer needed.
  156. void MaybeDeleteOldVersions();
  157. struct BySmallestKey;
  158. Status SortLevel(Version* v, uint64_t level);
  159. void GetOverlappingInputs(
  160. int level,
  161. const InternalKey& begin,
  162. const InternalKey& end,
  163. std::vector<FileMetaData*>* inputs);
  164. void GetRange(const std::vector<FileMetaData*>& inputs,
  165. InternalKey* smallest,
  166. InternalKey* largest);
  167. void GetRange2(const std::vector<FileMetaData*>& inputs1,
  168. const std::vector<FileMetaData*>& inputs2,
  169. InternalKey* smallest,
  170. InternalKey* largest);
  171. void SetupOtherInputs(Compaction* c);
  172. Env* const env_;
  173. const std::string dbname_;
  174. const Options* const options_;
  175. TableCache* const table_cache_;
  176. const InternalKeyComparator icmp_;
  177. uint64_t next_file_number_;
  178. uint64_t manifest_file_number_;
  179. uint64_t last_sequence_;
  180. uint64_t log_number_;
  181. uint64_t prev_log_number_; // 0 or backing store for memtable being compacted
  182. // Opened lazily
  183. WritableFile* descriptor_file_;
  184. log::Writer* descriptor_log_;
  185. // Versions are kept in a singly linked list that is never empty
  186. Version* current_; // Pointer to the last (newest) list entry
  187. Version* oldest_; // Pointer to the first (oldest) list entry
  188. // Map from large value reference to the set of <file numbers,internal_key>
  189. // values containing references to the value. We keep the
  190. // internal key as a std::string rather than as an InternalKey because
  191. // we want to be able to easily use a set.
  192. typedef std::set<std::pair<uint64_t, std::string> > LargeReferencesSet;
  193. typedef std::map<LargeValueRef, LargeReferencesSet> LargeValueMap;
  194. LargeValueMap large_value_refs_;
  195. // Per-level key at which the next compaction at that level should start.
  196. // Either an empty string, or a valid InternalKey.
  197. std::string compact_pointer_[config::kNumLevels];
  198. // No copying allowed
  199. VersionSet(const VersionSet&);
  200. void operator=(const VersionSet&);
  201. };
  202. // A Compaction encapsulates information about a compaction.
  203. class Compaction {
  204. public:
  205. ~Compaction();
  206. // Return the level that is being compacted. Inputs from "level"
  207. // and "level+1" will be merged to produce a set of "level+1" files.
  208. int level() const { return level_; }
  209. // Return the object that holds the edits to the descriptor done
  210. // by this compaction.
  211. VersionEdit* edit() { return &edit_; }
  212. // "which" must be either 0 or 1
  213. int num_input_files(int which) const { return inputs_[which].size(); }
  214. // Return the ith input file at "level()+which" ("which" must be 0 or 1).
  215. FileMetaData* input(int which, int i) const { return inputs_[which][i]; }
  216. // Maximum size of files to build during this compaction.
  217. uint64_t MaxOutputFileSize() const { return max_output_file_size_; }
  218. // Is this a trivial compaction that can be implemented by just
  219. // moving a single input file to the next level (no merging or splitting)
  220. bool IsTrivialMove() const;
  221. // Add all inputs to this compaction as delete operations to *edit.
  222. void AddInputDeletions(VersionEdit* edit);
  223. // Returns true if the information we have available guarantees that
  224. // the compaction is producing data in "level+1" for which no data exists
  225. // in levels greater than "level+1".
  226. bool IsBaseLevelForKey(const Slice& user_key);
  227. // Returns true iff we should stop building the current output
  228. // before processing "key".
  229. bool ShouldStopBefore(const InternalKey& key);
  230. // Release the input version for the compaction, once the compaction
  231. // is successful.
  232. void ReleaseInputs();
  233. private:
  234. friend class Version;
  235. friend class VersionSet;
  236. explicit Compaction(int level);
  237. int level_;
  238. uint64_t max_output_file_size_;
  239. Version* input_version_;
  240. VersionEdit edit_;
  241. // Each compaction reads inputs from "level_" and "level_+1"
  242. std::vector<FileMetaData*> inputs_[2]; // The two sets of inputs
  243. // State used to check for number of of overlapping grandparent files
  244. // (parent == level_ + 1, grandparent == level_ + 2)
  245. std::vector<FileMetaData*> grandparents_;
  246. int grandparent_index_; // Index in grandparent_starts_
  247. bool seen_key_; // Some output key has been seen
  248. int64_t overlapped_bytes_; // Bytes of overlap between current output
  249. // and grandparent files
  250. // State for implementing IsBaseLevelForKey
  251. // level_ptrs_ holds indices into input_version_->levels_: our state
  252. // is that we are positioned at one of the file ranges for each
  253. // higher level than the ones involved in this compaction (i.e. for
  254. // all L >= level_ + 2).
  255. int level_ptrs_[config::kNumLevels];
  256. };
  257. }
  258. #endif // STORAGE_LEVELDB_DB_VERSION_SET_H_