2011-03-18 22:37:00 +00: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 18:35:40 +00:00
|
|
|
#include "leveldb/table.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/cache.h"
|
2012-04-17 08:36:46 -07:00
|
|
|
#include "leveldb/comparator.h"
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/env.h"
|
2012-04-17 08:36:46 -07:00
|
|
|
#include "leveldb/filter_policy.h"
|
|
|
|
#include "leveldb/options.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "table/block.h"
|
2012-04-17 08:36:46 -07:00
|
|
|
#include "table/filter_block.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "table/format.h"
|
|
|
|
#include "table/two_level_iterator.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
struct Table::Rep {
|
|
|
|
~Rep() {
|
2012-04-17 08:36:46 -07:00
|
|
|
delete filter;
|
2019-05-02 11:01:00 -07:00
|
|
|
delete[] filter_data;
|
2011-03-18 22:37:00 +00:00
|
|
|
delete index_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
Options options;
|
|
|
|
Status status;
|
|
|
|
RandomAccessFile* file;
|
|
|
|
uint64_t cache_id;
|
2012-04-17 08:36:46 -07:00
|
|
|
FilterBlockReader* filter;
|
|
|
|
const char* filter_data;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
BlockHandle metaindex_handle; // Handle to metaindex_block: saved from footer
|
|
|
|
Block* index_block;
|
|
|
|
};
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Status Table::Open(const Options& options, RandomAccessFile* file,
|
|
|
|
uint64_t size, Table** table) {
|
2018-04-10 16:18:06 -07:00
|
|
|
*table = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
if (size < Footer::kEncodedLength) {
|
2014-09-16 14:19:52 -07:00
|
|
|
return Status::Corruption("file is too short to be an sstable");
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
Footer footer;
|
|
|
|
s = footer.DecodeFrom(&footer_input);
|
|
|
|
if (!s.ok()) return s;
|
|
|
|
|
|
|
|
// Read the index block
|
2017-09-01 11:44:05 -07:00
|
|
|
BlockContents index_block_contents;
|
2020-04-24 02:00:12 +00:00
|
|
|
ReadOptions opt;
|
|
|
|
if (options.paranoid_checks) {
|
|
|
|
opt.verify_checksums = true;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2020-04-24 02:00:12 +00:00
|
|
|
s = ReadBlock(file, opt, footer.index_handle(), &index_block_contents);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
// We've successfully read the footer and the index block: we're
|
|
|
|
// ready to serve requests.
|
2017-09-01 11:44:05 -07:00
|
|
|
Block* index_block = new Block(index_block_contents);
|
2011-03-18 22:37:00 +00:00
|
|
|
Rep* rep = new Table::Rep;
|
|
|
|
rep->options = options;
|
|
|
|
rep->file = file;
|
|
|
|
rep->metaindex_handle = footer.metaindex_handle();
|
|
|
|
rep->index_block = index_block;
|
|
|
|
rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0);
|
2018-04-10 16:18:06 -07:00
|
|
|
rep->filter_data = nullptr;
|
|
|
|
rep->filter = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
*table = new Table(rep);
|
2012-04-17 08:36:46 -07:00
|
|
|
(*table)->ReadMeta(footer);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:36:46 -07:00
|
|
|
void Table::ReadMeta(const Footer& footer) {
|
2018-04-10 16:18:06 -07:00
|
|
|
if (rep_->options.filter_policy == nullptr) {
|
2012-04-17 08:36:46 -07:00
|
|
|
return; // Do not need any metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
|
|
|
|
// it is an empty block.
|
|
|
|
ReadOptions opt;
|
2014-09-16 14:19:52 -07:00
|
|
|
if (rep_->options.paranoid_checks) {
|
|
|
|
opt.verify_checksums = true;
|
|
|
|
}
|
2012-04-17 08:36:46 -07:00
|
|
|
BlockContents contents;
|
|
|
|
if (!ReadBlock(rep_->file, opt, footer.metaindex_handle(), &contents).ok()) {
|
|
|
|
// Do not propagate errors since meta info is not needed for operation
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Block* meta = new Block(contents);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We might want to unify with ReadBlock() if we start
|
|
|
|
// requiring checksum verification in Table::Open.
|
|
|
|
ReadOptions opt;
|
2014-09-16 14:19:52 -07:00
|
|
|
if (rep_->options.paranoid_checks) {
|
|
|
|
opt.verify_checksums = true;
|
|
|
|
}
|
2012-04-17 08:36:46 -07:00
|
|
|
BlockContents block;
|
|
|
|
if (!ReadBlock(rep_->file, opt, filter_handle, &block).ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (block.heap_allocated) {
|
2019-05-02 11:01:00 -07:00
|
|
|
rep_->filter_data = block.data.data(); // Will need to delete later
|
2012-04-17 08:36:46 -07:00
|
|
|
}
|
|
|
|
rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Table::~Table() { delete rep_; }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
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.
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* Table::BlockReader(void* arg, const ReadOptions& options,
|
2011-03-18 22:37:00 +00:00
|
|
|
const Slice& index_value) {
|
|
|
|
Table* table = reinterpret_cast<Table*>(arg);
|
|
|
|
Cache* block_cache = table->rep_->options.block_cache;
|
2018-04-10 16:18:06 -07:00
|
|
|
Block* block = nullptr;
|
|
|
|
Cache::Handle* cache_handle = nullptr;
|
2011-03-18 22:37:00 +00: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()) {
|
2012-04-17 08:36:46 -07:00
|
|
|
BlockContents contents;
|
2018-04-10 16:18:06 -07:00
|
|
|
if (block_cache != nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
char cache_key_buffer[16];
|
|
|
|
EncodeFixed64(cache_key_buffer, table->rep_->cache_id);
|
2019-05-02 11:01:00 -07:00
|
|
|
EncodeFixed64(cache_key_buffer + 8, handle.offset());
|
2011-03-18 22:37:00 +00:00
|
|
|
Slice key(cache_key_buffer, sizeof(cache_key_buffer));
|
|
|
|
cache_handle = block_cache->Lookup(key);
|
2018-04-10 16:18:06 -07:00
|
|
|
if (cache_handle != nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
block = reinterpret_cast<Block*>(block_cache->Value(cache_handle));
|
|
|
|
} else {
|
2012-04-17 08:36:46 -07:00
|
|
|
s = ReadBlock(table->rep_->file, options, handle, &contents);
|
|
|
|
if (s.ok()) {
|
|
|
|
block = new Block(contents);
|
|
|
|
if (contents.cachable && options.fill_cache) {
|
2019-05-02 11:01:00 -07:00
|
|
|
cache_handle = block_cache->Insert(key, block, block->size(),
|
|
|
|
&DeleteCachedBlock);
|
2012-04-17 08:36:46 -07:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-04-17 08:36:46 -07:00
|
|
|
s = ReadBlock(table->rep_->file, options, handle, &contents);
|
|
|
|
if (s.ok()) {
|
|
|
|
block = new Block(contents);
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* iter;
|
2018-04-10 16:18:06 -07:00
|
|
|
if (block != nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
iter = block->NewIterator(table->rep_->options.comparator);
|
2018-04-10 16:18:06 -07:00
|
|
|
if (cache_handle == nullptr) {
|
|
|
|
iter->RegisterCleanup(&DeleteBlock, block, nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
|
|
|
iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iter = NewErrorIterator(s);
|
|
|
|
}
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* Table::NewIterator(const ReadOptions& options) const {
|
|
|
|
return NewTwoLevelIterator(
|
|
|
|
rep_->index_block->NewIterator(rep_->options.comparator),
|
|
|
|
&Table::BlockReader, const_cast<Table*>(this), options);
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Status Table::InternalGet(const ReadOptions& options, const Slice& k, void* arg,
|
|
|
|
void (*handle_result)(void*, const Slice&,
|
|
|
|
const Slice&)) {
|
2012-04-17 08:36:46 -07:00
|
|
|
Status s;
|
|
|
|
Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator);
|
|
|
|
iiter->Seek(k);
|
|
|
|
if (iiter->Valid()) {
|
|
|
|
Slice handle_value = iiter->value();
|
|
|
|
FilterBlockReader* filter = rep_->filter;
|
|
|
|
BlockHandle handle;
|
2019-05-02 11:01:00 -07:00
|
|
|
if (filter != nullptr && handle.DecodeFrom(&handle_value).ok() &&
|
2012-04-17 08:36:46 -07:00
|
|
|
!filter->KeyMayMatch(handle.offset(), k)) {
|
|
|
|
// Not found
|
|
|
|
} else {
|
|
|
|
Iterator* block_iter = BlockReader(this, options, iiter->value());
|
|
|
|
block_iter->Seek(k);
|
|
|
|
if (block_iter->Valid()) {
|
2019-05-02 11:01:00 -07:00
|
|
|
(*handle_result)(arg, block_iter->key(), block_iter->value());
|
2012-04-17 08:36:46 -07:00
|
|
|
}
|
|
|
|
s = block_iter->status();
|
|
|
|
delete block_iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s.ok()) {
|
|
|
|
s = iiter->status();
|
|
|
|
}
|
|
|
|
delete iiter;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00: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 17:22:06 +00:00
|
|
|
} // namespace leveldb
|