小组成员:10215300402-朱维清 & 10222140408 谷杰
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

398 linhas
14 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. #include "port/thread_annotations.h"
  23. namespace leveldb {
  24. namespace log { class Writer; }
  25. class Compaction;
  26. class Iterator;
  27. class MemTable;
  28. class TableBuilder;
  29. class TableCache;
  30. class Version;
  31. class VersionSet;
  32. class WritableFile;
  33. // Return the smallest index i such that files[i]->largest >= key.
  34. // Return files.size() if there is no such file.
  35. // REQUIRES: "files" contains a sorted list of non-overlapping files.
  36. extern int FindFile(const InternalKeyComparator& icmp,
  37. const std::vector<FileMetaData*>& files,
  38. const Slice& key);
  39. // Returns true iff some file in "files" overlaps the user key range
  40. // [*smallest,*largest].
  41. // smallest==NULL represents a key smaller than all keys in the DB.
  42. // largest==NULL represents a key largest than all keys in the DB.
  43. // REQUIRES: If disjoint_sorted_files, files[] contains disjoint ranges
  44. // in sorted order.
  45. extern bool SomeFileOverlapsRange(
  46. const InternalKeyComparator& icmp,
  47. bool disjoint_sorted_files,
  48. const std::vector<FileMetaData*>& files,
  49. const Slice* smallest_user_key,
  50. const Slice* largest_user_key);
  51. class Version {
  52. public:
  53. // Append to *iters a sequence of iterators that will
  54. // yield the contents of this Version when merged together.
  55. // REQUIRES: This version has been saved (see VersionSet::SaveTo)
  56. void AddIterators(const ReadOptions&, std::vector<Iterator*>* iters);
  57. // Lookup the value for key. If found, store it in *val and
  58. // return OK. Else return a non-OK status. Fills *stats.
  59. // REQUIRES: lock is not held
  60. struct GetStats {
  61. FileMetaData* seek_file;
  62. int seek_file_level;
  63. };
  64. Status Get(const ReadOptions&, const LookupKey& key, std::string* val,
  65. GetStats* stats);
  66. // Adds "stats" into the current state. Returns true if a new
  67. // compaction may need to be triggered, false otherwise.
  68. // REQUIRES: lock is held
  69. bool UpdateStats(const GetStats& stats);
  70. // Record a sample of bytes read at the specified internal key.
  71. // Samples are taken approximately once every config::kReadBytesPeriod
  72. // bytes. Returns true if a new compaction may need to be triggered.
  73. // REQUIRES: lock is held
  74. bool RecordReadSample(Slice key);
  75. // Reference count management (so Versions do not disappear out from
  76. // under live iterators)
  77. void Ref();
  78. void Unref();
  79. void GetOverlappingInputs(
  80. int level,
  81. const InternalKey* begin, // NULL means before all keys
  82. const InternalKey* end, // NULL means after all keys
  83. std::vector<FileMetaData*>* inputs);
  84. // Returns true iff some file in the specified level overlaps
  85. // some part of [*smallest_user_key,*largest_user_key].
  86. // smallest_user_key==NULL represents a key smaller than all keys in the DB.
  87. // largest_user_key==NULL represents a key largest than all keys in the DB.
  88. bool OverlapInLevel(int level,
  89. const Slice* smallest_user_key,
  90. const Slice* largest_user_key);
  91. // Return the level at which we should place a new memtable compaction
  92. // result that covers the range [smallest_user_key,largest_user_key].
  93. int PickLevelForMemTableOutput(const Slice& smallest_user_key,
  94. const Slice& largest_user_key);
  95. int NumFiles(int level) const { return files_[level].size(); }
  96. // Return a human readable string that describes this version's contents.
  97. std::string DebugString() const;
  98. private:
  99. friend class Compaction;
  100. friend class VersionSet;
  101. class LevelFileNumIterator;
  102. Iterator* NewConcatenatingIterator(const ReadOptions&, int level) const;
  103. // Call func(arg, level, f) for every file that overlaps user_key in
  104. // order from newest to oldest. If an invocation of func returns
  105. // false, makes no more calls.
  106. //
  107. // REQUIRES: user portion of internal_key == user_key.
  108. void ForEachOverlapping(Slice user_key, Slice internal_key,
  109. void* arg,
  110. bool (*func)(void*, int, FileMetaData*));
  111. VersionSet* vset_; // VersionSet to which this Version belongs
  112. Version* next_; // Next version in linked list
  113. Version* prev_; // Previous version in linked list
  114. int refs_; // Number of live refs to this version
  115. // List of files per level
  116. std::vector<FileMetaData*> files_[config::kNumLevels];
  117. // Next file to compact based on seek stats.
  118. FileMetaData* file_to_compact_;
  119. int file_to_compact_level_;
  120. // Level that should be compacted next and its compaction score.
  121. // Score < 1 means compaction is not strictly needed. These fields
  122. // are initialized by Finalize().
  123. double compaction_score_;
  124. int compaction_level_;
  125. explicit Version(VersionSet* vset)
  126. : vset_(vset), next_(this), prev_(this), refs_(0),
  127. file_to_compact_(NULL),
  128. file_to_compact_level_(-1),
  129. compaction_score_(-1),
  130. compaction_level_(-1) {
  131. }
  132. ~Version();
  133. // No copying allowed
  134. Version(const Version&);
  135. void operator=(const Version&);
  136. };
  137. class VersionSet {
  138. public:
  139. VersionSet(const std::string& dbname,
  140. const Options* options,
  141. TableCache* table_cache,
  142. const InternalKeyComparator*);
  143. ~VersionSet();
  144. // Apply *edit to the current version to form a new descriptor that
  145. // is both saved to persistent state and installed as the new
  146. // current version. Will release *mu while actually writing to the file.
  147. // REQUIRES: *mu is held on entry.
  148. // REQUIRES: no other thread concurrently calls LogAndApply()
  149. Status LogAndApply(VersionEdit* edit, port::Mutex* mu)
  150. EXCLUSIVE_LOCKS_REQUIRED(mu);
  151. // Recover the last saved descriptor from persistent storage.
  152. Status Recover(bool *save_manifest);
  153. // Return the current version.
  154. Version* current() const { return current_; }
  155. // Return the current manifest file number
  156. uint64_t ManifestFileNumber() const { return manifest_file_number_; }
  157. // Allocate and return a new file number
  158. uint64_t NewFileNumber() { return next_file_number_++; }
  159. // Arrange to reuse "file_number" unless a newer file number has
  160. // already been allocated.
  161. // REQUIRES: "file_number" was returned by a call to NewFileNumber().
  162. void ReuseFileNumber(uint64_t file_number) {
  163. if (next_file_number_ == file_number + 1) {
  164. next_file_number_ = file_number;
  165. }
  166. }
  167. // Return the number of Table files at the specified level.
  168. int NumLevelFiles(int level) const;
  169. // Return the combined file size of all files at the specified level.
  170. int64_t NumLevelBytes(int level) const;
  171. // Return the last sequence number.
  172. uint64_t LastSequence() const { return last_sequence_; }
  173. // Set the last sequence number to s.
  174. void SetLastSequence(uint64_t s) {
  175. assert(s >= last_sequence_);
  176. last_sequence_ = s;
  177. }
  178. // Mark the specified file number as used.
  179. void MarkFileNumberUsed(uint64_t number);
  180. // Return the current log file number.
  181. uint64_t LogNumber() const { return log_number_; }
  182. // Return the log file number for the log file that is currently
  183. // being compacted, or zero if there is no such log file.
  184. uint64_t PrevLogNumber() const { return prev_log_number_; }
  185. // Pick level and inputs for a new compaction.
  186. // Returns NULL if there is no compaction to be done.
  187. // Otherwise returns a pointer to a heap-allocated object that
  188. // describes the compaction. Caller should delete the result.
  189. Compaction* PickCompaction();
  190. // Return a compaction object for compacting the range [begin,end] in
  191. // the specified level. Returns NULL if there is nothing in that
  192. // level that overlaps the specified range. Caller should delete
  193. // the result.
  194. Compaction* CompactRange(
  195. int level,
  196. const InternalKey* begin,
  197. const InternalKey* end);
  198. // Return the maximum overlapping data (in bytes) at next level for any
  199. // file at a level >= 1.
  200. int64_t MaxNextLevelOverlappingBytes();
  201. // Create an iterator that reads over the compaction inputs for "*c".
  202. // The caller should delete the iterator when no longer needed.
  203. Iterator* MakeInputIterator(Compaction* c);
  204. // Returns true iff some level needs a compaction.
  205. bool NeedsCompaction() const {
  206. Version* v = current_;
  207. return (v->compaction_score_ >= 1) || (v->file_to_compact_ != NULL);
  208. }
  209. // Add all files listed in any live version to *live.
  210. // May also mutate some internal state.
  211. void AddLiveFiles(std::set<uint64_t>* live);
  212. // Return the approximate offset in the database of the data for
  213. // "key" as of version "v".
  214. uint64_t ApproximateOffsetOf(Version* v, const InternalKey& key);
  215. // Return a human-readable short (single-line) summary of the number
  216. // of files per level. Uses *scratch as backing store.
  217. struct LevelSummaryStorage {
  218. char buffer[100];
  219. };
  220. const char* LevelSummary(LevelSummaryStorage* scratch) const;
  221. private:
  222. class Builder;
  223. friend class Compaction;
  224. friend class Version;
  225. bool ReuseManifest(const std::string& dscname, const std::string& dscbase);
  226. void Finalize(Version* v);
  227. void GetRange(const std::vector<FileMetaData*>& inputs,
  228. InternalKey* smallest,
  229. InternalKey* largest);
  230. void GetRange2(const std::vector<FileMetaData*>& inputs1,
  231. const std::vector<FileMetaData*>& inputs2,
  232. InternalKey* smallest,
  233. InternalKey* largest);
  234. void SetupOtherInputs(Compaction* c);
  235. // Save current contents to *log
  236. Status WriteSnapshot(log::Writer* log);
  237. void AppendVersion(Version* v);
  238. Env* const env_;
  239. const std::string dbname_;
  240. const Options* const options_;
  241. TableCache* const table_cache_;
  242. const InternalKeyComparator icmp_;
  243. uint64_t next_file_number_;
  244. uint64_t manifest_file_number_;
  245. uint64_t last_sequence_;
  246. uint64_t log_number_;
  247. uint64_t prev_log_number_; // 0 or backing store for memtable being compacted
  248. // Opened lazily
  249. WritableFile* descriptor_file_;
  250. log::Writer* descriptor_log_;
  251. Version dummy_versions_; // Head of circular doubly-linked list of versions.
  252. Version* current_; // == dummy_versions_.prev_
  253. // Per-level key at which the next compaction at that level should start.
  254. // Either an empty string, or a valid InternalKey.
  255. std::string compact_pointer_[config::kNumLevels];
  256. // No copying allowed
  257. VersionSet(const VersionSet&);
  258. void operator=(const VersionSet&);
  259. };
  260. // A Compaction encapsulates information about a compaction.
  261. class Compaction {
  262. public:
  263. ~Compaction();
  264. // Return the level that is being compacted. Inputs from "level"
  265. // and "level+1" will be merged to produce a set of "level+1" files.
  266. int level() const { return level_; }
  267. // Return the object that holds the edits to the descriptor done
  268. // by this compaction.
  269. VersionEdit* edit() { return &edit_; }
  270. // "which" must be either 0 or 1
  271. int num_input_files(int which) const { return inputs_[which].size(); }
  272. // Return the ith input file at "level()+which" ("which" must be 0 or 1).
  273. FileMetaData* input(int which, int i) const { return inputs_[which][i]; }
  274. // Maximum size of files to build during this compaction.
  275. uint64_t MaxOutputFileSize() const { return max_output_file_size_; }
  276. // Is this a trivial compaction that can be implemented by just
  277. // moving a single input file to the next level (no merging or splitting)
  278. bool IsTrivialMove() const;
  279. // Add all inputs to this compaction as delete operations to *edit.
  280. void AddInputDeletions(VersionEdit* edit);
  281. // Returns true if the information we have available guarantees that
  282. // the compaction is producing data in "level+1" for which no data exists
  283. // in levels greater than "level+1".
  284. bool IsBaseLevelForKey(const Slice& user_key);
  285. // Returns true iff we should stop building the current output
  286. // before processing "internal_key".
  287. bool ShouldStopBefore(const Slice& internal_key);
  288. // Release the input version for the compaction, once the compaction
  289. // is successful.
  290. void ReleaseInputs();
  291. private:
  292. friend class Version;
  293. friend class VersionSet;
  294. explicit Compaction(int level);
  295. int level_;
  296. uint64_t max_output_file_size_;
  297. Version* input_version_;
  298. VersionEdit edit_;
  299. // Each compaction reads inputs from "level_" and "level_+1"
  300. std::vector<FileMetaData*> inputs_[2]; // The two sets of inputs
  301. // State used to check for number of of overlapping grandparent files
  302. // (parent == level_ + 1, grandparent == level_ + 2)
  303. std::vector<FileMetaData*> grandparents_;
  304. size_t grandparent_index_; // Index in grandparent_starts_
  305. bool seen_key_; // Some output key has been seen
  306. int64_t overlapped_bytes_; // Bytes of overlap between current output
  307. // and grandparent files
  308. // State for implementing IsBaseLevelForKey
  309. // level_ptrs_ holds indices into input_version_->levels_: our state
  310. // is that we are positioned at one of the file ranges for each
  311. // higher level than the ones involved in this compaction (i.e. for
  312. // all L >= level_ + 2).
  313. size_t level_ptrs_[config::kNumLevels];
  314. };
  315. } // namespace leveldb
  316. #endif // STORAGE_LEVELDB_DB_VERSION_SET_H_