2011-03-18 23:37:00 +01: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.
|
|
|
|
|
2011-03-30 20:35:40 +02:00
|
|
|
#include "leveldb/cache.h"
|
2012-04-17 17:36:46 +02:00
|
|
|
#include "leveldb/comparator.h"
|
2011-03-30 20:35:40 +02:00
|
|
|
#include "leveldb/env.h"
|
2012-04-17 17:36:46 +02:00
|
|
|
#include "leveldb/filter_policy.h"
|
|
|
|
#include "leveldb/options.h"
|
2012-11-03 05:02:40 +01:00
|
|
|
#include "leveldb/statistics.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "table/block.h"
|
2012-04-17 17:36:46 +02:00
|
|
|
#include "table/filter_block.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "table/format.h"
|
2013-04-23 00:20:20 +02:00
|
|
|
#include "table/table.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "table/two_level_iterator.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2013-02-01 00:20:24 +01:00
|
|
|
// The longest the prefix of the cache key used to identify blocks can be.
|
|
|
|
// We are using the fact that we know for Posix files the unique ID is three
|
|
|
|
// varints.
|
|
|
|
const size_t kMaxCacheKeyPrefixSize = kMaxVarint64Length*3+1;
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
struct Table::Rep {
|
|
|
|
~Rep() {
|
2012-04-17 17:36:46 +02:00
|
|
|
delete filter;
|
|
|
|
delete [] filter_data;
|
2011-03-18 23:37:00 +01:00
|
|
|
delete index_block;
|
|
|
|
}
|
2013-03-15 01:00:04 +01:00
|
|
|
Rep(const EnvOptions& storage_options) :
|
|
|
|
soptions(storage_options) {
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
Options options;
|
2013-03-15 01:00:04 +01:00
|
|
|
const EnvOptions& soptions;
|
2011-03-18 23:37:00 +01:00
|
|
|
Status status;
|
2013-01-20 11:07:13 +01:00
|
|
|
unique_ptr<RandomAccessFile> file;
|
2013-02-01 00:20:24 +01:00
|
|
|
char cache_key_prefix[kMaxCacheKeyPrefixSize];
|
|
|
|
size_t cache_key_prefix_size;
|
2012-04-17 17:36:46 +02:00
|
|
|
FilterBlockReader* filter;
|
|
|
|
const char* filter_data;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
BlockHandle metaindex_handle; // Handle to metaindex_block: saved from footer
|
|
|
|
Block* index_block;
|
|
|
|
};
|
|
|
|
|
2013-02-01 00:20:24 +01:00
|
|
|
// Helper function to setup the cache key's prefix for the Table.
|
|
|
|
void Table::SetupCacheKeyPrefix(Rep* rep) {
|
|
|
|
assert(kMaxCacheKeyPrefixSize >= 10);
|
|
|
|
rep->cache_key_prefix_size = 0;
|
|
|
|
if (rep->options.block_cache) {
|
|
|
|
rep->cache_key_prefix_size = rep->file->GetUniqueId(rep->cache_key_prefix,
|
|
|
|
kMaxCacheKeyPrefixSize);
|
|
|
|
|
|
|
|
if (rep->cache_key_prefix_size == 0) {
|
|
|
|
// If the prefix wasn't generated or was too long, we create one from the
|
|
|
|
// cache.
|
|
|
|
char* end = EncodeVarint64(rep->cache_key_prefix,
|
|
|
|
rep->options.block_cache->NewId());
|
|
|
|
rep->cache_key_prefix_size =
|
|
|
|
static_cast<size_t>(end - rep->cache_key_prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 08:47:56 +02:00
|
|
|
namespace { // anonymous namespace, not visible externally
|
|
|
|
|
|
|
|
// Read the block identified by "handle" from "file".
|
|
|
|
// The only relevant option is options.verify_checksums for now.
|
|
|
|
// Set *didIO to true if didIO is not null.
|
|
|
|
// On failure return non-OK.
|
|
|
|
// On success fill *result and return OK - caller owns *result
|
|
|
|
Status ReadBlock(RandomAccessFile* file,
|
|
|
|
const ReadOptions& options,
|
|
|
|
const BlockHandle& handle,
|
|
|
|
Block** result,
|
|
|
|
bool* didIO = nullptr) {
|
|
|
|
BlockContents contents;
|
|
|
|
Status s = ReadBlockContents(file, options, handle, &contents);
|
|
|
|
if (s.ok()) {
|
|
|
|
*result = new Block(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (didIO) {
|
|
|
|
*didIO = true;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
Status Table::Open(const Options& options,
|
2013-03-15 01:00:04 +01:00
|
|
|
const EnvOptions& soptions,
|
2013-01-20 11:07:13 +01:00
|
|
|
unique_ptr<RandomAccessFile>&& file,
|
2011-03-28 22:43:44 +02:00
|
|
|
uint64_t size,
|
2013-01-20 11:07:13 +01:00
|
|
|
unique_ptr<Table>* table) {
|
|
|
|
table->reset();
|
2011-03-18 23:37:00 +01:00
|
|
|
if (size < Footer::kEncodedLength) {
|
|
|
|
return Status::InvalidArgument("file is too short to be an sstable");
|
|
|
|
}
|
|
|
|
|
|
|
|
char footer_space[Footer::kEncodedLength];
|
|
|
|
Slice footer_input;
|
|
|
|
Status s = file->Read(size - Footer::kEncodedLength, Footer::kEncodedLength,
|
|
|
|
&footer_input, footer_space);
|
|
|
|
if (!s.ok()) return s;
|
|
|
|
|
2013-01-09 19:44:30 +01:00
|
|
|
// Check that we actually read the whole footer from the file. It may be
|
|
|
|
// that size isn't correct.
|
|
|
|
if (footer_input.size() != Footer::kEncodedLength) {
|
|
|
|
return Status::InvalidArgument("file is too short to be an sstable");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
Footer footer;
|
|
|
|
s = footer.DecodeFrom(&footer_input);
|
|
|
|
if (!s.ok()) return s;
|
|
|
|
|
2013-03-01 03:04:58 +01:00
|
|
|
Block* index_block = nullptr;
|
2013-04-23 08:47:56 +02:00
|
|
|
// TODO: we never really verify check sum for index block
|
|
|
|
s = ReadBlock(file.get(), ReadOptions(), footer.index_handle(), &index_block);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
// We've successfully read the footer and the index block: we're
|
|
|
|
// ready to serve requests.
|
2013-03-15 01:00:04 +01:00
|
|
|
Rep* rep = new Table::Rep(soptions);
|
2011-03-18 23:37:00 +01:00
|
|
|
rep->options = options;
|
2013-01-20 11:07:13 +01:00
|
|
|
rep->file = std::move(file);
|
2011-03-18 23:37:00 +01:00
|
|
|
rep->metaindex_handle = footer.metaindex_handle();
|
|
|
|
rep->index_block = index_block;
|
2013-02-01 00:20:24 +01:00
|
|
|
SetupCacheKeyPrefix(rep);
|
2013-03-01 03:04:58 +01:00
|
|
|
rep->filter_data = nullptr;
|
|
|
|
rep->filter = nullptr;
|
2013-01-20 11:07:13 +01:00
|
|
|
table->reset(new Table(rep));
|
2012-04-17 17:36:46 +02:00
|
|
|
(*table)->ReadMeta(footer);
|
2011-03-18 23:37:00 +01:00
|
|
|
} else {
|
|
|
|
if (index_block) delete index_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-05-18 00:53:01 +02:00
|
|
|
void Table::SetAccessHintForCompaction() {
|
|
|
|
switch (rep_->options.access_hint_on_compaction_start) {
|
|
|
|
case Options::NONE:
|
|
|
|
break;
|
|
|
|
case Options::NORMAL:
|
|
|
|
rep_->file->Hint(RandomAccessFile::NORMAL);
|
|
|
|
break;
|
|
|
|
case Options::SEQUENTIAL:
|
|
|
|
rep_->file->Hint(RandomAccessFile::SEQUENTIAL);
|
|
|
|
break;
|
|
|
|
case Options::WILLNEED:
|
|
|
|
rep_->file->Hint(RandomAccessFile::WILLNEED);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-17 17:36:46 +02:00
|
|
|
void Table::ReadMeta(const Footer& footer) {
|
2013-03-01 03:04:58 +01:00
|
|
|
if (rep_->options.filter_policy == nullptr) {
|
2012-04-17 17:36:46 +02:00
|
|
|
return; // Do not need any metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
|
|
|
|
// it is an empty block.
|
2013-04-23 08:47:56 +02:00
|
|
|
// TODO: we never really verify check sum for meta index block
|
|
|
|
Block* meta = nullptr;
|
|
|
|
if (!ReadBlock(rep_->file.get(), ReadOptions(), footer.metaindex_handle(),
|
|
|
|
&meta).ok()) {
|
2012-04-17 17:36:46 +02:00
|
|
|
// Do not propagate errors since meta info is not needed for operation
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* iter = meta->NewIterator(BytewiseComparator());
|
|
|
|
std::string key = "filter.";
|
|
|
|
key.append(rep_->options.filter_policy->Name());
|
|
|
|
iter->Seek(key);
|
|
|
|
if (iter->Valid() && iter->key() == Slice(key)) {
|
|
|
|
ReadFilter(iter->value());
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
delete meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Table::ReadFilter(const Slice& filter_handle_value) {
|
|
|
|
Slice v = filter_handle_value;
|
|
|
|
BlockHandle filter_handle;
|
|
|
|
if (!filter_handle.DecodeFrom(&v).ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-23 08:47:56 +02:00
|
|
|
// TODO: We might want to unify with ReadBlock() if we start
|
2012-04-17 17:36:46 +02:00
|
|
|
// requiring checksum verification in Table::Open.
|
|
|
|
ReadOptions opt;
|
|
|
|
BlockContents block;
|
2013-04-23 08:47:56 +02:00
|
|
|
if (!ReadBlockContents(rep_->file.get(), opt, filter_handle, &block).ok()) {
|
2012-04-17 17:36:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (block.heap_allocated) {
|
|
|
|
rep_->filter_data = block.data.data(); // Will need to delete later
|
|
|
|
}
|
|
|
|
rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
Table::~Table() {
|
|
|
|
delete rep_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DeleteBlock(void* arg, void* ignored) {
|
|
|
|
delete reinterpret_cast<Block*>(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DeleteCachedBlock(const Slice& key, void* value) {
|
|
|
|
Block* block = reinterpret_cast<Block*>(value);
|
|
|
|
delete block;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReleaseBlock(void* arg, void* h) {
|
|
|
|
Cache* cache = reinterpret_cast<Cache*>(arg);
|
|
|
|
Cache::Handle* handle = reinterpret_cast<Cache::Handle*>(h);
|
|
|
|
cache->Release(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert an index iterator value (i.e., an encoded BlockHandle)
|
|
|
|
// into an iterator over the contents of the corresponding block.
|
|
|
|
Iterator* Table::BlockReader(void* arg,
|
|
|
|
const ReadOptions& options,
|
2012-09-27 10:05:38 +02:00
|
|
|
const Slice& index_value,
|
|
|
|
bool* didIO) {
|
2011-03-18 23:37:00 +01:00
|
|
|
Table* table = reinterpret_cast<Table*>(arg);
|
2013-01-20 11:07:13 +01:00
|
|
|
Cache* block_cache = table->rep_->options.block_cache.get();
|
2013-03-27 19:27:39 +01:00
|
|
|
std::shared_ptr<Statistics> statistics = table->rep_->options.statistics;
|
2013-03-01 03:04:58 +01:00
|
|
|
Block* block = nullptr;
|
|
|
|
Cache::Handle* cache_handle = nullptr;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
BlockHandle handle;
|
|
|
|
Slice input = index_value;
|
|
|
|
Status s = handle.DecodeFrom(&input);
|
|
|
|
// We intentionally allow extra stuff in index_value so that we
|
|
|
|
// can add more features in the future.
|
|
|
|
|
|
|
|
if (s.ok()) {
|
2013-03-01 03:04:58 +01:00
|
|
|
if (block_cache != nullptr) {
|
2013-02-01 00:20:24 +01:00
|
|
|
char cache_key[kMaxCacheKeyPrefixSize + kMaxVarint64Length];
|
|
|
|
const size_t cache_key_prefix_size = table->rep_->cache_key_prefix_size;
|
|
|
|
assert(cache_key_prefix_size != 0);
|
|
|
|
assert(cache_key_prefix_size <= kMaxCacheKeyPrefixSize);
|
|
|
|
memcpy(cache_key, table->rep_->cache_key_prefix,
|
|
|
|
cache_key_prefix_size);
|
|
|
|
char* end = EncodeVarint64(cache_key + cache_key_prefix_size,
|
|
|
|
handle.offset());
|
|
|
|
Slice key(cache_key, static_cast<size_t>(end-cache_key));
|
2011-03-18 23:37:00 +01:00
|
|
|
cache_handle = block_cache->Lookup(key);
|
2013-03-01 03:04:58 +01:00
|
|
|
if (cache_handle != nullptr) {
|
2011-03-18 23:37:00 +01:00
|
|
|
block = reinterpret_cast<Block*>(block_cache->Value(cache_handle));
|
2012-11-03 05:02:40 +01:00
|
|
|
|
|
|
|
RecordTick(statistics, BLOCK_CACHE_HIT);
|
2011-03-18 23:37:00 +01:00
|
|
|
} else {
|
2013-04-23 08:47:56 +02:00
|
|
|
s = ReadBlock(table->rep_->file.get(), options, handle, &block, didIO);
|
2012-04-17 17:36:46 +02:00
|
|
|
if (s.ok()) {
|
2013-04-23 08:47:56 +02:00
|
|
|
if (block->isCachable() && options.fill_cache) {
|
2012-04-17 17:36:46 +02:00
|
|
|
cache_handle = block_cache->Insert(
|
2013-04-23 08:47:56 +02:00
|
|
|
key, block, block->size(), &DeleteCachedBlock);
|
2012-04-17 17:36:46 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2012-11-03 05:02:40 +01:00
|
|
|
|
|
|
|
RecordTick(statistics, BLOCK_CACHE_MISS);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-04-23 08:47:56 +02:00
|
|
|
s = ReadBlock(table->rep_->file.get(), options, handle, &block, didIO);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* iter;
|
2013-03-01 03:04:58 +01:00
|
|
|
if (block != nullptr) {
|
2011-03-18 23:37:00 +01:00
|
|
|
iter = block->NewIterator(table->rep_->options.comparator);
|
2013-03-01 03:04:58 +01:00
|
|
|
if (cache_handle == nullptr) {
|
|
|
|
iter->RegisterCleanup(&DeleteBlock, block, nullptr);
|
2011-03-18 23:37:00 +01:00
|
|
|
} else {
|
|
|
|
iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iter = NewErrorIterator(s);
|
|
|
|
}
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2012-09-27 10:05:38 +02:00
|
|
|
Iterator* Table::BlockReader(void* arg,
|
|
|
|
const ReadOptions& options,
|
2013-03-15 01:00:04 +01:00
|
|
|
const EnvOptions& soptions,
|
2013-05-18 00:53:01 +02:00
|
|
|
const Slice& index_value,
|
|
|
|
bool for_compaction) {
|
2013-03-01 03:04:58 +01:00
|
|
|
return BlockReader(arg, options, index_value, nullptr);
|
2012-09-27 10:05:38 +02:00
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
Iterator* Table::NewIterator(const ReadOptions& options) const {
|
|
|
|
return NewTwoLevelIterator(
|
|
|
|
rep_->index_block->NewIterator(rep_->options.comparator),
|
2013-03-15 01:00:04 +01:00
|
|
|
&Table::BlockReader, const_cast<Table*>(this), options, rep_->soptions);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2012-04-17 17:36:46 +02:00
|
|
|
Status Table::InternalGet(const ReadOptions& options, const Slice& k,
|
|
|
|
void* arg,
|
2013-05-18 00:53:01 +02:00
|
|
|
bool (*saver)(void*, const Slice&, const Slice&,
|
|
|
|
bool)) {
|
2012-04-17 17:36:46 +02:00
|
|
|
Status s;
|
|
|
|
Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator);
|
2013-03-21 23:59:47 +01:00
|
|
|
bool done = false;
|
|
|
|
for (iiter->Seek(k); iiter->Valid() && !done; iiter->Next()) {
|
2012-04-17 17:36:46 +02:00
|
|
|
Slice handle_value = iiter->value();
|
|
|
|
FilterBlockReader* filter = rep_->filter;
|
|
|
|
BlockHandle handle;
|
2013-03-01 03:04:58 +01:00
|
|
|
if (filter != nullptr &&
|
2012-04-17 17:36:46 +02:00
|
|
|
handle.DecodeFrom(&handle_value).ok() &&
|
|
|
|
!filter->KeyMayMatch(handle.offset(), k)) {
|
|
|
|
// Not found
|
2013-03-21 23:59:47 +01:00
|
|
|
// TODO: think about interaction with Merge. If a user key cannot
|
|
|
|
// cross one data block, we should be fine.
|
2012-11-09 03:18:34 +01:00
|
|
|
RecordTick(rep_->options.statistics, BLOOM_FILTER_USEFUL);
|
2013-03-21 23:59:47 +01:00
|
|
|
break;
|
2012-04-17 17:36:46 +02:00
|
|
|
} else {
|
2012-09-27 10:05:38 +02:00
|
|
|
bool didIO = false;
|
|
|
|
Iterator* block_iter = BlockReader(this, options, iiter->value(),
|
|
|
|
&didIO);
|
2013-03-21 23:59:47 +01:00
|
|
|
|
|
|
|
for (block_iter->Seek(k); block_iter->Valid(); block_iter->Next()) {
|
|
|
|
if (!(*saver)(arg, block_iter->key(), block_iter->value(), didIO)) {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-17 17:36:46 +02:00
|
|
|
}
|
|
|
|
s = block_iter->status();
|
|
|
|
delete block_iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s.ok()) {
|
|
|
|
s = iiter->status();
|
|
|
|
}
|
|
|
|
delete iiter;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:59:47 +01:00
|
|
|
bool SaveDidIO(void* arg, const Slice& key, const Slice& value, bool didIO) {
|
2013-02-01 00:20:24 +01:00
|
|
|
*reinterpret_cast<bool*>(arg) = didIO;
|
2013-03-21 23:59:47 +01:00
|
|
|
return false;
|
2013-02-01 00:20:24 +01:00
|
|
|
}
|
|
|
|
bool Table::TEST_KeyInCache(const ReadOptions& options, const Slice& key) {
|
|
|
|
// We use InternalGet() as it has logic that checks whether we read the
|
|
|
|
// block from the disk or not.
|
|
|
|
bool didIO = false;
|
|
|
|
Status s = InternalGet(options, key, &didIO, SaveDidIO);
|
|
|
|
assert(s.ok());
|
|
|
|
return !didIO;
|
|
|
|
}
|
2012-04-17 17:36:46 +02:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
uint64_t Table::ApproximateOffsetOf(const Slice& key) const {
|
|
|
|
Iterator* index_iter =
|
|
|
|
rep_->index_block->NewIterator(rep_->options.comparator);
|
|
|
|
index_iter->Seek(key);
|
|
|
|
uint64_t result;
|
|
|
|
if (index_iter->Valid()) {
|
|
|
|
BlockHandle handle;
|
|
|
|
Slice input = index_iter->value();
|
|
|
|
Status s = handle.DecodeFrom(&input);
|
|
|
|
if (s.ok()) {
|
|
|
|
result = handle.offset();
|
|
|
|
} else {
|
|
|
|
// Strange: we can't decode the block handle in the index block.
|
|
|
|
// We'll just return the offset of the metaindex block, which is
|
|
|
|
// close to the whole file size for this case.
|
|
|
|
result = rep_->metaindex_handle.offset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// key is past the last key in the file. Approximate the offset
|
|
|
|
// by returning the offset of the metaindex block (which is
|
|
|
|
// right near the end of the file).
|
|
|
|
result = rep_->metaindex_handle.offset();
|
|
|
|
}
|
|
|
|
delete index_iter;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-31 18:22:06 +01:00
|
|
|
} // namespace leveldb
|