2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2014-04-10 23:19:43 +02:00
|
|
|
#include <stdio.h>
|
2012-12-20 20:05:41 +01:00
|
|
|
#include <string>
|
2014-04-10 23:19:43 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2012-12-20 20:05:41 +01:00
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
2013-10-29 01:54:09 +01:00
|
|
|
#include "rocksdb/table.h"
|
2014-04-10 23:19:43 +02:00
|
|
|
#include "rocksdb/slice_transform.h"
|
2012-12-20 20:05:41 +01:00
|
|
|
#include "table/block.h"
|
|
|
|
#include "table/block_builder.h"
|
|
|
|
#include "table/format.h"
|
2014-04-10 23:19:43 +02:00
|
|
|
#include "table/block_hash_index.h"
|
2012-12-20 20:05:41 +01:00
|
|
|
#include "util/random.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-12-20 20:05:41 +01:00
|
|
|
|
|
|
|
static std::string RandomString(Random* rnd, int len) {
|
|
|
|
std::string r;
|
|
|
|
test::RandomString(rnd, len, &r);
|
|
|
|
return r;
|
|
|
|
}
|
2014-04-10 23:19:43 +02:00
|
|
|
std::string GenerateKey(int primary_key, int secondary_key, int padding_size,
|
|
|
|
Random *rnd) {
|
|
|
|
char buf[50];
|
|
|
|
char *p = &buf[0];
|
|
|
|
snprintf(buf, sizeof(buf), "%6d%4d", primary_key, secondary_key);
|
|
|
|
std::string k(p);
|
|
|
|
if (padding_size) {
|
|
|
|
k += RandomString(rnd, padding_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate random key value pairs.
|
|
|
|
// The generated key will be sorted. You can tune the parameters to generated
|
|
|
|
// different kinds of test key/value pairs for different scenario.
|
|
|
|
void GenerateRandomKVs(std::vector<std::string> *keys,
|
|
|
|
std::vector<std::string> *values, const int from,
|
|
|
|
const int len, const int step = 1,
|
|
|
|
const int padding_size = 0,
|
|
|
|
const int keys_share_prefix = 1) {
|
|
|
|
Random rnd(302);
|
|
|
|
|
|
|
|
// generate different prefix
|
|
|
|
for (int i = from; i < from + len; i += step) {
|
|
|
|
// generating keys that shares the prefix
|
|
|
|
for (int j = 0; j < keys_share_prefix; ++j) {
|
|
|
|
keys->emplace_back(GenerateKey(i, j, padding_size, &rnd));
|
|
|
|
|
|
|
|
// 100 bytes values
|
|
|
|
values->emplace_back(RandomString(&rnd, 100));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 20:05:41 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class BlockTest : public testing::Test {};
|
2012-12-20 20:05:41 +01:00
|
|
|
|
|
|
|
// block test
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(BlockTest, SimpleTest) {
|
2012-12-20 20:05:41 +01:00
|
|
|
Random rnd(301);
|
|
|
|
Options options = Options();
|
2014-01-27 22:53:22 +01:00
|
|
|
std::unique_ptr<InternalKeyComparator> ic;
|
|
|
|
ic.reset(new test::PlainInternalKeyComparator(options.comparator));
|
|
|
|
|
2012-12-20 20:05:41 +01:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
std::vector<std::string> values;
|
2014-09-02 20:49:38 +02:00
|
|
|
BlockBuilder builder(16);
|
2012-12-20 20:05:41 +01:00
|
|
|
int num_records = 100000;
|
|
|
|
|
2014-04-10 23:19:43 +02:00
|
|
|
GenerateRandomKVs(&keys, &values, 0, num_records);
|
2012-12-20 20:05:41 +01:00
|
|
|
// add a bunch of records to a block
|
|
|
|
for (int i = 0; i < num_records; i++) {
|
2014-04-10 23:19:43 +02:00
|
|
|
builder.Add(keys[i], values[i]);
|
2012-12-20 20:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// read serialized contents of the block
|
|
|
|
Slice rawblock = builder.Finish();
|
|
|
|
|
2013-04-23 00:20:20 +02:00
|
|
|
// create block reader
|
2012-12-20 20:05:41 +01:00
|
|
|
BlockContents contents;
|
|
|
|
contents.data = rawblock;
|
|
|
|
contents.cachable = false;
|
2014-08-16 00:05:09 +02:00
|
|
|
Block reader(std::move(contents));
|
2012-12-20 20:05:41 +01:00
|
|
|
|
|
|
|
// read contents of block sequentially
|
|
|
|
int count = 0;
|
2015-10-13 00:06:38 +02:00
|
|
|
InternalIterator *iter = reader.NewIterator(options.comparator);
|
2012-12-20 20:05:41 +01:00
|
|
|
for (iter->SeekToFirst();iter->Valid(); count++, iter->Next()) {
|
2013-04-23 00:20:20 +02:00
|
|
|
|
2012-12-20 20:05:41 +01:00
|
|
|
// read kv from block
|
|
|
|
Slice k = iter->key();
|
|
|
|
Slice v = iter->value();
|
|
|
|
|
|
|
|
// compare with lookaside array
|
|
|
|
ASSERT_EQ(k.ToString().compare(keys[count]), 0);
|
|
|
|
ASSERT_EQ(v.ToString().compare(values[count]), 0);
|
|
|
|
}
|
|
|
|
delete iter;
|
2013-04-23 00:20:20 +02:00
|
|
|
|
|
|
|
// read block contents randomly
|
2012-12-20 20:05:41 +01:00
|
|
|
iter = reader.NewIterator(options.comparator);
|
|
|
|
for (int i = 0; i < num_records; i++) {
|
|
|
|
|
|
|
|
// find a random key in the lookaside array
|
|
|
|
int index = rnd.Uniform(num_records);
|
|
|
|
Slice k(keys[index]);
|
|
|
|
|
|
|
|
// search in block for this key
|
|
|
|
iter->Seek(k);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
Slice v = iter->value();
|
|
|
|
ASSERT_EQ(v.ToString().compare(values[index]), 0);
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
|
2014-04-10 23:19:43 +02:00
|
|
|
// return the block contents
|
|
|
|
BlockContents GetBlockContents(std::unique_ptr<BlockBuilder> *builder,
|
|
|
|
const std::vector<std::string> &keys,
|
|
|
|
const std::vector<std::string> &values,
|
|
|
|
const int prefix_group_size = 1) {
|
2014-09-02 20:49:38 +02:00
|
|
|
builder->reset(new BlockBuilder(1 /* restart interval */));
|
2014-04-10 23:19:43 +02:00
|
|
|
|
|
|
|
// Add only half of the keys
|
|
|
|
for (size_t i = 0; i < keys.size(); ++i) {
|
|
|
|
(*builder)->Add(keys[i], values[i]);
|
|
|
|
}
|
|
|
|
Slice rawblock = (*builder)->Finish();
|
|
|
|
|
|
|
|
BlockContents contents;
|
|
|
|
contents.data = rawblock;
|
|
|
|
contents.cachable = false;
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:45:58 +02:00
|
|
|
void CheckBlockContents(BlockContents contents, const int max_key,
|
2014-04-10 23:19:43 +02:00
|
|
|
const std::vector<std::string> &keys,
|
|
|
|
const std::vector<std::string> &values) {
|
|
|
|
const size_t prefix_size = 6;
|
|
|
|
// create block reader
|
2014-09-18 01:45:58 +02:00
|
|
|
BlockContents contents_ref(contents.data, contents.cachable,
|
|
|
|
contents.compression_type);
|
|
|
|
Block reader1(std::move(contents));
|
|
|
|
Block reader2(std::move(contents_ref));
|
2014-04-10 23:19:43 +02:00
|
|
|
|
|
|
|
std::unique_ptr<const SliceTransform> prefix_extractor(
|
|
|
|
NewFixedPrefixTransform(prefix_size));
|
|
|
|
|
|
|
|
{
|
|
|
|
auto iter1 = reader1.NewIterator(nullptr);
|
|
|
|
auto iter2 = reader1.NewIterator(nullptr);
|
2014-05-15 23:09:03 +02:00
|
|
|
reader1.SetBlockHashIndex(CreateBlockHashIndexOnTheFly(
|
2014-11-11 22:47:22 +01:00
|
|
|
iter1, iter2, static_cast<uint32_t>(keys.size()), BytewiseComparator(),
|
2014-05-15 23:09:03 +02:00
|
|
|
prefix_extractor.get()));
|
2014-04-10 23:19:43 +02:00
|
|
|
|
|
|
|
delete iter1;
|
|
|
|
delete iter2;
|
|
|
|
}
|
|
|
|
|
2015-10-13 00:06:38 +02:00
|
|
|
std::unique_ptr<InternalIterator> hash_iter(
|
2014-08-26 01:14:30 +02:00
|
|
|
reader1.NewIterator(BytewiseComparator(), nullptr, false));
|
2014-04-10 23:19:43 +02:00
|
|
|
|
2015-10-13 00:06:38 +02:00
|
|
|
std::unique_ptr<InternalIterator> regular_iter(
|
2014-04-10 23:19:43 +02:00
|
|
|
reader2.NewIterator(BytewiseComparator()));
|
|
|
|
|
|
|
|
// Seek existent keys
|
|
|
|
for (size_t i = 0; i < keys.size(); i++) {
|
|
|
|
hash_iter->Seek(keys[i]);
|
|
|
|
ASSERT_OK(hash_iter->status());
|
|
|
|
ASSERT_TRUE(hash_iter->Valid());
|
|
|
|
|
|
|
|
Slice v = hash_iter->value();
|
|
|
|
ASSERT_EQ(v.ToString().compare(values[i]), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek non-existent keys.
|
|
|
|
// For hash index, if no key with a given prefix is not found, iterator will
|
|
|
|
// simply be set as invalid; whereas the binary search based iterator will
|
|
|
|
// return the one that is closest.
|
|
|
|
for (int i = 1; i < max_key - 1; i += 2) {
|
|
|
|
auto key = GenerateKey(i, 0, 0, nullptr);
|
|
|
|
hash_iter->Seek(key);
|
|
|
|
ASSERT_TRUE(!hash_iter->Valid());
|
|
|
|
|
|
|
|
regular_iter->Seek(key);
|
|
|
|
ASSERT_TRUE(regular_iter->Valid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In this test case, no two key share same prefix.
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(BlockTest, SimpleIndexHash) {
|
2014-04-10 23:19:43 +02:00
|
|
|
const int kMaxKey = 100000;
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
std::vector<std::string> values;
|
|
|
|
GenerateRandomKVs(&keys, &values, 0 /* first key id */,
|
|
|
|
kMaxKey /* last key id */, 2 /* step */,
|
|
|
|
8 /* padding size (8 bytes randomly generated suffix) */);
|
|
|
|
|
|
|
|
std::unique_ptr<BlockBuilder> builder;
|
|
|
|
auto contents = GetBlockContents(&builder, keys, values);
|
|
|
|
|
2014-09-18 01:45:58 +02:00
|
|
|
CheckBlockContents(std::move(contents), kMaxKey, keys, values);
|
2014-04-10 23:19:43 +02:00
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(BlockTest, IndexHashWithSharedPrefix) {
|
2014-04-10 23:19:43 +02:00
|
|
|
const int kMaxKey = 100000;
|
|
|
|
// for each prefix, there will be 5 keys starts with it.
|
|
|
|
const int kPrefixGroup = 5;
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
std::vector<std::string> values;
|
|
|
|
// Generate keys with same prefix.
|
|
|
|
GenerateRandomKVs(&keys, &values, 0, // first key id
|
|
|
|
kMaxKey, // last key id
|
|
|
|
2, // step
|
|
|
|
10, // padding size,
|
|
|
|
kPrefixGroup);
|
|
|
|
|
|
|
|
std::unique_ptr<BlockBuilder> builder;
|
|
|
|
auto contents = GetBlockContents(&builder, keys, values, kPrefixGroup);
|
|
|
|
|
2014-09-18 01:45:58 +02:00
|
|
|
CheckBlockContents(std::move(contents), kMaxKey, keys, values);
|
2014-04-10 23:19:43 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2012-12-20 20:05:41 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2012-12-20 20:05:41 +01:00
|
|
|
}
|