作者: 韩晨旭 10225101440 李畅 10225102463
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1533 řádky
48 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. #include "db/version_set.h"
  5. #include <algorithm>
  6. #include <stdio.h>
  7. #include "db/filename.h"
  8. #include "db/log_reader.h"
  9. #include "db/log_writer.h"
  10. #include "db/memtable.h"
  11. #include "db/table_cache.h"
  12. #include "leveldb/env.h"
  13. #include "leveldb/table_builder.h"
  14. #include "table/merger.h"
  15. #include "table/two_level_iterator.h"
  16. #include "util/coding.h"
  17. #include "util/logging.h"
  18. namespace leveldb {
  19. static size_t TargetFileSize(const Options* options) {
  20. return options->max_file_size;
  21. }
  22. // Maximum bytes of overlaps in grandparent (i.e., level+2) before we
  23. // stop building a single file in a level->level+1 compaction.
  24. static int64_t MaxGrandParentOverlapBytes(const Options* options) {
  25. return 10 * TargetFileSize(options);
  26. }
  27. // Maximum number of bytes in all compacted files. We avoid expanding
  28. // the lower level file set of a compaction if it would make the
  29. // total compaction cover more than this many bytes.
  30. static int64_t ExpandedCompactionByteSizeLimit(const Options* options) {
  31. return 25 * TargetFileSize(options);
  32. }
  33. static double MaxBytesForLevel(const Options* options, int level) {
  34. // Note: the result for level zero is not really used since we set
  35. // the level-0 compaction threshold based on number of files.
  36. // Result for both level-0 and level-1
  37. double result = 10. * 1048576.0;
  38. while (level > 1) {
  39. result *= 10;
  40. level--;
  41. }
  42. return result;
  43. }
  44. static uint64_t MaxFileSizeForLevel(const Options* options, int level) {
  45. // We could vary per level to reduce number of files?
  46. return TargetFileSize(options);
  47. }
  48. static int64_t TotalFileSize(const std::vector<FileMetaData*>& files) {
  49. int64_t sum = 0;
  50. for (size_t i = 0; i < files.size(); i++) {
  51. sum += files[i]->file_size;
  52. }
  53. return sum;
  54. }
  55. Version::~Version() {
  56. assert(refs_ == 0);
  57. // Remove from linked list
  58. prev_->next_ = next_;
  59. next_->prev_ = prev_;
  60. // Drop references to files
  61. for (int level = 0; level < config::kNumLevels; level++) {
  62. for (size_t i = 0; i < files_[level].size(); i++) {
  63. FileMetaData* f = files_[level][i];
  64. assert(f->refs > 0);
  65. f->refs--;
  66. if (f->refs <= 0) {
  67. delete f;
  68. }
  69. }
  70. }
  71. }
  72. int FindFile(const InternalKeyComparator& icmp,
  73. const std::vector<FileMetaData*>& files,
  74. const Slice& key) {
  75. uint32_t left = 0;
  76. uint32_t right = files.size();
  77. while (left < right) {
  78. uint32_t mid = (left + right) / 2;
  79. const FileMetaData* f = files[mid];
  80. if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) {
  81. // Key at "mid.largest" is < "target". Therefore all
  82. // files at or before "mid" are uninteresting.
  83. left = mid + 1;
  84. } else {
  85. // Key at "mid.largest" is >= "target". Therefore all files
  86. // after "mid" are uninteresting.
  87. right = mid;
  88. }
  89. }
  90. return right;
  91. }
  92. static bool AfterFile(const Comparator* ucmp,
  93. const Slice* user_key, const FileMetaData* f) {
  94. // null user_key occurs before all keys and is therefore never after *f
  95. return (user_key != nullptr &&
  96. ucmp->Compare(*user_key, f->largest.user_key()) > 0);
  97. }
  98. static bool BeforeFile(const Comparator* ucmp,
  99. const Slice* user_key, const FileMetaData* f) {
  100. // null user_key occurs after all keys and is therefore never before *f
  101. return (user_key != nullptr &&
  102. ucmp->Compare(*user_key, f->smallest.user_key()) < 0);
  103. }
  104. bool SomeFileOverlapsRange(
  105. const InternalKeyComparator& icmp,
  106. bool disjoint_sorted_files,
  107. const std::vector<FileMetaData*>& files,
  108. const Slice* smallest_user_key,
  109. const Slice* largest_user_key) {
  110. const Comparator* ucmp = icmp.user_comparator();
  111. if (!disjoint_sorted_files) {
  112. // Need to check against all files
  113. for (size_t i = 0; i < files.size(); i++) {
  114. const FileMetaData* f = files[i];
  115. if (AfterFile(ucmp, smallest_user_key, f) ||
  116. BeforeFile(ucmp, largest_user_key, f)) {
  117. // No overlap
  118. } else {
  119. return true; // Overlap
  120. }
  121. }
  122. return false;
  123. }
  124. // Binary search over file list
  125. uint32_t index = 0;
  126. if (smallest_user_key != nullptr) {
  127. // Find the earliest possible internal key for smallest_user_key
  128. InternalKey small(*smallest_user_key, kMaxSequenceNumber,kValueTypeForSeek);
  129. index = FindFile(icmp, files, small.Encode());
  130. }
  131. if (index >= files.size()) {
  132. // beginning of range is after all files, so no overlap.
  133. return false;
  134. }
  135. return !BeforeFile(ucmp, largest_user_key, files[index]);
  136. }
  137. // An internal iterator. For a given version/level pair, yields
  138. // information about the files in the level. For a given entry, key()
  139. // is the largest key that occurs in the file, and value() is an
  140. // 16-byte value containing the file number and file size, both
  141. // encoded using EncodeFixed64.
  142. class Version::LevelFileNumIterator : public Iterator {
  143. public:
  144. LevelFileNumIterator(const InternalKeyComparator& icmp,
  145. const std::vector<FileMetaData*>* flist)
  146. : icmp_(icmp),
  147. flist_(flist),
  148. index_(flist->size()) { // Marks as invalid
  149. }
  150. virtual bool Valid() const {
  151. return index_ < flist_->size();
  152. }
  153. virtual void Seek(const Slice& target) {
  154. index_ = FindFile(icmp_, *flist_, target);
  155. }
  156. virtual void SeekToFirst() { index_ = 0; }
  157. virtual void SeekToLast() {
  158. index_ = flist_->empty() ? 0 : flist_->size() - 1;
  159. }
  160. virtual void Next() {
  161. assert(Valid());
  162. index_++;
  163. }
  164. virtual void Prev() {
  165. assert(Valid());
  166. if (index_ == 0) {
  167. index_ = flist_->size(); // Marks as invalid
  168. } else {
  169. index_--;
  170. }
  171. }
  172. Slice key() const {
  173. assert(Valid());
  174. return (*flist_)[index_]->largest.Encode();
  175. }
  176. Slice value() const {
  177. assert(Valid());
  178. EncodeFixed64(value_buf_, (*flist_)[index_]->number);
  179. EncodeFixed64(value_buf_+8, (*flist_)[index_]->file_size);
  180. return Slice(value_buf_, sizeof(value_buf_));
  181. }
  182. virtual Status status() const { return Status::OK(); }
  183. private:
  184. const InternalKeyComparator icmp_;
  185. const std::vector<FileMetaData*>* const flist_;
  186. uint32_t index_;
  187. // Backing store for value(). Holds the file number and size.
  188. mutable char value_buf_[16];
  189. };
  190. static Iterator* GetFileIterator(void* arg,
  191. const ReadOptions& options,
  192. const Slice& file_value) {
  193. TableCache* cache = reinterpret_cast<TableCache*>(arg);
  194. if (file_value.size() != 16) {
  195. return NewErrorIterator(
  196. Status::Corruption("FileReader invoked with unexpected value"));
  197. } else {
  198. return cache->NewIterator(options,
  199. DecodeFixed64(file_value.data()),
  200. DecodeFixed64(file_value.data() + 8));
  201. }
  202. }
  203. Iterator* Version::NewConcatenatingIterator(const ReadOptions& options,
  204. int level) const {
  205. return NewTwoLevelIterator(
  206. new LevelFileNumIterator(vset_->icmp_, &files_[level]),
  207. &GetFileIterator, vset_->table_cache_, options);
  208. }
  209. void Version::AddIterators(const ReadOptions& options,
  210. std::vector<Iterator*>* iters) {
  211. // Merge all level zero files together since they may overlap
  212. for (size_t i = 0; i < files_[0].size(); i++) {
  213. iters->push_back(
  214. vset_->table_cache_->NewIterator(
  215. options, files_[0][i]->number, files_[0][i]->file_size));
  216. }
  217. // For levels > 0, we can use a concatenating iterator that sequentially
  218. // walks through the non-overlapping files in the level, opening them
  219. // lazily.
  220. for (int level = 1; level < config::kNumLevels; level++) {
  221. if (!files_[level].empty()) {
  222. iters->push_back(NewConcatenatingIterator(options, level));
  223. }
  224. }
  225. }
  226. // Callback from TableCache::Get()
  227. namespace {
  228. enum SaverState {
  229. kNotFound,
  230. kFound,
  231. kDeleted,
  232. kCorrupt,
  233. };
  234. struct Saver {
  235. SaverState state;
  236. const Comparator* ucmp;
  237. Slice user_key;
  238. std::string* value;
  239. };
  240. }
  241. static void SaveValue(void* arg, const Slice& ikey, const Slice& v) {
  242. Saver* s = reinterpret_cast<Saver*>(arg);
  243. ParsedInternalKey parsed_key;
  244. if (!ParseInternalKey(ikey, &parsed_key)) {
  245. s->state = kCorrupt;
  246. } else {
  247. if (s->ucmp->Compare(parsed_key.user_key, s->user_key) == 0) {
  248. s->state = (parsed_key.type == kTypeValue) ? kFound : kDeleted;
  249. if (s->state == kFound) {
  250. s->value->assign(v.data(), v.size());
  251. }
  252. }
  253. }
  254. }
  255. static bool NewestFirst(FileMetaData* a, FileMetaData* b) {
  256. return a->number > b->number;
  257. }
  258. void Version::ForEachOverlapping(Slice user_key, Slice internal_key,
  259. void* arg,
  260. bool (*func)(void*, int, FileMetaData*)) {
  261. // TODO(sanjay): Change Version::Get() to use this function.
  262. const Comparator* ucmp = vset_->icmp_.user_comparator();
  263. // Search level-0 in order from newest to oldest.
  264. std::vector<FileMetaData*> tmp;
  265. tmp.reserve(files_[0].size());
  266. for (uint32_t i = 0; i < files_[0].size(); i++) {
  267. FileMetaData* f = files_[0][i];
  268. if (ucmp->Compare(user_key, f->smallest.user_key()) >= 0 &&
  269. ucmp->Compare(user_key, f->largest.user_key()) <= 0) {
  270. tmp.push_back(f);
  271. }
  272. }
  273. if (!tmp.empty()) {
  274. std::sort(tmp.begin(), tmp.end(), NewestFirst);
  275. for (uint32_t i = 0; i < tmp.size(); i++) {
  276. if (!(*func)(arg, 0, tmp[i])) {
  277. return;
  278. }
  279. }
  280. }
  281. // Search other levels.
  282. for (int level = 1; level < config::kNumLevels; level++) {
  283. size_t num_files = files_[level].size();
  284. if (num_files == 0) continue;
  285. // Binary search to find earliest index whose largest key >= internal_key.
  286. uint32_t index = FindFile(vset_->icmp_, files_[level], internal_key);
  287. if (index < num_files) {
  288. FileMetaData* f = files_[level][index];
  289. if (ucmp->Compare(user_key, f->smallest.user_key()) < 0) {
  290. // All of "f" is past any data for user_key
  291. } else {
  292. if (!(*func)(arg, level, f)) {
  293. return;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. Status Version::Get(const ReadOptions& options,
  300. const LookupKey& k,
  301. std::string* value,
  302. GetStats* stats) {
  303. Slice ikey = k.internal_key();
  304. Slice user_key = k.user_key();
  305. const Comparator* ucmp = vset_->icmp_.user_comparator();
  306. Status s;
  307. stats->seek_file = nullptr;
  308. stats->seek_file_level = -1;
  309. FileMetaData* last_file_read = nullptr;
  310. int last_file_read_level = -1;
  311. // We can search level-by-level since entries never hop across
  312. // levels. Therefore we are guaranteed that if we find data
  313. // in an smaller level, later levels are irrelevant.
  314. std::vector<FileMetaData*> tmp;
  315. FileMetaData* tmp2;
  316. for (int level = 0; level < config::kNumLevels; level++) {
  317. size_t num_files = files_[level].size();
  318. if (num_files == 0) continue;
  319. // Get the list of files to search in this level
  320. FileMetaData* const* files = &files_[level][0];
  321. if (level == 0) {
  322. // Level-0 files may overlap each other. Find all files that
  323. // overlap user_key and process them in order from newest to oldest.
  324. tmp.reserve(num_files);
  325. for (uint32_t i = 0; i < num_files; i++) {
  326. FileMetaData* f = files[i];
  327. if (ucmp->Compare(user_key, f->smallest.user_key()) >= 0 &&
  328. ucmp->Compare(user_key, f->largest.user_key()) <= 0) {
  329. tmp.push_back(f);
  330. }
  331. }
  332. if (tmp.empty()) continue;
  333. std::sort(tmp.begin(), tmp.end(), NewestFirst);
  334. files = &tmp[0];
  335. num_files = tmp.size();
  336. } else {
  337. // Binary search to find earliest index whose largest key >= ikey.
  338. uint32_t index = FindFile(vset_->icmp_, files_[level], ikey);
  339. if (index >= num_files) {
  340. files = nullptr;
  341. num_files = 0;
  342. } else {
  343. tmp2 = files[index];
  344. if (ucmp->Compare(user_key, tmp2->smallest.user_key()) < 0) {
  345. // All of "tmp2" is past any data for user_key
  346. files = nullptr;
  347. num_files = 0;
  348. } else {
  349. files = &tmp2;
  350. num_files = 1;
  351. }
  352. }
  353. }
  354. for (uint32_t i = 0; i < num_files; ++i) {
  355. if (last_file_read != nullptr && stats->seek_file == nullptr) {
  356. // We have had more than one seek for this read. Charge the 1st file.
  357. stats->seek_file = last_file_read;
  358. stats->seek_file_level = last_file_read_level;
  359. }
  360. FileMetaData* f = files[i];
  361. last_file_read = f;
  362. last_file_read_level = level;
  363. Saver saver;
  364. saver.state = kNotFound;
  365. saver.ucmp = ucmp;
  366. saver.user_key = user_key;
  367. saver.value = value;
  368. s = vset_->table_cache_->Get(options, f->number, f->file_size,
  369. ikey, &saver, SaveValue);
  370. if (!s.ok()) {
  371. return s;
  372. }
  373. switch (saver.state) {
  374. case kNotFound:
  375. break; // Keep searching in other files
  376. case kFound:
  377. return s;
  378. case kDeleted:
  379. s = Status::NotFound(Slice()); // Use empty error message for speed
  380. return s;
  381. case kCorrupt:
  382. s = Status::Corruption("corrupted key for ", user_key);
  383. return s;
  384. }
  385. }
  386. }
  387. return Status::NotFound(Slice()); // Use an empty error message for speed
  388. }
  389. bool Version::UpdateStats(const GetStats& stats) {
  390. FileMetaData* f = stats.seek_file;
  391. if (f != nullptr) {
  392. f->allowed_seeks--;
  393. if (f->allowed_seeks <= 0 && file_to_compact_ == nullptr) {
  394. file_to_compact_ = f;
  395. file_to_compact_level_ = stats.seek_file_level;
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. bool Version::RecordReadSample(Slice internal_key) {
  402. ParsedInternalKey ikey;
  403. if (!ParseInternalKey(internal_key, &ikey)) {
  404. return false;
  405. }
  406. struct State {
  407. GetStats stats; // Holds first matching file
  408. int matches;
  409. static bool Match(void* arg, int level, FileMetaData* f) {
  410. State* state = reinterpret_cast<State*>(arg);
  411. state->matches++;
  412. if (state->matches == 1) {
  413. // Remember first match.
  414. state->stats.seek_file = f;
  415. state->stats.seek_file_level = level;
  416. }
  417. // We can stop iterating once we have a second match.
  418. return state->matches < 2;
  419. }
  420. };
  421. State state;
  422. state.matches = 0;
  423. ForEachOverlapping(ikey.user_key, internal_key, &state, &State::Match);
  424. // Must have at least two matches since we want to merge across
  425. // files. But what if we have a single file that contains many
  426. // overwrites and deletions? Should we have another mechanism for
  427. // finding such files?
  428. if (state.matches >= 2) {
  429. // 1MB cost is about 1 seek (see comment in Builder::Apply).
  430. return UpdateStats(state.stats);
  431. }
  432. return false;
  433. }
  434. void Version::Ref() {
  435. ++refs_;
  436. }
  437. void Version::Unref() {
  438. assert(this != &vset_->dummy_versions_);
  439. assert(refs_ >= 1);
  440. --refs_;
  441. if (refs_ == 0) {
  442. delete this;
  443. }
  444. }
  445. bool Version::OverlapInLevel(int level,
  446. const Slice* smallest_user_key,
  447. const Slice* largest_user_key) {
  448. return SomeFileOverlapsRange(vset_->icmp_, (level > 0), files_[level],
  449. smallest_user_key, largest_user_key);
  450. }
  451. int Version::PickLevelForMemTableOutput(
  452. const Slice& smallest_user_key,
  453. const Slice& largest_user_key) {
  454. int level = 0;
  455. if (!OverlapInLevel(0, &smallest_user_key, &largest_user_key)) {
  456. // Push to next level if there is no overlap in next level,
  457. // and the #bytes overlapping in the level after that are limited.
  458. InternalKey start(smallest_user_key, kMaxSequenceNumber, kValueTypeForSeek);
  459. InternalKey limit(largest_user_key, 0, static_cast<ValueType>(0));
  460. std::vector<FileMetaData*> overlaps;
  461. while (level < config::kMaxMemCompactLevel) {
  462. if (OverlapInLevel(level + 1, &smallest_user_key, &largest_user_key)) {
  463. break;
  464. }
  465. if (level + 2 < config::kNumLevels) {
  466. // Check that file does not overlap too many grandparent bytes.
  467. GetOverlappingInputs(level + 2, &start, &limit, &overlaps);
  468. const int64_t sum = TotalFileSize(overlaps);
  469. if (sum > MaxGrandParentOverlapBytes(vset_->options_)) {
  470. break;
  471. }
  472. }
  473. level++;
  474. }
  475. }
  476. return level;
  477. }
  478. // Store in "*inputs" all files in "level" that overlap [begin,end]
  479. void Version::GetOverlappingInputs(
  480. int level,
  481. const InternalKey* begin,
  482. const InternalKey* end,
  483. std::vector<FileMetaData*>* inputs) {
  484. assert(level >= 0);
  485. assert(level < config::kNumLevels);
  486. inputs->clear();
  487. Slice user_begin, user_end;
  488. if (begin != nullptr) {
  489. user_begin = begin->user_key();
  490. }
  491. if (end != nullptr) {
  492. user_end = end->user_key();
  493. }
  494. const Comparator* user_cmp = vset_->icmp_.user_comparator();
  495. for (size_t i = 0; i < files_[level].size(); ) {
  496. FileMetaData* f = files_[level][i++];
  497. const Slice file_start = f->smallest.user_key();
  498. const Slice file_limit = f->largest.user_key();
  499. if (begin != nullptr && user_cmp->Compare(file_limit, user_begin) < 0) {
  500. // "f" is completely before specified range; skip it
  501. } else if (end != nullptr && user_cmp->Compare(file_start, user_end) > 0) {
  502. // "f" is completely after specified range; skip it
  503. } else {
  504. inputs->push_back(f);
  505. if (level == 0) {
  506. // Level-0 files may overlap each other. So check if the newly
  507. // added file has expanded the range. If so, restart search.
  508. if (begin != nullptr && user_cmp->Compare(file_start, user_begin) < 0) {
  509. user_begin = file_start;
  510. inputs->clear();
  511. i = 0;
  512. } else if (end != nullptr && user_cmp->Compare(file_limit,
  513. user_end) > 0) {
  514. user_end = file_limit;
  515. inputs->clear();
  516. i = 0;
  517. }
  518. }
  519. }
  520. }
  521. }
  522. std::string Version::DebugString() const {
  523. std::string r;
  524. for (int level = 0; level < config::kNumLevels; level++) {
  525. // E.g.,
  526. // --- level 1 ---
  527. // 17:123['a' .. 'd']
  528. // 20:43['e' .. 'g']
  529. r.append("--- level ");
  530. AppendNumberTo(&r, level);
  531. r.append(" ---\n");
  532. const std::vector<FileMetaData*>& files = files_[level];
  533. for (size_t i = 0; i < files.size(); i++) {
  534. r.push_back(' ');
  535. AppendNumberTo(&r, files[i]->number);
  536. r.push_back(':');
  537. AppendNumberTo(&r, files[i]->file_size);
  538. r.append("[");
  539. r.append(files[i]->smallest.DebugString());
  540. r.append(" .. ");
  541. r.append(files[i]->largest.DebugString());
  542. r.append("]\n");
  543. }
  544. }
  545. return r;
  546. }
  547. // A helper class so we can efficiently apply a whole sequence
  548. // of edits to a particular state without creating intermediate
  549. // Versions that contain full copies of the intermediate state.
  550. class VersionSet::Builder {
  551. private:
  552. // Helper to sort by v->files_[file_number].smallest
  553. struct BySmallestKey {
  554. const InternalKeyComparator* internal_comparator;
  555. bool operator()(FileMetaData* f1, FileMetaData* f2) const {
  556. int r = internal_comparator->Compare(f1->smallest, f2->smallest);
  557. if (r != 0) {
  558. return (r < 0);
  559. } else {
  560. // Break ties by file number
  561. return (f1->number < f2->number);
  562. }
  563. }
  564. };
  565. typedef std::set<FileMetaData*, BySmallestKey> FileSet;
  566. struct LevelState {
  567. std::set<uint64_t> deleted_files;
  568. FileSet* added_files;
  569. };
  570. VersionSet* vset_;
  571. Version* base_;
  572. LevelState levels_[config::kNumLevels];
  573. public:
  574. // Initialize a builder with the files from *base and other info from *vset
  575. Builder(VersionSet* vset, Version* base)
  576. : vset_(vset),
  577. base_(base) {
  578. base_->Ref();
  579. BySmallestKey cmp;
  580. cmp.internal_comparator = &vset_->icmp_;
  581. for (int level = 0; level < config::kNumLevels; level++) {
  582. levels_[level].added_files = new FileSet(cmp);
  583. }
  584. }
  585. ~Builder() {
  586. for (int level = 0; level < config::kNumLevels; level++) {
  587. const FileSet* added = levels_[level].added_files;
  588. std::vector<FileMetaData*> to_unref;
  589. to_unref.reserve(added->size());
  590. for (FileSet::const_iterator it = added->begin();
  591. it != added->end(); ++it) {
  592. to_unref.push_back(*it);
  593. }
  594. delete added;
  595. for (uint32_t i = 0; i < to_unref.size(); i++) {
  596. FileMetaData* f = to_unref[i];
  597. f->refs--;
  598. if (f->refs <= 0) {
  599. delete f;
  600. }
  601. }
  602. }
  603. base_->Unref();
  604. }
  605. // Apply all of the edits in *edit to the current state.
  606. void Apply(VersionEdit* edit) {
  607. // Update compaction pointers
  608. for (size_t i = 0; i < edit->compact_pointers_.size(); i++) {
  609. const int level = edit->compact_pointers_[i].first;
  610. vset_->compact_pointer_[level] =
  611. edit->compact_pointers_[i].second.Encode().ToString();
  612. }
  613. // Delete files
  614. const VersionEdit::DeletedFileSet& del = edit->deleted_files_;
  615. for (VersionEdit::DeletedFileSet::const_iterator iter = del.begin();
  616. iter != del.end();
  617. ++iter) {
  618. const int level = iter->first;
  619. const uint64_t number = iter->second;
  620. levels_[level].deleted_files.insert(number);
  621. }
  622. // Add new files
  623. for (size_t i = 0; i < edit->new_files_.size(); i++) {
  624. const int level = edit->new_files_[i].first;
  625. FileMetaData* f = new FileMetaData(edit->new_files_[i].second);
  626. f->refs = 1;
  627. // We arrange to automatically compact this file after
  628. // a certain number of seeks. Let's assume:
  629. // (1) One seek costs 10ms
  630. // (2) Writing or reading 1MB costs 10ms (100MB/s)
  631. // (3) A compaction of 1MB does 25MB of IO:
  632. // 1MB read from this level
  633. // 10-12MB read from next level (boundaries may be misaligned)
  634. // 10-12MB written to next level
  635. // This implies that 25 seeks cost the same as the compaction
  636. // of 1MB of data. I.e., one seek costs approximately the
  637. // same as the compaction of 40KB of data. We are a little
  638. // conservative and allow approximately one seek for every 16KB
  639. // of data before triggering a compaction.
  640. f->allowed_seeks = (f->file_size / 16384);
  641. if (f->allowed_seeks < 100) f->allowed_seeks = 100;
  642. levels_[level].deleted_files.erase(f->number);
  643. levels_[level].added_files->insert(f);
  644. }
  645. }
  646. // Save the current state in *v.
  647. void SaveTo(Version* v) {
  648. BySmallestKey cmp;
  649. cmp.internal_comparator = &vset_->icmp_;
  650. for (int level = 0; level < config::kNumLevels; level++) {
  651. // Merge the set of added files with the set of pre-existing files.
  652. // Drop any deleted files. Store the result in *v.
  653. const std::vector<FileMetaData*>& base_files = base_->files_[level];
  654. std::vector<FileMetaData*>::const_iterator base_iter = base_files.begin();
  655. std::vector<FileMetaData*>::const_iterator base_end = base_files.end();
  656. const FileSet* added = levels_[level].added_files;
  657. v->files_[level].reserve(base_files.size() + added->size());
  658. for (FileSet::const_iterator added_iter = added->begin();
  659. added_iter != added->end();
  660. ++added_iter) {
  661. // Add all smaller files listed in base_
  662. for (std::vector<FileMetaData*>::const_iterator bpos
  663. = std::upper_bound(base_iter, base_end, *added_iter, cmp);
  664. base_iter != bpos;
  665. ++base_iter) {
  666. MaybeAddFile(v, level, *base_iter);
  667. }
  668. MaybeAddFile(v, level, *added_iter);
  669. }
  670. // Add remaining base files
  671. for (; base_iter != base_end; ++base_iter) {
  672. MaybeAddFile(v, level, *base_iter);
  673. }
  674. #ifndef NDEBUG
  675. // Make sure there is no overlap in levels > 0
  676. if (level > 0) {
  677. for (uint32_t i = 1; i < v->files_[level].size(); i++) {
  678. const InternalKey& prev_end = v->files_[level][i-1]->largest;
  679. const InternalKey& this_begin = v->files_[level][i]->smallest;
  680. if (vset_->icmp_.Compare(prev_end, this_begin) >= 0) {
  681. fprintf(stderr, "overlapping ranges in same level %s vs. %s\n",
  682. prev_end.DebugString().c_str(),
  683. this_begin.DebugString().c_str());
  684. abort();
  685. }
  686. }
  687. }
  688. #endif
  689. }
  690. }
  691. void MaybeAddFile(Version* v, int level, FileMetaData* f) {
  692. if (levels_[level].deleted_files.count(f->number) > 0) {
  693. // File is deleted: do nothing
  694. } else {
  695. std::vector<FileMetaData*>* files = &v->files_[level];
  696. if (level > 0 && !files->empty()) {
  697. // Must not overlap
  698. assert(vset_->icmp_.Compare((*files)[files->size()-1]->largest,
  699. f->smallest) < 0);
  700. }
  701. f->refs++;
  702. files->push_back(f);
  703. }
  704. }
  705. };
  706. VersionSet::VersionSet(const std::string& dbname,
  707. const Options* options,
  708. TableCache* table_cache,
  709. const InternalKeyComparator* cmp)
  710. : env_(options->env),
  711. dbname_(dbname),
  712. options_(options),
  713. table_cache_(table_cache),
  714. icmp_(*cmp),
  715. next_file_number_(2),
  716. manifest_file_number_(0), // Filled by Recover()
  717. last_sequence_(0),
  718. log_number_(0),
  719. prev_log_number_(0),
  720. descriptor_file_(nullptr),
  721. descriptor_log_(nullptr),
  722. dummy_versions_(this),
  723. current_(nullptr) {
  724. AppendVersion(new Version(this));
  725. }
  726. VersionSet::~VersionSet() {
  727. current_->Unref();
  728. assert(dummy_versions_.next_ == &dummy_versions_); // List must be empty
  729. delete descriptor_log_;
  730. delete descriptor_file_;
  731. }
  732. void VersionSet::AppendVersion(Version* v) {
  733. // Make "v" current
  734. assert(v->refs_ == 0);
  735. assert(v != current_);
  736. if (current_ != nullptr) {
  737. current_->Unref();
  738. }
  739. current_ = v;
  740. v->Ref();
  741. // Append to linked list
  742. v->prev_ = dummy_versions_.prev_;
  743. v->next_ = &dummy_versions_;
  744. v->prev_->next_ = v;
  745. v->next_->prev_ = v;
  746. }
  747. Status VersionSet::LogAndApply(VersionEdit* edit, port::Mutex* mu) {
  748. if (edit->has_log_number_) {
  749. assert(edit->log_number_ >= log_number_);
  750. assert(edit->log_number_ < next_file_number_);
  751. } else {
  752. edit->SetLogNumber(log_number_);
  753. }
  754. if (!edit->has_prev_log_number_) {
  755. edit->SetPrevLogNumber(prev_log_number_);
  756. }
  757. edit->SetNextFile(next_file_number_);
  758. edit->SetLastSequence(last_sequence_);
  759. Version* v = new Version(this);
  760. {
  761. Builder builder(this, current_);
  762. builder.Apply(edit);
  763. builder.SaveTo(v);
  764. }
  765. Finalize(v);
  766. // Initialize new descriptor log file if necessary by creating
  767. // a temporary file that contains a snapshot of the current version.
  768. std::string new_manifest_file;
  769. Status s;
  770. if (descriptor_log_ == nullptr) {
  771. // No reason to unlock *mu here since we only hit this path in the
  772. // first call to LogAndApply (when opening the database).
  773. assert(descriptor_file_ == nullptr);
  774. new_manifest_file = DescriptorFileName(dbname_, manifest_file_number_);
  775. edit->SetNextFile(next_file_number_);
  776. s = env_->NewWritableFile(new_manifest_file, &descriptor_file_);
  777. if (s.ok()) {
  778. descriptor_log_ = new log::Writer(descriptor_file_);
  779. s = WriteSnapshot(descriptor_log_);
  780. }
  781. }
  782. // Unlock during expensive MANIFEST log write
  783. {
  784. mu->Unlock();
  785. // Write new record to MANIFEST log
  786. if (s.ok()) {
  787. std::string record;
  788. edit->EncodeTo(&record);
  789. s = descriptor_log_->AddRecord(record);
  790. if (s.ok()) {
  791. s = descriptor_file_->Sync();
  792. }
  793. if (!s.ok()) {
  794. Log(options_->info_log, "MANIFEST write: %s\n", s.ToString().c_str());
  795. }
  796. }
  797. // If we just created a new descriptor file, install it by writing a
  798. // new CURRENT file that points to it.
  799. if (s.ok() && !new_manifest_file.empty()) {
  800. s = SetCurrentFile(env_, dbname_, manifest_file_number_);
  801. }
  802. mu->Lock();
  803. }
  804. // Install the new version
  805. if (s.ok()) {
  806. AppendVersion(v);
  807. log_number_ = edit->log_number_;
  808. prev_log_number_ = edit->prev_log_number_;
  809. } else {
  810. delete v;
  811. if (!new_manifest_file.empty()) {
  812. delete descriptor_log_;
  813. delete descriptor_file_;
  814. descriptor_log_ = nullptr;
  815. descriptor_file_ = nullptr;
  816. env_->DeleteFile(new_manifest_file);
  817. }
  818. }
  819. return s;
  820. }
  821. Status VersionSet::Recover(bool *save_manifest) {
  822. struct LogReporter : public log::Reader::Reporter {
  823. Status* status;
  824. virtual void Corruption(size_t bytes, const Status& s) {
  825. if (this->status->ok()) *this->status = s;
  826. }
  827. };
  828. // Read "CURRENT" file, which contains a pointer to the current manifest file
  829. std::string current;
  830. Status s = ReadFileToString(env_, CurrentFileName(dbname_), &current);
  831. if (!s.ok()) {
  832. return s;
  833. }
  834. if (current.empty() || current[current.size()-1] != '\n') {
  835. return Status::Corruption("CURRENT file does not end with newline");
  836. }
  837. current.resize(current.size() - 1);
  838. std::string dscname = dbname_ + "/" + current;
  839. SequentialFile* file;
  840. s = env_->NewSequentialFile(dscname, &file);
  841. if (!s.ok()) {
  842. if (s.IsNotFound()) {
  843. return Status::Corruption(
  844. "CURRENT points to a non-existent file", s.ToString());
  845. }
  846. return s;
  847. }
  848. bool have_log_number = false;
  849. bool have_prev_log_number = false;
  850. bool have_next_file = false;
  851. bool have_last_sequence = false;
  852. uint64_t next_file = 0;
  853. uint64_t last_sequence = 0;
  854. uint64_t log_number = 0;
  855. uint64_t prev_log_number = 0;
  856. Builder builder(this, current_);
  857. {
  858. LogReporter reporter;
  859. reporter.status = &s;
  860. log::Reader reader(file, &reporter, true/*checksum*/, 0/*initial_offset*/);
  861. Slice record;
  862. std::string scratch;
  863. while (reader.ReadRecord(&record, &scratch) && s.ok()) {
  864. VersionEdit edit;
  865. s = edit.DecodeFrom(record);
  866. if (s.ok()) {
  867. if (edit.has_comparator_ &&
  868. edit.comparator_ != icmp_.user_comparator()->Name()) {
  869. s = Status::InvalidArgument(
  870. edit.comparator_ + " does not match existing comparator ",
  871. icmp_.user_comparator()->Name());
  872. }
  873. }
  874. if (s.ok()) {
  875. builder.Apply(&edit);
  876. }
  877. if (edit.has_log_number_) {
  878. log_number = edit.log_number_;
  879. have_log_number = true;
  880. }
  881. if (edit.has_prev_log_number_) {
  882. prev_log_number = edit.prev_log_number_;
  883. have_prev_log_number = true;
  884. }
  885. if (edit.has_next_file_number_) {
  886. next_file = edit.next_file_number_;
  887. have_next_file = true;
  888. }
  889. if (edit.has_last_sequence_) {
  890. last_sequence = edit.last_sequence_;
  891. have_last_sequence = true;
  892. }
  893. }
  894. }
  895. delete file;
  896. file = nullptr;
  897. if (s.ok()) {
  898. if (!have_next_file) {
  899. s = Status::Corruption("no meta-nextfile entry in descriptor");
  900. } else if (!have_log_number) {
  901. s = Status::Corruption("no meta-lognumber entry in descriptor");
  902. } else if (!have_last_sequence) {
  903. s = Status::Corruption("no last-sequence-number entry in descriptor");
  904. }
  905. if (!have_prev_log_number) {
  906. prev_log_number = 0;
  907. }
  908. MarkFileNumberUsed(prev_log_number);
  909. MarkFileNumberUsed(log_number);
  910. }
  911. if (s.ok()) {
  912. Version* v = new Version(this);
  913. builder.SaveTo(v);
  914. // Install recovered version
  915. Finalize(v);
  916. AppendVersion(v);
  917. manifest_file_number_ = next_file;
  918. next_file_number_ = next_file + 1;
  919. last_sequence_ = last_sequence;
  920. log_number_ = log_number;
  921. prev_log_number_ = prev_log_number;
  922. // See if we can reuse the existing MANIFEST file.
  923. if (ReuseManifest(dscname, current)) {
  924. // No need to save new manifest
  925. } else {
  926. *save_manifest = true;
  927. }
  928. }
  929. return s;
  930. }
  931. bool VersionSet::ReuseManifest(const std::string& dscname,
  932. const std::string& dscbase) {
  933. if (!options_->reuse_logs) {
  934. return false;
  935. }
  936. FileType manifest_type;
  937. uint64_t manifest_number;
  938. uint64_t manifest_size;
  939. if (!ParseFileName(dscbase, &manifest_number, &manifest_type) ||
  940. manifest_type != kDescriptorFile ||
  941. !env_->GetFileSize(dscname, &manifest_size).ok() ||
  942. // Make new compacted MANIFEST if old one is too big
  943. manifest_size >= TargetFileSize(options_)) {
  944. return false;
  945. }
  946. assert(descriptor_file_ == nullptr);
  947. assert(descriptor_log_ == nullptr);
  948. Status r = env_->NewAppendableFile(dscname, &descriptor_file_);
  949. if (!r.ok()) {
  950. Log(options_->info_log, "Reuse MANIFEST: %s\n", r.ToString().c_str());
  951. assert(descriptor_file_ == nullptr);
  952. return false;
  953. }
  954. Log(options_->info_log, "Reusing MANIFEST %s\n", dscname.c_str());
  955. descriptor_log_ = new log::Writer(descriptor_file_, manifest_size);
  956. manifest_file_number_ = manifest_number;
  957. return true;
  958. }
  959. void VersionSet::MarkFileNumberUsed(uint64_t number) {
  960. if (next_file_number_ <= number) {
  961. next_file_number_ = number + 1;
  962. }
  963. }
  964. void VersionSet::Finalize(Version* v) {
  965. // Precomputed best level for next compaction
  966. int best_level = -1;
  967. double best_score = -1;
  968. for (int level = 0; level < config::kNumLevels-1; level++) {
  969. double score;
  970. if (level == 0) {
  971. // We treat level-0 specially by bounding the number of files
  972. // instead of number of bytes for two reasons:
  973. //
  974. // (1) With larger write-buffer sizes, it is nice not to do too
  975. // many level-0 compactions.
  976. //
  977. // (2) The files in level-0 are merged on every read and
  978. // therefore we wish to avoid too many files when the individual
  979. // file size is small (perhaps because of a small write-buffer
  980. // setting, or very high compression ratios, or lots of
  981. // overwrites/deletions).
  982. score = v->files_[level].size() /
  983. static_cast<double>(config::kL0_CompactionTrigger);
  984. } else {
  985. // Compute the ratio of current size to size limit.
  986. const uint64_t level_bytes = TotalFileSize(v->files_[level]);
  987. score =
  988. static_cast<double>(level_bytes) / MaxBytesForLevel(options_, level);
  989. }
  990. if (score > best_score) {
  991. best_level = level;
  992. best_score = score;
  993. }
  994. }
  995. v->compaction_level_ = best_level;
  996. v->compaction_score_ = best_score;
  997. }
  998. Status VersionSet::WriteSnapshot(log::Writer* log) {
  999. // TODO: Break up into multiple records to reduce memory usage on recovery?
  1000. // Save metadata
  1001. VersionEdit edit;
  1002. edit.SetComparatorName(icmp_.user_comparator()->Name());
  1003. // Save compaction pointers
  1004. for (int level = 0; level < config::kNumLevels; level++) {
  1005. if (!compact_pointer_[level].empty()) {
  1006. InternalKey key;
  1007. key.DecodeFrom(compact_pointer_[level]);
  1008. edit.SetCompactPointer(level, key);
  1009. }
  1010. }
  1011. // Save files
  1012. for (int level = 0; level < config::kNumLevels; level++) {
  1013. const std::vector<FileMetaData*>& files = current_->files_[level];
  1014. for (size_t i = 0; i < files.size(); i++) {
  1015. const FileMetaData* f = files[i];
  1016. edit.AddFile(level, f->number, f->file_size, f->smallest, f->largest);
  1017. }
  1018. }
  1019. std::string record;
  1020. edit.EncodeTo(&record);
  1021. return log->AddRecord(record);
  1022. }
  1023. int VersionSet::NumLevelFiles(int level) const {
  1024. assert(level >= 0);
  1025. assert(level < config::kNumLevels);
  1026. return current_->files_[level].size();
  1027. }
  1028. const char* VersionSet::LevelSummary(LevelSummaryStorage* scratch) const {
  1029. // Update code if kNumLevels changes
  1030. assert(config::kNumLevels == 7);
  1031. snprintf(scratch->buffer, sizeof(scratch->buffer),
  1032. "files[ %d %d %d %d %d %d %d ]",
  1033. int(current_->files_[0].size()),
  1034. int(current_->files_[1].size()),
  1035. int(current_->files_[2].size()),
  1036. int(current_->files_[3].size()),
  1037. int(current_->files_[4].size()),
  1038. int(current_->files_[5].size()),
  1039. int(current_->files_[6].size()));
  1040. return scratch->buffer;
  1041. }
  1042. uint64_t VersionSet::ApproximateOffsetOf(Version* v, const InternalKey& ikey) {
  1043. uint64_t result = 0;
  1044. for (int level = 0; level < config::kNumLevels; level++) {
  1045. const std::vector<FileMetaData*>& files = v->files_[level];
  1046. for (size_t i = 0; i < files.size(); i++) {
  1047. if (icmp_.Compare(files[i]->largest, ikey) <= 0) {
  1048. // Entire file is before "ikey", so just add the file size
  1049. result += files[i]->file_size;
  1050. } else if (icmp_.Compare(files[i]->smallest, ikey) > 0) {
  1051. // Entire file is after "ikey", so ignore
  1052. if (level > 0) {
  1053. // Files other than level 0 are sorted by meta->smallest, so
  1054. // no further files in this level will contain data for
  1055. // "ikey".
  1056. break;
  1057. }
  1058. } else {
  1059. // "ikey" falls in the range for this table. Add the
  1060. // approximate offset of "ikey" within the table.
  1061. Table* tableptr;
  1062. Iterator* iter = table_cache_->NewIterator(
  1063. ReadOptions(), files[i]->number, files[i]->file_size, &tableptr);
  1064. if (tableptr != nullptr) {
  1065. result += tableptr->ApproximateOffsetOf(ikey.Encode());
  1066. }
  1067. delete iter;
  1068. }
  1069. }
  1070. }
  1071. return result;
  1072. }
  1073. void VersionSet::AddLiveFiles(std::set<uint64_t>* live) {
  1074. for (Version* v = dummy_versions_.next_;
  1075. v != &dummy_versions_;
  1076. v = v->next_) {
  1077. for (int level = 0; level < config::kNumLevels; level++) {
  1078. const std::vector<FileMetaData*>& files = v->files_[level];
  1079. for (size_t i = 0; i < files.size(); i++) {
  1080. live->insert(files[i]->number);
  1081. }
  1082. }
  1083. }
  1084. }
  1085. int64_t VersionSet::NumLevelBytes(int level) const {
  1086. assert(level >= 0);
  1087. assert(level < config::kNumLevels);
  1088. return TotalFileSize(current_->files_[level]);
  1089. }
  1090. int64_t VersionSet::MaxNextLevelOverlappingBytes() {
  1091. int64_t result = 0;
  1092. std::vector<FileMetaData*> overlaps;
  1093. for (int level = 1; level < config::kNumLevels - 1; level++) {
  1094. for (size_t i = 0; i < current_->files_[level].size(); i++) {
  1095. const FileMetaData* f = current_->files_[level][i];
  1096. current_->GetOverlappingInputs(level+1, &f->smallest, &f->largest,
  1097. &overlaps);
  1098. const int64_t sum = TotalFileSize(overlaps);
  1099. if (sum > result) {
  1100. result = sum;
  1101. }
  1102. }
  1103. }
  1104. return result;
  1105. }
  1106. // Stores the minimal range that covers all entries in inputs in
  1107. // *smallest, *largest.
  1108. // REQUIRES: inputs is not empty
  1109. void VersionSet::GetRange(const std::vector<FileMetaData*>& inputs,
  1110. InternalKey* smallest,
  1111. InternalKey* largest) {
  1112. assert(!inputs.empty());
  1113. smallest->Clear();
  1114. largest->Clear();
  1115. for (size_t i = 0; i < inputs.size(); i++) {
  1116. FileMetaData* f = inputs[i];
  1117. if (i == 0) {
  1118. *smallest = f->smallest;
  1119. *largest = f->largest;
  1120. } else {
  1121. if (icmp_.Compare(f->smallest, *smallest) < 0) {
  1122. *smallest = f->smallest;
  1123. }
  1124. if (icmp_.Compare(f->largest, *largest) > 0) {
  1125. *largest = f->largest;
  1126. }
  1127. }
  1128. }
  1129. }
  1130. // Stores the minimal range that covers all entries in inputs1 and inputs2
  1131. // in *smallest, *largest.
  1132. // REQUIRES: inputs is not empty
  1133. void VersionSet::GetRange2(const std::vector<FileMetaData*>& inputs1,
  1134. const std::vector<FileMetaData*>& inputs2,
  1135. InternalKey* smallest,
  1136. InternalKey* largest) {
  1137. std::vector<FileMetaData*> all = inputs1;
  1138. all.insert(all.end(), inputs2.begin(), inputs2.end());
  1139. GetRange(all, smallest, largest);
  1140. }
  1141. Iterator* VersionSet::MakeInputIterator(Compaction* c) {
  1142. ReadOptions options;
  1143. options.verify_checksums = options_->paranoid_checks;
  1144. options.fill_cache = false;
  1145. // Level-0 files have to be merged together. For other levels,
  1146. // we will make a concatenating iterator per level.
  1147. // TODO(opt): use concatenating iterator for level-0 if there is no overlap
  1148. const int space = (c->level() == 0 ? c->inputs_[0].size() + 1 : 2);
  1149. Iterator** list = new Iterator*[space];
  1150. int num = 0;
  1151. for (int which = 0; which < 2; which++) {
  1152. if (!c->inputs_[which].empty()) {
  1153. if (c->level() + which == 0) {
  1154. const std::vector<FileMetaData*>& files = c->inputs_[which];
  1155. for (size_t i = 0; i < files.size(); i++) {
  1156. list[num++] = table_cache_->NewIterator(
  1157. options, files[i]->number, files[i]->file_size);
  1158. }
  1159. } else {
  1160. // Create concatenating iterator for the files from this level
  1161. list[num++] = NewTwoLevelIterator(
  1162. new Version::LevelFileNumIterator(icmp_, &c->inputs_[which]),
  1163. &GetFileIterator, table_cache_, options);
  1164. }
  1165. }
  1166. }
  1167. assert(num <= space);
  1168. Iterator* result = NewMergingIterator(&icmp_, list, num);
  1169. delete[] list;
  1170. return result;
  1171. }
  1172. Compaction* VersionSet::PickCompaction() {
  1173. Compaction* c;
  1174. int level;
  1175. // We prefer compactions triggered by too much data in a level over
  1176. // the compactions triggered by seeks.
  1177. const bool size_compaction = (current_->compaction_score_ >= 1);
  1178. const bool seek_compaction = (current_->file_to_compact_ != nullptr);
  1179. if (size_compaction) {
  1180. level = current_->compaction_level_;
  1181. assert(level >= 0);
  1182. assert(level+1 < config::kNumLevels);
  1183. c = new Compaction(options_, level);
  1184. // Pick the first file that comes after compact_pointer_[level]
  1185. for (size_t i = 0; i < current_->files_[level].size(); i++) {
  1186. FileMetaData* f = current_->files_[level][i];
  1187. if (compact_pointer_[level].empty() ||
  1188. icmp_.Compare(f->largest.Encode(), compact_pointer_[level]) > 0) {
  1189. c->inputs_[0].push_back(f);
  1190. break;
  1191. }
  1192. }
  1193. if (c->inputs_[0].empty()) {
  1194. // Wrap-around to the beginning of the key space
  1195. c->inputs_[0].push_back(current_->files_[level][0]);
  1196. }
  1197. } else if (seek_compaction) {
  1198. level = current_->file_to_compact_level_;
  1199. c = new Compaction(options_, level);
  1200. c->inputs_[0].push_back(current_->file_to_compact_);
  1201. } else {
  1202. return nullptr;
  1203. }
  1204. c->input_version_ = current_;
  1205. c->input_version_->Ref();
  1206. // Files in level 0 may overlap each other, so pick up all overlapping ones
  1207. if (level == 0) {
  1208. InternalKey smallest, largest;
  1209. GetRange(c->inputs_[0], &smallest, &largest);
  1210. // Note that the next call will discard the file we placed in
  1211. // c->inputs_[0] earlier and replace it with an overlapping set
  1212. // which will include the picked file.
  1213. current_->GetOverlappingInputs(0, &smallest, &largest, &c->inputs_[0]);
  1214. assert(!c->inputs_[0].empty());
  1215. }
  1216. SetupOtherInputs(c);
  1217. return c;
  1218. }
  1219. void VersionSet::SetupOtherInputs(Compaction* c) {
  1220. const int level = c->level();
  1221. InternalKey smallest, largest;
  1222. GetRange(c->inputs_[0], &smallest, &largest);
  1223. current_->GetOverlappingInputs(level+1, &smallest, &largest, &c->inputs_[1]);
  1224. // Get entire range covered by compaction
  1225. InternalKey all_start, all_limit;
  1226. GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit);
  1227. // See if we can grow the number of inputs in "level" without
  1228. // changing the number of "level+1" files we pick up.
  1229. if (!c->inputs_[1].empty()) {
  1230. std::vector<FileMetaData*> expanded0;
  1231. current_->GetOverlappingInputs(level, &all_start, &all_limit, &expanded0);
  1232. const int64_t inputs0_size = TotalFileSize(c->inputs_[0]);
  1233. const int64_t inputs1_size = TotalFileSize(c->inputs_[1]);
  1234. const int64_t expanded0_size = TotalFileSize(expanded0);
  1235. if (expanded0.size() > c->inputs_[0].size() &&
  1236. inputs1_size + expanded0_size <
  1237. ExpandedCompactionByteSizeLimit(options_)) {
  1238. InternalKey new_start, new_limit;
  1239. GetRange(expanded0, &new_start, &new_limit);
  1240. std::vector<FileMetaData*> expanded1;
  1241. current_->GetOverlappingInputs(level+1, &new_start, &new_limit,
  1242. &expanded1);
  1243. if (expanded1.size() == c->inputs_[1].size()) {
  1244. Log(options_->info_log,
  1245. "Expanding@%d %d+%d (%ld+%ld bytes) to %d+%d (%ld+%ld bytes)\n",
  1246. level,
  1247. int(c->inputs_[0].size()),
  1248. int(c->inputs_[1].size()),
  1249. long(inputs0_size), long(inputs1_size),
  1250. int(expanded0.size()),
  1251. int(expanded1.size()),
  1252. long(expanded0_size), long(inputs1_size));
  1253. smallest = new_start;
  1254. largest = new_limit;
  1255. c->inputs_[0] = expanded0;
  1256. c->inputs_[1] = expanded1;
  1257. GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit);
  1258. }
  1259. }
  1260. }
  1261. // Compute the set of grandparent files that overlap this compaction
  1262. // (parent == level+1; grandparent == level+2)
  1263. if (level + 2 < config::kNumLevels) {
  1264. current_->GetOverlappingInputs(level + 2, &all_start, &all_limit,
  1265. &c->grandparents_);
  1266. }
  1267. // Update the place where we will do the next compaction for this level.
  1268. // We update this immediately instead of waiting for the VersionEdit
  1269. // to be applied so that if the compaction fails, we will try a different
  1270. // key range next time.
  1271. compact_pointer_[level] = largest.Encode().ToString();
  1272. c->edit_.SetCompactPointer(level, largest);
  1273. }
  1274. Compaction* VersionSet::CompactRange(
  1275. int level,
  1276. const InternalKey* begin,
  1277. const InternalKey* end) {
  1278. std::vector<FileMetaData*> inputs;
  1279. current_->GetOverlappingInputs(level, begin, end, &inputs);
  1280. if (inputs.empty()) {
  1281. return nullptr;
  1282. }
  1283. // Avoid compacting too much in one shot in case the range is large.
  1284. // But we cannot do this for level-0 since level-0 files can overlap
  1285. // and we must not pick one file and drop another older file if the
  1286. // two files overlap.
  1287. if (level > 0) {
  1288. const uint64_t limit = MaxFileSizeForLevel(options_, level);
  1289. uint64_t total = 0;
  1290. for (size_t i = 0; i < inputs.size(); i++) {
  1291. uint64_t s = inputs[i]->file_size;
  1292. total += s;
  1293. if (total >= limit) {
  1294. inputs.resize(i + 1);
  1295. break;
  1296. }
  1297. }
  1298. }
  1299. Compaction* c = new Compaction(options_, level);
  1300. c->input_version_ = current_;
  1301. c->input_version_->Ref();
  1302. c->inputs_[0] = inputs;
  1303. SetupOtherInputs(c);
  1304. return c;
  1305. }
  1306. Compaction::Compaction(const Options* options, int level)
  1307. : level_(level),
  1308. max_output_file_size_(MaxFileSizeForLevel(options, level)),
  1309. input_version_(nullptr),
  1310. grandparent_index_(0),
  1311. seen_key_(false),
  1312. overlapped_bytes_(0) {
  1313. for (int i = 0; i < config::kNumLevels; i++) {
  1314. level_ptrs_[i] = 0;
  1315. }
  1316. }
  1317. Compaction::~Compaction() {
  1318. if (input_version_ != nullptr) {
  1319. input_version_->Unref();
  1320. }
  1321. }
  1322. bool Compaction::IsTrivialMove() const {
  1323. const VersionSet* vset = input_version_->vset_;
  1324. // Avoid a move if there is lots of overlapping grandparent data.
  1325. // Otherwise, the move could create a parent file that will require
  1326. // a very expensive merge later on.
  1327. return (num_input_files(0) == 1 && num_input_files(1) == 0 &&
  1328. TotalFileSize(grandparents_) <=
  1329. MaxGrandParentOverlapBytes(vset->options_));
  1330. }
  1331. void Compaction::AddInputDeletions(VersionEdit* edit) {
  1332. for (int which = 0; which < 2; which++) {
  1333. for (size_t i = 0; i < inputs_[which].size(); i++) {
  1334. edit->DeleteFile(level_ + which, inputs_[which][i]->number);
  1335. }
  1336. }
  1337. }
  1338. bool Compaction::IsBaseLevelForKey(const Slice& user_key) {
  1339. // Maybe use binary search to find right entry instead of linear search?
  1340. const Comparator* user_cmp = input_version_->vset_->icmp_.user_comparator();
  1341. for (int lvl = level_ + 2; lvl < config::kNumLevels; lvl++) {
  1342. const std::vector<FileMetaData*>& files = input_version_->files_[lvl];
  1343. for (; level_ptrs_[lvl] < files.size(); ) {
  1344. FileMetaData* f = files[level_ptrs_[lvl]];
  1345. if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) {
  1346. // We've advanced far enough
  1347. if (user_cmp->Compare(user_key, f->smallest.user_key()) >= 0) {
  1348. // Key falls in this file's range, so definitely not base level
  1349. return false;
  1350. }
  1351. break;
  1352. }
  1353. level_ptrs_[lvl]++;
  1354. }
  1355. }
  1356. return true;
  1357. }
  1358. bool Compaction::ShouldStopBefore(const Slice& internal_key) {
  1359. const VersionSet* vset = input_version_->vset_;
  1360. // Scan to find earliest grandparent file that contains key.
  1361. const InternalKeyComparator* icmp = &vset->icmp_;
  1362. while (grandparent_index_ < grandparents_.size() &&
  1363. icmp->Compare(internal_key,
  1364. grandparents_[grandparent_index_]->largest.Encode()) > 0) {
  1365. if (seen_key_) {
  1366. overlapped_bytes_ += grandparents_[grandparent_index_]->file_size;
  1367. }
  1368. grandparent_index_++;
  1369. }
  1370. seen_key_ = true;
  1371. if (overlapped_bytes_ > MaxGrandParentOverlapBytes(vset->options_)) {
  1372. // Too much overlap for current output; start new output
  1373. overlapped_bytes_ = 0;
  1374. return true;
  1375. } else {
  1376. return false;
  1377. }
  1378. }
  1379. void Compaction::ReleaseInputs() {
  1380. if (input_version_ != nullptr) {
  1381. input_version_->Unref();
  1382. input_version_ = nullptr;
  1383. }
  1384. }
  1385. } // namespace leveldb