fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
517 lines
20 KiB
517 lines
20 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// 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).
|
||
11 years ago
|
|
||
|
#ifndef ROCKSDB_LITE
|
||
6 years ago
|
#include "table/cuckoo/cuckoo_table_builder.h"
|
||
11 years ago
|
|
||
|
#include <assert.h>
|
||
|
#include <algorithm>
|
||
11 years ago
|
#include <limits>
|
||
11 years ago
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
|
#include "db/dbformat.h"
|
||
|
#include "rocksdb/env.h"
|
||
|
#include "rocksdb/table.h"
|
||
6 years ago
|
#include "table/block_based/block_builder.h"
|
||
|
#include "table/cuckoo/cuckoo_table_factory.h"
|
||
11 years ago
|
#include "table/format.h"
|
||
|
#include "table/meta_blocks.h"
|
||
|
#include "util/autovector.h"
|
||
10 years ago
|
#include "util/file_reader_writer.h"
|
||
11 years ago
|
#include "util/random.h"
|
||
10 years ago
|
#include "util/string_util.h"
|
||
11 years ago
|
|
||
|
namespace rocksdb {
|
||
11 years ago
|
const std::string CuckooTablePropertyNames::kEmptyKey =
|
||
|
"rocksdb.cuckoo.bucket.empty.key";
|
||
10 years ago
|
const std::string CuckooTablePropertyNames::kNumHashFunc =
|
||
11 years ago
|
"rocksdb.cuckoo.hash.num";
|
||
10 years ago
|
const std::string CuckooTablePropertyNames::kHashTableSize =
|
||
|
"rocksdb.cuckoo.hash.size";
|
||
11 years ago
|
const std::string CuckooTablePropertyNames::kValueLength =
|
||
|
"rocksdb.cuckoo.value.length";
|
||
11 years ago
|
const std::string CuckooTablePropertyNames::kIsLastLevel =
|
||
|
"rocksdb.cuckoo.file.islastlevel";
|
||
10 years ago
|
const std::string CuckooTablePropertyNames::kCuckooBlockSize =
|
||
|
"rocksdb.cuckoo.hash.cuckooblocksize";
|
||
10 years ago
|
const std::string CuckooTablePropertyNames::kIdentityAsFirstHash =
|
||
|
"rocksdb.cuckoo.hash.identityfirst";
|
||
10 years ago
|
const std::string CuckooTablePropertyNames::kUseModuleHash =
|
||
|
"rocksdb.cuckoo.hash.usemodule";
|
||
10 years ago
|
const std::string CuckooTablePropertyNames::kUserKeyLength =
|
||
|
"rocksdb.cuckoo.hash.userkeylength";
|
||
11 years ago
|
|
||
|
// Obtained by running echo rocksdb.table.cuckoo | sha1sum
|
||
|
extern const uint64_t kCuckooTableMagicNumber = 0x926789d0c5f17873ull;
|
||
|
|
||
|
CuckooTableBuilder::CuckooTableBuilder(
|
||
10 years ago
|
WritableFileWriter* file, double max_hash_table_ratio,
|
||
11 years ago
|
uint32_t max_num_hash_table, uint32_t max_search_depth,
|
||
10 years ago
|
const Comparator* user_comparator, uint32_t cuckoo_block_size,
|
||
10 years ago
|
bool use_module_hash, bool identity_as_first_hash,
|
||
9 years ago
|
uint64_t (*get_slice_hash)(const Slice&, uint32_t, uint64_t),
|
||
|
uint32_t column_family_id, const std::string& column_family_name)
|
||
10 years ago
|
: num_hash_func_(2),
|
||
11 years ago
|
file_(file),
|
||
10 years ago
|
max_hash_table_ratio_(max_hash_table_ratio),
|
||
10 years ago
|
max_num_hash_func_(max_num_hash_table),
|
||
11 years ago
|
max_search_depth_(max_search_depth),
|
||
10 years ago
|
cuckoo_block_size_(std::max(1U, cuckoo_block_size)),
|
||
10 years ago
|
hash_table_size_(use_module_hash ? 0 : 2),
|
||
11 years ago
|
is_last_level_file_(false),
|
||
|
has_seen_first_key_(false),
|
||
10 years ago
|
has_seen_first_value_(false),
|
||
10 years ago
|
key_size_(0),
|
||
|
value_size_(0),
|
||
|
num_entries_(0),
|
||
10 years ago
|
num_values_(0),
|
||
10 years ago
|
ucomp_(user_comparator),
|
||
10 years ago
|
use_module_hash_(use_module_hash),
|
||
10 years ago
|
identity_as_first_hash_(identity_as_first_hash),
|
||
11 years ago
|
get_slice_hash_(get_slice_hash),
|
||
|
closed_(false) {
|
||
11 years ago
|
// Data is in a huge block.
|
||
|
properties_.num_data_blocks = 1;
|
||
|
properties_.index_size = 0;
|
||
|
properties_.filter_size = 0;
|
||
9 years ago
|
properties_.column_family_id = column_family_id;
|
||
|
properties_.column_family_name = column_family_name;
|
||
11 years ago
|
}
|
||
|
|
||
|
void CuckooTableBuilder::Add(const Slice& key, const Slice& value) {
|
||
10 years ago
|
if (num_entries_ >= kMaxVectorIdx - 1) {
|
||
11 years ago
|
status_ = Status::NotSupported("Number of keys in a file must be < 2^32-1");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
ParsedInternalKey ikey;
|
||
|
if (!ParseInternalKey(key, &ikey)) {
|
||
|
status_ = Status::Corruption("Unable to parse key into inernal key.");
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
if (ikey.type != kTypeDeletion && ikey.type != kTypeValue) {
|
||
|
status_ = Status::NotSupported("Unsupported key type " +
|
||
10 years ago
|
ToString(ikey.type));
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
// Determine if we can ignore the sequence number and value type from
|
||
|
// internal keys by looking at sequence number from first key. We assume
|
||
|
// that if first key has a zero sequence number, then all the remaining
|
||
|
// keys will have zero seq. no.
|
||
|
if (!has_seen_first_key_) {
|
||
|
is_last_level_file_ = ikey.sequence == 0;
|
||
|
has_seen_first_key_ = true;
|
||
10 years ago
|
smallest_user_key_.assign(ikey.user_key.data(), ikey.user_key.size());
|
||
|
largest_user_key_.assign(ikey.user_key.data(), ikey.user_key.size());
|
||
10 years ago
|
key_size_ = is_last_level_file_ ? ikey.user_key.size() : key.size();
|
||
10 years ago
|
}
|
||
|
if (key_size_ != (is_last_level_file_ ? ikey.user_key.size() : key.size())) {
|
||
|
status_ = Status::NotSupported("all keys have to be the same size");
|
||
|
return;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
if (ikey.type == kTypeValue) {
|
||
|
if (!has_seen_first_value_) {
|
||
|
has_seen_first_value_ = true;
|
||
|
value_size_ = value.size();
|
||
|
}
|
||
|
if (value_size_ != value.size()) {
|
||
|
status_ = Status::NotSupported("all values have to be the same size");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (is_last_level_file_) {
|
||
|
kvs_.append(ikey.user_key.data(), ikey.user_key.size());
|
||
|
} else {
|
||
|
kvs_.append(key.data(), key.size());
|
||
|
}
|
||
|
kvs_.append(value.data(), value.size());
|
||
|
++num_values_;
|
||
11 years ago
|
} else {
|
||
10 years ago
|
if (is_last_level_file_) {
|
||
|
deleted_keys_.append(ikey.user_key.data(), ikey.user_key.size());
|
||
|
} else {
|
||
|
deleted_keys_.append(key.data(), key.size());
|
||
|
}
|
||
11 years ago
|
}
|
||
10 years ago
|
++num_entries_;
|
||
11 years ago
|
|
||
10 years ago
|
// In order to fill the empty buckets in the hash table, we identify a
|
||
|
// key which is not used so far (unused_user_key). We determine this by
|
||
|
// maintaining smallest and largest keys inserted so far in bytewise order
|
||
|
// and use them to find a key outside this range in Finish() operation.
|
||
|
// Note that this strategy is independent of user comparator used here.
|
||
|
if (ikey.user_key.compare(smallest_user_key_) < 0) {
|
||
|
smallest_user_key_.assign(ikey.user_key.data(), ikey.user_key.size());
|
||
|
} else if (ikey.user_key.compare(largest_user_key_) > 0) {
|
||
|
largest_user_key_.assign(ikey.user_key.data(), ikey.user_key.size());
|
||
11 years ago
|
}
|
||
10 years ago
|
if (!use_module_hash_) {
|
||
10 years ago
|
if (hash_table_size_ < num_entries_ / max_hash_table_ratio_) {
|
||
10 years ago
|
hash_table_size_ *= 2;
|
||
|
}
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
bool CuckooTableBuilder::IsDeletedKey(uint64_t idx) const {
|
||
|
assert(closed_);
|
||
|
return idx >= num_values_;
|
||
|
}
|
||
|
|
||
10 years ago
|
Slice CuckooTableBuilder::GetKey(uint64_t idx) const {
|
||
10 years ago
|
assert(closed_);
|
||
|
if (IsDeletedKey(idx)) {
|
||
6 years ago
|
return Slice(&deleted_keys_[static_cast<size_t>((idx - num_values_) * key_size_)], static_cast<size_t>(key_size_));
|
||
10 years ago
|
}
|
||
6 years ago
|
return Slice(&kvs_[static_cast<size_t>(idx * (key_size_ + value_size_))], static_cast<size_t>(key_size_));
|
||
10 years ago
|
}
|
||
|
|
||
|
Slice CuckooTableBuilder::GetUserKey(uint64_t idx) const {
|
||
10 years ago
|
assert(closed_);
|
||
10 years ago
|
return is_last_level_file_ ? GetKey(idx) : ExtractUserKey(GetKey(idx));
|
||
|
}
|
||
|
|
||
|
Slice CuckooTableBuilder::GetValue(uint64_t idx) const {
|
||
10 years ago
|
assert(closed_);
|
||
|
if (IsDeletedKey(idx)) {
|
||
6 years ago
|
static std::string empty_value(static_cast<unsigned int>(value_size_), 'a');
|
||
10 years ago
|
return Slice(empty_value);
|
||
|
}
|
||
6 years ago
|
return Slice(&kvs_[static_cast<size_t>(idx * (key_size_ + value_size_) + key_size_)], static_cast<size_t>(value_size_));
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
Status CuckooTableBuilder::MakeHashTable(std::vector<CuckooBucket>* buckets) {
|
||
6 years ago
|
buckets->resize(static_cast<size_t>(hash_table_size_ + cuckoo_block_size_ - 1));
|
||
10 years ago
|
uint32_t make_space_for_key_call_id = 0;
|
||
10 years ago
|
for (uint32_t vector_idx = 0; vector_idx < num_entries_; vector_idx++) {
|
||
7 years ago
|
uint64_t bucket_id = 0;
|
||
11 years ago
|
bool bucket_found = false;
|
||
|
autovector<uint64_t> hash_vals;
|
||
10 years ago
|
Slice user_key = GetUserKey(vector_idx);
|
||
10 years ago
|
for (uint32_t hash_cnt = 0; hash_cnt < num_hash_func_ && !bucket_found;
|
||
|
++hash_cnt) {
|
||
10 years ago
|
uint64_t hash_val = CuckooHash(user_key, hash_cnt, use_module_hash_,
|
||
|
hash_table_size_, identity_as_first_hash_, get_slice_hash_);
|
||
10 years ago
|
// If there is a collision, check next cuckoo_block_size_ locations for
|
||
|
// empty locations. While checking, if we reach end of the hash table,
|
||
|
// stop searching and proceed for next hash function.
|
||
|
for (uint32_t block_idx = 0; block_idx < cuckoo_block_size_;
|
||
|
++block_idx, ++hash_val) {
|
||
6 years ago
|
if ((*buckets)[static_cast<size_t>(hash_val)].vector_idx == kMaxVectorIdx) {
|
||
10 years ago
|
bucket_id = hash_val;
|
||
|
bucket_found = true;
|
||
|
break;
|
||
|
} else {
|
||
10 years ago
|
if (ucomp_->Compare(user_key,
|
||
6 years ago
|
GetUserKey((*buckets)[static_cast<size_t>(hash_val)].vector_idx)) == 0) {
|
||
10 years ago
|
return Status::NotSupported("Same key is being inserted again.");
|
||
|
}
|
||
|
hash_vals.push_back(hash_val);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
while (!bucket_found && !MakeSpaceForKey(hash_vals,
|
||
10 years ago
|
++make_space_for_key_call_id, buckets, &bucket_id)) {
|
||
11 years ago
|
// Rehash by increashing number of hash tables.
|
||
10 years ago
|
if (num_hash_func_ >= max_num_hash_func_) {
|
||
|
return Status::NotSupported("Too many collisions. Unable to hash.");
|
||
11 years ago
|
}
|
||
|
// We don't really need to rehash the entire table because old hashes are
|
||
|
// still valid and we only increased the number of hash functions.
|
||
10 years ago
|
uint64_t hash_val = CuckooHash(user_key, num_hash_func_, use_module_hash_,
|
||
|
hash_table_size_, identity_as_first_hash_, get_slice_hash_);
|
||
10 years ago
|
++num_hash_func_;
|
||
|
for (uint32_t block_idx = 0; block_idx < cuckoo_block_size_;
|
||
|
++block_idx, ++hash_val) {
|
||
6 years ago
|
if ((*buckets)[static_cast<size_t>(hash_val)].vector_idx == kMaxVectorIdx) {
|
||
10 years ago
|
bucket_found = true;
|
||
|
bucket_id = hash_val;
|
||
|
break;
|
||
|
} else {
|
||
|
hash_vals.push_back(hash_val);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
6 years ago
|
(*buckets)[static_cast<size_t>(bucket_id)].vector_idx = vector_idx;
|
||
11 years ago
|
}
|
||
|
return Status::OK();
|
||
|
}
|
||
11 years ago
|
|
||
|
Status CuckooTableBuilder::Finish() {
|
||
|
assert(!closed_);
|
||
|
closed_ = true;
|
||
11 years ago
|
std::vector<CuckooBucket> buckets;
|
||
10 years ago
|
Status s;
|
||
10 years ago
|
std::string unused_bucket;
|
||
10 years ago
|
if (num_entries_ > 0) {
|
||
10 years ago
|
// Calculate the real hash size if module hash is enabled.
|
||
|
if (use_module_hash_) {
|
||
9 years ago
|
hash_table_size_ =
|
||
9 years ago
|
static_cast<uint64_t>(num_entries_ / max_hash_table_ratio_);
|
||
10 years ago
|
}
|
||
10 years ago
|
s = MakeHashTable(&buckets);
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
// Determine unused_user_key to fill empty buckets.
|
||
10 years ago
|
std::string unused_user_key = smallest_user_key_;
|
||
10 years ago
|
int curr_pos = static_cast<int>(unused_user_key.size()) - 1;
|
||
11 years ago
|
while (curr_pos >= 0) {
|
||
10 years ago
|
--unused_user_key[curr_pos];
|
||
|
if (Slice(unused_user_key).compare(smallest_user_key_) < 0) {
|
||
11 years ago
|
break;
|
||
|
}
|
||
|
--curr_pos;
|
||
|
}
|
||
10 years ago
|
if (curr_pos < 0) {
|
||
|
// Try using the largest key to identify an unused key.
|
||
|
unused_user_key = largest_user_key_;
|
||
10 years ago
|
curr_pos = static_cast<int>(unused_user_key.size()) - 1;
|
||
10 years ago
|
while (curr_pos >= 0) {
|
||
|
++unused_user_key[curr_pos];
|
||
|
if (Slice(unused_user_key).compare(largest_user_key_) > 0) {
|
||
|
break;
|
||
|
}
|
||
|
--curr_pos;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
if (curr_pos < 0) {
|
||
11 years ago
|
return Status::Corruption("Unable to find unused key");
|
||
|
}
|
||
11 years ago
|
if (is_last_level_file_) {
|
||
10 years ago
|
unused_bucket = unused_user_key;
|
||
11 years ago
|
} else {
|
||
10 years ago
|
ParsedInternalKey ikey(unused_user_key, 0, kTypeValue);
|
||
11 years ago
|
AppendInternalKey(&unused_bucket, ikey);
|
||
|
}
|
||
11 years ago
|
}
|
||
10 years ago
|
properties_.num_entries = num_entries_;
|
||
6 years ago
|
properties_.num_deletions = num_entries_ - num_values_;
|
||
10 years ago
|
properties_.fixed_key_len = key_size_;
|
||
11 years ago
|
properties_.user_collected_properties[
|
||
|
CuckooTablePropertyNames::kValueLength].assign(
|
||
10 years ago
|
reinterpret_cast<const char*>(&value_size_), sizeof(value_size_));
|
||
11 years ago
|
|
||
10 years ago
|
uint64_t bucket_size = key_size_ + value_size_;
|
||
6 years ago
|
unused_bucket.resize(static_cast<size_t>(bucket_size), 'a');
|
||
11 years ago
|
// Write the table.
|
||
11 years ago
|
uint32_t num_added = 0;
|
||
11 years ago
|
for (auto& bucket : buckets) {
|
||
|
if (bucket.vector_idx == kMaxVectorIdx) {
|
||
11 years ago
|
s = file_->Append(Slice(unused_bucket));
|
||
|
} else {
|
||
11 years ago
|
++num_added;
|
||
10 years ago
|
s = file_->Append(GetKey(bucket.vector_idx));
|
||
11 years ago
|
if (s.ok()) {
|
||
10 years ago
|
if (value_size_ > 0) {
|
||
|
s = file_->Append(GetValue(bucket.vector_idx));
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
assert(num_added == NumEntries());
|
||
11 years ago
|
properties_.raw_key_size = num_added * properties_.fixed_key_len;
|
||
10 years ago
|
properties_.raw_value_size = num_added * value_size_;
|
||
11 years ago
|
|
||
11 years ago
|
uint64_t offset = buckets.size() * bucket_size;
|
||
11 years ago
|
properties_.data_size = offset;
|
||
6 years ago
|
unused_bucket.resize(static_cast<size_t>(properties_.fixed_key_len));
|
||
11 years ago
|
properties_.user_collected_properties[
|
||
|
CuckooTablePropertyNames::kEmptyKey] = unused_bucket;
|
||
11 years ago
|
properties_.user_collected_properties[
|
||
10 years ago
|
CuckooTablePropertyNames::kNumHashFunc].assign(
|
||
|
reinterpret_cast<char*>(&num_hash_func_), sizeof(num_hash_func_));
|
||
|
|
||
11 years ago
|
properties_.user_collected_properties[
|
||
10 years ago
|
CuckooTablePropertyNames::kHashTableSize].assign(
|
||
10 years ago
|
reinterpret_cast<const char*>(&hash_table_size_),
|
||
|
sizeof(hash_table_size_));
|
||
11 years ago
|
properties_.user_collected_properties[
|
||
|
CuckooTablePropertyNames::kIsLastLevel].assign(
|
||
|
reinterpret_cast<const char*>(&is_last_level_file_),
|
||
|
sizeof(is_last_level_file_));
|
||
10 years ago
|
properties_.user_collected_properties[
|
||
|
CuckooTablePropertyNames::kCuckooBlockSize].assign(
|
||
|
reinterpret_cast<const char*>(&cuckoo_block_size_),
|
||
|
sizeof(cuckoo_block_size_));
|
||
10 years ago
|
properties_.user_collected_properties[
|
||
|
CuckooTablePropertyNames::kIdentityAsFirstHash].assign(
|
||
|
reinterpret_cast<const char*>(&identity_as_first_hash_),
|
||
|
sizeof(identity_as_first_hash_));
|
||
10 years ago
|
properties_.user_collected_properties[
|
||
|
CuckooTablePropertyNames::kUseModuleHash].assign(
|
||
|
reinterpret_cast<const char*>(&use_module_hash_),
|
||
|
sizeof(use_module_hash_));
|
||
10 years ago
|
uint32_t user_key_len = static_cast<uint32_t>(smallest_user_key_.size());
|
||
|
properties_.user_collected_properties[
|
||
|
CuckooTablePropertyNames::kUserKeyLength].assign(
|
||
|
reinterpret_cast<const char*>(&user_key_len),
|
||
|
sizeof(user_key_len));
|
||
11 years ago
|
|
||
|
// Write meta blocks.
|
||
11 years ago
|
MetaIndexBuilder meta_index_builder;
|
||
11 years ago
|
PropertyBlockBuilder property_block_builder;
|
||
|
|
||
|
property_block_builder.AddTableProperty(properties_);
|
||
|
property_block_builder.Add(properties_.user_collected_properties);
|
||
|
Slice property_block = property_block_builder.Finish();
|
||
|
BlockHandle property_block_handle;
|
||
|
property_block_handle.set_offset(offset);
|
||
|
property_block_handle.set_size(property_block.size());
|
||
11 years ago
|
s = file_->Append(property_block);
|
||
11 years ago
|
offset += property_block.size();
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
|
||
11 years ago
|
meta_index_builder.Add(kPropertiesBlock, property_block_handle);
|
||
|
Slice meta_index_block = meta_index_builder.Finish();
|
||
11 years ago
|
|
||
|
BlockHandle meta_index_block_handle;
|
||
|
meta_index_block_handle.set_offset(offset);
|
||
|
meta_index_block_handle.set_size(meta_index_block.size());
|
||
|
s = file_->Append(meta_index_block);
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
|
||
10 years ago
|
Footer footer(kCuckooTableMagicNumber, 1);
|
||
11 years ago
|
footer.set_metaindex_handle(meta_index_block_handle);
|
||
|
footer.set_index_handle(BlockHandle::NullBlockHandle());
|
||
|
std::string footer_encoding;
|
||
|
footer.EncodeTo(&footer_encoding);
|
||
|
s = file_->Append(footer_encoding);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
void CuckooTableBuilder::Abandon() {
|
||
|
assert(!closed_);
|
||
|
closed_ = true;
|
||
|
}
|
||
|
|
||
|
uint64_t CuckooTableBuilder::NumEntries() const {
|
||
10 years ago
|
return num_entries_;
|
||
11 years ago
|
}
|
||
|
|
||
|
uint64_t CuckooTableBuilder::FileSize() const {
|
||
|
if (closed_) {
|
||
|
return file_->GetFileSize();
|
||
10 years ago
|
} else if (num_entries_ == 0) {
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (use_module_hash_) {
|
||
9 years ago
|
return static_cast<uint64_t>((key_size_ + value_size_) *
|
||
|
num_entries_ / max_hash_table_ratio_);
|
||
10 years ago
|
} else {
|
||
|
// Account for buckets being a power of two.
|
||
|
// As elements are added, file size remains constant for a while and
|
||
|
// doubles its size. Since compaction algorithm stops adding elements
|
||
|
// only after it exceeds the file limit, we account for the extra element
|
||
|
// being added here.
|
||
|
uint64_t expected_hash_table_size = hash_table_size_;
|
||
10 years ago
|
if (expected_hash_table_size < (num_entries_ + 1) / max_hash_table_ratio_) {
|
||
10 years ago
|
expected_hash_table_size *= 2;
|
||
|
}
|
||
10 years ago
|
return (key_size_ + value_size_) * expected_hash_table_size - 1;
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// This method is invoked when there is no place to insert the target key.
|
||
|
// It searches for a set of elements that can be moved to accommodate target
|
||
|
// key. The search is a BFS graph traversal with first level (hash_vals)
|
||
|
// being all the buckets target key could go to.
|
||
|
// Then, from each node (curr_node), we find all the buckets that curr_node
|
||
|
// could go to. They form the children of curr_node in the tree.
|
||
|
// We continue the traversal until we find an empty bucket, in which case, we
|
||
|
// move all elements along the path from first level to this empty bucket, to
|
||
|
// make space for target key which is inserted at first level (*bucket_id).
|
||
|
// If tree depth exceedes max depth, we return false indicating failure.
|
||
11 years ago
|
bool CuckooTableBuilder::MakeSpaceForKey(
|
||
|
const autovector<uint64_t>& hash_vals,
|
||
10 years ago
|
const uint32_t make_space_for_key_call_id,
|
||
|
std::vector<CuckooBucket>* buckets, uint64_t* bucket_id) {
|
||
11 years ago
|
struct CuckooNode {
|
||
11 years ago
|
uint64_t bucket_id;
|
||
|
uint32_t depth;
|
||
11 years ago
|
uint32_t parent_pos;
|
||
10 years ago
|
CuckooNode(uint64_t _bucket_id, uint32_t _depth, int _parent_pos)
|
||
|
: bucket_id(_bucket_id), depth(_depth), parent_pos(_parent_pos) {}
|
||
11 years ago
|
};
|
||
|
// This is BFS search tree that is stored simply as a vector.
|
||
|
// Each node stores the index of parent node in the vector.
|
||
|
std::vector<CuckooNode> tree;
|
||
11 years ago
|
// We want to identify already visited buckets in the current method call so
|
||
|
// that we don't add same buckets again for exploration in the tree.
|
||
10 years ago
|
// We do this by maintaining a count of current method call in
|
||
|
// make_space_for_key_call_id, which acts as a unique id for this invocation
|
||
|
// of the method. We store this number into the nodes that we explore in
|
||
|
// current method call.
|
||
11 years ago
|
// It is unlikely for the increment operation to overflow because the maximum
|
||
10 years ago
|
// no. of times this will be called is <= max_num_hash_func_ + num_entries_.
|
||
10 years ago
|
for (uint32_t hash_cnt = 0; hash_cnt < num_hash_func_; ++hash_cnt) {
|
||
10 years ago
|
uint64_t bid = hash_vals[hash_cnt];
|
||
6 years ago
|
(*buckets)[static_cast<size_t>(bid)].make_space_for_key_call_id = make_space_for_key_call_id;
|
||
10 years ago
|
tree.push_back(CuckooNode(bid, 0, 0));
|
||
11 years ago
|
}
|
||
|
bool null_found = false;
|
||
11 years ago
|
uint32_t curr_pos = 0;
|
||
11 years ago
|
while (!null_found && curr_pos < tree.size()) {
|
||
|
CuckooNode& curr_node = tree[curr_pos];
|
||
11 years ago
|
uint32_t curr_depth = curr_node.depth;
|
||
|
if (curr_depth >= max_search_depth_) {
|
||
11 years ago
|
break;
|
||
|
}
|
||
6 years ago
|
CuckooBucket& curr_bucket = (*buckets)[static_cast<size_t>(curr_node.bucket_id)];
|
||
10 years ago
|
for (uint32_t hash_cnt = 0;
|
||
|
hash_cnt < num_hash_func_ && !null_found; ++hash_cnt) {
|
||
10 years ago
|
uint64_t child_bucket_id = CuckooHash(GetUserKey(curr_bucket.vector_idx),
|
||
10 years ago
|
hash_cnt, use_module_hash_, hash_table_size_, identity_as_first_hash_,
|
||
10 years ago
|
get_slice_hash_);
|
||
10 years ago
|
// Iterate inside Cuckoo Block.
|
||
10 years ago
|
for (uint32_t block_idx = 0; block_idx < cuckoo_block_size_;
|
||
|
++block_idx, ++child_bucket_id) {
|
||
6 years ago
|
if ((*buckets)[static_cast<size_t>(child_bucket_id)].make_space_for_key_call_id ==
|
||
10 years ago
|
make_space_for_key_call_id) {
|
||
|
continue;
|
||
|
}
|
||
6 years ago
|
(*buckets)[static_cast<size_t>(child_bucket_id)].make_space_for_key_call_id =
|
||
10 years ago
|
make_space_for_key_call_id;
|
||
|
tree.push_back(CuckooNode(child_bucket_id, curr_depth + 1,
|
||
|
curr_pos));
|
||
6 years ago
|
if ((*buckets)[static_cast<size_t>(child_bucket_id)].vector_idx == kMaxVectorIdx) {
|
||
10 years ago
|
null_found = true;
|
||
|
break;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
++curr_pos;
|
||
|
}
|
||
|
|
||
|
if (null_found) {
|
||
11 years ago
|
// There is an empty node in tree.back(). Now, traverse the path from this
|
||
|
// empty node to top of the tree and at every node in the path, replace
|
||
|
// child with the parent. Stop when first level is reached in the tree
|
||
10 years ago
|
// (happens when 0 <= bucket_to_replace_pos < num_hash_func_) and return
|
||
11 years ago
|
// this location in first level for target key to be inserted.
|
||
10 years ago
|
uint32_t bucket_to_replace_pos = static_cast<uint32_t>(tree.size()) - 1;
|
||
10 years ago
|
while (bucket_to_replace_pos >= num_hash_func_) {
|
||
11 years ago
|
CuckooNode& curr_node = tree[bucket_to_replace_pos];
|
||
6 years ago
|
(*buckets)[static_cast<size_t>(curr_node.bucket_id)] =
|
||
|
(*buckets)[static_cast<size_t>(tree[curr_node.parent_pos].bucket_id)];
|
||
11 years ago
|
bucket_to_replace_pos = curr_node.parent_pos;
|
||
11 years ago
|
}
|
||
11 years ago
|
*bucket_id = tree[bucket_to_replace_pos].bucket_id;
|
||
11 years ago
|
}
|
||
11 years ago
|
return null_found;
|
||
11 years ago
|
}
|
||
|
|
||
|
} // namespace rocksdb
|
||
|
#endif // ROCKSDB_LITE
|