2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2014-09-25 20:14:01 +02:00
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2015-07-14 03:10:31 +02:00
|
|
|
#include "db/compacted_db_impl.h"
|
2014-09-25 20:14:01 +02:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/version_set.h"
|
2014-09-29 20:09:09 +02:00
|
|
|
#include "table/get_context.h"
|
2014-09-25 20:14:01 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
extern void MarkKeyMayExist(void* arg);
|
|
|
|
extern bool SaveValue(void* arg, const ParsedInternalKey& parsed_key,
|
2014-09-29 20:09:09 +02:00
|
|
|
const Slice& v, bool hit_and_return);
|
2014-09-25 20:14:01 +02:00
|
|
|
|
|
|
|
CompactedDBImpl::CompactedDBImpl(
|
|
|
|
const DBOptions& options, const std::string& dbname)
|
|
|
|
: DBImpl(options, dbname) {
|
|
|
|
}
|
|
|
|
|
|
|
|
CompactedDBImpl::~CompactedDBImpl() {
|
|
|
|
}
|
|
|
|
|
2014-09-25 22:34:51 +02:00
|
|
|
size_t CompactedDBImpl::FindFile(const Slice& key) {
|
2014-09-25 20:14:01 +02:00
|
|
|
size_t left = 0;
|
|
|
|
size_t right = files_.num_files - 1;
|
|
|
|
while (left < right) {
|
|
|
|
size_t mid = (left + right) >> 1;
|
|
|
|
const FdWithKeyRange& f = files_.files[mid];
|
|
|
|
if (user_comparator_->Compare(ExtractUserKey(f.largest_key), 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;
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 22:34:51 +02:00
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CompactedDBImpl::Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle*, const Slice& key, std::string* value) {
|
2014-09-29 20:09:09 +02:00
|
|
|
GetContext get_context(user_comparator_, nullptr, nullptr, nullptr,
|
2015-03-03 19:59:36 +01:00
|
|
|
GetContext::kNotFound, key, value, nullptr, nullptr,
|
2016-11-04 02:40:23 +01:00
|
|
|
nullptr, nullptr);
|
2014-09-25 20:14:01 +02:00
|
|
|
LookupKey lkey(key, kMaxSequenceNumber);
|
2014-09-29 20:09:09 +02:00
|
|
|
files_.files[FindFile(key)].fd.table_reader->Get(
|
|
|
|
options, lkey.internal_key(), &get_context);
|
|
|
|
if (get_context.State() == GetContext::kFound) {
|
2014-09-25 20:14:01 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
return Status::NotFound();
|
|
|
|
}
|
|
|
|
|
2014-09-25 22:34:51 +02:00
|
|
|
std::vector<Status> CompactedDBImpl::MultiGet(const ReadOptions& options,
|
|
|
|
const std::vector<ColumnFamilyHandle*>&,
|
|
|
|
const std::vector<Slice>& keys, std::vector<std::string>* values) {
|
|
|
|
autovector<TableReader*, 16> reader_list;
|
|
|
|
for (const auto& key : keys) {
|
|
|
|
const FdWithKeyRange& f = files_.files[FindFile(key)];
|
|
|
|
if (user_comparator_->Compare(key, ExtractUserKey(f.smallest_key)) < 0) {
|
|
|
|
reader_list.push_back(nullptr);
|
|
|
|
} else {
|
|
|
|
LookupKey lkey(key, kMaxSequenceNumber);
|
|
|
|
f.fd.table_reader->Prepare(lkey.internal_key());
|
|
|
|
reader_list.push_back(f.fd.table_reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<Status> statuses(keys.size(), Status::NotFound());
|
|
|
|
values->resize(keys.size());
|
|
|
|
int idx = 0;
|
|
|
|
for (auto* r : reader_list) {
|
|
|
|
if (r != nullptr) {
|
2014-09-29 20:09:09 +02:00
|
|
|
GetContext get_context(user_comparator_, nullptr, nullptr, nullptr,
|
|
|
|
GetContext::kNotFound, keys[idx], &(*values)[idx],
|
2016-11-04 02:40:23 +01:00
|
|
|
nullptr, nullptr, nullptr, nullptr);
|
2014-09-25 22:34:51 +02:00
|
|
|
LookupKey lkey(keys[idx], kMaxSequenceNumber);
|
2014-09-29 20:09:09 +02:00
|
|
|
r->Get(options, lkey.internal_key(), &get_context);
|
|
|
|
if (get_context.State() == GetContext::kFound) {
|
2014-09-25 22:34:51 +02:00
|
|
|
statuses[idx] = Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
return statuses;
|
|
|
|
}
|
|
|
|
|
2014-09-25 20:14:01 +02:00
|
|
|
Status CompactedDBImpl::Init(const Options& options) {
|
|
|
|
mutex_.Lock();
|
|
|
|
ColumnFamilyDescriptor cf(kDefaultColumnFamilyName,
|
|
|
|
ColumnFamilyOptions(options));
|
2016-04-22 00:32:06 +02:00
|
|
|
Status s = Recover({cf}, true /* read only */, false, true);
|
2014-09-25 20:14:01 +02:00
|
|
|
if (s.ok()) {
|
|
|
|
cfd_ = reinterpret_cast<ColumnFamilyHandleImpl*>(
|
|
|
|
DefaultColumnFamily())->cfd();
|
|
|
|
delete cfd_->InstallSuperVersion(new SuperVersion(), &mutex_);
|
|
|
|
}
|
|
|
|
mutex_.Unlock();
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-11-20 19:49:32 +01:00
|
|
|
NewThreadStatusCfInfo(cfd_);
|
2014-09-25 20:14:01 +02:00
|
|
|
version_ = cfd_->GetSuperVersion()->current;
|
|
|
|
user_comparator_ = cfd_->user_comparator();
|
2014-10-31 16:48:19 +01:00
|
|
|
auto* vstorage = version_->storage_info();
|
2015-04-02 01:55:08 +02:00
|
|
|
if (vstorage->num_non_empty_levels() == 0) {
|
|
|
|
return Status::NotSupported("no file exists");
|
|
|
|
}
|
2014-10-27 23:49:46 +01:00
|
|
|
const LevelFilesBrief& l0 = vstorage->LevelFilesBrief(0);
|
2014-09-25 20:14:01 +02:00
|
|
|
// L0 should not have files
|
2014-10-28 18:03:13 +01:00
|
|
|
if (l0.num_files > 1) {
|
2014-09-25 20:14:01 +02:00
|
|
|
return Status::NotSupported("L0 contain more than 1 file");
|
|
|
|
}
|
2014-10-28 18:03:13 +01:00
|
|
|
if (l0.num_files == 1) {
|
2014-11-04 02:45:55 +01:00
|
|
|
if (vstorage->num_non_empty_levels() > 1) {
|
2014-09-25 20:14:01 +02:00
|
|
|
return Status::NotSupported("Both L0 and other level contain files");
|
|
|
|
}
|
2014-10-28 18:03:13 +01:00
|
|
|
files_ = l0;
|
2014-09-25 20:14:01 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int i = 1; i < vstorage->num_non_empty_levels() - 1; ++i) {
|
2014-10-27 23:49:46 +01:00
|
|
|
if (vstorage->LevelFilesBrief(i).num_files > 0) {
|
2014-09-25 20:14:01 +02:00
|
|
|
return Status::NotSupported("Other levels also contain files");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-04 02:45:55 +01:00
|
|
|
int level = vstorage->num_non_empty_levels() - 1;
|
2014-10-27 23:49:46 +01:00
|
|
|
if (vstorage->LevelFilesBrief(level).num_files > 0) {
|
|
|
|
files_ = vstorage->LevelFilesBrief(level);
|
2014-09-25 20:14:01 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
return Status::NotSupported("no file exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CompactedDBImpl::Open(const Options& options,
|
|
|
|
const std::string& dbname, DB** dbptr) {
|
|
|
|
*dbptr = nullptr;
|
|
|
|
|
|
|
|
if (options.max_open_files != -1) {
|
|
|
|
return Status::InvalidArgument("require max_open_files = -1");
|
|
|
|
}
|
|
|
|
if (options.merge_operator.get() != nullptr) {
|
|
|
|
return Status::InvalidArgument("merge operator is not supported");
|
|
|
|
}
|
|
|
|
DBOptions db_options(options);
|
|
|
|
std::unique_ptr<CompactedDBImpl> db(new CompactedDBImpl(db_options, dbname));
|
|
|
|
Status s = db->Init(options);
|
|
|
|
if (s.ok()) {
|
2016-09-24 01:34:04 +02:00
|
|
|
Log(INFO_LEVEL, db->immutable_db_options_.info_log,
|
2014-09-29 21:45:04 +02:00
|
|
|
"Opened the db as fully compacted mode");
|
2016-09-24 01:34:04 +02:00
|
|
|
LogFlush(db->immutable_db_options_.info_log);
|
2014-09-25 20:14:01 +02:00
|
|
|
*dbptr = db.release();
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // ROCKSDB_LITE
|