2013-10-29 04:34:02 +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.
|
|
|
|
|
|
|
|
#include "table/plain_table_reader.h"
|
|
|
|
|
2014-01-27 22:53:22 +01:00
|
|
|
#include <string>
|
2014-02-14 00:27:59 +01:00
|
|
|
#include <vector>
|
2013-10-29 04:34:02 +01:00
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
|
|
|
|
|
|
|
#include "rocksdb/cache.h"
|
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/filter_policy.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/statistics.h"
|
|
|
|
|
|
|
|
#include "table/block.h"
|
|
|
|
#include "table/filter_block.h"
|
|
|
|
#include "table/format.h"
|
2013-12-06 01:51:26 +01:00
|
|
|
#include "table/meta_blocks.h"
|
2013-10-29 04:34:02 +01:00
|
|
|
#include "table/two_level_iterator.h"
|
2014-01-28 06:58:46 +01:00
|
|
|
#include "table/plain_table_factory.h"
|
2013-10-29 04:34:02 +01:00
|
|
|
|
|
|
|
#include "util/coding.h"
|
2013-12-20 18:35:24 +01:00
|
|
|
#include "util/dynamic_bloom.h"
|
2013-10-29 04:34:02 +01:00
|
|
|
#include "util/hash.h"
|
|
|
|
#include "util/histogram.h"
|
2013-11-21 20:11:02 +01:00
|
|
|
#include "util/murmurhash.h"
|
2013-10-29 04:34:02 +01:00
|
|
|
#include "util/perf_context_imp.h"
|
|
|
|
#include "util/stop_watch.h"
|
|
|
|
|
2013-11-21 20:11:02 +01:00
|
|
|
|
2013-10-29 04:34:02 +01:00
|
|
|
namespace rocksdb {
|
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
namespace {
|
2013-12-20 18:35:24 +01:00
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
inline uint32_t GetSliceHash(const Slice& s) {
|
2013-12-20 18:35:24 +01:00
|
|
|
return Hash(s.data(), s.size(), 397) ;
|
|
|
|
}
|
2014-01-25 06:10:19 +01:00
|
|
|
|
|
|
|
inline uint32_t GetBucketIdFromHash(uint32_t hash, uint32_t num_buckets) {
|
2013-12-20 18:35:24 +01:00
|
|
|
return hash % num_buckets;
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
// Safely getting a uint32_t element from a char array, where, starting from
|
|
|
|
// `base`, every 4 bytes are considered as an fixed 32 bit integer.
|
|
|
|
inline uint32_t GetFixed32Element(const char* base, size_t offset) {
|
|
|
|
return DecodeFixed32(base + offset * sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Iterator to iterate IndexedTable
|
|
|
|
class PlainTableIterator : public Iterator {
|
|
|
|
public:
|
2014-02-08 01:25:38 +01:00
|
|
|
explicit PlainTableIterator(PlainTableReader* table, bool use_prefix_seek);
|
2014-01-25 06:10:19 +01:00
|
|
|
~PlainTableIterator();
|
|
|
|
|
|
|
|
bool Valid() const;
|
|
|
|
|
|
|
|
void SeekToFirst();
|
|
|
|
|
|
|
|
void SeekToLast();
|
|
|
|
|
|
|
|
void Seek(const Slice& target);
|
|
|
|
|
|
|
|
void Next();
|
|
|
|
|
|
|
|
void Prev();
|
|
|
|
|
|
|
|
Slice key() const;
|
|
|
|
|
|
|
|
Slice value() const;
|
|
|
|
|
|
|
|
Status status() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
PlainTableReader* table_;
|
2014-02-08 01:25:38 +01:00
|
|
|
bool use_prefix_seek_;
|
2014-01-25 06:10:19 +01:00
|
|
|
uint32_t offset_;
|
|
|
|
uint32_t next_offset_;
|
|
|
|
Slice key_;
|
|
|
|
Slice value_;
|
|
|
|
Status status_;
|
2014-01-27 22:53:22 +01:00
|
|
|
std::string tmp_str_;
|
2014-01-25 06:10:19 +01:00
|
|
|
// No copying allowed
|
|
|
|
PlainTableIterator(const PlainTableIterator&) = delete;
|
|
|
|
void operator=(const Iterator&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern const uint64_t kPlainTableMagicNumber;
|
2014-02-14 00:27:59 +01:00
|
|
|
PlainTableReader::PlainTableReader(
|
|
|
|
const Options& options, unique_ptr<RandomAccessFile>&& file,
|
|
|
|
const EnvOptions& storage_options, const InternalKeyComparator& icomparator,
|
|
|
|
uint64_t file_size, int bloom_bits_per_key, double hash_table_ratio,
|
|
|
|
size_t index_sparseness, const TableProperties* table_properties)
|
|
|
|
: options_(options),
|
|
|
|
soptions_(storage_options),
|
|
|
|
file_(std::move(file)),
|
2014-01-27 22:53:22 +01:00
|
|
|
internal_comparator_(icomparator),
|
2014-01-25 06:10:19 +01:00
|
|
|
file_size_(file_size),
|
|
|
|
kHashTableRatio(hash_table_ratio),
|
|
|
|
kBloomBitsPerKey(bloom_bits_per_key),
|
2014-02-08 01:25:38 +01:00
|
|
|
kIndexIntervalForSamePrefixKeys(index_sparseness),
|
2014-01-25 06:10:19 +01:00
|
|
|
table_properties_(table_properties),
|
2014-02-08 04:26:49 +01:00
|
|
|
data_end_offset_(table_properties_->data_size),
|
2014-02-14 00:27:59 +01:00
|
|
|
user_key_len_(table_properties->fixed_key_len) {
|
|
|
|
assert(kHashTableRatio >= 0.0);
|
|
|
|
}
|
2013-10-29 04:34:02 +01:00
|
|
|
|
|
|
|
PlainTableReader::~PlainTableReader() {
|
|
|
|
}
|
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
Status PlainTableReader::Open(
|
|
|
|
const Options& options, const EnvOptions& soptions,
|
|
|
|
const InternalKeyComparator& internal_comparator,
|
|
|
|
unique_ptr<RandomAccessFile>&& file, uint64_t file_size,
|
|
|
|
unique_ptr<TableReader>* table_reader, const int bloom_bits_per_key,
|
|
|
|
double hash_table_ratio, size_t index_sparseness) {
|
2013-10-29 04:34:02 +01:00
|
|
|
assert(options.allow_mmap_reads);
|
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
if (file_size > kMaxFileSize) {
|
2013-11-21 20:11:02 +01:00
|
|
|
return Status::NotSupported("File is too large for PlainTableReader!");
|
|
|
|
}
|
|
|
|
|
2014-02-08 04:26:49 +01:00
|
|
|
TableProperties* props = nullptr;
|
2014-01-25 06:10:19 +01:00
|
|
|
auto s = ReadTableProperties(file.get(), file_size, kPlainTableMagicNumber,
|
2014-02-08 04:26:49 +01:00
|
|
|
options.env, options.info_log.get(), &props);
|
2013-12-06 01:51:26 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
std::unique_ptr<PlainTableReader> new_reader(new PlainTableReader(
|
2014-02-14 00:27:59 +01:00
|
|
|
options, std::move(file), soptions, internal_comparator, file_size,
|
|
|
|
bloom_bits_per_key, hash_table_ratio, index_sparseness, props));
|
2013-12-06 01:51:26 +01:00
|
|
|
|
|
|
|
// -- Populate Index
|
|
|
|
s = new_reader->PopulateIndex();
|
2013-10-29 04:34:02 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2013-12-06 01:51:26 +01:00
|
|
|
|
|
|
|
*table_reader = std::move(new_reader);
|
2013-10-29 04:34:02 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableReader::SetupForCompaction() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlainTableReader::PrefixMayMatch(const Slice& internal_prefix) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* PlainTableReader::NewIterator(const ReadOptions& options) {
|
2014-02-08 01:25:38 +01:00
|
|
|
return new PlainTableIterator(this, options.prefix_seek);
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 18:35:24 +01:00
|
|
|
struct PlainTableReader::IndexRecord {
|
|
|
|
uint32_t hash; // hash of the prefix
|
|
|
|
uint32_t offset; // offset of a row
|
|
|
|
IndexRecord* next;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper class to track all the index records
|
|
|
|
class PlainTableReader::IndexRecordList {
|
2014-01-25 06:10:19 +01:00
|
|
|
public:
|
|
|
|
explicit IndexRecordList(size_t num_records_per_group)
|
|
|
|
: kNumRecordsPerGroup(num_records_per_group),
|
|
|
|
current_group_(nullptr),
|
|
|
|
num_records_in_current_group_(num_records_per_group) {}
|
2013-12-20 18:35:24 +01:00
|
|
|
|
|
|
|
~IndexRecordList() {
|
|
|
|
for (size_t i = 0; i < groups_.size(); i++) {
|
|
|
|
delete[] groups_[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRecord(murmur_t hash, uint32_t offset) {
|
2014-01-25 06:10:19 +01:00
|
|
|
if (num_records_in_current_group_ == kNumRecordsPerGroup) {
|
2013-12-20 18:35:24 +01:00
|
|
|
current_group_ = AllocateNewGroup();
|
|
|
|
num_records_in_current_group_ = 0;
|
|
|
|
}
|
2014-01-25 06:10:19 +01:00
|
|
|
auto& new_record = current_group_[num_records_in_current_group_++];
|
2013-12-20 18:35:24 +01:00
|
|
|
new_record.hash = hash;
|
|
|
|
new_record.offset = offset;
|
|
|
|
new_record.next = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
size_t GetNumRecords() const {
|
|
|
|
return (groups_.size() - 1) * kNumRecordsPerGroup +
|
|
|
|
num_records_in_current_group_;
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
|
|
|
IndexRecord* At(size_t index) {
|
2014-01-25 06:10:19 +01:00
|
|
|
return &(groups_[index / kNumRecordsPerGroup][index % kNumRecordsPerGroup]);
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
private:
|
2013-12-20 18:35:24 +01:00
|
|
|
IndexRecord* AllocateNewGroup() {
|
2014-01-25 06:10:19 +01:00
|
|
|
IndexRecord* result = new IndexRecord[kNumRecordsPerGroup];
|
2013-12-20 18:35:24 +01:00
|
|
|
groups_.push_back(result);
|
|
|
|
return result;
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
2014-01-25 06:10:19 +01:00
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
// Each group in `groups_` contains fix-sized records (determined by
|
|
|
|
// kNumRecordsPerGroup). Which can help us minimize the cost if resizing
|
|
|
|
// occurs.
|
2014-01-25 06:10:19 +01:00
|
|
|
const size_t kNumRecordsPerGroup;
|
2013-12-20 18:35:24 +01:00
|
|
|
IndexRecord* current_group_;
|
|
|
|
// List of arrays allocated
|
|
|
|
std::vector<IndexRecord*> groups_;
|
|
|
|
size_t num_records_in_current_group_;
|
|
|
|
};
|
2013-11-21 20:11:02 +01:00
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
Status PlainTableReader::PopulateIndexRecordList(IndexRecordList* record_list,
|
2014-02-14 00:27:59 +01:00
|
|
|
int* num_prefixes) const {
|
2013-10-29 04:34:02 +01:00
|
|
|
Slice prev_key_prefix_slice;
|
2013-12-20 18:35:24 +01:00
|
|
|
uint32_t prev_key_prefix_hash = 0;
|
2013-11-21 20:11:02 +01:00
|
|
|
uint32_t pos = data_start_offset_;
|
2014-02-14 00:27:59 +01:00
|
|
|
int num_keys_per_prefix = 0;
|
2014-01-25 06:10:19 +01:00
|
|
|
bool is_first_record = true;
|
2013-10-29 04:34:02 +01:00
|
|
|
HistogramImpl keys_per_prefix_hist;
|
2013-11-21 20:11:02 +01:00
|
|
|
// Need map to be ordered to make sure sub indexes generated
|
|
|
|
// are in order.
|
2013-12-20 18:35:24 +01:00
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
*num_prefixes = 0;
|
2013-12-06 01:51:26 +01:00
|
|
|
while (pos < data_end_offset_) {
|
2013-11-21 20:11:02 +01:00
|
|
|
uint32_t key_offset = pos;
|
2014-01-27 22:53:22 +01:00
|
|
|
ParsedInternalKey key;
|
2014-01-25 06:10:19 +01:00
|
|
|
Slice value_slice;
|
2014-02-14 00:27:59 +01:00
|
|
|
Status s = Next(&pos, &key, &value_slice);
|
2014-02-08 01:25:38 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (bloom_) {
|
|
|
|
// total order mode and bloom filter is enabled.
|
|
|
|
bloom_->AddHash(GetSliceHash(key.user_key));
|
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
Slice key_prefix_slice = GetPrefix(key);
|
2013-10-29 04:34:02 +01:00
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
if (is_first_record || prev_key_prefix_slice != key_prefix_slice) {
|
2014-02-08 01:25:38 +01:00
|
|
|
++(*num_prefixes);
|
2014-01-25 06:10:19 +01:00
|
|
|
if (!is_first_record) {
|
2014-02-14 00:27:59 +01:00
|
|
|
keys_per_prefix_hist.Add(num_keys_per_prefix);
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
2014-02-14 00:27:59 +01:00
|
|
|
num_keys_per_prefix = 0;
|
2013-10-29 04:34:02 +01:00
|
|
|
prev_key_prefix_slice = key_prefix_slice;
|
2013-12-20 18:35:24 +01:00
|
|
|
prev_key_prefix_hash = GetSliceHash(key_prefix_slice);
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
if (kIndexIntervalForSamePrefixKeys == 0 ||
|
2014-02-14 00:27:59 +01:00
|
|
|
num_keys_per_prefix++ % kIndexIntervalForSamePrefixKeys == 0) {
|
2014-01-25 06:10:19 +01:00
|
|
|
// Add an index key for every kIndexIntervalForSamePrefixKeys keys
|
|
|
|
record_list->AddRecord(prev_key_prefix_hash, key_offset);
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
2014-01-25 06:10:19 +01:00
|
|
|
is_first_record = false;
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
2014-01-25 06:10:19 +01:00
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
keys_per_prefix_hist.Add(num_keys_per_prefix);
|
2013-12-20 18:35:24 +01:00
|
|
|
Log(options_.info_log, "Number of Keys per prefix Histogram: %s",
|
|
|
|
keys_per_prefix_hist.ToString().c_str());
|
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
return Status::OK();
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
void PlainTableReader::AllocateIndexAndBloom(int num_prefixes) {
|
2014-02-14 00:27:59 +01:00
|
|
|
index_.reset();
|
2014-01-25 06:10:19 +01:00
|
|
|
|
2014-04-02 00:00:48 +02:00
|
|
|
if (options_.prefix_extractor.get() != nullptr) {
|
2014-02-08 01:25:38 +01:00
|
|
|
uint32_t bloom_total_bits = num_prefixes * kBloomBitsPerKey;
|
|
|
|
if (bloom_total_bits > 0) {
|
2014-03-28 17:21:20 +01:00
|
|
|
bloom_.reset(new DynamicBloom(bloom_total_bits, options_.bloom_locality));
|
2014-02-08 01:25:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:00:48 +02:00
|
|
|
if (options_.prefix_extractor.get() == nullptr || kHashTableRatio <= 0) {
|
2014-02-08 01:25:38 +01:00
|
|
|
// Fall back to pure binary search if the user fails to specify a prefix
|
|
|
|
// extractor.
|
2014-02-14 00:27:59 +01:00
|
|
|
index_size_ = 1;
|
2014-02-08 01:25:38 +01:00
|
|
|
} else {
|
|
|
|
double hash_table_size_multipier = 1.0 / kHashTableRatio;
|
2014-02-14 00:27:59 +01:00
|
|
|
index_size_ = num_prefixes * hash_table_size_multipier + 1;
|
2013-11-22 00:13:45 +01:00
|
|
|
}
|
2014-02-14 00:27:59 +01:00
|
|
|
index_.reset(new uint32_t[index_size_]);
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
2013-11-21 20:11:02 +01:00
|
|
|
|
2013-12-20 18:35:24 +01:00
|
|
|
size_t PlainTableReader::BucketizeIndexesAndFillBloom(
|
2014-02-14 00:27:59 +01:00
|
|
|
IndexRecordList* record_list, std::vector<IndexRecord*>* hash_to_offsets,
|
2014-03-07 02:30:46 +01:00
|
|
|
std::vector<uint32_t>* entries_per_bucket) {
|
2013-12-20 18:35:24 +01:00
|
|
|
bool first = true;
|
|
|
|
uint32_t prev_hash = 0;
|
2014-02-14 00:27:59 +01:00
|
|
|
size_t num_records = record_list->GetNumRecords();
|
2013-12-20 18:35:24 +01:00
|
|
|
for (size_t i = 0; i < num_records; i++) {
|
2014-02-14 00:27:59 +01:00
|
|
|
IndexRecord* index_record = record_list->At(i);
|
2013-12-20 18:35:24 +01:00
|
|
|
uint32_t cur_hash = index_record->hash;
|
|
|
|
if (first || prev_hash != cur_hash) {
|
|
|
|
prev_hash = cur_hash;
|
|
|
|
first = false;
|
2014-02-08 01:25:38 +01:00
|
|
|
if (bloom_ && !IsTotalOrderMode()) {
|
2013-12-20 18:35:24 +01:00
|
|
|
bloom_->AddHash(cur_hash);
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-14 00:27:59 +01:00
|
|
|
uint32_t bucket = GetBucketIdFromHash(cur_hash, index_size_);
|
2014-01-25 06:10:19 +01:00
|
|
|
IndexRecord* prev_bucket_head = (*hash_to_offsets)[bucket];
|
2013-12-20 18:35:24 +01:00
|
|
|
index_record->next = prev_bucket_head;
|
2014-01-25 06:10:19 +01:00
|
|
|
(*hash_to_offsets)[bucket] = index_record;
|
2014-03-07 02:30:46 +01:00
|
|
|
(*entries_per_bucket)[bucket]++;
|
|
|
|
}
|
|
|
|
size_t sub_index_size = 0;
|
|
|
|
for (auto entry_count : *entries_per_bucket) {
|
|
|
|
if (entry_count <= 1) {
|
|
|
|
continue;
|
2013-11-22 00:13:45 +01:00
|
|
|
}
|
2014-03-07 02:30:46 +01:00
|
|
|
// Only buckets with more than 1 entry will have subindex.
|
|
|
|
sub_index_size += VarintLength(entry_count);
|
|
|
|
// total bytes needed to store these entries' in-file offsets.
|
|
|
|
sub_index_size += entry_count * kOffsetLen;
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
2014-03-07 02:30:46 +01:00
|
|
|
return sub_index_size;
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
2013-11-22 00:13:45 +01:00
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
void PlainTableReader::FillIndexes(
|
2014-03-07 02:30:46 +01:00
|
|
|
const size_t kSubIndexSize,
|
2014-01-25 06:10:19 +01:00
|
|
|
const std::vector<IndexRecord*>& hash_to_offsets,
|
2014-03-07 02:30:46 +01:00
|
|
|
const std::vector<uint32_t>& entries_per_bucket) {
|
|
|
|
Log(options_.info_log, "Reserving %zu bytes for plain table's sub_index",
|
|
|
|
kSubIndexSize);
|
|
|
|
sub_index_.reset(new char[kSubIndexSize]);
|
2013-12-20 18:35:24 +01:00
|
|
|
size_t sub_index_offset = 0;
|
2014-02-14 00:27:59 +01:00
|
|
|
for (int i = 0; i < index_size_; i++) {
|
2014-03-07 02:30:46 +01:00
|
|
|
uint32_t num_keys_for_bucket = entries_per_bucket[i];
|
2013-11-21 20:11:02 +01:00
|
|
|
switch (num_keys_for_bucket) {
|
|
|
|
case 0:
|
|
|
|
// No key for bucket
|
2014-02-14 00:27:59 +01:00
|
|
|
index_[i] = data_end_offset_;
|
2013-11-21 20:11:02 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// point directly to the file offset
|
2014-02-14 00:27:59 +01:00
|
|
|
index_[i] = hash_to_offsets[i]->offset;
|
2013-11-21 20:11:02 +01:00
|
|
|
break;
|
|
|
|
default:
|
2013-12-20 18:35:24 +01:00
|
|
|
// point to second level indexes.
|
2014-02-14 00:27:59 +01:00
|
|
|
index_[i] = sub_index_offset | kSubIndexMask;
|
|
|
|
char* prev_ptr = &sub_index_[sub_index_offset];
|
|
|
|
char* cur_ptr = EncodeVarint32(prev_ptr, num_keys_for_bucket);
|
2014-01-25 06:10:19 +01:00
|
|
|
sub_index_offset += (cur_ptr - prev_ptr);
|
2014-02-14 00:27:59 +01:00
|
|
|
char* sub_index_pos = &sub_index_[sub_index_offset];
|
2014-01-25 06:10:19 +01:00
|
|
|
IndexRecord* record = hash_to_offsets[i];
|
2013-12-20 18:35:24 +01:00
|
|
|
int j;
|
2014-01-25 06:10:19 +01:00
|
|
|
for (j = num_keys_for_bucket - 1; j >= 0 && record;
|
|
|
|
j--, record = record->next) {
|
2014-02-14 00:27:59 +01:00
|
|
|
EncodeFixed32(sub_index_pos + j * sizeof(uint32_t), record->offset);
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
|
|
|
assert(j == -1 && record == nullptr);
|
|
|
|
sub_index_offset += kOffsetLen * num_keys_for_bucket;
|
2014-03-07 02:30:46 +01:00
|
|
|
assert(sub_index_offset <= kSubIndexSize);
|
2013-12-20 18:35:24 +01:00
|
|
|
break;
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-07 02:30:46 +01:00
|
|
|
assert(sub_index_offset == kSubIndexSize);
|
2013-10-29 04:34:02 +01:00
|
|
|
|
2013-11-21 20:11:02 +01:00
|
|
|
Log(options_.info_log, "hash table size: %d, suffix_map length %zu",
|
2014-03-07 02:30:46 +01:00
|
|
|
index_size_, kSubIndexSize);
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status PlainTableReader::PopulateIndex() {
|
2014-02-08 01:25:38 +01:00
|
|
|
// options.prefix_extractor is requried for a hash-based look-up.
|
2014-04-02 00:00:48 +02:00
|
|
|
if (options_.prefix_extractor.get() == nullptr && kHashTableRatio != 0) {
|
2014-02-08 01:25:38 +01:00
|
|
|
return Status::NotSupported(
|
|
|
|
"PlainTable requires a prefix extractor enable prefix hash mode.");
|
|
|
|
}
|
|
|
|
|
2013-12-20 18:35:24 +01:00
|
|
|
// Get mmapped memory to file_data_.
|
|
|
|
Status s = file_->Read(0, file_size_, &file_data_, nullptr);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-01-25 06:10:19 +01:00
|
|
|
IndexRecordList record_list(kRecordsPerGroup);
|
|
|
|
// First, read the whole file, for every kIndexIntervalForSamePrefixKeys rows
|
|
|
|
// for a prefix (starting from the first one), generate a record of (hash,
|
|
|
|
// offset) and append it to IndexRecordList, which is a data structure created
|
|
|
|
// to store them.
|
2014-02-08 01:25:38 +01:00
|
|
|
int num_prefixes;
|
|
|
|
|
|
|
|
// Allocate bloom filter here for total order mode.
|
|
|
|
if (IsTotalOrderMode()) {
|
|
|
|
uint32_t num_bloom_bits = table_properties_->num_entries * kBloomBitsPerKey;
|
|
|
|
if (num_bloom_bits > 0) {
|
2014-03-28 17:21:20 +01:00
|
|
|
bloom_.reset(new DynamicBloom(num_bloom_bits, options_.bloom_locality));
|
2014-02-08 01:25:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
s = PopulateIndexRecordList(&record_list, &num_prefixes);
|
2014-02-08 01:25:38 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2013-12-20 18:35:24 +01:00
|
|
|
// Calculated hash table and bloom filter size and allocate memory for indexes
|
|
|
|
// and bloom filter based on the number of prefixes.
|
2014-01-25 06:10:19 +01:00
|
|
|
AllocateIndexAndBloom(num_prefixes);
|
2013-12-20 18:35:24 +01:00
|
|
|
|
|
|
|
// Bucketize all the index records to a temp data structure, in which for
|
|
|
|
// each bucket, we generate a linked list of IndexRecord, in reversed order.
|
2014-02-14 00:27:59 +01:00
|
|
|
std::vector<IndexRecord*> hash_to_offsets(index_size_, nullptr);
|
2014-03-07 02:30:46 +01:00
|
|
|
std::vector<uint32_t> entries_per_bucket(index_size_, 0);
|
2014-01-25 06:10:19 +01:00
|
|
|
size_t sub_index_size_needed = BucketizeIndexesAndFillBloom(
|
2014-03-07 02:30:46 +01:00
|
|
|
&record_list, &hash_to_offsets, &entries_per_bucket);
|
2013-12-20 18:35:24 +01:00
|
|
|
// From the temp data structure, populate indexes.
|
2014-03-07 02:30:46 +01:00
|
|
|
FillIndexes(sub_index_size_needed, hash_to_offsets, entries_per_bucket);
|
2013-10-29 04:34:02 +01:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2013-12-20 18:35:24 +01:00
|
|
|
Status PlainTableReader::GetOffset(const Slice& target, const Slice& prefix,
|
|
|
|
uint32_t prefix_hash, bool& prefix_matched,
|
2014-02-14 00:27:59 +01:00
|
|
|
uint32_t* offset) const {
|
2013-11-21 20:11:02 +01:00
|
|
|
prefix_matched = false;
|
2014-02-14 00:27:59 +01:00
|
|
|
int bucket = GetBucketIdFromHash(prefix_hash, index_size_);
|
|
|
|
uint32_t bucket_value = index_[bucket];
|
2013-11-21 20:11:02 +01:00
|
|
|
if (bucket_value == data_end_offset_) {
|
2014-02-14 00:27:59 +01:00
|
|
|
*offset = data_end_offset_;
|
2013-12-20 18:35:24 +01:00
|
|
|
return Status::OK();
|
2013-11-21 20:11:02 +01:00
|
|
|
} else if ((bucket_value & kSubIndexMask) == 0) {
|
|
|
|
// point directly to the file
|
2014-02-14 00:27:59 +01:00
|
|
|
*offset = bucket_value;
|
2013-12-20 18:35:24 +01:00
|
|
|
return Status::OK();
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 18:35:24 +01:00
|
|
|
// point to sub-index, need to do a binary search
|
2013-10-29 04:34:02 +01:00
|
|
|
uint32_t low = 0;
|
2013-11-21 20:11:02 +01:00
|
|
|
uint64_t prefix_index_offset = bucket_value ^ kSubIndexMask;
|
2013-12-20 18:35:24 +01:00
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
const char* index_ptr = &sub_index_[prefix_index_offset];
|
2014-01-17 21:22:39 +01:00
|
|
|
uint32_t upper_bound = 0;
|
2014-02-14 00:27:59 +01:00
|
|
|
const char* base_ptr = GetVarint32Ptr(index_ptr, index_ptr + 4, &upper_bound);
|
2013-11-21 20:11:02 +01:00
|
|
|
uint32_t high = upper_bound;
|
2014-01-27 22:53:22 +01:00
|
|
|
ParsedInternalKey mid_key;
|
|
|
|
ParsedInternalKey parsed_target;
|
|
|
|
if (!ParseInternalKey(target, &parsed_target)) {
|
|
|
|
return Status::Corruption(Slice());
|
|
|
|
}
|
2013-10-29 04:34:02 +01:00
|
|
|
|
2013-11-21 20:11:02 +01:00
|
|
|
// The key is between [low, high). Do a binary search between it.
|
2013-10-29 04:34:02 +01:00
|
|
|
while (high - low > 1) {
|
|
|
|
uint32_t mid = (high + low) / 2;
|
2014-02-14 00:27:59 +01:00
|
|
|
uint32_t file_offset = GetFixed32Element(base_ptr, mid);
|
2013-12-20 18:35:24 +01:00
|
|
|
size_t tmp;
|
2014-02-08 01:25:38 +01:00
|
|
|
Status s = ReadKey(file_data_.data() + file_offset, &mid_key, &tmp);
|
2013-12-20 18:35:24 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
int cmp_result = internal_comparator_.Compare(mid_key, parsed_target);
|
|
|
|
if (cmp_result < 0) {
|
2013-10-29 04:34:02 +01:00
|
|
|
low = mid;
|
|
|
|
} else {
|
|
|
|
if (cmp_result == 0) {
|
|
|
|
// Happen to have found the exact key or target is smaller than the
|
|
|
|
// first key after base_offset.
|
2013-11-21 20:11:02 +01:00
|
|
|
prefix_matched = true;
|
2014-02-14 00:27:59 +01:00
|
|
|
*offset = file_offset;
|
2013-12-20 18:35:24 +01:00
|
|
|
return Status::OK();
|
2013-10-29 04:34:02 +01:00
|
|
|
} else {
|
|
|
|
high = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 18:35:24 +01:00
|
|
|
// Both of the key at the position low or low+1 could share the same
|
|
|
|
// prefix as target. We need to rule out one of them to avoid to go
|
|
|
|
// to the wrong prefix.
|
2014-01-27 22:53:22 +01:00
|
|
|
ParsedInternalKey low_key;
|
2013-12-20 18:35:24 +01:00
|
|
|
size_t tmp;
|
2014-02-14 00:27:59 +01:00
|
|
|
uint32_t low_key_offset = GetFixed32Element(base_ptr, low);
|
2014-02-08 01:25:38 +01:00
|
|
|
Status s = ReadKey(file_data_.data() + low_key_offset, &low_key, &tmp);
|
2013-12-20 18:35:24 +01:00
|
|
|
if (GetPrefix(low_key) == prefix) {
|
|
|
|
prefix_matched = true;
|
2014-02-14 00:27:59 +01:00
|
|
|
*offset = low_key_offset;
|
2013-12-20 18:35:24 +01:00
|
|
|
} else if (low + 1 < upper_bound) {
|
|
|
|
// There is possible a next prefix, return it
|
2013-11-21 20:11:02 +01:00
|
|
|
prefix_matched = false;
|
2014-02-14 00:27:59 +01:00
|
|
|
*offset = GetFixed32Element(base_ptr, low + 1);
|
2013-12-20 18:35:24 +01:00
|
|
|
} else {
|
|
|
|
// target is larger than a key of the last prefix in this bucket
|
|
|
|
// but with a different prefix. Key does not exist.
|
2014-02-14 00:27:59 +01:00
|
|
|
*offset = data_end_offset_;
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
2013-12-20 18:35:24 +01:00
|
|
|
return Status::OK();
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
bool PlainTableReader::MatchBloom(uint32_t hash) const {
|
2014-04-02 00:00:48 +02:00
|
|
|
return bloom_.get() == nullptr || bloom_->MayContainHash(hash);
|
2013-11-22 00:13:45 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
Slice PlainTableReader::GetPrefix(const ParsedInternalKey& target) const {
|
|
|
|
return GetPrefixFromUserKey(target.user_key);
|
2014-01-27 22:53:22 +01:00
|
|
|
}
|
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
Status PlainTableReader::ReadKey(const char* start, ParsedInternalKey* key,
|
2014-02-08 01:25:38 +01:00
|
|
|
size_t* bytes_read) const {
|
2014-01-17 21:22:39 +01:00
|
|
|
const char* key_ptr = nullptr;
|
2014-02-08 01:25:38 +01:00
|
|
|
*bytes_read = 0;
|
2014-01-27 22:53:22 +01:00
|
|
|
size_t user_key_size = 0;
|
2013-12-20 18:35:24 +01:00
|
|
|
if (IsFixedLength()) {
|
2014-01-27 22:53:22 +01:00
|
|
|
user_key_size = user_key_len_;
|
2014-02-14 00:27:59 +01:00
|
|
|
key_ptr = start;
|
2013-12-20 18:35:24 +01:00
|
|
|
} else {
|
2014-01-27 22:53:22 +01:00
|
|
|
uint32_t tmp_size = 0;
|
2014-02-14 00:27:59 +01:00
|
|
|
key_ptr =
|
|
|
|
GetVarint32Ptr(start, file_data_.data() + data_end_offset_, &tmp_size);
|
2014-01-27 22:53:22 +01:00
|
|
|
if (key_ptr == nullptr) {
|
2014-02-26 23:36:54 +01:00
|
|
|
return Status::Corruption(
|
|
|
|
"Unexpected EOF when reading the next key's size");
|
2014-01-27 22:53:22 +01:00
|
|
|
}
|
|
|
|
user_key_size = (size_t)tmp_size;
|
2014-02-14 00:27:59 +01:00
|
|
|
*bytes_read = key_ptr - start;
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
if (key_ptr + user_key_size + 1 >= file_data_.data() + data_end_offset_) {
|
2014-02-26 23:36:54 +01:00
|
|
|
return Status::Corruption("Unexpected EOF when reading the next key");
|
2013-12-20 18:35:24 +01:00
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
|
|
|
|
if (*(key_ptr + user_key_size) == PlainTableFactory::kValueTypeSeqId0) {
|
|
|
|
// Special encoding for the row with seqID=0
|
|
|
|
key->user_key = Slice(key_ptr, user_key_size);
|
|
|
|
key->sequence = 0;
|
|
|
|
key->type = kTypeValue;
|
2014-02-08 01:25:38 +01:00
|
|
|
*bytes_read += user_key_size + 1;
|
2014-01-27 22:53:22 +01:00
|
|
|
} else {
|
2014-02-14 00:27:59 +01:00
|
|
|
if (start + user_key_size + 8 >= file_data_.data() + data_end_offset_) {
|
2014-02-26 23:36:54 +01:00
|
|
|
return Status::Corruption(
|
|
|
|
"Unexpected EOF when reading internal bytes of the next key");
|
2014-01-27 22:53:22 +01:00
|
|
|
}
|
|
|
|
if (!ParseInternalKey(Slice(key_ptr, user_key_size + 8), key)) {
|
2014-02-26 23:36:54 +01:00
|
|
|
return Status::Corruption(
|
|
|
|
Slice("Incorrect value type found when reading the next key"));
|
2014-01-27 22:53:22 +01:00
|
|
|
}
|
2014-02-08 01:25:38 +01:00
|
|
|
*bytes_read += user_key_size + 8;
|
2014-01-27 22:53:22 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 18:35:24 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2013-11-22 00:13:45 +01:00
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
Status PlainTableReader::Next(uint32_t* offset, ParsedInternalKey* key,
|
|
|
|
Slice* value) const {
|
|
|
|
if (*offset == data_end_offset_) {
|
|
|
|
*offset = data_end_offset_;
|
2013-11-21 20:11:02 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
if (*offset > data_end_offset_) {
|
2013-11-21 20:11:02 +01:00
|
|
|
return Status::Corruption("Offset is out of file size");
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
2014-02-14 00:27:59 +01:00
|
|
|
const char* start = file_data_.data() + *offset;
|
2013-12-20 18:35:24 +01:00
|
|
|
size_t bytes_for_key;
|
2014-02-14 00:27:59 +01:00
|
|
|
Status s = ReadKey(start, key, &bytes_for_key);
|
2014-02-26 23:36:54 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2013-11-21 20:11:02 +01:00
|
|
|
uint32_t value_size;
|
2014-02-14 00:27:59 +01:00
|
|
|
const char* value_ptr = GetVarint32Ptr(
|
|
|
|
start + bytes_for_key, file_data_.data() + data_end_offset_, &value_size);
|
2013-11-21 20:11:02 +01:00
|
|
|
if (value_ptr == nullptr) {
|
2014-02-26 23:36:54 +01:00
|
|
|
return Status::Corruption(
|
|
|
|
"Unexpected EOF when reading the next value's size.");
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
2014-02-14 00:27:59 +01:00
|
|
|
*offset = *offset + (value_ptr - start) + value_size;
|
|
|
|
if (*offset > data_end_offset_) {
|
2014-02-26 23:36:54 +01:00
|
|
|
return Status::Corruption("Unexpected EOF when reading the next value. ");
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
|
|
|
*value = Slice(value_ptr, value_size);
|
2013-10-29 04:34:02 +01:00
|
|
|
|
2013-11-21 20:11:02 +01:00
|
|
|
return Status::OK();
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 22:53:22 +01:00
|
|
|
Status PlainTableReader::Get(const ReadOptions& ro, const Slice& target,
|
|
|
|
void* arg,
|
|
|
|
bool (*saver)(void*, const ParsedInternalKey&,
|
|
|
|
const Slice&, bool),
|
|
|
|
void (*mark_key_may_exist)(void*)) {
|
2013-11-21 20:11:02 +01:00
|
|
|
// Check bloom filter first.
|
2014-02-08 01:25:38 +01:00
|
|
|
Slice prefix_slice;
|
|
|
|
uint32_t prefix_hash;
|
|
|
|
if (IsTotalOrderMode()) {
|
|
|
|
// Match whole user key for bloom filter check.
|
|
|
|
if (!MatchBloom(GetSliceHash(GetUserKey(target)))) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
// in total order mode, there is only one bucket 0, and we always use empty
|
|
|
|
// prefix.
|
|
|
|
prefix_slice = Slice();
|
|
|
|
prefix_hash = 0;
|
|
|
|
} else {
|
|
|
|
prefix_slice = GetPrefix(target);
|
|
|
|
prefix_hash = GetSliceHash(prefix_slice);
|
|
|
|
if (!MatchBloom(prefix_hash)) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
2013-11-21 20:11:02 +01:00
|
|
|
uint32_t offset;
|
|
|
|
bool prefix_match;
|
2014-02-08 01:25:38 +01:00
|
|
|
Status s =
|
|
|
|
GetOffset(target, prefix_slice, prefix_hash, prefix_match, &offset);
|
2013-12-20 18:35:24 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
ParsedInternalKey found_key;
|
|
|
|
ParsedInternalKey parsed_target;
|
|
|
|
if (!ParseInternalKey(target, &parsed_target)) {
|
|
|
|
return Status::Corruption(Slice());
|
|
|
|
}
|
|
|
|
|
2013-10-29 04:34:02 +01:00
|
|
|
Slice found_value;
|
2013-11-21 20:11:02 +01:00
|
|
|
while (offset < data_end_offset_) {
|
2014-02-14 00:27:59 +01:00
|
|
|
Status s = Next(&offset, &found_key, &found_value);
|
2013-11-21 20:11:02 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (!prefix_match) {
|
|
|
|
// Need to verify prefix for the first key found if it is not yet
|
|
|
|
// checked.
|
2013-12-20 18:35:24 +01:00
|
|
|
if (GetPrefix(found_key) != prefix_slice) {
|
|
|
|
return Status::OK();
|
2013-11-21 20:11:02 +01:00
|
|
|
}
|
|
|
|
prefix_match = true;
|
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
if (internal_comparator_.Compare(found_key, parsed_target) >= 0) {
|
|
|
|
if (!(*saver)(arg, found_key, found_value, true)) {
|
|
|
|
break;
|
|
|
|
}
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-21 20:11:02 +01:00
|
|
|
return Status::OK();
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t PlainTableReader::ApproximateOffsetOf(const Slice& key) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 01:25:38 +01:00
|
|
|
PlainTableIterator::PlainTableIterator(PlainTableReader* table,
|
|
|
|
bool use_prefix_seek)
|
|
|
|
: table_(table), use_prefix_seek_(use_prefix_seek) {
|
2013-12-20 18:35:24 +01:00
|
|
|
next_offset_ = offset_ = table_->data_end_offset_;
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PlainTableIterator::~PlainTableIterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlainTableIterator::Valid() const {
|
2013-11-21 20:11:02 +01:00
|
|
|
return offset_ < table_->data_end_offset_
|
|
|
|
&& offset_ >= table_->data_start_offset_;
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::SeekToFirst() {
|
2013-11-21 20:11:02 +01:00
|
|
|
next_offset_ = table_->data_start_offset_;
|
2013-12-20 18:35:24 +01:00
|
|
|
if (next_offset_ >= table_->data_end_offset_) {
|
|
|
|
next_offset_ = offset_ = table_->data_end_offset_;
|
|
|
|
} else {
|
|
|
|
Next();
|
|
|
|
}
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::SeekToLast() {
|
|
|
|
assert(false);
|
2014-02-08 01:25:38 +01:00
|
|
|
status_ = Status::NotSupported("SeekToLast() is not supported in PlainTable");
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::Seek(const Slice& target) {
|
2014-02-08 01:25:38 +01:00
|
|
|
// If the user doesn't set prefix seek option and we are not able to do a
|
|
|
|
// total Seek(). assert failure.
|
2014-02-14 00:27:59 +01:00
|
|
|
if (!use_prefix_seek_ && table_->index_size_ > 1) {
|
2014-02-08 01:25:38 +01:00
|
|
|
assert(false);
|
|
|
|
status_ = Status::NotSupported(
|
|
|
|
"PlainTable cannot issue non-prefix seek unless in total order mode.");
|
|
|
|
offset_ = next_offset_ = table_->data_end_offset_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice prefix_slice = table_->GetPrefix(target);
|
2014-04-02 00:00:48 +02:00
|
|
|
uint32_t prefix_hash = 0;
|
|
|
|
// Bloom filter is ignored in total-order mode.
|
|
|
|
if (!table_->IsTotalOrderMode()) {
|
2014-02-08 01:25:38 +01:00
|
|
|
prefix_hash = GetSliceHash(prefix_slice);
|
2014-04-02 00:00:48 +02:00
|
|
|
if (!table_->MatchBloom(prefix_hash)) {
|
|
|
|
offset_ = next_offset_ = table_->data_end_offset_;
|
|
|
|
return;
|
|
|
|
}
|
2013-11-22 00:13:45 +01:00
|
|
|
}
|
2013-11-21 20:11:02 +01:00
|
|
|
bool prefix_match;
|
2013-12-20 18:35:24 +01:00
|
|
|
status_ = table_->GetOffset(target, prefix_slice, prefix_hash, prefix_match,
|
2014-02-08 01:25:38 +01:00
|
|
|
&next_offset_);
|
2013-12-20 18:35:24 +01:00
|
|
|
if (!status_.ok()) {
|
|
|
|
offset_ = next_offset_ = table_->data_end_offset_;
|
|
|
|
return;
|
|
|
|
}
|
2013-11-21 20:11:02 +01:00
|
|
|
|
|
|
|
if (next_offset_ < table_-> data_end_offset_) {
|
|
|
|
for (Next(); status_.ok() && Valid(); Next()) {
|
|
|
|
if (!prefix_match) {
|
|
|
|
// Need to verify the first key's prefix
|
2013-12-20 18:35:24 +01:00
|
|
|
if (table_->GetPrefix(key()) != prefix_slice) {
|
2013-11-21 20:11:02 +01:00
|
|
|
offset_ = next_offset_ = table_->data_end_offset_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prefix_match = true;
|
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
if (table_->internal_comparator_.Compare(key(), target) >= 0) {
|
2013-11-21 20:11:02 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
2013-11-21 20:11:02 +01:00
|
|
|
} else {
|
|
|
|
offset_ = table_->data_end_offset_;
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::Next() {
|
|
|
|
offset_ = next_offset_;
|
2014-01-27 22:53:22 +01:00
|
|
|
if (offset_ < table_->data_end_offset_) {
|
|
|
|
Slice tmp_slice;
|
|
|
|
ParsedInternalKey parsed_key;
|
2014-02-14 00:27:59 +01:00
|
|
|
status_ = table_->Next(&next_offset_, &parsed_key, &value_);
|
2014-01-27 22:53:22 +01:00
|
|
|
if (status_.ok()) {
|
|
|
|
// Make a copy in this case. TODO optimize.
|
|
|
|
tmp_str_.clear();
|
|
|
|
AppendInternalKey(&tmp_str_, parsed_key);
|
|
|
|
key_ = Slice(tmp_str_);
|
|
|
|
} else {
|
|
|
|
offset_ = next_offset_ = table_->data_end_offset_;
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 04:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIterator::Prev() {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice PlainTableIterator::key() const {
|
2014-01-27 22:53:22 +01:00
|
|
|
assert(Valid());
|
2013-10-29 04:34:02 +01:00
|
|
|
return key_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice PlainTableIterator::value() const {
|
2014-01-27 22:53:22 +01:00
|
|
|
assert(Valid());
|
2013-10-29 04:34:02 +01:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status PlainTableIterator::status() const {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|