2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-07-19 01:58:13 +02:00
|
|
|
|
2014-11-25 05:47:27 +01:00
|
|
|
#ifndef ROCKSDB_LITE
|
2021-09-29 13:01:57 +02:00
|
|
|
#include "table/plain/plain_table_index.h"
|
2014-11-25 05:47:27 +01:00
|
|
|
|
2019-06-06 22:52:39 +02:00
|
|
|
#include <cinttypes>
|
2014-11-11 22:47:22 +01:00
|
|
|
|
2021-09-29 13:01:57 +02:00
|
|
|
#include "logging/logging.h"
|
2014-07-19 01:58:13 +02:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/hash.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-07-19 01:58:13 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
inline uint32_t GetBucketIdFromHash(uint32_t hash, uint32_t num_buckets) {
|
|
|
|
assert(num_buckets > 0);
|
|
|
|
return hash % num_buckets;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:31:33 +02:00
|
|
|
Status PlainTableIndex::InitFromRawData(Slice data) {
|
|
|
|
if (!GetVarint32(&data, &index_size_)) {
|
|
|
|
return Status::Corruption("Couldn't read the index size!");
|
|
|
|
}
|
2014-07-19 01:58:13 +02:00
|
|
|
assert(index_size_ > 0);
|
2014-07-21 19:31:33 +02:00
|
|
|
if (!GetVarint32(&data, &num_prefixes_)) {
|
|
|
|
return Status::Corruption("Couldn't read the index size!");
|
|
|
|
}
|
2014-11-11 22:47:22 +01:00
|
|
|
sub_index_size_ =
|
|
|
|
static_cast<uint32_t>(data.size()) - index_size_ * kOffsetLen;
|
2014-07-19 01:58:13 +02:00
|
|
|
|
|
|
|
char* index_data_begin = const_cast<char*>(data.data());
|
|
|
|
index_ = reinterpret_cast<uint32_t*>(index_data_begin);
|
|
|
|
sub_index_ = reinterpret_cast<char*>(index_ + index_size_);
|
2014-07-21 19:31:33 +02:00
|
|
|
return Status::OK();
|
2014-07-19 01:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PlainTableIndex::IndexSearchResult PlainTableIndex::GetOffset(
|
|
|
|
uint32_t prefix_hash, uint32_t* bucket_value) const {
|
|
|
|
int bucket = GetBucketIdFromHash(prefix_hash, index_size_);
|
2017-04-22 05:41:37 +02:00
|
|
|
GetUnaligned(index_ + bucket, bucket_value);
|
2014-07-19 01:58:13 +02:00
|
|
|
if ((*bucket_value & kSubIndexMask) == kSubIndexMask) {
|
|
|
|
*bucket_value ^= kSubIndexMask;
|
|
|
|
return kSubindex;
|
|
|
|
}
|
|
|
|
if (*bucket_value >= kMaxFileSize) {
|
|
|
|
return kNoPrefixForBucket;
|
|
|
|
} else {
|
|
|
|
// point directly to the file
|
|
|
|
return kDirectToFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:39:30 +01:00
|
|
|
void PlainTableIndexBuilder::IndexRecordList::AddRecord(uint32_t hash,
|
2014-07-19 01:58:13 +02:00
|
|
|
uint32_t offset) {
|
|
|
|
if (num_records_in_current_group_ == kNumRecordsPerGroup) {
|
|
|
|
current_group_ = AllocateNewGroup();
|
|
|
|
num_records_in_current_group_ = 0;
|
|
|
|
}
|
|
|
|
auto& new_record = current_group_[num_records_in_current_group_++];
|
2014-11-13 20:39:30 +01:00
|
|
|
new_record.hash = hash;
|
2014-07-19 01:58:13 +02:00
|
|
|
new_record.offset = offset;
|
|
|
|
new_record.next = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIndexBuilder::AddKeyPrefix(Slice key_prefix_slice,
|
2014-11-11 22:47:22 +01:00
|
|
|
uint32_t key_offset) {
|
2014-07-19 01:58:13 +02:00
|
|
|
if (is_first_record_ || prev_key_prefix_ != key_prefix_slice.ToString()) {
|
|
|
|
++num_prefixes_;
|
|
|
|
if (!is_first_record_) {
|
|
|
|
keys_per_prefix_hist_.Add(num_keys_per_prefix_);
|
|
|
|
}
|
|
|
|
num_keys_per_prefix_ = 0;
|
|
|
|
prev_key_prefix_ = key_prefix_slice.ToString();
|
|
|
|
prev_key_prefix_hash_ = GetSliceHash(key_prefix_slice);
|
|
|
|
due_index_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (due_index_) {
|
|
|
|
// Add an index key for every kIndexIntervalForSamePrefixKeys keys
|
|
|
|
record_list_.AddRecord(prev_key_prefix_hash_, key_offset);
|
|
|
|
due_index_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_keys_per_prefix_++;
|
|
|
|
if (index_sparseness_ == 0 || num_keys_per_prefix_ % index_sparseness_ == 0) {
|
|
|
|
due_index_ = true;
|
|
|
|
}
|
|
|
|
is_first_record_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice PlainTableIndexBuilder::Finish() {
|
|
|
|
AllocateIndex();
|
|
|
|
std::vector<IndexRecord*> hash_to_offsets(index_size_, nullptr);
|
|
|
|
std::vector<uint32_t> entries_per_bucket(index_size_, 0);
|
|
|
|
BucketizeIndexes(&hash_to_offsets, &entries_per_bucket);
|
|
|
|
|
|
|
|
keys_per_prefix_hist_.Add(num_keys_per_prefix_);
|
2021-04-26 21:43:02 +02:00
|
|
|
ROCKS_LOG_INFO(ioptions_.logger, "Number of Keys per prefix Histogram: %s",
|
2017-03-16 03:22:52 +01:00
|
|
|
keys_per_prefix_hist_.ToString().c_str());
|
2014-07-19 01:58:13 +02:00
|
|
|
|
|
|
|
// From the temp data structure, populate indexes.
|
|
|
|
return FillIndexes(hash_to_offsets, entries_per_bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIndexBuilder::AllocateIndex() {
|
|
|
|
if (prefix_extractor_ == nullptr || hash_table_ratio_ <= 0) {
|
|
|
|
// Fall back to pure binary search if the user fails to specify a prefix
|
|
|
|
// extractor.
|
|
|
|
index_size_ = 1;
|
|
|
|
} else {
|
|
|
|
double hash_table_size_multipier = 1.0 / hash_table_ratio_;
|
2016-02-10 00:12:00 +01:00
|
|
|
index_size_ =
|
2015-11-19 20:47:12 +01:00
|
|
|
static_cast<uint32_t>(num_prefixes_ * hash_table_size_multipier) + 1;
|
2014-07-19 01:58:13 +02:00
|
|
|
assert(index_size_ > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlainTableIndexBuilder::BucketizeIndexes(
|
|
|
|
std::vector<IndexRecord*>* hash_to_offsets,
|
|
|
|
std::vector<uint32_t>* entries_per_bucket) {
|
|
|
|
bool first = true;
|
|
|
|
uint32_t prev_hash = 0;
|
|
|
|
size_t num_records = record_list_.GetNumRecords();
|
|
|
|
for (size_t i = 0; i < num_records; i++) {
|
|
|
|
IndexRecord* index_record = record_list_.At(i);
|
|
|
|
uint32_t cur_hash = index_record->hash;
|
|
|
|
if (first || prev_hash != cur_hash) {
|
|
|
|
prev_hash = cur_hash;
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
uint32_t bucket = GetBucketIdFromHash(cur_hash, index_size_);
|
|
|
|
IndexRecord* prev_bucket_head = (*hash_to_offsets)[bucket];
|
|
|
|
index_record->next = prev_bucket_head;
|
|
|
|
(*hash_to_offsets)[bucket] = index_record;
|
|
|
|
(*entries_per_bucket)[bucket]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_index_size_ = 0;
|
|
|
|
for (auto entry_count : *entries_per_bucket) {
|
|
|
|
if (entry_count <= 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// 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 * PlainTableIndex::kOffsetLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice PlainTableIndexBuilder::FillIndexes(
|
|
|
|
const std::vector<IndexRecord*>& hash_to_offsets,
|
|
|
|
const std::vector<uint32_t>& entries_per_bucket) {
|
2021-04-26 21:43:02 +02:00
|
|
|
ROCKS_LOG_DEBUG(ioptions_.logger,
|
2017-03-16 03:22:52 +01:00
|
|
|
"Reserving %" PRIu32 " bytes for plain table's sub_index",
|
|
|
|
sub_index_size_);
|
2014-07-19 01:58:13 +02:00
|
|
|
auto total_allocate_size = GetTotalSize();
|
|
|
|
char* allocated = arena_->AllocateAligned(
|
2021-04-26 21:43:02 +02:00
|
|
|
total_allocate_size, huge_page_tlb_size_, ioptions_.logger);
|
2014-07-19 01:58:13 +02:00
|
|
|
|
|
|
|
auto temp_ptr = EncodeVarint32(allocated, index_size_);
|
|
|
|
uint32_t* index =
|
|
|
|
reinterpret_cast<uint32_t*>(EncodeVarint32(temp_ptr, num_prefixes_));
|
|
|
|
char* sub_index = reinterpret_cast<char*>(index + index_size_);
|
|
|
|
|
2014-11-11 22:47:22 +01:00
|
|
|
uint32_t sub_index_offset = 0;
|
2014-07-19 01:58:13 +02:00
|
|
|
for (uint32_t i = 0; i < index_size_; i++) {
|
|
|
|
uint32_t num_keys_for_bucket = entries_per_bucket[i];
|
|
|
|
switch (num_keys_for_bucket) {
|
|
|
|
case 0:
|
|
|
|
// No key for bucket
|
2017-04-22 05:41:37 +02:00
|
|
|
PutUnaligned(index + i, (uint32_t)PlainTableIndex::kMaxFileSize);
|
2014-07-19 01:58:13 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// point directly to the file offset
|
2017-04-22 05:41:37 +02:00
|
|
|
PutUnaligned(index + i, hash_to_offsets[i]->offset);
|
2014-07-19 01:58:13 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// point to second level indexes.
|
2017-04-22 05:41:37 +02:00
|
|
|
PutUnaligned(index + i, sub_index_offset | PlainTableIndex::kSubIndexMask);
|
2014-07-19 01:58:13 +02:00
|
|
|
char* prev_ptr = &sub_index[sub_index_offset];
|
|
|
|
char* cur_ptr = EncodeVarint32(prev_ptr, num_keys_for_bucket);
|
2015-11-19 20:47:12 +01:00
|
|
|
sub_index_offset += static_cast<uint32_t>(cur_ptr - prev_ptr);
|
2014-07-19 01:58:13 +02:00
|
|
|
char* sub_index_pos = &sub_index[sub_index_offset];
|
|
|
|
IndexRecord* record = hash_to_offsets[i];
|
|
|
|
int j;
|
|
|
|
for (j = num_keys_for_bucket - 1; j >= 0 && record;
|
|
|
|
j--, record = record->next) {
|
|
|
|
EncodeFixed32(sub_index_pos + j * sizeof(uint32_t), record->offset);
|
|
|
|
}
|
|
|
|
assert(j == -1 && record == nullptr);
|
|
|
|
sub_index_offset += PlainTableIndex::kOffsetLen * num_keys_for_bucket;
|
|
|
|
assert(sub_index_offset <= sub_index_size_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(sub_index_offset == sub_index_size_);
|
|
|
|
|
2021-04-26 21:43:02 +02:00
|
|
|
ROCKS_LOG_DEBUG(ioptions_.logger,
|
2019-04-04 21:05:42 +02:00
|
|
|
"hash table size: %" PRIu32 ", suffix_map length %" PRIu32,
|
2017-03-16 03:22:52 +01:00
|
|
|
index_size_, sub_index_size_);
|
2014-07-19 01:58:13 +02:00
|
|
|
return Slice(allocated, GetTotalSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string PlainTableIndexBuilder::kPlainTableIndexBlock =
|
|
|
|
"PlainTableIndexBlock";
|
2021-09-01 23:14:50 +02:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-11-25 05:47:27 +01:00
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|