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.
650 lines
27 KiB
650 lines
27 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
7 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).
|
||
10 years ago
|
|
||
9 years ago
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
10 years ago
|
#include <vector>
|
||
|
#include <string>
|
||
|
#include <map>
|
||
|
#include <utility>
|
||
|
|
||
|
#include "table/meta_blocks.h"
|
||
6 years ago
|
#include "table/cuckoo/cuckoo_table_builder.h"
|
||
9 years ago
|
#include "util/file_reader_writer.h"
|
||
6 years ago
|
#include "test_util/testharness.h"
|
||
|
#include "test_util/testutil.h"
|
||
10 years ago
|
|
||
|
namespace rocksdb {
|
||
|
extern const uint64_t kCuckooTableMagicNumber;
|
||
|
|
||
|
namespace {
|
||
10 years ago
|
std::unordered_map<std::string, std::vector<uint64_t>> hash_map;
|
||
10 years ago
|
|
||
10 years ago
|
uint64_t GetSliceHash(const Slice& s, uint32_t index,
|
||
7 years ago
|
uint64_t /*max_num_buckets*/) {
|
||
10 years ago
|
return hash_map[s.ToString()][index];
|
||
|
}
|
||
|
} // namespace
|
||
|
|
||
10 years ago
|
class CuckooBuilderTest : public testing::Test {
|
||
10 years ago
|
public:
|
||
|
CuckooBuilderTest() {
|
||
|
env_ = Env::Default();
|
||
10 years ago
|
Options options;
|
||
|
options.allow_mmap_reads = true;
|
||
|
env_options_ = EnvOptions(options);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void CheckFileContents(const std::vector<std::string>& keys,
|
||
|
const std::vector<std::string>& values,
|
||
|
const std::vector<uint64_t>& expected_locations,
|
||
10 years ago
|
std::string expected_unused_bucket, uint64_t expected_table_size,
|
||
|
uint32_t expected_num_hash_func, bool expected_is_last_level,
|
||
|
uint32_t expected_cuckoo_block_size = 1) {
|
||
6 years ago
|
uint64_t num_deletions = 0;
|
||
|
for (const auto& key : keys) {
|
||
|
ParsedInternalKey parsed;
|
||
|
if (ParseInternalKey(key, &parsed) && parsed.type == kTypeDeletion) {
|
||
|
num_deletions++;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
// Read file
|
||
6 years ago
|
std::unique_ptr<RandomAccessFile> read_file;
|
||
10 years ago
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &read_file, env_options_));
|
||
|
uint64_t read_file_size;
|
||
|
ASSERT_OK(env_->GetFileSize(fname, &read_file_size));
|
||
|
|
||
7 years ago
|
// @lint-ignore TXT2 T25377293 Grandfathered in
|
||
7 years ago
|
Options options;
|
||
|
options.allow_mmap_reads = true;
|
||
|
ImmutableCFOptions ioptions(options);
|
||
8 years ago
|
|
||
10 years ago
|
// Assert Table Properties.
|
||
|
TableProperties* props = nullptr;
|
||
6 years ago
|
std::unique_ptr<RandomAccessFileReader> file_reader(
|
||
7 years ago
|
new RandomAccessFileReader(std::move(read_file), fname));
|
||
9 years ago
|
ASSERT_OK(ReadTableProperties(file_reader.get(), read_file_size,
|
||
8 years ago
|
kCuckooTableMagicNumber, ioptions,
|
||
6 years ago
|
&props, true /* compression_type_missing */));
|
||
10 years ago
|
// Check unused bucket.
|
||
10 years ago
|
std::string unused_key = props->user_collected_properties[
|
||
|
CuckooTablePropertyNames::kEmptyKey];
|
||
10 years ago
|
ASSERT_EQ(expected_unused_bucket.substr(0,
|
||
|
props->fixed_key_len), unused_key);
|
||
10 years ago
|
|
||
8 years ago
|
uint64_t value_len_found =
|
||
|
*reinterpret_cast<const uint64_t*>(props->user_collected_properties[
|
||
10 years ago
|
CuckooTablePropertyNames::kValueLength].data());
|
||
10 years ago
|
ASSERT_EQ(values.empty() ? 0 : values[0].size(), value_len_found);
|
||
10 years ago
|
ASSERT_EQ(props->raw_value_size, values.size()*value_len_found);
|
||
10 years ago
|
const uint64_t table_size =
|
||
10 years ago
|
*reinterpret_cast<const uint64_t*>(props->user_collected_properties[
|
||
10 years ago
|
CuckooTablePropertyNames::kHashTableSize].data());
|
||
|
ASSERT_EQ(expected_table_size, table_size);
|
||
|
const uint32_t num_hash_func_found =
|
||
10 years ago
|
*reinterpret_cast<const uint32_t*>(props->user_collected_properties[
|
||
10 years ago
|
CuckooTablePropertyNames::kNumHashFunc].data());
|
||
|
ASSERT_EQ(expected_num_hash_func, num_hash_func_found);
|
||
|
const uint32_t cuckoo_block_size =
|
||
|
*reinterpret_cast<const uint32_t*>(props->user_collected_properties[
|
||
|
CuckooTablePropertyNames::kCuckooBlockSize].data());
|
||
|
ASSERT_EQ(expected_cuckoo_block_size, cuckoo_block_size);
|
||
10 years ago
|
const bool is_last_level_found =
|
||
|
*reinterpret_cast<const bool*>(props->user_collected_properties[
|
||
|
CuckooTablePropertyNames::kIsLastLevel].data());
|
||
|
ASSERT_EQ(expected_is_last_level, is_last_level_found);
|
||
10 years ago
|
|
||
|
ASSERT_EQ(props->num_entries, keys.size());
|
||
6 years ago
|
ASSERT_EQ(props->num_deletions, num_deletions);
|
||
10 years ago
|
ASSERT_EQ(props->fixed_key_len, keys.empty() ? 0 : keys[0].size());
|
||
|
ASSERT_EQ(props->data_size, expected_unused_bucket.size() *
|
||
|
(expected_table_size + expected_cuckoo_block_size - 1));
|
||
|
ASSERT_EQ(props->raw_key_size, keys.size()*props->fixed_key_len);
|
||
9 years ago
|
ASSERT_EQ(props->column_family_id, 0);
|
||
|
ASSERT_EQ(props->column_family_name, kDefaultColumnFamilyName);
|
||
10 years ago
|
delete props;
|
||
10 years ago
|
|
||
10 years ago
|
// Check contents of the bucket.
|
||
10 years ago
|
std::vector<bool> keys_found(keys.size(), false);
|
||
10 years ago
|
size_t bucket_size = expected_unused_bucket.size();
|
||
10 years ago
|
for (uint32_t i = 0; i < table_size + cuckoo_block_size - 1; ++i) {
|
||
10 years ago
|
Slice read_slice;
|
||
9 years ago
|
ASSERT_OK(file_reader->Read(i * bucket_size, bucket_size, &read_slice,
|
||
|
nullptr));
|
||
10 years ago
|
size_t key_idx =
|
||
|
std::find(expected_locations.begin(), expected_locations.end(), i) -
|
||
|
expected_locations.begin();
|
||
10 years ago
|
if (key_idx == keys.size()) {
|
||
8 years ago
|
// i is not one of the expected locations. Empty bucket.
|
||
7 years ago
|
if (read_slice.data() == nullptr) {
|
||
|
ASSERT_EQ(0, expected_unused_bucket.size());
|
||
|
} else {
|
||
|
ASSERT_EQ(read_slice.compare(expected_unused_bucket), 0);
|
||
|
}
|
||
10 years ago
|
} else {
|
||
|
keys_found[key_idx] = true;
|
||
|
ASSERT_EQ(read_slice.compare(keys[key_idx] + values[key_idx]), 0);
|
||
|
}
|
||
|
}
|
||
|
for (auto key_found : keys_found) {
|
||
9 years ago
|
// Check that all keys wereReader found.
|
||
10 years ago
|
ASSERT_TRUE(key_found);
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
std::string GetInternalKey(Slice user_key, bool zero_seqno,
|
||
|
ValueType type = kTypeValue) {
|
||
10 years ago
|
IterKey ikey;
|
||
6 years ago
|
ikey.SetInternalKey(user_key, zero_seqno ? 0 : 1000, type);
|
||
8 years ago
|
return ikey.GetInternalKey().ToString();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
uint64_t NextPowOf2(uint64_t num) {
|
||
|
uint64_t n = 2;
|
||
|
while (n <= num) {
|
||
|
n *= 2;
|
||
|
}
|
||
|
return n;
|
||
|
}
|
||
|
|
||
9 years ago
|
uint64_t GetExpectedTableSize(uint64_t num) {
|
||
|
return NextPowOf2(static_cast<uint64_t>(num / kHashTableRatio));
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
Env* env_;
|
||
10 years ago
|
EnvOptions env_options_;
|
||
10 years ago
|
std::string fname;
|
||
10 years ago
|
const double kHashTableRatio = 0.9;
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, SuccessWithEmptyFile) {
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("EmptyFile");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, 4, 100,
|
||
|
BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
10 years ago
|
ASSERT_EQ(0UL, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
CheckFileContents({}, {}, {}, "", 2, 2, false);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WriteSuccessNoCollisionFullKey) {
|
||
6 years ago
|
for (auto type : {kTypeValue, kTypeDeletion}) {
|
||
|
uint32_t num_hash_fun = 4;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03", "key04"};
|
||
|
std::vector<std::string> values;
|
||
|
if (type == kTypeValue) {
|
||
|
values = {"v01", "v02", "v03", "v04"};
|
||
|
} else {
|
||
|
values = {"", "", "", ""};
|
||
|
}
|
||
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1, 2, 3}},
|
||
|
{user_keys[1], {1, 2, 3, 4}},
|
||
|
{user_keys[2], {2, 3, 4, 5}},
|
||
|
{user_keys[3], {3, 4, 5, 6}}};
|
||
|
hash_map = std::move(hm);
|
||
|
|
||
|
std::vector<uint64_t> expected_locations = {0, 1, 2, 3};
|
||
|
std::vector<std::string> keys;
|
||
|
for (auto& user_key : user_keys) {
|
||
|
keys.push_back(GetInternalKey(user_key, false, type));
|
||
|
}
|
||
|
uint64_t expected_table_size = GetExpectedTableSize(keys.size());
|
||
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("NoCollisionFullKey");
|
||
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
100, BytewiseComparator(), 1, false, false,
|
||
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
6 years ago
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(keys[i]), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
|
}
|
||
|
size_t bucket_size = keys[0].size() + values[0].size();
|
||
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
|
ASSERT_OK(builder.Finish());
|
||
|
ASSERT_OK(file_writer->Close());
|
||
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
|
|
||
|
std::string expected_unused_bucket = GetInternalKey("key00", true);
|
||
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(keys, values, expected_locations, expected_unused_bucket,
|
||
|
expected_table_size, 2, false);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WriteSuccessWithCollisionFullKey) {
|
||
10 years ago
|
uint32_t num_hash_fun = 4;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03", "key04"};
|
||
|
std::vector<std::string> values = {"v01", "v02", "v03", "v04"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1, 2, 3}},
|
||
|
{user_keys[1], {0, 1, 2, 3}},
|
||
|
{user_keys[2], {0, 1, 2, 3}},
|
||
|
{user_keys[3], {0, 1, 2, 3}},
|
||
10 years ago
|
};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
|
|
||
10 years ago
|
std::vector<uint64_t> expected_locations = {0, 1, 2, 3};
|
||
|
std::vector<std::string> keys;
|
||
|
for (auto& user_key : user_keys) {
|
||
|
keys.push_back(GetInternalKey(user_key, false));
|
||
10 years ago
|
}
|
||
9 years ago
|
uint64_t expected_table_size = GetExpectedTableSize(keys.size());
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("WithCollisionFullKey");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
100, BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(keys[i]), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
10 years ago
|
}
|
||
10 years ago
|
size_t bucket_size = keys[0].size() + values[0].size();
|
||
10 years ago
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
10 years ago
|
|
||
10 years ago
|
std::string expected_unused_bucket = GetInternalKey("key00", true);
|
||
10 years ago
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(keys, values, expected_locations,
|
||
10 years ago
|
expected_unused_bucket, expected_table_size, 4, false);
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WriteSuccessWithCollisionAndCuckooBlock) {
|
||
10 years ago
|
uint32_t num_hash_fun = 4;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03", "key04"};
|
||
|
std::vector<std::string> values = {"v01", "v02", "v03", "v04"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1, 2, 3}},
|
||
|
{user_keys[1], {0, 1, 2, 3}},
|
||
|
{user_keys[2], {0, 1, 2, 3}},
|
||
|
{user_keys[3], {0, 1, 2, 3}},
|
||
10 years ago
|
};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
|
|
||
10 years ago
|
std::vector<uint64_t> expected_locations = {0, 1, 2, 3};
|
||
|
std::vector<std::string> keys;
|
||
|
for (auto& user_key : user_keys) {
|
||
|
keys.push_back(GetInternalKey(user_key, false));
|
||
|
}
|
||
9 years ago
|
uint64_t expected_table_size = GetExpectedTableSize(keys.size());
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
10 years ago
|
uint32_t cuckoo_block_size = 2;
|
||
6 years ago
|
fname = test::PerThreadDBPath("WithCollisionFullKey2");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(
|
||
|
file_writer.get(), kHashTableRatio, num_hash_fun, 100,
|
||
|
BytewiseComparator(), cuckoo_block_size, false, false, GetSliceHash,
|
||
|
0 /* column_family_id */, kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(keys[i]), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
|
}
|
||
10 years ago
|
size_t bucket_size = keys[0].size() + values[0].size();
|
||
10 years ago
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
10 years ago
|
|
||
|
std::string expected_unused_bucket = GetInternalKey("key00", true);
|
||
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(keys, values, expected_locations,
|
||
|
expected_unused_bucket, expected_table_size, 3, false, cuckoo_block_size);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WithCollisionPathFullKey) {
|
||
10 years ago
|
// Have two hash functions. Insert elements with overlapping hashes.
|
||
|
// Finally insert an element with hash value somewhere in the middle
|
||
|
// so that it displaces all the elements after that.
|
||
|
uint32_t num_hash_fun = 2;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03",
|
||
|
"key04", "key05"};
|
||
|
std::vector<std::string> values = {"v01", "v02", "v03", "v04", "v05"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1}},
|
||
|
{user_keys[1], {1, 2}},
|
||
|
{user_keys[2], {2, 3}},
|
||
|
{user_keys[3], {3, 4}},
|
||
|
{user_keys[4], {0, 2}},
|
||
10 years ago
|
};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
|
|
||
10 years ago
|
std::vector<uint64_t> expected_locations = {0, 1, 3, 4, 2};
|
||
|
std::vector<std::string> keys;
|
||
|
for (auto& user_key : user_keys) {
|
||
|
keys.push_back(GetInternalKey(user_key, false));
|
||
10 years ago
|
}
|
||
9 years ago
|
uint64_t expected_table_size = GetExpectedTableSize(keys.size());
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("WithCollisionPathFullKey");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
100, BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(keys[i]), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
|
}
|
||
10 years ago
|
size_t bucket_size = keys[0].size() + values[0].size();
|
||
10 years ago
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
10 years ago
|
|
||
|
std::string expected_unused_bucket = GetInternalKey("key00", true);
|
||
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(keys, values, expected_locations,
|
||
|
expected_unused_bucket, expected_table_size, 2, false);
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WithCollisionPathFullKeyAndCuckooBlock) {
|
||
10 years ago
|
uint32_t num_hash_fun = 2;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03",
|
||
|
"key04", "key05"};
|
||
|
std::vector<std::string> values = {"v01", "v02", "v03", "v04", "v05"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1}},
|
||
|
{user_keys[1], {1, 2}},
|
||
|
{user_keys[2], {3, 4}},
|
||
|
{user_keys[3], {4, 5}},
|
||
|
{user_keys[4], {0, 3}},
|
||
10 years ago
|
};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
|
|
||
10 years ago
|
std::vector<uint64_t> expected_locations = {2, 1, 3, 4, 0};
|
||
|
std::vector<std::string> keys;
|
||
|
for (auto& user_key : user_keys) {
|
||
|
keys.push_back(GetInternalKey(user_key, false));
|
||
|
}
|
||
9 years ago
|
uint64_t expected_table_size = GetExpectedTableSize(keys.size());
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("WithCollisionPathFullKeyAndCuckooBlock");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
100, BytewiseComparator(), 2, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(keys[i]), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
10 years ago
|
}
|
||
10 years ago
|
size_t bucket_size = keys[0].size() + values[0].size();
|
||
10 years ago
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
10 years ago
|
|
||
10 years ago
|
std::string expected_unused_bucket = GetInternalKey("key00", true);
|
||
10 years ago
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(keys, values, expected_locations,
|
||
10 years ago
|
expected_unused_bucket, expected_table_size, 2, false, 2);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WriteSuccessNoCollisionUserKey) {
|
||
10 years ago
|
uint32_t num_hash_fun = 4;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03", "key04"};
|
||
|
std::vector<std::string> values = {"v01", "v02", "v03", "v04"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1, 2, 3}},
|
||
|
{user_keys[1], {1, 2, 3, 4}},
|
||
|
{user_keys[2], {2, 3, 4, 5}},
|
||
|
{user_keys[3], {3, 4, 5, 6}}};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
|
|
||
10 years ago
|
std::vector<uint64_t> expected_locations = {0, 1, 2, 3};
|
||
9 years ago
|
uint64_t expected_table_size = GetExpectedTableSize(user_keys.size());
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("NoCollisionUserKey");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
100, BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(GetInternalKey(user_keys[i], true)), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
10 years ago
|
}
|
||
10 years ago
|
size_t bucket_size = user_keys[0].size() + values[0].size();
|
||
10 years ago
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
10 years ago
|
|
||
10 years ago
|
std::string expected_unused_bucket = "key00";
|
||
10 years ago
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(user_keys, values, expected_locations,
|
||
10 years ago
|
expected_unused_bucket, expected_table_size, 2, true);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WriteSuccessWithCollisionUserKey) {
|
||
10 years ago
|
uint32_t num_hash_fun = 4;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03", "key04"};
|
||
|
std::vector<std::string> values = {"v01", "v02", "v03", "v04"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1, 2, 3}},
|
||
|
{user_keys[1], {0, 1, 2, 3}},
|
||
|
{user_keys[2], {0, 1, 2, 3}},
|
||
|
{user_keys[3], {0, 1, 2, 3}},
|
||
10 years ago
|
};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
|
|
||
10 years ago
|
std::vector<uint64_t> expected_locations = {0, 1, 2, 3};
|
||
9 years ago
|
uint64_t expected_table_size = GetExpectedTableSize(user_keys.size());
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("WithCollisionUserKey");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
100, BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(GetInternalKey(user_keys[i], true)), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
|
}
|
||
10 years ago
|
size_t bucket_size = user_keys[0].size() + values[0].size();
|
||
10 years ago
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
10 years ago
|
|
||
10 years ago
|
std::string expected_unused_bucket = "key00";
|
||
10 years ago
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(user_keys, values, expected_locations,
|
||
10 years ago
|
expected_unused_bucket, expected_table_size, 4, true);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, WithCollisionPathUserKey) {
|
||
10 years ago
|
uint32_t num_hash_fun = 2;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03",
|
||
|
"key04", "key05"};
|
||
|
std::vector<std::string> values = {"v01", "v02", "v03", "v04", "v05"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1}},
|
||
|
{user_keys[1], {1, 2}},
|
||
|
{user_keys[2], {2, 3}},
|
||
|
{user_keys[3], {3, 4}},
|
||
|
{user_keys[4], {0, 2}},
|
||
10 years ago
|
};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
|
|
||
10 years ago
|
std::vector<uint64_t> expected_locations = {0, 1, 3, 4, 2};
|
||
9 years ago
|
uint64_t expected_table_size = GetExpectedTableSize(user_keys.size());
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("WithCollisionPathUserKey");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
2, BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(GetInternalKey(user_keys[i], true)), Slice(values[i]));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
10 years ago
|
}
|
||
10 years ago
|
size_t bucket_size = user_keys[0].size() + values[0].size();
|
||
10 years ago
|
ASSERT_EQ(expected_table_size * bucket_size - 1, builder.FileSize());
|
||
10 years ago
|
ASSERT_OK(builder.Finish());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
ASSERT_LE(expected_table_size * bucket_size, builder.FileSize());
|
||
10 years ago
|
|
||
10 years ago
|
std::string expected_unused_bucket = "key00";
|
||
10 years ago
|
expected_unused_bucket += std::string(values[0].size(), 'a');
|
||
|
CheckFileContents(user_keys, values, expected_locations,
|
||
10 years ago
|
expected_unused_bucket, expected_table_size, 2, true);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, FailWhenCollisionPathTooLong) {
|
||
10 years ago
|
// Have two hash functions. Insert elements with overlapping hashes.
|
||
10 years ago
|
// Finally try inserting an element with hash value somewhere in the middle
|
||
|
// and it should fail because the no. of elements to displace is too high.
|
||
|
uint32_t num_hash_fun = 2;
|
||
|
std::vector<std::string> user_keys = {"key01", "key02", "key03",
|
||
|
"key04", "key05"};
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{user_keys[0], {0, 1}},
|
||
|
{user_keys[1], {1, 2}},
|
||
|
{user_keys[2], {2, 3}},
|
||
|
{user_keys[3], {3, 4}},
|
||
|
{user_keys[4], {0, 1}},
|
||
10 years ago
|
};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("WithCollisionPathUserKey");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
2, BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
for (uint32_t i = 0; i < user_keys.size(); i++) {
|
||
|
builder.Add(Slice(GetInternalKey(user_keys[i], false)), Slice("value"));
|
||
|
ASSERT_EQ(builder.NumEntries(), i + 1);
|
||
|
ASSERT_OK(builder.status());
|
||
10 years ago
|
}
|
||
10 years ago
|
ASSERT_TRUE(builder.Finish().IsNotSupported());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(CuckooBuilderTest, FailWhenSameKeyInserted) {
|
||
9 years ago
|
// Need to have a temporary variable here as VS compiler does not currently
|
||
|
// support operator= with initializer_list as a parameter
|
||
|
std::unordered_map<std::string, std::vector<uint64_t>> hm = {
|
||
|
{"repeatedkey", {0, 1, 2, 3}}};
|
||
9 years ago
|
hash_map = std::move(hm);
|
||
10 years ago
|
uint32_t num_hash_fun = 4;
|
||
|
std::string user_key = "repeatedkey";
|
||
10 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> writable_file;
|
||
6 years ago
|
fname = test::PerThreadDBPath("FailWhenSameKeyInserted");
|
||
10 years ago
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, env_options_));
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> file_writer(
|
||
6 years ago
|
new WritableFileWriter(std::move(writable_file), fname, EnvOptions()));
|
||
9 years ago
|
CuckooTableBuilder builder(file_writer.get(), kHashTableRatio, num_hash_fun,
|
||
|
100, BytewiseComparator(), 1, false, false,
|
||
9 years ago
|
GetSliceHash, 0 /* column_family_id */,
|
||
|
kDefaultColumnFamilyName);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
|
||
|
builder.Add(Slice(GetInternalKey(user_key, false)), Slice("value1"));
|
||
10 years ago
|
ASSERT_EQ(builder.NumEntries(), 1u);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
builder.Add(Slice(GetInternalKey(user_key, true)), Slice("value2"));
|
||
10 years ago
|
ASSERT_EQ(builder.NumEntries(), 2u);
|
||
10 years ago
|
ASSERT_OK(builder.status());
|
||
|
|
||
|
ASSERT_TRUE(builder.Finish().IsNotSupported());
|
||
9 years ago
|
ASSERT_OK(file_writer->Close());
|
||
10 years ago
|
}
|
||
|
} // namespace rocksdb
|
||
|
|
||
10 years ago
|
int main(int argc, char** argv) {
|
||
|
::testing::InitGoogleTest(&argc, argv);
|
||
|
return RUN_ALL_TESTS();
|
||
|
}
|
||
9 years ago
|
|
||
|
#else
|
||
|
#include <stdio.h>
|
||
|
|
||
7 years ago
|
int main(int /*argc*/, char** /*argv*/) {
|
||
9 years ago
|
fprintf(stderr, "SKIPPED as Cuckoo table is not supported in ROCKSDB_LITE\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
#endif // ROCKSDB_LITE
|