2011-03-19 06:37:00 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "db/version_set.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2019-05-03 02:01:00 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/filename.h"
|
|
|
|
#include "db/log_reader.h"
|
|
|
|
#include "db/log_writer.h"
|
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/table_cache.h"
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/table_builder.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "table/merger.h"
|
|
|
|
#include "table/two_level_iterator.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2018-04-17 03:27:06 +08:00
|
|
|
static size_t TargetFileSize(const Options* options) {
|
2016-09-27 19:50:38 +08:00
|
|
|
return options->max_file_size;
|
|
|
|
}
|
2011-03-23 07:24:02 +08:00
|
|
|
|
|
|
|
// Maximum bytes of overlaps in grandparent (i.e., level+2) before we
|
2011-03-23 02:32:49 +08:00
|
|
|
// stop building a single file in a level->level+1 compaction.
|
2016-09-27 19:50:38 +08:00
|
|
|
static int64_t MaxGrandParentOverlapBytes(const Options* options) {
|
|
|
|
return 10 * TargetFileSize(options);
|
|
|
|
}
|
2011-03-23 02:32:49 +08:00
|
|
|
|
2012-02-03 01:34:14 +08:00
|
|
|
// Maximum number of bytes in all compacted files. We avoid expanding
|
|
|
|
// the lower level file set of a compaction if it would make the
|
|
|
|
// total compaction cover more than this many bytes.
|
2016-09-27 19:50:38 +08:00
|
|
|
static int64_t ExpandedCompactionByteSizeLimit(const Options* options) {
|
|
|
|
return 25 * TargetFileSize(options);
|
|
|
|
}
|
2012-02-03 01:34:14 +08:00
|
|
|
|
2016-09-27 19:50:38 +08:00
|
|
|
static double MaxBytesForLevel(const Options* options, int level) {
|
2011-04-13 03:38:58 +08:00
|
|
|
// Note: the result for level zero is not really used since we set
|
|
|
|
// the level-0 compaction threshold based on number of files.
|
2016-09-27 19:50:38 +08:00
|
|
|
|
|
|
|
// Result for both level-0 and level-1
|
|
|
|
double result = 10. * 1048576.0;
|
2011-04-13 03:38:58 +08:00
|
|
|
while (level > 1) {
|
|
|
|
result *= 10;
|
|
|
|
level--;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2011-04-13 03:38:58 +08:00
|
|
|
return result;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 19:50:38 +08:00
|
|
|
static uint64_t MaxFileSizeForLevel(const Options* options, int level) {
|
|
|
|
// We could vary per level to reduce number of files?
|
|
|
|
return TargetFileSize(options);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2011-10-06 07:30:28 +08:00
|
|
|
static int64_t TotalFileSize(const std::vector<FileMetaData*>& files) {
|
|
|
|
int64_t sum = 0;
|
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
|
|
|
sum += files[i]->file_size;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
Version::~Version() {
|
|
|
|
assert(refs_ == 0);
|
2011-05-21 10:17:43 +08:00
|
|
|
|
|
|
|
// Remove from linked list
|
|
|
|
prev_->next_ = next_;
|
|
|
|
next_->prev_ = prev_;
|
|
|
|
|
|
|
|
// Drop references to files
|
2011-03-19 06:37:00 +08:00
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < files_[level].size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
FileMetaData* f = files_[level][i];
|
2011-05-21 10:17:43 +08:00
|
|
|
assert(f->refs > 0);
|
2011-03-19 06:37:00 +08:00
|
|
|
f->refs--;
|
|
|
|
if (f->refs <= 0) {
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-22 10:36:45 +08:00
|
|
|
int FindFile(const InternalKeyComparator& icmp,
|
2019-05-03 02:01:00 +08:00
|
|
|
const std::vector<FileMetaData*>& files, const Slice& key) {
|
2011-06-22 10:36:45 +08:00
|
|
|
uint32_t left = 0;
|
|
|
|
uint32_t right = files.size();
|
|
|
|
while (left < right) {
|
|
|
|
uint32_t mid = (left + right) / 2;
|
|
|
|
const FileMetaData* f = files[mid];
|
|
|
|
if (icmp.InternalKeyComparator::Compare(f->largest.Encode(), key) < 0) {
|
|
|
|
// Key at "mid.largest" is < "target". Therefore all
|
|
|
|
// files at or before "mid" are uninteresting.
|
|
|
|
left = mid + 1;
|
|
|
|
} else {
|
|
|
|
// Key at "mid.largest" is >= "target". Therefore all files
|
|
|
|
// after "mid" are uninteresting.
|
|
|
|
right = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
static bool AfterFile(const Comparator* ucmp, const Slice* user_key,
|
|
|
|
const FileMetaData* f) {
|
2018-04-11 07:18:06 +08:00
|
|
|
// null user_key occurs before all keys and is therefore never after *f
|
|
|
|
return (user_key != nullptr &&
|
2011-10-06 07:30:28 +08:00
|
|
|
ucmp->Compare(*user_key, f->largest.user_key()) > 0);
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
static bool BeforeFile(const Comparator* ucmp, const Slice* user_key,
|
|
|
|
const FileMetaData* f) {
|
2018-04-11 07:18:06 +08:00
|
|
|
// null user_key occurs after all keys and is therefore never before *f
|
|
|
|
return (user_key != nullptr &&
|
2011-10-06 07:30:28 +08:00
|
|
|
ucmp->Compare(*user_key, f->smallest.user_key()) < 0);
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
bool SomeFileOverlapsRange(const InternalKeyComparator& icmp,
|
|
|
|
bool disjoint_sorted_files,
|
|
|
|
const std::vector<FileMetaData*>& files,
|
|
|
|
const Slice* smallest_user_key,
|
|
|
|
const Slice* largest_user_key) {
|
2011-10-06 07:30:28 +08:00
|
|
|
const Comparator* ucmp = icmp.user_comparator();
|
|
|
|
if (!disjoint_sorted_files) {
|
|
|
|
// Need to check against all files
|
2012-05-31 00:45:46 +08:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
2011-10-06 07:30:28 +08:00
|
|
|
const FileMetaData* f = files[i];
|
|
|
|
if (AfterFile(ucmp, smallest_user_key, f) ||
|
|
|
|
BeforeFile(ucmp, largest_user_key, f)) {
|
|
|
|
// No overlap
|
|
|
|
} else {
|
|
|
|
return true; // Overlap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary search over file list
|
|
|
|
uint32_t index = 0;
|
2018-04-11 07:18:06 +08:00
|
|
|
if (smallest_user_key != nullptr) {
|
2011-10-06 07:30:28 +08:00
|
|
|
// Find the earliest possible internal key for smallest_user_key
|
2019-05-03 02:01:00 +08:00
|
|
|
InternalKey small_key(*smallest_user_key, kMaxSequenceNumber,
|
|
|
|
kValueTypeForSeek);
|
2019-03-02 05:12:01 +08:00
|
|
|
index = FindFile(icmp, files, small_key.Encode());
|
2011-10-06 07:30:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= files.size()) {
|
|
|
|
// beginning of range is after all files, so no overlap.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !BeforeFile(ucmp, largest_user_key, files[index]);
|
2011-06-22 10:36:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
// An internal iterator. For a given version/level pair, yields
|
|
|
|
// information about the files in the level. For a given entry, key()
|
|
|
|
// is the largest key that occurs in the file, and value() is an
|
2011-03-29 04:43:44 +08:00
|
|
|
// 16-byte value containing the file number and file size, both
|
|
|
|
// encoded using EncodeFixed64.
|
2011-03-19 06:37:00 +08:00
|
|
|
class Version::LevelFileNumIterator : public Iterator {
|
|
|
|
public:
|
2011-05-21 10:17:43 +08:00
|
|
|
LevelFileNumIterator(const InternalKeyComparator& icmp,
|
2011-03-19 06:37:00 +08:00
|
|
|
const std::vector<FileMetaData*>* flist)
|
2019-05-03 02:01:00 +08:00
|
|
|
: icmp_(icmp), flist_(flist), index_(flist->size()) { // Marks as invalid
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
bool Valid() const override { return index_ < flist_->size(); }
|
|
|
|
void Seek(const Slice& target) override {
|
2011-06-22 10:36:45 +08:00
|
|
|
index_ = FindFile(icmp_, *flist_, target);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
void SeekToFirst() override { index_ = 0; }
|
|
|
|
void SeekToLast() override {
|
2011-03-19 06:37:00 +08:00
|
|
|
index_ = flist_->empty() ? 0 : flist_->size() - 1;
|
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
void Next() override {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(Valid());
|
|
|
|
index_++;
|
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
void Prev() override {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(Valid());
|
|
|
|
if (index_ == 0) {
|
|
|
|
index_ = flist_->size(); // Marks as invalid
|
|
|
|
} else {
|
|
|
|
index_--;
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
Slice key() const override {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(Valid());
|
|
|
|
return (*flist_)[index_]->largest.Encode();
|
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
Slice value() const override {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(Valid());
|
|
|
|
EncodeFixed64(value_buf_, (*flist_)[index_]->number);
|
2019-05-03 02:01:00 +08:00
|
|
|
EncodeFixed64(value_buf_ + 8, (*flist_)[index_]->file_size);
|
2011-03-19 06:37:00 +08:00
|
|
|
return Slice(value_buf_, sizeof(value_buf_));
|
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
Status status() const override { return Status::OK(); }
|
2019-05-03 02:01:00 +08:00
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
private:
|
|
|
|
const InternalKeyComparator icmp_;
|
|
|
|
const std::vector<FileMetaData*>* const flist_;
|
2011-04-21 06:48:11 +08:00
|
|
|
uint32_t index_;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2011-03-29 04:43:44 +08:00
|
|
|
// Backing store for value(). Holds the file number and size.
|
|
|
|
mutable char value_buf_[16];
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
static Iterator* GetFileIterator(void* arg, const ReadOptions& options,
|
2011-03-19 06:37:00 +08:00
|
|
|
const Slice& file_value) {
|
|
|
|
TableCache* cache = reinterpret_cast<TableCache*>(arg);
|
2011-03-29 04:43:44 +08:00
|
|
|
if (file_value.size() != 16) {
|
2011-03-19 06:37:00 +08:00
|
|
|
return NewErrorIterator(
|
|
|
|
Status::Corruption("FileReader invoked with unexpected value"));
|
|
|
|
} else {
|
2019-05-03 02:01:00 +08:00
|
|
|
return cache->NewIterator(options, DecodeFixed64(file_value.data()),
|
2011-03-29 04:43:44 +08:00
|
|
|
DecodeFixed64(file_value.data() + 8));
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* Version::NewConcatenatingIterator(const ReadOptions& options,
|
|
|
|
int level) const {
|
|
|
|
return NewTwoLevelIterator(
|
2019-05-03 02:01:00 +08:00
|
|
|
new LevelFileNumIterator(vset_->icmp_, &files_[level]), &GetFileIterator,
|
|
|
|
vset_->table_cache_, options);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Version::AddIterators(const ReadOptions& options,
|
|
|
|
std::vector<Iterator*>* iters) {
|
|
|
|
// Merge all level zero files together since they may overlap
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < files_[0].size(); i++) {
|
2019-05-03 02:01:00 +08:00
|
|
|
iters->push_back(vset_->table_cache_->NewIterator(
|
|
|
|
options, files_[0][i]->number, files_[0][i]->file_size));
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For levels > 0, we can use a concatenating iterator that sequentially
|
|
|
|
// walks through the non-overlapping files in the level, opening them
|
|
|
|
// lazily.
|
|
|
|
for (int level = 1; level < config::kNumLevels; level++) {
|
|
|
|
if (!files_[level].empty()) {
|
|
|
|
iters->push_back(NewConcatenatingIterator(options, level));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-17 23:36:46 +08:00
|
|
|
// Callback from TableCache::Get()
|
|
|
|
namespace {
|
|
|
|
enum SaverState {
|
|
|
|
kNotFound,
|
|
|
|
kFound,
|
|
|
|
kDeleted,
|
|
|
|
kCorrupt,
|
|
|
|
};
|
|
|
|
struct Saver {
|
|
|
|
SaverState state;
|
|
|
|
const Comparator* ucmp;
|
|
|
|
Slice user_key;
|
|
|
|
std::string* value;
|
|
|
|
};
|
2019-05-03 02:01:00 +08:00
|
|
|
} // namespace
|
2012-04-17 23:36:46 +08:00
|
|
|
static void SaveValue(void* arg, const Slice& ikey, const Slice& v) {
|
|
|
|
Saver* s = reinterpret_cast<Saver*>(arg);
|
2011-06-22 10:36:45 +08:00
|
|
|
ParsedInternalKey parsed_key;
|
2012-04-17 23:36:46 +08:00
|
|
|
if (!ParseInternalKey(ikey, &parsed_key)) {
|
|
|
|
s->state = kCorrupt;
|
|
|
|
} else {
|
|
|
|
if (s->ucmp->Compare(parsed_key.user_key, s->user_key) == 0) {
|
|
|
|
s->state = (parsed_key.type == kTypeValue) ? kFound : kDeleted;
|
|
|
|
if (s->state == kFound) {
|
|
|
|
s->value->assign(v.data(), v.size());
|
|
|
|
}
|
2011-06-22 10:36:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool NewestFirst(FileMetaData* a, FileMetaData* b) {
|
|
|
|
return a->number > b->number;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
void Version::ForEachOverlapping(Slice user_key, Slice internal_key, void* arg,
|
2013-08-22 02:12:47 +08:00
|
|
|
bool (*func)(void*, int, FileMetaData*)) {
|
|
|
|
// TODO(sanjay): Change Version::Get() to use this function.
|
|
|
|
const Comparator* ucmp = vset_->icmp_.user_comparator();
|
|
|
|
|
|
|
|
// Search level-0 in order from newest to oldest.
|
|
|
|
std::vector<FileMetaData*> tmp;
|
|
|
|
tmp.reserve(files_[0].size());
|
|
|
|
for (uint32_t i = 0; i < files_[0].size(); i++) {
|
|
|
|
FileMetaData* f = files_[0][i];
|
|
|
|
if (ucmp->Compare(user_key, f->smallest.user_key()) >= 0 &&
|
|
|
|
ucmp->Compare(user_key, f->largest.user_key()) <= 0) {
|
|
|
|
tmp.push_back(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!tmp.empty()) {
|
|
|
|
std::sort(tmp.begin(), tmp.end(), NewestFirst);
|
|
|
|
for (uint32_t i = 0; i < tmp.size(); i++) {
|
|
|
|
if (!(*func)(arg, 0, tmp[i])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search other levels.
|
|
|
|
for (int level = 1; level < config::kNumLevels; level++) {
|
|
|
|
size_t num_files = files_[level].size();
|
|
|
|
if (num_files == 0) continue;
|
|
|
|
|
|
|
|
// Binary search to find earliest index whose largest key >= internal_key.
|
|
|
|
uint32_t index = FindFile(vset_->icmp_, files_[level], internal_key);
|
|
|
|
if (index < num_files) {
|
|
|
|
FileMetaData* f = files_[level][index];
|
|
|
|
if (ucmp->Compare(user_key, f->smallest.user_key()) < 0) {
|
|
|
|
// All of "f" is past any data for user_key
|
|
|
|
} else {
|
|
|
|
if (!(*func)(arg, level, f)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
Status Version::Get(const ReadOptions& options, const LookupKey& k,
|
|
|
|
std::string* value, GetStats* stats) {
|
2011-06-22 10:36:45 +08:00
|
|
|
Slice ikey = k.internal_key();
|
|
|
|
Slice user_key = k.user_key();
|
|
|
|
const Comparator* ucmp = vset_->icmp_.user_comparator();
|
|
|
|
Status s;
|
|
|
|
|
2018-04-11 07:18:06 +08:00
|
|
|
stats->seek_file = nullptr;
|
2011-06-22 10:36:45 +08:00
|
|
|
stats->seek_file_level = -1;
|
2018-04-11 07:18:06 +08:00
|
|
|
FileMetaData* last_file_read = nullptr;
|
2011-09-02 03:08:02 +08:00
|
|
|
int last_file_read_level = -1;
|
2011-06-22 10:36:45 +08:00
|
|
|
|
|
|
|
// We can search level-by-level since entries never hop across
|
|
|
|
// levels. Therefore we are guaranteed that if we find data
|
2019-03-12 01:36:11 +08:00
|
|
|
// in a smaller level, later levels are irrelevant.
|
2011-06-22 10:36:45 +08:00
|
|
|
std::vector<FileMetaData*> tmp;
|
|
|
|
FileMetaData* tmp2;
|
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
|
|
|
size_t num_files = files_[level].size();
|
|
|
|
if (num_files == 0) continue;
|
|
|
|
|
|
|
|
// Get the list of files to search in this level
|
|
|
|
FileMetaData* const* files = &files_[level][0];
|
|
|
|
if (level == 0) {
|
|
|
|
// Level-0 files may overlap each other. Find all files that
|
|
|
|
// overlap user_key and process them in order from newest to oldest.
|
|
|
|
tmp.reserve(num_files);
|
2011-08-06 08:19:37 +08:00
|
|
|
for (uint32_t i = 0; i < num_files; i++) {
|
2011-06-22 10:36:45 +08:00
|
|
|
FileMetaData* f = files[i];
|
|
|
|
if (ucmp->Compare(user_key, f->smallest.user_key()) >= 0 &&
|
|
|
|
ucmp->Compare(user_key, f->largest.user_key()) <= 0) {
|
|
|
|
tmp.push_back(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmp.empty()) continue;
|
|
|
|
|
|
|
|
std::sort(tmp.begin(), tmp.end(), NewestFirst);
|
|
|
|
files = &tmp[0];
|
|
|
|
num_files = tmp.size();
|
|
|
|
} else {
|
|
|
|
// Binary search to find earliest index whose largest key >= ikey.
|
|
|
|
uint32_t index = FindFile(vset_->icmp_, files_[level], ikey);
|
|
|
|
if (index >= num_files) {
|
2018-04-11 07:18:06 +08:00
|
|
|
files = nullptr;
|
2011-06-22 10:36:45 +08:00
|
|
|
num_files = 0;
|
|
|
|
} else {
|
|
|
|
tmp2 = files[index];
|
|
|
|
if (ucmp->Compare(user_key, tmp2->smallest.user_key()) < 0) {
|
|
|
|
// All of "tmp2" is past any data for user_key
|
2018-04-11 07:18:06 +08:00
|
|
|
files = nullptr;
|
2011-06-22 10:36:45 +08:00
|
|
|
num_files = 0;
|
|
|
|
} else {
|
|
|
|
files = &tmp2;
|
|
|
|
num_files = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-06 08:19:37 +08:00
|
|
|
for (uint32_t i = 0; i < num_files; ++i) {
|
2018-04-11 07:18:06 +08:00
|
|
|
if (last_file_read != nullptr && stats->seek_file == nullptr) {
|
2011-06-22 10:36:45 +08:00
|
|
|
// We have had more than one seek for this read. Charge the 1st file.
|
|
|
|
stats->seek_file = last_file_read;
|
2011-09-02 03:08:02 +08:00
|
|
|
stats->seek_file_level = last_file_read_level;
|
2011-06-22 10:36:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileMetaData* f = files[i];
|
|
|
|
last_file_read = f;
|
2011-09-02 03:08:02 +08:00
|
|
|
last_file_read_level = level;
|
2011-06-22 10:36:45 +08:00
|
|
|
|
2012-04-17 23:36:46 +08:00
|
|
|
Saver saver;
|
|
|
|
saver.state = kNotFound;
|
|
|
|
saver.ucmp = ucmp;
|
|
|
|
saver.user_key = user_key;
|
|
|
|
saver.value = value;
|
2019-05-03 02:01:00 +08:00
|
|
|
s = vset_->table_cache_->Get(options, f->number, f->file_size, ikey,
|
|
|
|
&saver, SaveValue);
|
2012-04-17 23:36:46 +08:00
|
|
|
if (!s.ok()) {
|
2011-06-22 10:36:45 +08:00
|
|
|
return s;
|
2012-04-17 23:36:46 +08:00
|
|
|
}
|
|
|
|
switch (saver.state) {
|
|
|
|
case kNotFound:
|
2019-05-03 02:01:00 +08:00
|
|
|
break; // Keep searching in other files
|
2012-04-17 23:36:46 +08:00
|
|
|
case kFound:
|
|
|
|
return s;
|
|
|
|
case kDeleted:
|
|
|
|
s = Status::NotFound(Slice()); // Use empty error message for speed
|
|
|
|
return s;
|
|
|
|
case kCorrupt:
|
|
|
|
s = Status::Corruption("corrupted key for ", user_key);
|
2011-06-22 10:36:45 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::NotFound(Slice()); // Use an empty error message for speed
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Version::UpdateStats(const GetStats& stats) {
|
|
|
|
FileMetaData* f = stats.seek_file;
|
2018-04-11 07:18:06 +08:00
|
|
|
if (f != nullptr) {
|
2011-06-22 10:36:45 +08:00
|
|
|
f->allowed_seeks--;
|
2018-04-11 07:18:06 +08:00
|
|
|
if (f->allowed_seeks <= 0 && file_to_compact_ == nullptr) {
|
2011-06-22 10:36:45 +08:00
|
|
|
file_to_compact_ = f;
|
|
|
|
file_to_compact_level_ = stats.seek_file_level;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:12:47 +08:00
|
|
|
bool Version::RecordReadSample(Slice internal_key) {
|
|
|
|
ParsedInternalKey ikey;
|
|
|
|
if (!ParseInternalKey(internal_key, &ikey)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
GetStats stats; // Holds first matching file
|
|
|
|
int matches;
|
|
|
|
|
|
|
|
static bool Match(void* arg, int level, FileMetaData* f) {
|
|
|
|
State* state = reinterpret_cast<State*>(arg);
|
|
|
|
state->matches++;
|
|
|
|
if (state->matches == 1) {
|
|
|
|
// Remember first match.
|
|
|
|
state->stats.seek_file = f;
|
|
|
|
state->stats.seek_file_level = level;
|
|
|
|
}
|
|
|
|
// We can stop iterating once we have a second match.
|
|
|
|
return state->matches < 2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
State state;
|
|
|
|
state.matches = 0;
|
|
|
|
ForEachOverlapping(ikey.user_key, internal_key, &state, &State::Match);
|
|
|
|
|
|
|
|
// Must have at least two matches since we want to merge across
|
|
|
|
// files. But what if we have a single file that contains many
|
|
|
|
// overwrites and deletions? Should we have another mechanism for
|
|
|
|
// finding such files?
|
|
|
|
if (state.matches >= 2) {
|
|
|
|
// 1MB cost is about 1 seek (see comment in Builder::Apply).
|
|
|
|
return UpdateStats(state.stats);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
void Version::Ref() { ++refs_; }
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
void Version::Unref() {
|
2011-05-21 10:17:43 +08:00
|
|
|
assert(this != &vset_->dummy_versions_);
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(refs_ >= 1);
|
|
|
|
--refs_;
|
|
|
|
if (refs_ == 0) {
|
2011-05-21 10:17:43 +08:00
|
|
|
delete this;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
bool Version::OverlapInLevel(int level, const Slice* smallest_user_key,
|
2011-10-06 07:30:28 +08:00
|
|
|
const Slice* largest_user_key) {
|
|
|
|
return SomeFileOverlapsRange(vset_->icmp_, (level > 0), files_[level],
|
|
|
|
smallest_user_key, largest_user_key);
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
int Version::PickLevelForMemTableOutput(const Slice& smallest_user_key,
|
|
|
|
const Slice& largest_user_key) {
|
2011-10-06 07:30:28 +08:00
|
|
|
int level = 0;
|
|
|
|
if (!OverlapInLevel(0, &smallest_user_key, &largest_user_key)) {
|
|
|
|
// Push to next level if there is no overlap in next level,
|
|
|
|
// and the #bytes overlapping in the level after that are limited.
|
|
|
|
InternalKey start(smallest_user_key, kMaxSequenceNumber, kValueTypeForSeek);
|
|
|
|
InternalKey limit(largest_user_key, 0, static_cast<ValueType>(0));
|
|
|
|
std::vector<FileMetaData*> overlaps;
|
|
|
|
while (level < config::kMaxMemCompactLevel) {
|
|
|
|
if (OverlapInLevel(level + 1, &smallest_user_key, &largest_user_key)) {
|
|
|
|
break;
|
|
|
|
}
|
2013-08-22 02:12:47 +08:00
|
|
|
if (level + 2 < config::kNumLevels) {
|
|
|
|
// Check that file does not overlap too many grandparent bytes.
|
|
|
|
GetOverlappingInputs(level + 2, &start, &limit, &overlaps);
|
|
|
|
const int64_t sum = TotalFileSize(overlaps);
|
2016-09-27 19:50:38 +08:00
|
|
|
if (sum > MaxGrandParentOverlapBytes(vset_->options_)) {
|
2013-08-22 02:12:47 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-10-06 07:30:28 +08:00
|
|
|
}
|
|
|
|
level++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store in "*inputs" all files in "level" that overlap [begin,end]
|
2019-05-03 02:01:00 +08:00
|
|
|
void Version::GetOverlappingInputs(int level, const InternalKey* begin,
|
|
|
|
const InternalKey* end,
|
|
|
|
std::vector<FileMetaData*>* inputs) {
|
2013-08-22 02:12:47 +08:00
|
|
|
assert(level >= 0);
|
|
|
|
assert(level < config::kNumLevels);
|
2011-10-06 07:30:28 +08:00
|
|
|
inputs->clear();
|
|
|
|
Slice user_begin, user_end;
|
2018-04-11 07:18:06 +08:00
|
|
|
if (begin != nullptr) {
|
2011-10-06 07:30:28 +08:00
|
|
|
user_begin = begin->user_key();
|
|
|
|
}
|
2018-04-11 07:18:06 +08:00
|
|
|
if (end != nullptr) {
|
2011-10-06 07:30:28 +08:00
|
|
|
user_end = end->user_key();
|
|
|
|
}
|
|
|
|
const Comparator* user_cmp = vset_->icmp_.user_comparator();
|
2019-05-03 02:01:00 +08:00
|
|
|
for (size_t i = 0; i < files_[level].size();) {
|
2011-11-01 01:22:06 +08:00
|
|
|
FileMetaData* f = files_[level][i++];
|
|
|
|
const Slice file_start = f->smallest.user_key();
|
|
|
|
const Slice file_limit = f->largest.user_key();
|
2018-04-11 07:18:06 +08:00
|
|
|
if (begin != nullptr && user_cmp->Compare(file_limit, user_begin) < 0) {
|
2011-10-06 07:30:28 +08:00
|
|
|
// "f" is completely before specified range; skip it
|
2018-04-11 07:18:06 +08:00
|
|
|
} else if (end != nullptr && user_cmp->Compare(file_start, user_end) > 0) {
|
2011-10-06 07:30:28 +08:00
|
|
|
// "f" is completely after specified range; skip it
|
|
|
|
} else {
|
|
|
|
inputs->push_back(f);
|
2011-11-01 01:22:06 +08:00
|
|
|
if (level == 0) {
|
|
|
|
// Level-0 files may overlap each other. So check if the newly
|
|
|
|
// added file has expanded the range. If so, restart search.
|
2018-04-11 07:18:06 +08:00
|
|
|
if (begin != nullptr && user_cmp->Compare(file_start, user_begin) < 0) {
|
2011-11-01 01:22:06 +08:00
|
|
|
user_begin = file_start;
|
|
|
|
inputs->clear();
|
|
|
|
i = 0;
|
2019-05-03 02:01:00 +08:00
|
|
|
} else if (end != nullptr &&
|
|
|
|
user_cmp->Compare(file_limit, user_end) > 0) {
|
2011-11-01 01:22:06 +08:00
|
|
|
user_end = file_limit;
|
|
|
|
inputs->clear();
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-10-06 07:30:28 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-22 10:36:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
std::string Version::DebugString() const {
|
|
|
|
std::string r;
|
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
2011-06-22 10:36:45 +08:00
|
|
|
// E.g.,
|
|
|
|
// --- level 1 ---
|
|
|
|
// 17:123['a' .. 'd']
|
|
|
|
// 20:43['e' .. 'g']
|
|
|
|
r.append("--- level ");
|
2011-03-19 06:37:00 +08:00
|
|
|
AppendNumberTo(&r, level);
|
2011-06-22 10:36:45 +08:00
|
|
|
r.append(" ---\n");
|
2011-03-19 06:37:00 +08:00
|
|
|
const std::vector<FileMetaData*>& files = files_[level];
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
r.push_back(' ');
|
|
|
|
AppendNumberTo(&r, files[i]->number);
|
|
|
|
r.push_back(':');
|
|
|
|
AppendNumberTo(&r, files[i]->file_size);
|
2011-10-06 07:30:28 +08:00
|
|
|
r.append("[");
|
|
|
|
r.append(files[i]->smallest.DebugString());
|
|
|
|
r.append(" .. ");
|
|
|
|
r.append(files[i]->largest.DebugString());
|
|
|
|
r.append("]\n");
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper class so we can efficiently apply a whole sequence
|
|
|
|
// of edits to a particular state without creating intermediate
|
|
|
|
// Versions that contain full copies of the intermediate state.
|
|
|
|
class VersionSet::Builder {
|
|
|
|
private:
|
2011-05-21 10:17:43 +08:00
|
|
|
// Helper to sort by v->files_[file_number].smallest
|
|
|
|
struct BySmallestKey {
|
|
|
|
const InternalKeyComparator* internal_comparator;
|
|
|
|
|
|
|
|
bool operator()(FileMetaData* f1, FileMetaData* f2) const {
|
|
|
|
int r = internal_comparator->Compare(f1->smallest, f2->smallest);
|
|
|
|
if (r != 0) {
|
|
|
|
return (r < 0);
|
|
|
|
} else {
|
|
|
|
// Break ties by file number
|
|
|
|
return (f1->number < f2->number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::set<FileMetaData*, BySmallestKey> FileSet;
|
|
|
|
struct LevelState {
|
|
|
|
std::set<uint64_t> deleted_files;
|
|
|
|
FileSet* added_files;
|
|
|
|
};
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
VersionSet* vset_;
|
2011-05-21 10:17:43 +08:00
|
|
|
Version* base_;
|
|
|
|
LevelState levels_[config::kNumLevels];
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Initialize a builder with the files from *base and other info from *vset
|
2019-05-03 02:01:00 +08:00
|
|
|
Builder(VersionSet* vset, Version* base) : vset_(vset), base_(base) {
|
2011-05-21 10:17:43 +08:00
|
|
|
base_->Ref();
|
|
|
|
BySmallestKey cmp;
|
|
|
|
cmp.internal_comparator = &vset_->icmp_;
|
2011-03-19 06:37:00 +08:00
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
2011-05-21 10:17:43 +08:00
|
|
|
levels_[level].added_files = new FileSet(cmp);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Builder() {
|
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
2011-07-20 07:36:47 +08:00
|
|
|
const FileSet* added = levels_[level].added_files;
|
|
|
|
std::vector<FileMetaData*> to_unref;
|
|
|
|
to_unref.reserve(added->size());
|
2019-05-03 02:01:00 +08:00
|
|
|
for (FileSet::const_iterator it = added->begin(); it != added->end();
|
|
|
|
++it) {
|
2011-07-20 07:36:47 +08:00
|
|
|
to_unref.push_back(*it);
|
|
|
|
}
|
|
|
|
delete added;
|
2011-08-06 08:19:37 +08:00
|
|
|
for (uint32_t i = 0; i < to_unref.size(); i++) {
|
2011-05-21 10:17:43 +08:00
|
|
|
FileMetaData* f = to_unref[i];
|
2011-03-19 06:37:00 +08:00
|
|
|
f->refs--;
|
|
|
|
if (f->refs <= 0) {
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-21 10:17:43 +08:00
|
|
|
base_->Unref();
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply all of the edits in *edit to the current state.
|
|
|
|
void Apply(VersionEdit* edit) {
|
|
|
|
// Update compaction pointers
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < edit->compact_pointers_.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
const int level = edit->compact_pointers_[i].first;
|
|
|
|
vset_->compact_pointer_[level] =
|
|
|
|
edit->compact_pointers_[i].second.Encode().ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete files
|
2019-05-05 08:40:21 +08:00
|
|
|
for (const auto& deleted_file_set_kvp : edit->deleted_files_) {
|
|
|
|
const int level = deleted_file_set_kvp.first;
|
|
|
|
const uint64_t number = deleted_file_set_kvp.second;
|
2011-05-21 10:17:43 +08:00
|
|
|
levels_[level].deleted_files.insert(number);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add new files
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < edit->new_files_.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
const int level = edit->new_files_[i].first;
|
|
|
|
FileMetaData* f = new FileMetaData(edit->new_files_[i].second);
|
|
|
|
f->refs = 1;
|
2011-06-22 10:36:45 +08:00
|
|
|
|
|
|
|
// We arrange to automatically compact this file after
|
|
|
|
// a certain number of seeks. Let's assume:
|
|
|
|
// (1) One seek costs 10ms
|
|
|
|
// (2) Writing or reading 1MB costs 10ms (100MB/s)
|
|
|
|
// (3) A compaction of 1MB does 25MB of IO:
|
|
|
|
// 1MB read from this level
|
|
|
|
// 10-12MB read from next level (boundaries may be misaligned)
|
|
|
|
// 10-12MB written to next level
|
|
|
|
// This implies that 25 seeks cost the same as the compaction
|
|
|
|
// of 1MB of data. I.e., one seek costs approximately the
|
|
|
|
// same as the compaction of 40KB of data. We are a little
|
|
|
|
// conservative and allow approximately one seek for every 16KB
|
|
|
|
// of data before triggering a compaction.
|
2019-03-02 05:12:01 +08:00
|
|
|
f->allowed_seeks = static_cast<int>((f->file_size / 16384U));
|
2011-06-22 10:36:45 +08:00
|
|
|
if (f->allowed_seeks < 100) f->allowed_seeks = 100;
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
levels_[level].deleted_files.erase(f->number);
|
|
|
|
levels_[level].added_files->insert(f);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the current state in *v.
|
|
|
|
void SaveTo(Version* v) {
|
2011-05-21 10:17:43 +08:00
|
|
|
BySmallestKey cmp;
|
|
|
|
cmp.internal_comparator = &vset_->icmp_;
|
2011-03-19 06:37:00 +08:00
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
2011-05-21 10:17:43 +08:00
|
|
|
// Merge the set of added files with the set of pre-existing files.
|
|
|
|
// Drop any deleted files. Store the result in *v.
|
|
|
|
const std::vector<FileMetaData*>& base_files = base_->files_[level];
|
|
|
|
std::vector<FileMetaData*>::const_iterator base_iter = base_files.begin();
|
|
|
|
std::vector<FileMetaData*>::const_iterator base_end = base_files.end();
|
2019-05-05 08:40:21 +08:00
|
|
|
const FileSet* added_files = levels_[level].added_files;
|
|
|
|
v->files_[level].reserve(base_files.size() + added_files->size());
|
|
|
|
for (const auto& added_file : *added_files) {
|
2011-05-21 10:17:43 +08:00
|
|
|
// Add all smaller files listed in base_
|
2019-05-03 02:01:00 +08:00
|
|
|
for (std::vector<FileMetaData*>::const_iterator bpos =
|
2019-05-05 08:40:21 +08:00
|
|
|
std::upper_bound(base_iter, base_end, added_file, cmp);
|
2019-05-03 02:01:00 +08:00
|
|
|
base_iter != bpos; ++base_iter) {
|
2011-05-21 10:17:43 +08:00
|
|
|
MaybeAddFile(v, level, *base_iter);
|
|
|
|
}
|
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
MaybeAddFile(v, level, added_file);
|
2011-05-21 10:17:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add remaining base files
|
|
|
|
for (; base_iter != base_end; ++base_iter) {
|
|
|
|
MaybeAddFile(v, level, *base_iter);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2011-05-21 10:17:43 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure there is no overlap in levels > 0
|
|
|
|
if (level > 0) {
|
2011-08-06 08:19:37 +08:00
|
|
|
for (uint32_t i = 1; i < v->files_[level].size(); i++) {
|
2019-05-03 02:01:00 +08:00
|
|
|
const InternalKey& prev_end = v->files_[level][i - 1]->largest;
|
2011-05-21 10:17:43 +08:00
|
|
|
const InternalKey& this_begin = v->files_[level][i]->smallest;
|
|
|
|
if (vset_->icmp_.Compare(prev_end, this_begin) >= 0) {
|
|
|
|
fprintf(stderr, "overlapping ranges in same level %s vs. %s\n",
|
2011-10-06 07:30:28 +08:00
|
|
|
prev_end.DebugString().c_str(),
|
|
|
|
this_begin.DebugString().c_str());
|
2011-05-21 10:17:43 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaybeAddFile(Version* v, int level, FileMetaData* f) {
|
|
|
|
if (levels_[level].deleted_files.count(f->number) > 0) {
|
|
|
|
// File is deleted: do nothing
|
|
|
|
} else {
|
2011-06-22 10:36:45 +08:00
|
|
|
std::vector<FileMetaData*>* files = &v->files_[level];
|
|
|
|
if (level > 0 && !files->empty()) {
|
|
|
|
// Must not overlap
|
2019-05-03 02:01:00 +08:00
|
|
|
assert(vset_->icmp_.Compare((*files)[files->size() - 1]->largest,
|
2011-06-22 10:36:45 +08:00
|
|
|
f->smallest) < 0);
|
|
|
|
}
|
2011-05-21 10:17:43 +08:00
|
|
|
f->refs++;
|
2011-06-22 10:36:45 +08:00
|
|
|
files->push_back(f);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
VersionSet::VersionSet(const std::string& dbname, const Options* options,
|
2011-03-19 06:37:00 +08:00
|
|
|
TableCache* table_cache,
|
|
|
|
const InternalKeyComparator* cmp)
|
|
|
|
: env_(options->env),
|
|
|
|
dbname_(dbname),
|
|
|
|
options_(options),
|
|
|
|
table_cache_(table_cache),
|
|
|
|
icmp_(*cmp),
|
|
|
|
next_file_number_(2),
|
|
|
|
manifest_file_number_(0), // Filled by Recover()
|
2011-04-13 03:38:58 +08:00
|
|
|
last_sequence_(0),
|
|
|
|
log_number_(0),
|
|
|
|
prev_log_number_(0),
|
2018-04-11 07:18:06 +08:00
|
|
|
descriptor_file_(nullptr),
|
|
|
|
descriptor_log_(nullptr),
|
2011-05-21 10:17:43 +08:00
|
|
|
dummy_versions_(this),
|
2018-04-11 07:18:06 +08:00
|
|
|
current_(nullptr) {
|
2011-05-21 10:17:43 +08:00
|
|
|
AppendVersion(new Version(this));
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VersionSet::~VersionSet() {
|
2011-05-21 10:17:43 +08:00
|
|
|
current_->Unref();
|
|
|
|
assert(dummy_versions_.next_ == &dummy_versions_); // List must be empty
|
2011-03-19 06:37:00 +08:00
|
|
|
delete descriptor_log_;
|
|
|
|
delete descriptor_file_;
|
|
|
|
}
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
void VersionSet::AppendVersion(Version* v) {
|
|
|
|
// Make "v" current
|
|
|
|
assert(v->refs_ == 0);
|
|
|
|
assert(v != current_);
|
2018-04-11 07:18:06 +08:00
|
|
|
if (current_ != nullptr) {
|
2011-05-21 10:17:43 +08:00
|
|
|
current_->Unref();
|
|
|
|
}
|
|
|
|
current_ = v;
|
|
|
|
v->Ref();
|
|
|
|
|
|
|
|
// Append to linked list
|
|
|
|
v->prev_ = dummy_versions_.prev_;
|
|
|
|
v->next_ = &dummy_versions_;
|
|
|
|
v->prev_->next_ = v;
|
|
|
|
v->next_->prev_ = v;
|
|
|
|
}
|
|
|
|
|
2011-09-02 03:08:02 +08:00
|
|
|
Status VersionSet::LogAndApply(VersionEdit* edit, port::Mutex* mu) {
|
2011-04-13 03:38:58 +08:00
|
|
|
if (edit->has_log_number_) {
|
|
|
|
assert(edit->log_number_ >= log_number_);
|
|
|
|
assert(edit->log_number_ < next_file_number_);
|
|
|
|
} else {
|
|
|
|
edit->SetLogNumber(log_number_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!edit->has_prev_log_number_) {
|
|
|
|
edit->SetPrevLogNumber(prev_log_number_);
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
edit->SetNextFile(next_file_number_);
|
2011-04-13 03:38:58 +08:00
|
|
|
edit->SetLastSequence(last_sequence_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
Version* v = new Version(this);
|
|
|
|
{
|
|
|
|
Builder builder(this, current_);
|
|
|
|
builder.Apply(edit);
|
|
|
|
builder.SaveTo(v);
|
|
|
|
}
|
2011-05-21 10:17:43 +08:00
|
|
|
Finalize(v);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Initialize new descriptor log file if necessary by creating
|
|
|
|
// a temporary file that contains a snapshot of the current version.
|
2011-05-21 10:17:43 +08:00
|
|
|
std::string new_manifest_file;
|
|
|
|
Status s;
|
2018-04-11 07:18:06 +08:00
|
|
|
if (descriptor_log_ == nullptr) {
|
2011-09-02 03:08:02 +08:00
|
|
|
// No reason to unlock *mu here since we only hit this path in the
|
|
|
|
// first call to LogAndApply (when opening the database).
|
2018-04-11 07:18:06 +08:00
|
|
|
assert(descriptor_file_ == nullptr);
|
2011-05-21 10:17:43 +08:00
|
|
|
new_manifest_file = DescriptorFileName(dbname_, manifest_file_number_);
|
|
|
|
edit->SetNextFile(next_file_number_);
|
|
|
|
s = env_->NewWritableFile(new_manifest_file, &descriptor_file_);
|
|
|
|
if (s.ok()) {
|
|
|
|
descriptor_log_ = new log::Writer(descriptor_file_);
|
|
|
|
s = WriteSnapshot(descriptor_log_);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 03:08:02 +08:00
|
|
|
// Unlock during expensive MANIFEST log write
|
|
|
|
{
|
|
|
|
mu->Unlock();
|
|
|
|
|
|
|
|
// Write new record to MANIFEST log
|
2011-03-19 06:37:00 +08:00
|
|
|
if (s.ok()) {
|
2011-09-02 03:08:02 +08:00
|
|
|
std::string record;
|
|
|
|
edit->EncodeTo(&record);
|
|
|
|
s = descriptor_log_->AddRecord(record);
|
|
|
|
if (s.ok()) {
|
|
|
|
s = descriptor_file_->Sync();
|
|
|
|
}
|
2013-01-08 05:17:43 +08:00
|
|
|
if (!s.ok()) {
|
|
|
|
Log(options_->info_log, "MANIFEST write: %s\n", s.ToString().c_str());
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2011-09-02 03:08:02 +08:00
|
|
|
// If we just created a new descriptor file, install it by writing a
|
|
|
|
// new CURRENT file that points to it.
|
|
|
|
if (s.ok() && !new_manifest_file.empty()) {
|
|
|
|
s = SetCurrentFile(env_, dbname_, manifest_file_number_);
|
|
|
|
}
|
|
|
|
|
|
|
|
mu->Lock();
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install the new version
|
|
|
|
if (s.ok()) {
|
2011-05-21 10:17:43 +08:00
|
|
|
AppendVersion(v);
|
2011-04-13 03:38:58 +08:00
|
|
|
log_number_ = edit->log_number_;
|
|
|
|
prev_log_number_ = edit->prev_log_number_;
|
2011-03-19 06:37:00 +08:00
|
|
|
} else {
|
|
|
|
delete v;
|
|
|
|
if (!new_manifest_file.empty()) {
|
|
|
|
delete descriptor_log_;
|
|
|
|
delete descriptor_file_;
|
2018-04-11 07:18:06 +08:00
|
|
|
descriptor_log_ = nullptr;
|
|
|
|
descriptor_file_ = nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
env_->DeleteFile(new_manifest_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
Status VersionSet::Recover(bool* save_manifest) {
|
2011-03-19 06:37:00 +08:00
|
|
|
struct LogReporter : public log::Reader::Reporter {
|
|
|
|
Status* status;
|
2019-05-10 05:00:07 +08:00
|
|
|
void Corruption(size_t bytes, const Status& s) override {
|
2011-03-19 06:37:00 +08:00
|
|
|
if (this->status->ok()) *this->status = s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read "CURRENT" file, which contains a pointer to the current manifest file
|
|
|
|
std::string current;
|
|
|
|
Status s = ReadFileToString(env_, CurrentFileName(dbname_), ¤t);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2019-05-03 02:01:00 +08:00
|
|
|
if (current.empty() || current[current.size() - 1] != '\n') {
|
2011-03-19 06:37:00 +08:00
|
|
|
return Status::Corruption("CURRENT file does not end with newline");
|
|
|
|
}
|
|
|
|
current.resize(current.size() - 1);
|
|
|
|
|
|
|
|
std::string dscname = dbname_ + "/" + current;
|
|
|
|
SequentialFile* file;
|
|
|
|
s = env_->NewSequentialFile(dscname, &file);
|
|
|
|
if (!s.ok()) {
|
2017-07-11 04:32:58 +08:00
|
|
|
if (s.IsNotFound()) {
|
2019-05-03 02:01:00 +08:00
|
|
|
return Status::Corruption("CURRENT points to a non-existent file",
|
|
|
|
s.ToString());
|
2017-07-11 04:32:58 +08:00
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool have_log_number = false;
|
2011-04-13 03:38:58 +08:00
|
|
|
bool have_prev_log_number = false;
|
2011-03-19 06:37:00 +08:00
|
|
|
bool have_next_file = false;
|
|
|
|
bool have_last_sequence = false;
|
|
|
|
uint64_t next_file = 0;
|
2011-04-13 03:38:58 +08:00
|
|
|
uint64_t last_sequence = 0;
|
|
|
|
uint64_t log_number = 0;
|
|
|
|
uint64_t prev_log_number = 0;
|
2011-03-19 06:37:00 +08:00
|
|
|
Builder builder(this, current_);
|
|
|
|
|
|
|
|
{
|
|
|
|
LogReporter reporter;
|
|
|
|
reporter.status = &s;
|
2019-05-03 02:01:00 +08:00
|
|
|
log::Reader reader(file, &reporter, true /*checksum*/,
|
|
|
|
0 /*initial_offset*/);
|
2011-03-19 06:37:00 +08:00
|
|
|
Slice record;
|
|
|
|
std::string scratch;
|
|
|
|
while (reader.ReadRecord(&record, &scratch) && s.ok()) {
|
|
|
|
VersionEdit edit;
|
|
|
|
s = edit.DecodeFrom(record);
|
|
|
|
if (s.ok()) {
|
|
|
|
if (edit.has_comparator_ &&
|
|
|
|
edit.comparator_ != icmp_.user_comparator()->Name()) {
|
|
|
|
s = Status::InvalidArgument(
|
2012-10-13 02:53:12 +08:00
|
|
|
edit.comparator_ + " does not match existing comparator ",
|
2011-03-19 06:37:00 +08:00
|
|
|
icmp_.user_comparator()->Name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
builder.Apply(&edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edit.has_log_number_) {
|
2011-04-13 03:38:58 +08:00
|
|
|
log_number = edit.log_number_;
|
2011-03-19 06:37:00 +08:00
|
|
|
have_log_number = true;
|
|
|
|
}
|
|
|
|
|
2011-04-13 03:38:58 +08:00
|
|
|
if (edit.has_prev_log_number_) {
|
|
|
|
prev_log_number = edit.prev_log_number_;
|
|
|
|
have_prev_log_number = true;
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
if (edit.has_next_file_number_) {
|
|
|
|
next_file = edit.next_file_number_;
|
|
|
|
have_next_file = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edit.has_last_sequence_) {
|
2011-04-13 03:38:58 +08:00
|
|
|
last_sequence = edit.last_sequence_;
|
2011-03-19 06:37:00 +08:00
|
|
|
have_last_sequence = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete file;
|
2018-04-11 07:18:06 +08:00
|
|
|
file = nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
if (!have_next_file) {
|
|
|
|
s = Status::Corruption("no meta-nextfile entry in descriptor");
|
|
|
|
} else if (!have_log_number) {
|
|
|
|
s = Status::Corruption("no meta-lognumber entry in descriptor");
|
|
|
|
} else if (!have_last_sequence) {
|
|
|
|
s = Status::Corruption("no last-sequence-number entry in descriptor");
|
|
|
|
}
|
2011-04-13 03:38:58 +08:00
|
|
|
|
|
|
|
if (!have_prev_log_number) {
|
|
|
|
prev_log_number = 0;
|
|
|
|
}
|
2011-09-02 03:08:02 +08:00
|
|
|
|
|
|
|
MarkFileNumberUsed(prev_log_number);
|
|
|
|
MarkFileNumberUsed(log_number);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
Version* v = new Version(this);
|
|
|
|
builder.SaveTo(v);
|
2011-05-21 10:17:43 +08:00
|
|
|
// Install recovered version
|
|
|
|
Finalize(v);
|
|
|
|
AppendVersion(v);
|
|
|
|
manifest_file_number_ = next_file;
|
|
|
|
next_file_number_ = next_file + 1;
|
|
|
|
last_sequence_ = last_sequence;
|
|
|
|
log_number_ = log_number;
|
|
|
|
prev_log_number_ = prev_log_number;
|
2014-12-12 00:13:18 +08:00
|
|
|
|
|
|
|
// See if we can reuse the existing MANIFEST file.
|
|
|
|
if (ReuseManifest(dscname, current)) {
|
|
|
|
// No need to save new manifest
|
|
|
|
} else {
|
|
|
|
*save_manifest = true;
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:13:18 +08:00
|
|
|
bool VersionSet::ReuseManifest(const std::string& dscname,
|
|
|
|
const std::string& dscbase) {
|
2015-06-18 01:34:42 +08:00
|
|
|
if (!options_->reuse_logs) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-12 00:13:18 +08:00
|
|
|
FileType manifest_type;
|
|
|
|
uint64_t manifest_number;
|
|
|
|
uint64_t manifest_size;
|
|
|
|
if (!ParseFileName(dscbase, &manifest_number, &manifest_type) ||
|
|
|
|
manifest_type != kDescriptorFile ||
|
|
|
|
!env_->GetFileSize(dscname, &manifest_size).ok() ||
|
|
|
|
// Make new compacted MANIFEST if old one is too big
|
2016-09-27 19:50:38 +08:00
|
|
|
manifest_size >= TargetFileSize(options_)) {
|
2014-12-12 00:13:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-11 07:18:06 +08:00
|
|
|
assert(descriptor_file_ == nullptr);
|
|
|
|
assert(descriptor_log_ == nullptr);
|
2014-12-12 00:13:18 +08:00
|
|
|
Status r = env_->NewAppendableFile(dscname, &descriptor_file_);
|
|
|
|
if (!r.ok()) {
|
|
|
|
Log(options_->info_log, "Reuse MANIFEST: %s\n", r.ToString().c_str());
|
2018-04-11 07:18:06 +08:00
|
|
|
assert(descriptor_file_ == nullptr);
|
2014-12-12 00:13:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(options_->info_log, "Reusing MANIFEST %s\n", dscname.c_str());
|
|
|
|
descriptor_log_ = new log::Writer(descriptor_file_, manifest_size);
|
|
|
|
manifest_file_number_ = manifest_number;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-02 03:08:02 +08:00
|
|
|
void VersionSet::MarkFileNumberUsed(uint64_t number) {
|
|
|
|
if (next_file_number_ <= number) {
|
|
|
|
next_file_number_ = number + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
void VersionSet::Finalize(Version* v) {
|
2011-03-19 06:37:00 +08:00
|
|
|
// Precomputed best level for next compaction
|
|
|
|
int best_level = -1;
|
|
|
|
double best_score = -1;
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
for (int level = 0; level < config::kNumLevels - 1; level++) {
|
2011-04-13 03:38:58 +08:00
|
|
|
double score;
|
2011-03-19 06:37:00 +08:00
|
|
|
if (level == 0) {
|
2011-04-13 03:38:58 +08:00
|
|
|
// We treat level-0 specially by bounding the number of files
|
|
|
|
// instead of number of bytes for two reasons:
|
|
|
|
//
|
|
|
|
// (1) With larger write-buffer sizes, it is nice not to do too
|
|
|
|
// many level-0 compactions.
|
|
|
|
//
|
|
|
|
// (2) The files in level-0 are merged on every read and
|
|
|
|
// therefore we wish to avoid too many files when the individual
|
|
|
|
// file size is small (perhaps because of a small write-buffer
|
|
|
|
// setting, or very high compression ratios, or lots of
|
|
|
|
// overwrites/deletions).
|
2011-05-21 10:17:43 +08:00
|
|
|
score = v->files_[level].size() /
|
2019-05-03 02:01:00 +08:00
|
|
|
static_cast<double>(config::kL0_CompactionTrigger);
|
2011-04-13 03:38:58 +08:00
|
|
|
} else {
|
|
|
|
// Compute the ratio of current size to size limit.
|
|
|
|
const uint64_t level_bytes = TotalFileSize(v->files_[level]);
|
2016-09-27 19:50:38 +08:00
|
|
|
score =
|
|
|
|
static_cast<double>(level_bytes) / MaxBytesForLevel(options_, level);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (score > best_score) {
|
|
|
|
best_level = level;
|
|
|
|
best_score = score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->compaction_level_ = best_level;
|
|
|
|
v->compaction_score_ = best_score;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status VersionSet::WriteSnapshot(log::Writer* log) {
|
|
|
|
// TODO: Break up into multiple records to reduce memory usage on recovery?
|
|
|
|
|
|
|
|
// Save metadata
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.SetComparatorName(icmp_.user_comparator()->Name());
|
|
|
|
|
|
|
|
// Save compaction pointers
|
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
|
|
|
if (!compact_pointer_[level].empty()) {
|
|
|
|
InternalKey key;
|
|
|
|
key.DecodeFrom(compact_pointer_[level]);
|
|
|
|
edit.SetCompactPointer(level, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save files
|
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
|
|
|
const std::vector<FileMetaData*>& files = current_->files_[level];
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
const FileMetaData* f = files[i];
|
|
|
|
edit.AddFile(level, f->number, f->file_size, f->smallest, f->largest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string record;
|
|
|
|
edit.EncodeTo(&record);
|
|
|
|
return log->AddRecord(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
int VersionSet::NumLevelFiles(int level) const {
|
|
|
|
assert(level >= 0);
|
|
|
|
assert(level < config::kNumLevels);
|
|
|
|
return current_->files_[level].size();
|
|
|
|
}
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
const char* VersionSet::LevelSummary(LevelSummaryStorage* scratch) const {
|
|
|
|
// Update code if kNumLevels changes
|
2019-05-03 02:01:00 +08:00
|
|
|
static_assert(config::kNumLevels == 7, "");
|
2011-05-21 10:17:43 +08:00
|
|
|
snprintf(scratch->buffer, sizeof(scratch->buffer),
|
2019-05-03 02:01:00 +08:00
|
|
|
"files[ %d %d %d %d %d %d %d ]", int(current_->files_[0].size()),
|
|
|
|
int(current_->files_[1].size()), int(current_->files_[2].size()),
|
|
|
|
int(current_->files_[3].size()), int(current_->files_[4].size()),
|
|
|
|
int(current_->files_[5].size()), int(current_->files_[6].size()));
|
2011-05-21 10:17:43 +08:00
|
|
|
return scratch->buffer;
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
uint64_t VersionSet::ApproximateOffsetOf(Version* v, const InternalKey& ikey) {
|
|
|
|
uint64_t result = 0;
|
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
|
|
|
const std::vector<FileMetaData*>& files = v->files_[level];
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
if (icmp_.Compare(files[i]->largest, ikey) <= 0) {
|
|
|
|
// Entire file is before "ikey", so just add the file size
|
|
|
|
result += files[i]->file_size;
|
|
|
|
} else if (icmp_.Compare(files[i]->smallest, ikey) > 0) {
|
|
|
|
// Entire file is after "ikey", so ignore
|
|
|
|
if (level > 0) {
|
|
|
|
// Files other than level 0 are sorted by meta->smallest, so
|
|
|
|
// no further files in this level will contain data for
|
|
|
|
// "ikey".
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// "ikey" falls in the range for this table. Add the
|
|
|
|
// approximate offset of "ikey" within the table.
|
|
|
|
Table* tableptr;
|
|
|
|
Iterator* iter = table_cache_->NewIterator(
|
2011-03-29 04:43:44 +08:00
|
|
|
ReadOptions(), files[i]->number, files[i]->file_size, &tableptr);
|
2018-04-11 07:18:06 +08:00
|
|
|
if (tableptr != nullptr) {
|
2011-03-19 06:37:00 +08:00
|
|
|
result += tableptr->ApproximateOffsetOf(ikey.Encode());
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSet::AddLiveFiles(std::set<uint64_t>* live) {
|
2019-05-03 02:01:00 +08:00
|
|
|
for (Version* v = dummy_versions_.next_; v != &dummy_versions_;
|
2011-05-21 10:17:43 +08:00
|
|
|
v = v->next_) {
|
2011-03-19 06:37:00 +08:00
|
|
|
for (int level = 0; level < config::kNumLevels; level++) {
|
|
|
|
const std::vector<FileMetaData*>& files = v->files_[level];
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
live->insert(files[i]->number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-13 03:38:58 +08:00
|
|
|
int64_t VersionSet::NumLevelBytes(int level) const {
|
|
|
|
assert(level >= 0);
|
|
|
|
assert(level < config::kNumLevels);
|
|
|
|
return TotalFileSize(current_->files_[level]);
|
2011-03-23 07:24:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t VersionSet::MaxNextLevelOverlappingBytes() {
|
|
|
|
int64_t result = 0;
|
2011-03-23 02:32:49 +08:00
|
|
|
std::vector<FileMetaData*> overlaps;
|
2011-06-22 10:36:45 +08:00
|
|
|
for (int level = 1; level < config::kNumLevels - 1; level++) {
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < current_->files_[level].size(); i++) {
|
2011-03-23 02:32:49 +08:00
|
|
|
const FileMetaData* f = current_->files_[level][i];
|
2019-05-03 02:01:00 +08:00
|
|
|
current_->GetOverlappingInputs(level + 1, &f->smallest, &f->largest,
|
2011-10-06 07:30:28 +08:00
|
|
|
&overlaps);
|
2011-03-23 07:24:02 +08:00
|
|
|
const int64_t sum = TotalFileSize(overlaps);
|
2011-03-23 02:32:49 +08:00
|
|
|
if (sum > result) {
|
|
|
|
result = sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
// Stores the minimal range that covers all entries in inputs in
|
|
|
|
// *smallest, *largest.
|
|
|
|
// REQUIRES: inputs is not empty
|
|
|
|
void VersionSet::GetRange(const std::vector<FileMetaData*>& inputs,
|
2019-05-03 02:01:00 +08:00
|
|
|
InternalKey* smallest, InternalKey* largest) {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(!inputs.empty());
|
|
|
|
smallest->Clear();
|
|
|
|
largest->Clear();
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < inputs.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
FileMetaData* f = inputs[i];
|
|
|
|
if (i == 0) {
|
|
|
|
*smallest = f->smallest;
|
|
|
|
*largest = f->largest;
|
|
|
|
} else {
|
|
|
|
if (icmp_.Compare(f->smallest, *smallest) < 0) {
|
|
|
|
*smallest = f->smallest;
|
|
|
|
}
|
|
|
|
if (icmp_.Compare(f->largest, *largest) > 0) {
|
|
|
|
*largest = f->largest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:32:49 +08:00
|
|
|
// Stores the minimal range that covers all entries in inputs1 and inputs2
|
|
|
|
// in *smallest, *largest.
|
|
|
|
// REQUIRES: inputs is not empty
|
|
|
|
void VersionSet::GetRange2(const std::vector<FileMetaData*>& inputs1,
|
|
|
|
const std::vector<FileMetaData*>& inputs2,
|
2019-05-03 02:01:00 +08:00
|
|
|
InternalKey* smallest, InternalKey* largest) {
|
2011-03-23 02:32:49 +08:00
|
|
|
std::vector<FileMetaData*> all = inputs1;
|
|
|
|
all.insert(all.end(), inputs2.begin(), inputs2.end());
|
|
|
|
GetRange(all, smallest, largest);
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
Iterator* VersionSet::MakeInputIterator(Compaction* c) {
|
|
|
|
ReadOptions options;
|
|
|
|
options.verify_checksums = options_->paranoid_checks;
|
|
|
|
options.fill_cache = false;
|
|
|
|
|
|
|
|
// Level-0 files have to be merged together. For other levels,
|
|
|
|
// we will make a concatenating iterator per level.
|
|
|
|
// TODO(opt): use concatenating iterator for level-0 if there is no overlap
|
|
|
|
const int space = (c->level() == 0 ? c->inputs_[0].size() + 1 : 2);
|
|
|
|
Iterator** list = new Iterator*[space];
|
|
|
|
int num = 0;
|
|
|
|
for (int which = 0; which < 2; which++) {
|
|
|
|
if (!c->inputs_[which].empty()) {
|
|
|
|
if (c->level() + which == 0) {
|
|
|
|
const std::vector<FileMetaData*>& files = c->inputs_[which];
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
2019-05-03 02:01:00 +08:00
|
|
|
list[num++] = table_cache_->NewIterator(options, files[i]->number,
|
|
|
|
files[i]->file_size);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Create concatenating iterator for the files from this level
|
|
|
|
list[num++] = NewTwoLevelIterator(
|
2011-05-21 10:17:43 +08:00
|
|
|
new Version::LevelFileNumIterator(icmp_, &c->inputs_[which]),
|
2011-03-19 06:37:00 +08:00
|
|
|
&GetFileIterator, table_cache_, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(num <= space);
|
|
|
|
Iterator* result = NewMergingIterator(&icmp_, list, num);
|
|
|
|
delete[] list;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Compaction* VersionSet::PickCompaction() {
|
2011-06-22 10:36:45 +08:00
|
|
|
Compaction* c;
|
|
|
|
int level;
|
|
|
|
|
|
|
|
// We prefer compactions triggered by too much data in a level over
|
|
|
|
// the compactions triggered by seeks.
|
|
|
|
const bool size_compaction = (current_->compaction_score_ >= 1);
|
2018-04-11 07:18:06 +08:00
|
|
|
const bool seek_compaction = (current_->file_to_compact_ != nullptr);
|
2011-06-22 10:36:45 +08:00
|
|
|
if (size_compaction) {
|
|
|
|
level = current_->compaction_level_;
|
|
|
|
assert(level >= 0);
|
2019-05-03 02:01:00 +08:00
|
|
|
assert(level + 1 < config::kNumLevels);
|
2016-09-27 19:50:38 +08:00
|
|
|
c = new Compaction(options_, level);
|
2011-06-22 10:36:45 +08:00
|
|
|
|
|
|
|
// Pick the first file that comes after compact_pointer_[level]
|
|
|
|
for (size_t i = 0; i < current_->files_[level].size(); i++) {
|
|
|
|
FileMetaData* f = current_->files_[level][i];
|
|
|
|
if (compact_pointer_[level].empty() ||
|
|
|
|
icmp_.Compare(f->largest.Encode(), compact_pointer_[level]) > 0) {
|
|
|
|
c->inputs_[0].push_back(f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c->inputs_[0].empty()) {
|
|
|
|
// Wrap-around to the beginning of the key space
|
|
|
|
c->inputs_[0].push_back(current_->files_[level][0]);
|
|
|
|
}
|
|
|
|
} else if (seek_compaction) {
|
|
|
|
level = current_->file_to_compact_level_;
|
2016-09-27 19:50:38 +08:00
|
|
|
c = new Compaction(options_, level);
|
2011-06-22 10:36:45 +08:00
|
|
|
c->inputs_[0].push_back(current_->file_to_compact_);
|
|
|
|
} else {
|
2018-04-11 07:18:06 +08:00
|
|
|
return nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c->input_version_ = current_;
|
|
|
|
c->input_version_->Ref();
|
|
|
|
|
|
|
|
// Files in level 0 may overlap each other, so pick up all overlapping ones
|
|
|
|
if (level == 0) {
|
2011-03-23 02:32:49 +08:00
|
|
|
InternalKey smallest, largest;
|
|
|
|
GetRange(c->inputs_[0], &smallest, &largest);
|
2011-03-19 06:37:00 +08:00
|
|
|
// Note that the next call will discard the file we placed in
|
|
|
|
// c->inputs_[0] earlier and replace it with an overlapping set
|
|
|
|
// which will include the picked file.
|
2011-10-06 07:30:28 +08:00
|
|
|
current_->GetOverlappingInputs(0, &smallest, &largest, &c->inputs_[0]);
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(!c->inputs_[0].empty());
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:32:49 +08:00
|
|
|
SetupOtherInputs(c);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:10:37 +08:00
|
|
|
// Finds the largest key in a vector of files. Returns true if files it not
|
|
|
|
// empty.
|
|
|
|
bool FindLargestKey(const InternalKeyComparator& icmp,
|
|
|
|
const std::vector<FileMetaData*>& files,
|
|
|
|
InternalKey* largest_key) {
|
2016-01-25 10:10:16 +08:00
|
|
|
if (files.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-12 10:10:37 +08:00
|
|
|
*largest_key = files[0]->largest;
|
2016-01-25 10:10:16 +08:00
|
|
|
for (size_t i = 1; i < files.size(); ++i) {
|
|
|
|
FileMetaData* f = files[i];
|
2019-04-12 10:10:37 +08:00
|
|
|
if (icmp.Compare(f->largest, *largest_key) > 0) {
|
|
|
|
*largest_key = f->largest;
|
2016-01-25 10:10:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:10:37 +08:00
|
|
|
// Finds minimum file b2=(l2, u2) in level file for which l2 > u1 and
|
|
|
|
// user_key(l2) = user_key(u1)
|
|
|
|
FileMetaData* FindSmallestBoundaryFile(
|
|
|
|
const InternalKeyComparator& icmp,
|
|
|
|
const std::vector<FileMetaData*>& level_files,
|
|
|
|
const InternalKey& largest_key) {
|
2016-01-25 10:10:16 +08:00
|
|
|
const Comparator* user_cmp = icmp.user_comparator();
|
2019-04-12 10:10:37 +08:00
|
|
|
FileMetaData* smallest_boundary_file = nullptr;
|
|
|
|
for (size_t i = 0; i < level_files.size(); ++i) {
|
|
|
|
FileMetaData* f = level_files[i];
|
|
|
|
if (icmp.Compare(f->smallest, largest_key) > 0 &&
|
|
|
|
user_cmp->Compare(f->smallest.user_key(), largest_key.user_key()) ==
|
|
|
|
0) {
|
|
|
|
if (smallest_boundary_file == nullptr ||
|
|
|
|
icmp.Compare(f->smallest, smallest_boundary_file->smallest) < 0) {
|
|
|
|
smallest_boundary_file = f;
|
2016-01-25 10:10:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 10:10:37 +08:00
|
|
|
return smallest_boundary_file;
|
2016-01-25 10:10:16 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 10:10:37 +08:00
|
|
|
// Extracts the largest file b1 from |compaction_files| and then searches for a
|
|
|
|
// b2 in |level_files| for which user_key(u1) = user_key(l2). If it finds such a
|
|
|
|
// file b2 (known as a boundary file) it adds it to |compaction_files| and then
|
|
|
|
// searches again using this new upper bound.
|
2016-01-25 10:10:16 +08:00
|
|
|
//
|
2019-04-12 10:10:37 +08:00
|
|
|
// If there are two blocks, b1=(l1, u1) and b2=(l2, u2) and
|
|
|
|
// user_key(u1) = user_key(l2), and if we compact b1 but not b2 then a
|
|
|
|
// subsequent get operation will yield an incorrect result because it will
|
|
|
|
// return the record from b2 in level i rather than from b1 because it searches
|
|
|
|
// level by level for records matching the supplied user key.
|
2016-01-25 10:10:16 +08:00
|
|
|
//
|
|
|
|
// parameters:
|
2019-04-12 10:10:37 +08:00
|
|
|
// in level_files: List of files to search for boundary files.
|
|
|
|
// in/out compaction_files: List of files to extend by adding boundary files.
|
2016-01-25 10:10:16 +08:00
|
|
|
void AddBoundaryInputs(const InternalKeyComparator& icmp,
|
2019-04-12 10:10:37 +08:00
|
|
|
const std::vector<FileMetaData*>& level_files,
|
|
|
|
std::vector<FileMetaData*>* compaction_files) {
|
|
|
|
InternalKey largest_key;
|
2016-01-25 10:10:16 +08:00
|
|
|
|
2019-04-12 10:10:37 +08:00
|
|
|
// Quick return if compaction_files is empty.
|
|
|
|
if (!FindLargestKey(icmp, *compaction_files, &largest_key)) {
|
2016-01-25 10:10:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:10:37 +08:00
|
|
|
bool continue_searching = true;
|
|
|
|
while (continue_searching) {
|
|
|
|
FileMetaData* smallest_boundary_file =
|
|
|
|
FindSmallestBoundaryFile(icmp, level_files, largest_key);
|
2016-01-25 10:10:16 +08:00
|
|
|
|
2019-04-12 10:10:37 +08:00
|
|
|
// If a boundary file was found advance largest_key, otherwise we're done.
|
|
|
|
if (smallest_boundary_file != NULL) {
|
|
|
|
compaction_files->push_back(smallest_boundary_file);
|
|
|
|
largest_key = smallest_boundary_file->largest;
|
2016-01-25 10:10:16 +08:00
|
|
|
} else {
|
2019-04-12 10:10:37 +08:00
|
|
|
continue_searching = false;
|
2016-01-25 10:10:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:32:49 +08:00
|
|
|
void VersionSet::SetupOtherInputs(Compaction* c) {
|
|
|
|
const int level = c->level();
|
|
|
|
InternalKey smallest, largest;
|
2016-01-25 10:10:16 +08:00
|
|
|
|
|
|
|
AddBoundaryInputs(icmp_, current_->files_[level], &c->inputs_[0]);
|
2011-03-23 02:32:49 +08:00
|
|
|
GetRange(c->inputs_[0], &smallest, &largest);
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
current_->GetOverlappingInputs(level + 1, &smallest, &largest,
|
|
|
|
&c->inputs_[1]);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2011-03-23 02:32:49 +08:00
|
|
|
// Get entire range covered by compaction
|
|
|
|
InternalKey all_start, all_limit;
|
|
|
|
GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit);
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
// See if we can grow the number of inputs in "level" without
|
|
|
|
// changing the number of "level+1" files we pick up.
|
|
|
|
if (!c->inputs_[1].empty()) {
|
|
|
|
std::vector<FileMetaData*> expanded0;
|
2011-10-06 07:30:28 +08:00
|
|
|
current_->GetOverlappingInputs(level, &all_start, &all_limit, &expanded0);
|
2016-01-25 10:10:16 +08:00
|
|
|
AddBoundaryInputs(icmp_, current_->files_[level], &expanded0);
|
2012-02-03 01:34:14 +08:00
|
|
|
const int64_t inputs0_size = TotalFileSize(c->inputs_[0]);
|
|
|
|
const int64_t inputs1_size = TotalFileSize(c->inputs_[1]);
|
|
|
|
const int64_t expanded0_size = TotalFileSize(expanded0);
|
|
|
|
if (expanded0.size() > c->inputs_[0].size() &&
|
2016-09-27 19:50:38 +08:00
|
|
|
inputs1_size + expanded0_size <
|
|
|
|
ExpandedCompactionByteSizeLimit(options_)) {
|
2011-03-19 06:37:00 +08:00
|
|
|
InternalKey new_start, new_limit;
|
|
|
|
GetRange(expanded0, &new_start, &new_limit);
|
|
|
|
std::vector<FileMetaData*> expanded1;
|
2019-05-03 02:01:00 +08:00
|
|
|
current_->GetOverlappingInputs(level + 1, &new_start, &new_limit,
|
2011-10-06 07:30:28 +08:00
|
|
|
&expanded1);
|
2011-03-19 06:37:00 +08:00
|
|
|
if (expanded1.size() == c->inputs_[1].size()) {
|
2011-07-21 10:40:18 +08:00
|
|
|
Log(options_->info_log,
|
2012-02-03 01:34:14 +08:00
|
|
|
"Expanding@%d %d+%d (%ld+%ld bytes) to %d+%d (%ld+%ld bytes)\n",
|
2019-05-03 02:01:00 +08:00
|
|
|
level, int(c->inputs_[0].size()), int(c->inputs_[1].size()),
|
|
|
|
long(inputs0_size), long(inputs1_size), int(expanded0.size()),
|
|
|
|
int(expanded1.size()), long(expanded0_size), long(inputs1_size));
|
2011-03-19 06:37:00 +08:00
|
|
|
smallest = new_start;
|
|
|
|
largest = new_limit;
|
|
|
|
c->inputs_[0] = expanded0;
|
|
|
|
c->inputs_[1] = expanded1;
|
2011-03-23 02:32:49 +08:00
|
|
|
GetRange2(c->inputs_[0], c->inputs_[1], &all_start, &all_limit);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:32:49 +08:00
|
|
|
// Compute the set of grandparent files that overlap this compaction
|
|
|
|
// (parent == level+1; grandparent == level+2)
|
|
|
|
if (level + 2 < config::kNumLevels) {
|
2011-10-06 07:30:28 +08:00
|
|
|
current_->GetOverlappingInputs(level + 2, &all_start, &all_limit,
|
|
|
|
&c->grandparents_);
|
2011-03-23 02:32:49 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
// Update the place where we will do the next compaction for this level.
|
|
|
|
// We update this immediately instead of waiting for the VersionEdit
|
|
|
|
// to be applied so that if the compaction fails, we will try a different
|
|
|
|
// key range next time.
|
|
|
|
compact_pointer_[level] = largest.Encode().ToString();
|
|
|
|
c->edit_.SetCompactPointer(level, largest);
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
Compaction* VersionSet::CompactRange(int level, const InternalKey* begin,
|
|
|
|
const InternalKey* end) {
|
2011-03-19 06:37:00 +08:00
|
|
|
std::vector<FileMetaData*> inputs;
|
2011-10-06 07:30:28 +08:00
|
|
|
current_->GetOverlappingInputs(level, begin, end, &inputs);
|
2011-03-19 06:37:00 +08:00
|
|
|
if (inputs.empty()) {
|
2018-04-11 07:18:06 +08:00
|
|
|
return nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2011-10-06 07:30:28 +08:00
|
|
|
// Avoid compacting too much in one shot in case the range is large.
|
2013-06-14 07:14:06 +08:00
|
|
|
// But we cannot do this for level-0 since level-0 files can overlap
|
|
|
|
// and we must not pick one file and drop another older file if the
|
|
|
|
// two files overlap.
|
|
|
|
if (level > 0) {
|
2016-09-27 19:50:38 +08:00
|
|
|
const uint64_t limit = MaxFileSizeForLevel(options_, level);
|
2013-06-14 07:14:06 +08:00
|
|
|
uint64_t total = 0;
|
|
|
|
for (size_t i = 0; i < inputs.size(); i++) {
|
|
|
|
uint64_t s = inputs[i]->file_size;
|
|
|
|
total += s;
|
|
|
|
if (total >= limit) {
|
|
|
|
inputs.resize(i + 1);
|
|
|
|
break;
|
|
|
|
}
|
2011-10-06 07:30:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:50:38 +08:00
|
|
|
Compaction* c = new Compaction(options_, level);
|
2011-03-19 06:37:00 +08:00
|
|
|
c->input_version_ = current_;
|
|
|
|
c->input_version_->Ref();
|
|
|
|
c->inputs_[0] = inputs;
|
2011-03-23 02:32:49 +08:00
|
|
|
SetupOtherInputs(c);
|
2011-03-19 06:37:00 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:50:38 +08:00
|
|
|
Compaction::Compaction(const Options* options, int level)
|
2011-03-19 06:37:00 +08:00
|
|
|
: level_(level),
|
2016-09-27 19:50:38 +08:00
|
|
|
max_output_file_size_(MaxFileSizeForLevel(options, level)),
|
2018-04-11 07:18:06 +08:00
|
|
|
input_version_(nullptr),
|
2011-03-23 02:32:49 +08:00
|
|
|
grandparent_index_(0),
|
2011-03-23 07:24:02 +08:00
|
|
|
seen_key_(false),
|
|
|
|
overlapped_bytes_(0) {
|
2011-03-19 06:37:00 +08:00
|
|
|
for (int i = 0; i < config::kNumLevels; i++) {
|
|
|
|
level_ptrs_[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Compaction::~Compaction() {
|
2018-04-11 07:18:06 +08:00
|
|
|
if (input_version_ != nullptr) {
|
2011-03-19 06:37:00 +08:00
|
|
|
input_version_->Unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:32:49 +08:00
|
|
|
bool Compaction::IsTrivialMove() const {
|
2016-09-27 19:50:38 +08:00
|
|
|
const VersionSet* vset = input_version_->vset_;
|
2011-03-23 07:24:02 +08:00
|
|
|
// Avoid a move if there is lots of overlapping grandparent data.
|
2011-03-23 02:32:49 +08:00
|
|
|
// Otherwise, the move could create a parent file that will require
|
|
|
|
// a very expensive merge later on.
|
2016-09-27 19:50:38 +08:00
|
|
|
return (num_input_files(0) == 1 && num_input_files(1) == 0 &&
|
|
|
|
TotalFileSize(grandparents_) <=
|
|
|
|
MaxGrandParentOverlapBytes(vset->options_));
|
2011-03-23 02:32:49 +08:00
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
void Compaction::AddInputDeletions(VersionEdit* edit) {
|
|
|
|
for (int which = 0; which < 2; which++) {
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < inputs_[which].size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
edit->DeleteFile(level_ + which, inputs_[which][i]->number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Compaction::IsBaseLevelForKey(const Slice& user_key) {
|
|
|
|
// Maybe use binary search to find right entry instead of linear search?
|
|
|
|
const Comparator* user_cmp = input_version_->vset_->icmp_.user_comparator();
|
|
|
|
for (int lvl = level_ + 2; lvl < config::kNumLevels; lvl++) {
|
|
|
|
const std::vector<FileMetaData*>& files = input_version_->files_[lvl];
|
2019-05-14 00:31:30 +08:00
|
|
|
while (level_ptrs_[lvl] < files.size()) {
|
2011-03-19 06:37:00 +08:00
|
|
|
FileMetaData* f = files[level_ptrs_[lvl]];
|
|
|
|
if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) {
|
|
|
|
// We've advanced far enough
|
|
|
|
if (user_cmp->Compare(user_key, f->smallest.user_key()) >= 0) {
|
|
|
|
// Key falls in this file's range, so definitely not base level
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
level_ptrs_[lvl]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
bool Compaction::ShouldStopBefore(const Slice& internal_key) {
|
2016-09-27 19:50:38 +08:00
|
|
|
const VersionSet* vset = input_version_->vset_;
|
2011-03-23 02:32:49 +08:00
|
|
|
// Scan to find earliest grandparent file that contains key.
|
2016-09-27 19:50:38 +08:00
|
|
|
const InternalKeyComparator* icmp = &vset->icmp_;
|
2011-03-23 02:32:49 +08:00
|
|
|
while (grandparent_index_ < grandparents_.size() &&
|
2019-05-03 02:01:00 +08:00
|
|
|
icmp->Compare(internal_key,
|
|
|
|
grandparents_[grandparent_index_]->largest.Encode()) >
|
|
|
|
0) {
|
2011-03-23 07:24:02 +08:00
|
|
|
if (seen_key_) {
|
|
|
|
overlapped_bytes_ += grandparents_[grandparent_index_]->file_size;
|
|
|
|
}
|
2011-03-23 02:32:49 +08:00
|
|
|
grandparent_index_++;
|
|
|
|
}
|
2011-03-23 07:24:02 +08:00
|
|
|
seen_key_ = true;
|
2011-03-23 02:32:49 +08:00
|
|
|
|
2016-09-27 19:50:38 +08:00
|
|
|
if (overlapped_bytes_ > MaxGrandParentOverlapBytes(vset->options_)) {
|
2011-03-23 07:24:02 +08:00
|
|
|
// Too much overlap for current output; start new output
|
|
|
|
overlapped_bytes_ = 0;
|
2011-03-23 02:32:49 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
void Compaction::ReleaseInputs() {
|
2018-04-11 07:18:06 +08:00
|
|
|
if (input_version_ != nullptr) {
|
2011-03-19 06:37:00 +08:00
|
|
|
input_version_->Unref();
|
2018-04-11 07:18:06 +08:00
|
|
|
input_version_ = nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|