25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

308 satır
10 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. private:
  138. class Builder;
  139. friend class Compaction;
  140. friend class Version;
  141. Status Finalize(Version* v);
  142. // Delete any old versions that are no longer needed.
  143. void MaybeDeleteOldVersions();
  144. struct BySmallestKey;
  145. Status SortLevel(Version* v, uint64_t level);
  146. void GetOverlappingInputs(
  147. int level,
  148. const InternalKey& begin,
  149. const InternalKey& end,
  150. std::vector<FileMetaData*>* inputs);
  151. void GetRange(const std::vector<FileMetaData*>& inputs,
  152. InternalKey* smallest,
  153. InternalKey* largest);
  154. void GetRange2(const std::vector<FileMetaData*>& inputs1,
  155. const std::vector<FileMetaData*>& inputs2,
  156. InternalKey* smallest,
  157. InternalKey* largest);
  158. void SetupOtherInputs(Compaction* c);
  159. Env* const env_;
  160. const std::string dbname_;
  161. const Options* const options_;
  162. TableCache* const table_cache_;
  163. const InternalKeyComparator icmp_;
  164. uint64_t next_file_number_;
  165. uint64_t manifest_file_number_;
  166. uint64_t last_sequence_;
  167. uint64_t log_number_;
  168. uint64_t prev_log_number_; // 0 or backing store for memtable being compacted
  169. // Opened lazily
  170. WritableFile* descriptor_file_;
  171. log::Writer* descriptor_log_;
  172. // Versions are kept in a singly linked list that is never empty
  173. Version* current_; // Pointer to the last (newest) list entry
  174. Version* oldest_; // Pointer to the first (oldest) list entry
  175. // Per-level key at which the next compaction at that level should start.
  176. // Either an empty string, or a valid InternalKey.
  177. std::string compact_pointer_[config::kNumLevels];
  178. // No copying allowed
  179. VersionSet(const VersionSet&);
  180. void operator=(const VersionSet&);
  181. };
  182. // A Compaction encapsulates information about a compaction.
  183. class Compaction {
  184. public:
  185. ~Compaction();
  186. // Return the level that is being compacted. Inputs from "level"
  187. // and "level+1" will be merged to produce a set of "level+1" files.
  188. int level() const { return level_; }
  189. // Return the object that holds the edits to the descriptor done
  190. // by this compaction.
  191. VersionEdit* edit() { return &edit_; }
  192. // "which" must be either 0 or 1
  193. int num_input_files(int which) const { return inputs_[which].size(); }
  194. // Return the ith input file at "level()+which" ("which" must be 0 or 1).
  195. FileMetaData* input(int which, int i) const { return inputs_[which][i]; }
  196. // Maximum size of files to build during this compaction.
  197. uint64_t MaxOutputFileSize() const { return max_output_file_size_; }
  198. // Is this a trivial compaction that can be implemented by just
  199. // moving a single input file to the next level (no merging or splitting)
  200. bool IsTrivialMove() const;
  201. // Add all inputs to this compaction as delete operations to *edit.
  202. void AddInputDeletions(VersionEdit* edit);
  203. // Returns true if the information we have available guarantees that
  204. // the compaction is producing data in "level+1" for which no data exists
  205. // in levels greater than "level+1".
  206. bool IsBaseLevelForKey(const Slice& user_key);
  207. // Returns true iff we should stop building the current output
  208. // before processing "key".
  209. bool ShouldStopBefore(const InternalKey& key);
  210. // Release the input version for the compaction, once the compaction
  211. // is successful.
  212. void ReleaseInputs();
  213. private:
  214. friend class Version;
  215. friend class VersionSet;
  216. explicit Compaction(int level);
  217. int level_;
  218. uint64_t max_output_file_size_;
  219. Version* input_version_;
  220. VersionEdit edit_;
  221. // Each compaction reads inputs from "level_" and "level_+1"
  222. std::vector<FileMetaData*> inputs_[2]; // The two sets of inputs
  223. // State used to check for number of of overlapping grandparent files
  224. // (parent == level_ + 1, grandparent == level_ + 2)
  225. std::vector<FileMetaData*> grandparents_;
  226. size_t grandparent_index_; // Index in grandparent_starts_
  227. bool seen_key_; // Some output key has been seen
  228. int64_t overlapped_bytes_; // Bytes of overlap between current output
  229. // and grandparent files
  230. // State for implementing IsBaseLevelForKey
  231. // level_ptrs_ holds indices into input_version_->levels_: our state
  232. // is that we are positioned at one of the file ranges for each
  233. // higher level than the ones involved in this compaction (i.e. for
  234. // all L >= level_ + 2).
  235. size_t level_ptrs_[config::kNumLevels];
  236. };
  237. }
  238. #endif // STORAGE_LEVELDB_DB_VERSION_SET_H_