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-02-20 01:50:00 +01:00
|
|
|
|
2015-07-20 20:24:54 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2014-05-09 02:25:13 +02:00
|
|
|
#ifndef GFLAGS
|
|
|
|
#include <cstdio>
|
|
|
|
int main() {
|
2015-09-16 03:10:36 +02:00
|
|
|
fprintf(stderr, "Please install gflags to run this test... Skipping...\n");
|
|
|
|
return 0;
|
2014-05-09 02:25:13 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2013-10-23 06:59:44 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-05-31 20:52:59 +02:00
|
|
|
#include "db/db_impl/db_impl.h"
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/histogram.h"
|
2013-10-23 06:59:44 +02:00
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/db.h"
|
2015-11-05 22:24:05 +01:00
|
|
|
#include "rocksdb/filter_policy.h"
|
2016-08-30 05:55:39 +02:00
|
|
|
#include "rocksdb/memtablerep.h"
|
2013-10-23 06:59:44 +02:00
|
|
|
#include "rocksdb/perf_context.h"
|
2014-02-20 01:50:00 +01:00
|
|
|
#include "rocksdb/slice_transform.h"
|
2015-11-05 22:24:05 +01:00
|
|
|
#include "rocksdb/table.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "test_util/testharness.h"
|
2017-12-01 19:40:45 +01:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/gflags_compat.h"
|
2016-10-11 22:54:26 +02:00
|
|
|
#include "util/random.h"
|
2013-10-23 06:59:44 +02:00
|
|
|
#include "util/stop_watch.h"
|
2015-03-20 01:29:37 +01:00
|
|
|
#include "util/string_util.h"
|
2016-10-11 22:54:26 +02:00
|
|
|
#include "utilities/merge_operators.h"
|
2013-10-23 06:59:44 +02:00
|
|
|
|
2017-12-01 19:40:45 +01:00
|
|
|
using GFLAGS_NAMESPACE::ParseCommandLineFlags;
|
2014-05-09 02:25:13 +02:00
|
|
|
|
2013-10-23 06:59:44 +02:00
|
|
|
DEFINE_bool(trigger_deadlock, false,
|
|
|
|
"issue delete in range scan to trigger PrefixHashMap deadlock");
|
2014-11-11 22:47:22 +01:00
|
|
|
DEFINE_int32(bucket_count, 100000, "number of buckets");
|
2013-10-23 06:59:44 +02:00
|
|
|
DEFINE_uint64(num_locks, 10001, "number of locks");
|
|
|
|
DEFINE_bool(random_prefix, false, "randomize prefix");
|
2013-11-27 23:27:02 +01:00
|
|
|
DEFINE_uint64(total_prefixes, 100000, "total number of prefixes");
|
|
|
|
DEFINE_uint64(items_per_prefix, 1, "total number of values per prefix");
|
|
|
|
DEFINE_int64(write_buffer_size, 33554432, "");
|
2014-11-11 22:47:22 +01:00
|
|
|
DEFINE_int32(max_write_buffer_number, 2, "");
|
|
|
|
DEFINE_int32(min_write_buffer_number_to_merge, 1, "");
|
2013-11-22 22:31:00 +01:00
|
|
|
DEFINE_int32(skiplist_height, 4, "");
|
2016-06-04 02:02:10 +02:00
|
|
|
DEFINE_double(memtable_prefix_bloom_size_ratio, 0.1, "");
|
2016-07-27 03:05:30 +02:00
|
|
|
DEFINE_int32(memtable_huge_page_size, 2 * 1024 * 1024, "");
|
2013-11-27 23:27:02 +01:00
|
|
|
DEFINE_int32(value_size, 40, "");
|
2016-10-11 22:54:26 +02:00
|
|
|
DEFINE_bool(enable_print, false, "Print options generated to console.");
|
2013-10-23 06:59:44 +02:00
|
|
|
|
|
|
|
// Path to the database on file system
|
2018-07-14 02:18:39 +02:00
|
|
|
const std::string kDbName = rocksdb::test::PerThreadDBPath("prefix_test");
|
2013-10-23 06:59:44 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
struct TestKey {
|
|
|
|
uint64_t prefix;
|
|
|
|
uint64_t sorted;
|
|
|
|
|
2014-10-31 19:59:54 +01:00
|
|
|
TestKey(uint64_t _prefix, uint64_t _sorted)
|
|
|
|
: prefix(_prefix), sorted(_sorted) {}
|
2013-10-23 06:59:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// return a slice backed by test_key
|
2017-04-22 05:41:37 +02:00
|
|
|
inline Slice TestKeyToSlice(std::string &s, const TestKey& test_key) {
|
|
|
|
s.clear();
|
|
|
|
PutFixed64(&s, test_key.prefix);
|
|
|
|
PutFixed64(&s, test_key.sorted);
|
|
|
|
return Slice(s.c_str(), s.size());
|
2013-10-23 06:59:44 +02:00
|
|
|
}
|
|
|
|
|
2017-04-22 05:41:37 +02:00
|
|
|
inline const TestKey SliceToTestKey(const Slice& slice) {
|
|
|
|
return TestKey(DecodeFixed64(slice.data()),
|
|
|
|
DecodeFixed64(slice.data() + 8));
|
2013-10-23 06:59:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class TestKeyComparator : public Comparator {
|
|
|
|
public:
|
2013-11-20 08:24:12 +01:00
|
|
|
|
|
|
|
// Compare needs to be aware of the possibility of a and/or b is
|
|
|
|
// prefix only
|
2019-02-14 22:52:47 +01:00
|
|
|
int Compare(const Slice& a, const Slice& b) const override {
|
2017-04-22 05:41:37 +02:00
|
|
|
const TestKey kkey_a = SliceToTestKey(a);
|
|
|
|
const TestKey kkey_b = SliceToTestKey(b);
|
|
|
|
const TestKey *key_a = &kkey_a;
|
|
|
|
const TestKey *key_b = &kkey_b;
|
2013-10-23 06:59:44 +02:00
|
|
|
if (key_a->prefix != key_b->prefix) {
|
|
|
|
if (key_a->prefix < key_b->prefix) return -1;
|
|
|
|
if (key_a->prefix > key_b->prefix) return 1;
|
|
|
|
} else {
|
rocksdb: Replace ASSERT* with EXPECT* in functions that does not return void value
Summary:
gtest does not use exceptions to fail a unit test by design, and `ASSERT*`s are implemented using `return`. As a consequence we cannot use `ASSERT*` in a function that does not return `void` value ([[ https://code.google.com/p/googletest/wiki/AdvancedGuide#Assertion_Placement | 1]]), and have to fix our existing code. This diff does this in a generic way, with no manual changes.
In order to detect all existing `ASSERT*` that are used in functions that doesn't return void value, I change the code to generate compile errors for such cases.
In `util/testharness.h` I defined `EXPECT*` assertions, the same way as `ASSERT*`, and redefined `ASSERT*` to return `void`. Then executed:
```lang=bash
% USE_CLANG=1 make all -j55 -k 2> build.log
% perl -naF: -e 'print "-- -number=".$F[1]." ".$F[0]."\n" if /: error:/' \
build.log | xargs -L 1 perl -spi -e 's/ASSERT/EXPECT/g if $. == $number'
% make format
```
After that I reverted back change to `ASSERT*` in `util/testharness.h`. But preserved introduced `EXPECT*`, which is the same as `ASSERT*`. This will be deleted once switched to gtest.
This diff is independent and contains manual changes only in `util/testharness.h`.
Test Plan:
Make sure all tests are passing.
```lang=bash
% USE_CLANG=1 make check
```
Reviewers: igor, lgalanis, sdong, yufei.zhu, rven, meyering
Reviewed By: meyering
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D33333
2015-03-17 04:52:32 +01:00
|
|
|
EXPECT_TRUE(key_a->prefix == key_b->prefix);
|
2013-11-20 08:24:12 +01:00
|
|
|
// note, both a and b could be prefix only
|
|
|
|
if (a.size() != b.size()) {
|
|
|
|
// one of them is prefix
|
rocksdb: Replace ASSERT* with EXPECT* in functions that does not return void value
Summary:
gtest does not use exceptions to fail a unit test by design, and `ASSERT*`s are implemented using `return`. As a consequence we cannot use `ASSERT*` in a function that does not return `void` value ([[ https://code.google.com/p/googletest/wiki/AdvancedGuide#Assertion_Placement | 1]]), and have to fix our existing code. This diff does this in a generic way, with no manual changes.
In order to detect all existing `ASSERT*` that are used in functions that doesn't return void value, I change the code to generate compile errors for such cases.
In `util/testharness.h` I defined `EXPECT*` assertions, the same way as `ASSERT*`, and redefined `ASSERT*` to return `void`. Then executed:
```lang=bash
% USE_CLANG=1 make all -j55 -k 2> build.log
% perl -naF: -e 'print "-- -number=".$F[1]." ".$F[0]."\n" if /: error:/' \
build.log | xargs -L 1 perl -spi -e 's/ASSERT/EXPECT/g if $. == $number'
% make format
```
After that I reverted back change to `ASSERT*` in `util/testharness.h`. But preserved introduced `EXPECT*`, which is the same as `ASSERT*`. This will be deleted once switched to gtest.
This diff is independent and contains manual changes only in `util/testharness.h`.
Test Plan:
Make sure all tests are passing.
```lang=bash
% USE_CLANG=1 make check
```
Reviewers: igor, lgalanis, sdong, yufei.zhu, rven, meyering
Reviewed By: meyering
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D33333
2015-03-17 04:52:32 +01:00
|
|
|
EXPECT_TRUE(
|
|
|
|
(a.size() == sizeof(uint64_t) && b.size() == sizeof(TestKey)) ||
|
|
|
|
(b.size() == sizeof(uint64_t) && a.size() == sizeof(TestKey)));
|
2013-11-20 08:24:12 +01:00
|
|
|
if (a.size() < b.size()) return -1;
|
|
|
|
if (a.size() > b.size()) return 1;
|
|
|
|
} else {
|
|
|
|
// both a and b are prefix
|
|
|
|
if (a.size() == sizeof(uint64_t)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// both a and b are whole key
|
rocksdb: Replace ASSERT* with EXPECT* in functions that does not return void value
Summary:
gtest does not use exceptions to fail a unit test by design, and `ASSERT*`s are implemented using `return`. As a consequence we cannot use `ASSERT*` in a function that does not return `void` value ([[ https://code.google.com/p/googletest/wiki/AdvancedGuide#Assertion_Placement | 1]]), and have to fix our existing code. This diff does this in a generic way, with no manual changes.
In order to detect all existing `ASSERT*` that are used in functions that doesn't return void value, I change the code to generate compile errors for such cases.
In `util/testharness.h` I defined `EXPECT*` assertions, the same way as `ASSERT*`, and redefined `ASSERT*` to return `void`. Then executed:
```lang=bash
% USE_CLANG=1 make all -j55 -k 2> build.log
% perl -naF: -e 'print "-- -number=".$F[1]." ".$F[0]."\n" if /: error:/' \
build.log | xargs -L 1 perl -spi -e 's/ASSERT/EXPECT/g if $. == $number'
% make format
```
After that I reverted back change to `ASSERT*` in `util/testharness.h`. But preserved introduced `EXPECT*`, which is the same as `ASSERT*`. This will be deleted once switched to gtest.
This diff is independent and contains manual changes only in `util/testharness.h`.
Test Plan:
Make sure all tests are passing.
```lang=bash
% USE_CLANG=1 make check
```
Reviewers: igor, lgalanis, sdong, yufei.zhu, rven, meyering
Reviewed By: meyering
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D33333
2015-03-17 04:52:32 +01:00
|
|
|
EXPECT_TRUE(a.size() == sizeof(TestKey) && b.size() == sizeof(TestKey));
|
2013-11-20 08:24:12 +01:00
|
|
|
if (key_a->sorted < key_b->sorted) return -1;
|
|
|
|
if (key_a->sorted > key_b->sorted) return 1;
|
|
|
|
if (key_a->sorted == key_b->sorted) return 0;
|
|
|
|
}
|
2013-10-23 06:59:44 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-11 22:54:26 +02:00
|
|
|
bool operator()(const TestKey& a, const TestKey& b) const {
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string sa, sb;
|
|
|
|
return Compare(TestKeyToSlice(sa, a), TestKeyToSlice(sb, b)) < 0;
|
2016-10-11 22:54:26 +02:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
const char* Name() const override { return "TestKeyComparator"; }
|
2013-10-23 06:59:44 +02:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
void FindShortestSeparator(std::string* /*start*/,
|
|
|
|
const Slice& /*limit*/) const override {}
|
2013-10-23 06:59:44 +02:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
void FindShortSuccessor(std::string* /*key*/) const override {}
|
2013-10-23 06:59:44 +02:00
|
|
|
};
|
|
|
|
|
2014-04-10 06:17:14 +02:00
|
|
|
namespace {
|
2014-02-20 01:50:00 +01:00
|
|
|
void PutKey(DB* db, WriteOptions write_options, uint64_t prefix,
|
|
|
|
uint64_t suffix, const Slice& value) {
|
|
|
|
TestKey test_key(prefix, suffix);
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2014-02-20 01:50:00 +01:00
|
|
|
ASSERT_OK(db->Put(write_options, key, value));
|
|
|
|
}
|
|
|
|
|
2016-10-11 22:54:26 +02:00
|
|
|
void PutKey(DB* db, WriteOptions write_options, const TestKey& test_key,
|
|
|
|
const Slice& value) {
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2016-10-11 22:54:26 +02:00
|
|
|
ASSERT_OK(db->Put(write_options, key, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergeKey(DB* db, WriteOptions write_options, const TestKey& test_key,
|
|
|
|
const Slice& value) {
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2016-10-11 22:54:26 +02:00
|
|
|
ASSERT_OK(db->Merge(write_options, key, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteKey(DB* db, WriteOptions write_options, const TestKey& test_key) {
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2016-10-11 22:54:26 +02:00
|
|
|
ASSERT_OK(db->Delete(write_options, key));
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:50:00 +01:00
|
|
|
void SeekIterator(Iterator* iter, uint64_t prefix, uint64_t suffix) {
|
|
|
|
TestKey test_key(prefix, suffix);
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2014-02-20 01:50:00 +01:00
|
|
|
iter->Seek(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string kNotFoundResult = "NOT_FOUND";
|
|
|
|
|
|
|
|
std::string Get(DB* db, const ReadOptions& read_options, uint64_t prefix,
|
|
|
|
uint64_t suffix) {
|
|
|
|
TestKey test_key(prefix, suffix);
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s2;
|
|
|
|
Slice key = TestKeyToSlice(s2, test_key);
|
2014-02-20 01:50:00 +01:00
|
|
|
|
|
|
|
std::string result;
|
|
|
|
Status s = db->Get(read_options, key, &result);
|
|
|
|
if (s.IsNotFound()) {
|
|
|
|
result = kNotFoundResult;
|
|
|
|
} else if (!s.ok()) {
|
|
|
|
result = s.ToString();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-07-28 03:45:53 +02:00
|
|
|
|
|
|
|
class SamePrefixTransform : public SliceTransform {
|
|
|
|
private:
|
|
|
|
const Slice prefix_;
|
|
|
|
std::string name_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SamePrefixTransform(const Slice& prefix)
|
|
|
|
: prefix_(prefix), name_("rocksdb.SamePrefix." + prefix.ToString()) {}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
const char* Name() const override { return name_.c_str(); }
|
2016-07-28 03:45:53 +02:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
Slice Transform(const Slice& src) const override {
|
2016-07-28 03:45:53 +02:00
|
|
|
assert(InDomain(src));
|
|
|
|
return prefix_;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
bool InDomain(const Slice& src) const override {
|
2016-07-28 03:45:53 +02:00
|
|
|
if (src.size() >= prefix_.size()) {
|
|
|
|
return Slice(src.data(), prefix_.size()) == prefix_;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
bool InRange(const Slice& dst) const override { return dst == prefix_; }
|
2018-06-27 00:56:26 +02:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
bool FullLengthEnabled(size_t* /*len*/) const override { return false; }
|
2016-07-28 03:45:53 +02:00
|
|
|
};
|
|
|
|
|
2014-04-10 06:17:14 +02:00
|
|
|
} // namespace
|
2014-02-20 01:50:00 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class PrefixTest : public testing::Test {
|
2013-10-23 06:59:44 +02:00
|
|
|
public:
|
|
|
|
std::shared_ptr<DB> OpenDb() {
|
|
|
|
DB* db;
|
|
|
|
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.write_buffer_size = FLAGS_write_buffer_size;
|
|
|
|
options.max_write_buffer_number = FLAGS_max_write_buffer_number;
|
|
|
|
options.min_write_buffer_number_to_merge =
|
|
|
|
FLAGS_min_write_buffer_number_to_merge;
|
|
|
|
|
2016-06-04 02:02:10 +02:00
|
|
|
options.memtable_prefix_bloom_size_ratio =
|
|
|
|
FLAGS_memtable_prefix_bloom_size_ratio;
|
2016-07-27 03:05:30 +02:00
|
|
|
options.memtable_huge_page_size = FLAGS_memtable_huge_page_size;
|
2013-11-27 23:27:02 +01:00
|
|
|
|
2015-11-05 22:24:05 +01:00
|
|
|
options.prefix_extractor.reset(NewFixedPrefixTransform(8));
|
|
|
|
BlockBasedTableOptions bbto;
|
|
|
|
bbto.filter_policy.reset(NewBloomFilterPolicy(10, false));
|
|
|
|
bbto.whole_key_filtering = false;
|
|
|
|
options.table_factory.reset(NewBlockBasedTableFactory(bbto));
|
2016-11-16 18:24:52 +01:00
|
|
|
options.allow_concurrent_memtable_write = false;
|
2015-11-05 22:24:05 +01:00
|
|
|
|
2013-10-23 06:59:44 +02:00
|
|
|
Status s = DB::Open(options, kDbName, &db);
|
rocksdb: Replace ASSERT* with EXPECT* in functions that does not return void value
Summary:
gtest does not use exceptions to fail a unit test by design, and `ASSERT*`s are implemented using `return`. As a consequence we cannot use `ASSERT*` in a function that does not return `void` value ([[ https://code.google.com/p/googletest/wiki/AdvancedGuide#Assertion_Placement | 1]]), and have to fix our existing code. This diff does this in a generic way, with no manual changes.
In order to detect all existing `ASSERT*` that are used in functions that doesn't return void value, I change the code to generate compile errors for such cases.
In `util/testharness.h` I defined `EXPECT*` assertions, the same way as `ASSERT*`, and redefined `ASSERT*` to return `void`. Then executed:
```lang=bash
% USE_CLANG=1 make all -j55 -k 2> build.log
% perl -naF: -e 'print "-- -number=".$F[1]." ".$F[0]."\n" if /: error:/' \
build.log | xargs -L 1 perl -spi -e 's/ASSERT/EXPECT/g if $. == $number'
% make format
```
After that I reverted back change to `ASSERT*` in `util/testharness.h`. But preserved introduced `EXPECT*`, which is the same as `ASSERT*`. This will be deleted once switched to gtest.
This diff is independent and contains manual changes only in `util/testharness.h`.
Test Plan:
Make sure all tests are passing.
```lang=bash
% USE_CLANG=1 make check
```
Reviewers: igor, lgalanis, sdong, yufei.zhu, rven, meyering
Reviewed By: meyering
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D33333
2015-03-17 04:52:32 +01:00
|
|
|
EXPECT_OK(s);
|
2013-10-23 06:59:44 +02:00
|
|
|
return std::shared_ptr<DB>(db);
|
|
|
|
}
|
2013-12-27 21:56:27 +01:00
|
|
|
|
2014-02-20 01:50:00 +01:00
|
|
|
void FirstOption() {
|
|
|
|
option_config_ = kBegin;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NextOptions(int bucket_count) {
|
2013-12-27 21:56:27 +01:00
|
|
|
// skip some options
|
|
|
|
option_config_++;
|
|
|
|
if (option_config_ < kEnd) {
|
2014-03-10 20:56:46 +01:00
|
|
|
options.prefix_extractor.reset(NewFixedPrefixTransform(8));
|
2013-12-27 21:56:27 +01:00
|
|
|
switch(option_config_) {
|
|
|
|
case kHashSkipList:
|
2014-03-10 20:56:46 +01:00
|
|
|
options.memtable_factory.reset(
|
|
|
|
NewHashSkipListRepFactory(bucket_count, FLAGS_skiplist_height));
|
2013-12-27 21:56:27 +01:00
|
|
|
return true;
|
|
|
|
case kHashLinkList:
|
2014-03-10 20:56:46 +01:00
|
|
|
options.memtable_factory.reset(
|
|
|
|
NewHashLinkListRepFactory(bucket_count));
|
2013-12-27 21:56:27 +01:00
|
|
|
return true;
|
2014-05-04 22:55:53 +02:00
|
|
|
case kHashLinkListHugePageTlb:
|
|
|
|
options.memtable_factory.reset(
|
|
|
|
NewHashLinkListRepFactory(bucket_count, 2 * 1024 * 1024));
|
|
|
|
return true;
|
2014-07-01 20:05:05 +02:00
|
|
|
case kHashLinkListTriggerSkipList:
|
|
|
|
options.memtable_factory.reset(
|
|
|
|
NewHashLinkListRepFactory(bucket_count, 0, 3));
|
|
|
|
return true;
|
2013-12-27 21:56:27 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-10 07:25:03 +01:00
|
|
|
PrefixTest() : option_config_(kBegin) {
|
|
|
|
options.comparator = new TestKeyComparator();
|
|
|
|
}
|
2019-02-14 22:52:47 +01:00
|
|
|
~PrefixTest() override { delete options.comparator; }
|
|
|
|
|
2013-10-23 06:59:44 +02:00
|
|
|
protected:
|
2013-12-27 21:56:27 +01:00
|
|
|
enum OptionConfig {
|
|
|
|
kBegin,
|
|
|
|
kHashSkipList,
|
|
|
|
kHashLinkList,
|
2014-05-04 22:55:53 +02:00
|
|
|
kHashLinkListHugePageTlb,
|
2014-07-01 20:05:05 +02:00
|
|
|
kHashLinkListTriggerSkipList,
|
2013-12-27 21:56:27 +01:00
|
|
|
kEnd
|
|
|
|
};
|
|
|
|
int option_config_;
|
2013-10-23 06:59:44 +02:00
|
|
|
Options options;
|
|
|
|
};
|
|
|
|
|
2016-07-28 03:45:53 +02:00
|
|
|
TEST(SamePrefixTest, InDomainTest) {
|
|
|
|
DB* db;
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.prefix_extractor.reset(new SamePrefixTransform("HHKB"));
|
|
|
|
BlockBasedTableOptions bbto;
|
|
|
|
bbto.filter_policy.reset(NewBloomFilterPolicy(10, false));
|
|
|
|
bbto.whole_key_filtering = false;
|
|
|
|
options.table_factory.reset(NewBlockBasedTableFactory(bbto));
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
{
|
2016-10-18 22:41:33 +02:00
|
|
|
ASSERT_OK(DestroyDB(kDbName, Options()));
|
2016-07-28 03:45:53 +02:00
|
|
|
ASSERT_OK(DB::Open(options, kDbName, &db));
|
|
|
|
ASSERT_OK(db->Put(write_options, "HHKB pro2", "Mar 24, 2006"));
|
|
|
|
ASSERT_OK(db->Put(write_options, "HHKB pro2 Type-S", "June 29, 2011"));
|
|
|
|
ASSERT_OK(db->Put(write_options, "Realforce 87u", "idk"));
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
std::string result;
|
|
|
|
auto db_iter = db->NewIterator(ReadOptions());
|
|
|
|
|
|
|
|
db_iter->Seek("Realforce 87u");
|
|
|
|
ASSERT_TRUE(db_iter->Valid());
|
|
|
|
ASSERT_OK(db_iter->status());
|
|
|
|
ASSERT_EQ(db_iter->key(), "Realforce 87u");
|
|
|
|
ASSERT_EQ(db_iter->value(), "idk");
|
|
|
|
|
|
|
|
delete db_iter;
|
|
|
|
delete db;
|
|
|
|
ASSERT_OK(DestroyDB(kDbName, Options()));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ASSERT_OK(DB::Open(options, kDbName, &db));
|
|
|
|
ASSERT_OK(db->Put(write_options, "pikachu", "1"));
|
|
|
|
ASSERT_OK(db->Put(write_options, "Meowth", "1"));
|
|
|
|
ASSERT_OK(db->Put(write_options, "Mewtwo", "idk"));
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
std::string result;
|
|
|
|
auto db_iter = db->NewIterator(ReadOptions());
|
|
|
|
|
|
|
|
db_iter->Seek("Mewtwo");
|
|
|
|
ASSERT_TRUE(db_iter->Valid());
|
|
|
|
ASSERT_OK(db_iter->status());
|
|
|
|
delete db_iter;
|
|
|
|
delete db;
|
|
|
|
ASSERT_OK(DestroyDB(kDbName, Options()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(PrefixTest, TestResult) {
|
2014-02-20 01:50:00 +01:00
|
|
|
for (int num_buckets = 1; num_buckets <= 2; num_buckets++) {
|
|
|
|
FirstOption();
|
|
|
|
while (NextOptions(num_buckets)) {
|
|
|
|
std::cout << "*** Mem table: " << options.memtable_factory->Name()
|
|
|
|
<< " number of buckets: " << num_buckets
|
|
|
|
<< std::endl;
|
|
|
|
DestroyDB(kDbName, Options());
|
|
|
|
auto db = OpenDb();
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
|
|
|
|
// 1. Insert one row.
|
|
|
|
Slice v16("v16");
|
|
|
|
PutKey(db.get(), write_options, 1, 6, v16);
|
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(read_options));
|
|
|
|
SeekIterator(iter.get(), 1, 6);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v16 == iter->value());
|
|
|
|
SeekIterator(iter.get(), 1, 5);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v16 == iter->value());
|
|
|
|
SeekIterator(iter.get(), 1, 5);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v16 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 2, 0);
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
|
|
|
|
ASSERT_EQ(v16.ToString(), Get(db.get(), read_options, 1, 6));
|
|
|
|
ASSERT_EQ(kNotFoundResult, Get(db.get(), read_options, 1, 5));
|
|
|
|
ASSERT_EQ(kNotFoundResult, Get(db.get(), read_options, 1, 7));
|
|
|
|
ASSERT_EQ(kNotFoundResult, Get(db.get(), read_options, 0, 6));
|
|
|
|
ASSERT_EQ(kNotFoundResult, Get(db.get(), read_options, 2, 6));
|
|
|
|
|
|
|
|
// 2. Insert an entry for the same prefix as the last entry in the bucket.
|
|
|
|
Slice v17("v17");
|
|
|
|
PutKey(db.get(), write_options, 1, 7, v17);
|
|
|
|
iter.reset(db->NewIterator(read_options));
|
|
|
|
SeekIterator(iter.get(), 1, 7);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 6);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v16 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 2, 0);
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
|
|
|
|
// 3. Insert an entry for the same prefix as the head of the bucket.
|
|
|
|
Slice v15("v15");
|
|
|
|
PutKey(db.get(), write_options, 1, 5, v15);
|
|
|
|
iter.reset(db->NewIterator(read_options));
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 7);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 5);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v15 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v16 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 5);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v15 == iter->value());
|
|
|
|
|
|
|
|
ASSERT_EQ(v15.ToString(), Get(db.get(), read_options, 1, 5));
|
|
|
|
ASSERT_EQ(v16.ToString(), Get(db.get(), read_options, 1, 6));
|
|
|
|
ASSERT_EQ(v17.ToString(), Get(db.get(), read_options, 1, 7));
|
|
|
|
|
|
|
|
// 4. Insert an entry with a larger prefix
|
|
|
|
Slice v22("v22");
|
|
|
|
PutKey(db.get(), write_options, 2, 2, v22);
|
|
|
|
iter.reset(db->NewIterator(read_options));
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 2, 2);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v22 == iter->value());
|
|
|
|
SeekIterator(iter.get(), 2, 0);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v22 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 5);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v15 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 7);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
|
|
|
|
// 5. Insert an entry with a smaller prefix
|
|
|
|
Slice v02("v02");
|
|
|
|
PutKey(db.get(), write_options, 0, 2, v02);
|
|
|
|
iter.reset(db->NewIterator(read_options));
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 0, 2);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v02 == iter->value());
|
|
|
|
SeekIterator(iter.get(), 0, 0);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v02 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 2, 0);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v22 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 5);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v15 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 7);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
|
|
|
|
// 6. Insert to the beginning and the end of the first prefix
|
|
|
|
Slice v13("v13");
|
|
|
|
Slice v18("v18");
|
|
|
|
PutKey(db.get(), write_options, 1, 3, v13);
|
|
|
|
PutKey(db.get(), write_options, 1, 8, v18);
|
|
|
|
iter.reset(db->NewIterator(read_options));
|
|
|
|
SeekIterator(iter.get(), 1, 7);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 1, 3);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v13 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v15 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v16 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v18 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 0, 0);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v02 == iter->value());
|
|
|
|
|
|
|
|
SeekIterator(iter.get(), 2, 0);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v22 == iter->value());
|
|
|
|
|
|
|
|
ASSERT_EQ(v22.ToString(), Get(db.get(), read_options, 2, 2));
|
|
|
|
ASSERT_EQ(v02.ToString(), Get(db.get(), read_options, 0, 2));
|
|
|
|
ASSERT_EQ(v13.ToString(), Get(db.get(), read_options, 1, 3));
|
|
|
|
ASSERT_EQ(v15.ToString(), Get(db.get(), read_options, 1, 5));
|
|
|
|
ASSERT_EQ(v16.ToString(), Get(db.get(), read_options, 1, 6));
|
|
|
|
ASSERT_EQ(v17.ToString(), Get(db.get(), read_options, 1, 7));
|
|
|
|
ASSERT_EQ(v18.ToString(), Get(db.get(), read_options, 1, 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 22:24:05 +01:00
|
|
|
// Show results in prefix
|
|
|
|
TEST_F(PrefixTest, PrefixValid) {
|
|
|
|
for (int num_buckets = 1; num_buckets <= 2; num_buckets++) {
|
|
|
|
FirstOption();
|
|
|
|
while (NextOptions(num_buckets)) {
|
|
|
|
std::cout << "*** Mem table: " << options.memtable_factory->Name()
|
|
|
|
<< " number of buckets: " << num_buckets << std::endl;
|
|
|
|
DestroyDB(kDbName, Options());
|
|
|
|
auto db = OpenDb();
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
|
|
|
|
// Insert keys with common prefix and one key with different
|
|
|
|
Slice v16("v16");
|
|
|
|
Slice v17("v17");
|
|
|
|
Slice v18("v18");
|
|
|
|
Slice v19("v19");
|
|
|
|
PutKey(db.get(), write_options, 12345, 6, v16);
|
|
|
|
PutKey(db.get(), write_options, 12345, 7, v17);
|
|
|
|
PutKey(db.get(), write_options, 12345, 8, v18);
|
|
|
|
PutKey(db.get(), write_options, 12345, 9, v19);
|
|
|
|
PutKey(db.get(), write_options, 12346, 8, v16);
|
|
|
|
db->Flush(FlushOptions());
|
2017-04-22 05:41:37 +02:00
|
|
|
TestKey test_key(12346, 8);
|
|
|
|
std::string s;
|
|
|
|
db->Delete(write_options, TestKeyToSlice(s, test_key));
|
2015-11-05 22:24:05 +01:00
|
|
|
db->Flush(FlushOptions());
|
|
|
|
read_options.prefix_same_as_start = true;
|
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(read_options));
|
|
|
|
SeekIterator(iter.get(), 12345, 6);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v16 == iter->value());
|
|
|
|
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v17 == iter->value());
|
|
|
|
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v18 == iter->value());
|
|
|
|
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_TRUE(v19 == iter->value());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_FALSE(iter->Valid());
|
|
|
|
ASSERT_EQ(kNotFoundResult, Get(db.get(), read_options, 12346, 8));
|
2016-04-29 01:48:03 +02:00
|
|
|
|
|
|
|
// Verify seeking past the prefix won't return a result.
|
|
|
|
SeekIterator(iter.get(), 12345, 10);
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
2015-11-05 22:24:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(PrefixTest, DynamicPrefixIterator) {
|
2014-02-20 01:50:00 +01:00
|
|
|
while (NextOptions(FLAGS_bucket_count)) {
|
2013-12-27 21:56:27 +01:00
|
|
|
std::cout << "*** Mem table: " << options.memtable_factory->Name()
|
|
|
|
<< std::endl;
|
|
|
|
DestroyDB(kDbName, Options());
|
|
|
|
auto db = OpenDb();
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
|
|
|
|
std::vector<uint64_t> prefixes;
|
|
|
|
for (uint64_t i = 0; i < FLAGS_total_prefixes; ++i) {
|
|
|
|
prefixes.push_back(i);
|
|
|
|
}
|
2013-11-04 01:32:46 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
if (FLAGS_random_prefix) {
|
|
|
|
std::random_shuffle(prefixes.begin(), prefixes.end());
|
|
|
|
}
|
2013-11-04 01:32:46 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
HistogramImpl hist_put_time;
|
|
|
|
HistogramImpl hist_put_comparison;
|
2013-11-04 01:32:46 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
// insert x random prefix, each with y continuous element.
|
|
|
|
for (auto prefix : prefixes) {
|
|
|
|
for (uint64_t sorted = 0; sorted < FLAGS_items_per_prefix; sorted++) {
|
|
|
|
TestKey test_key(prefix, sorted);
|
|
|
|
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2013-12-27 21:56:27 +01:00
|
|
|
std::string value(FLAGS_value_size, 0);
|
|
|
|
|
2017-06-03 02:12:39 +02:00
|
|
|
get_perf_context()->Reset();
|
2013-12-27 21:56:27 +01:00
|
|
|
StopWatchNano timer(Env::Default(), true);
|
|
|
|
ASSERT_OK(db->Put(write_options, key, value));
|
|
|
|
hist_put_time.Add(timer.ElapsedNanos());
|
2017-06-03 02:12:39 +02:00
|
|
|
hist_put_comparison.Add(get_perf_context()->user_key_comparison_count);
|
2013-12-27 21:56:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Put key comparison: \n" << hist_put_comparison.ToString()
|
|
|
|
<< "Put time: \n" << hist_put_time.ToString();
|
2013-11-04 01:32:46 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
// test seek existing keys
|
|
|
|
HistogramImpl hist_seek_time;
|
|
|
|
HistogramImpl hist_seek_comparison;
|
2013-11-27 23:27:02 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(read_options));
|
2013-11-04 01:32:46 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
for (auto prefix : prefixes) {
|
|
|
|
TestKey test_key(prefix, FLAGS_items_per_prefix / 2);
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2014-11-25 05:44:49 +01:00
|
|
|
std::string value = "v" + ToString(0);
|
2013-11-04 01:32:46 +01:00
|
|
|
|
2017-06-03 02:12:39 +02:00
|
|
|
get_perf_context()->Reset();
|
2013-11-27 23:27:02 +01:00
|
|
|
StopWatchNano timer(Env::Default(), true);
|
2014-04-25 21:21:34 +02:00
|
|
|
auto key_prefix = options.prefix_extractor->Transform(key);
|
2013-12-27 21:56:27 +01:00
|
|
|
uint64_t total_keys = 0;
|
2014-04-25 21:21:34 +02:00
|
|
|
for (iter->Seek(key);
|
|
|
|
iter->Valid() && iter->key().starts_with(key_prefix);
|
|
|
|
iter->Next()) {
|
2013-12-27 21:56:27 +01:00
|
|
|
if (FLAGS_trigger_deadlock) {
|
|
|
|
std::cout << "Behold the deadlock!\n";
|
|
|
|
db->Delete(write_options, iter->key());
|
|
|
|
}
|
|
|
|
total_keys++;
|
|
|
|
}
|
|
|
|
hist_seek_time.Add(timer.ElapsedNanos());
|
2017-06-03 02:12:39 +02:00
|
|
|
hist_seek_comparison.Add(get_perf_context()->user_key_comparison_count);
|
2013-12-27 21:56:27 +01:00
|
|
|
ASSERT_EQ(total_keys, FLAGS_items_per_prefix - FLAGS_items_per_prefix/2);
|
2013-11-04 01:32:46 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
std::cout << "Seek key comparison: \n"
|
|
|
|
<< hist_seek_comparison.ToString()
|
|
|
|
<< "Seek time: \n"
|
|
|
|
<< hist_seek_time.ToString();
|
2013-11-27 23:27:02 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
// test non-existing keys
|
|
|
|
HistogramImpl hist_no_seek_time;
|
|
|
|
HistogramImpl hist_no_seek_comparison;
|
2013-11-04 01:32:46 +01:00
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
for (auto prefix = FLAGS_total_prefixes;
|
|
|
|
prefix < FLAGS_total_prefixes + 10000;
|
|
|
|
prefix++) {
|
|
|
|
TestKey test_key(prefix, 0);
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice key = TestKeyToSlice(s, test_key);
|
2013-12-27 21:56:27 +01:00
|
|
|
|
2017-06-03 02:12:39 +02:00
|
|
|
get_perf_context()->Reset();
|
2013-12-27 21:56:27 +01:00
|
|
|
StopWatchNano timer(Env::Default(), true);
|
|
|
|
iter->Seek(key);
|
|
|
|
hist_no_seek_time.Add(timer.ElapsedNanos());
|
2017-06-03 02:12:39 +02:00
|
|
|
hist_no_seek_comparison.Add(get_perf_context()->user_key_comparison_count);
|
2013-12-27 21:56:27 +01:00
|
|
|
ASSERT_TRUE(!iter->Valid());
|
2013-11-04 01:32:46 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:56:27 +01:00
|
|
|
std::cout << "non-existing Seek key comparison: \n"
|
|
|
|
<< hist_no_seek_comparison.ToString()
|
|
|
|
<< "non-existing Seek time: \n"
|
|
|
|
<< hist_no_seek_time.ToString();
|
2013-11-04 01:32:46 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-23 06:59:44 +02:00
|
|
|
|
2016-10-11 22:54:26 +02:00
|
|
|
TEST_F(PrefixTest, PrefixSeekModePrev) {
|
|
|
|
// Only for SkipListFactory
|
|
|
|
options.memtable_factory.reset(new SkipListFactory);
|
|
|
|
options.merge_operator = MergeOperators::CreatePutOperator();
|
|
|
|
options.write_buffer_size = 1024 * 1024;
|
|
|
|
Random rnd(1);
|
|
|
|
for (size_t m = 1; m < 100; m++) {
|
|
|
|
std::cout << "[" + std::to_string(m) + "]" + "*** Mem table: "
|
|
|
|
<< options.memtable_factory->Name() << std::endl;
|
|
|
|
DestroyDB(kDbName, Options());
|
|
|
|
auto db = OpenDb();
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
std::map<TestKey, std::string, TestKeyComparator> entry_maps[3], whole_map;
|
|
|
|
for (uint64_t i = 0; i < 10; i++) {
|
|
|
|
int div = i % 3 + 1;
|
|
|
|
for (uint64_t j = 0; j < 10; j++) {
|
|
|
|
whole_map[TestKey(i, j)] = entry_maps[rnd.Uniform(div)][TestKey(i, j)] =
|
|
|
|
'v' + std::to_string(i) + std::to_string(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<TestKey, std::string, TestKeyComparator> type_map;
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
|
|
|
for (auto& kv : entry_maps[i]) {
|
|
|
|
if (rnd.OneIn(3)) {
|
|
|
|
PutKey(db.get(), write_options, kv.first, kv.second);
|
|
|
|
type_map[kv.first] = "value";
|
|
|
|
} else {
|
|
|
|
MergeKey(db.get(), write_options, kv.first, kv.second);
|
|
|
|
type_map[kv.first] = "merge";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < 2) {
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 2; i++) {
|
|
|
|
for (auto& kv : entry_maps[i]) {
|
|
|
|
if (rnd.OneIn(10)) {
|
|
|
|
whole_map.erase(kv.first);
|
|
|
|
DeleteKey(db.get(), write_options, kv.first);
|
|
|
|
entry_maps[2][kv.first] = "delete";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAGS_enable_print) {
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
|
|
|
for (auto& kv : entry_maps[i]) {
|
|
|
|
std::cout << "[" << i << "]" << kv.first.prefix << kv.first.sorted
|
|
|
|
<< " " << kv.second + " " + type_map[kv.first] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(read_options));
|
|
|
|
for (uint64_t prefix = 0; prefix < 10; prefix++) {
|
|
|
|
uint64_t start_suffix = rnd.Uniform(9);
|
|
|
|
SeekIterator(iter.get(), prefix, start_suffix);
|
|
|
|
auto it = whole_map.find(TestKey(prefix, start_suffix));
|
|
|
|
if (it == whole_map.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT_NE(it, whole_map.end());
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
if (FLAGS_enable_print) {
|
|
|
|
std::cout << "round " << prefix
|
2017-04-22 05:41:37 +02:00
|
|
|
<< " iter: " << SliceToTestKey(iter->key()).prefix
|
|
|
|
<< SliceToTestKey(iter->key()).sorted
|
2016-10-11 22:54:26 +02:00
|
|
|
<< " | map: " << it->first.prefix << it->first.sorted << " | "
|
|
|
|
<< iter->value().ToString() << " " << it->second << std::endl;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(iter->value(), it->second);
|
|
|
|
uint64_t stored_prefix = prefix;
|
|
|
|
for (size_t k = 0; k < 9; k++) {
|
|
|
|
if (rnd.OneIn(2) || it == whole_map.begin()) {
|
|
|
|
iter->Next();
|
2019-05-15 22:14:18 +02:00
|
|
|
++it;
|
2016-10-11 22:54:26 +02:00
|
|
|
if (FLAGS_enable_print) {
|
|
|
|
std::cout << "Next >> ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iter->Prev();
|
|
|
|
it--;
|
|
|
|
if (FLAGS_enable_print) {
|
|
|
|
std::cout << "Prev >> ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!iter->Valid() ||
|
2017-04-22 05:41:37 +02:00
|
|
|
SliceToTestKey(iter->key()).prefix != stored_prefix) {
|
2016-10-11 22:54:26 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-04-22 05:41:37 +02:00
|
|
|
stored_prefix = SliceToTestKey(iter->key()).prefix;
|
2016-10-11 22:54:26 +02:00
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_NE(it, whole_map.end());
|
|
|
|
ASSERT_EQ(iter->value(), it->second);
|
|
|
|
if (FLAGS_enable_print) {
|
2017-04-22 05:41:37 +02:00
|
|
|
std::cout << "iter: " << SliceToTestKey(iter->key()).prefix
|
|
|
|
<< SliceToTestKey(iter->key()).sorted
|
2016-10-11 22:54:26 +02:00
|
|
|
<< " | map: " << it->first.prefix << it->first.sorted
|
|
|
|
<< " | " << iter->value().ToString() << " " << it->second
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 06:59:44 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 22:54:26 +02:00
|
|
|
TEST_F(PrefixTest, PrefixSeekModePrev2) {
|
|
|
|
// Only for SkipListFactory
|
|
|
|
// test the case
|
|
|
|
// iter1 iter2
|
|
|
|
// | prefix | suffix | | prefix | suffix |
|
|
|
|
// | 1 | 1 | | 1 | 2 |
|
|
|
|
// | 1 | 3 | | 1 | 4 |
|
|
|
|
// | 2 | 1 | | 3 | 3 |
|
|
|
|
// | 2 | 2 | | 3 | 4 |
|
|
|
|
// after seek(15), iter1 will be at 21 and iter2 will be 33.
|
|
|
|
// Then if call Prev() in prefix mode where SeekForPrev(21) gets called,
|
|
|
|
// iter2 should turn to invalid state because of bloom filter.
|
|
|
|
options.memtable_factory.reset(new SkipListFactory);
|
|
|
|
options.write_buffer_size = 1024 * 1024;
|
|
|
|
std::string v13("v13");
|
|
|
|
DestroyDB(kDbName, Options());
|
|
|
|
auto db = OpenDb();
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 2), "v12");
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 4), "v14");
|
|
|
|
PutKey(db.get(), write_options, TestKey(3, 3), "v33");
|
|
|
|
PutKey(db.get(), write_options, TestKey(3, 4), "v34");
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
reinterpret_cast<DBImpl*>(db.get())->TEST_WaitForFlushMemTable();
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 1), "v11");
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 3), "v13");
|
|
|
|
PutKey(db.get(), write_options, TestKey(2, 1), "v21");
|
|
|
|
PutKey(db.get(), write_options, TestKey(2, 2), "v22");
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
reinterpret_cast<DBImpl*>(db.get())->TEST_WaitForFlushMemTable();
|
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(read_options));
|
|
|
|
SeekIterator(iter.get(), 1, 5);
|
|
|
|
iter->Prev();
|
|
|
|
ASSERT_EQ(iter->value(), v13);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PrefixTest, PrefixSeekModePrev3) {
|
|
|
|
// Only for SkipListFactory
|
|
|
|
// test SeekToLast() with iterate_upper_bound_ in prefix_seek_mode
|
|
|
|
options.memtable_factory.reset(new SkipListFactory);
|
|
|
|
options.write_buffer_size = 1024 * 1024;
|
|
|
|
std::string v14("v14");
|
|
|
|
TestKey upper_bound_key = TestKey(1, 5);
|
2017-04-22 05:41:37 +02:00
|
|
|
std::string s;
|
|
|
|
Slice upper_bound = TestKeyToSlice(s, upper_bound_key);
|
2016-10-11 22:54:26 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
DestroyDB(kDbName, Options());
|
|
|
|
auto db = OpenDb();
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
read_options.iterate_upper_bound = &upper_bound;
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 2), "v12");
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 4), "v14");
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
reinterpret_cast<DBImpl*>(db.get())->TEST_WaitForFlushMemTable();
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 1), "v11");
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 3), "v13");
|
|
|
|
PutKey(db.get(), write_options, TestKey(2, 1), "v21");
|
|
|
|
PutKey(db.get(), write_options, TestKey(2, 2), "v22");
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
reinterpret_cast<DBImpl*>(db.get())->TEST_WaitForFlushMemTable();
|
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(read_options));
|
|
|
|
iter->SeekToLast();
|
|
|
|
ASSERT_EQ(iter->value(), v14);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
DestroyDB(kDbName, Options());
|
|
|
|
auto db = OpenDb();
|
|
|
|
WriteOptions write_options;
|
|
|
|
ReadOptions read_options;
|
|
|
|
read_options.iterate_upper_bound = &upper_bound;
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 2), "v12");
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 4), "v14");
|
|
|
|
PutKey(db.get(), write_options, TestKey(3, 3), "v33");
|
|
|
|
PutKey(db.get(), write_options, TestKey(3, 4), "v34");
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
reinterpret_cast<DBImpl*>(db.get())->TEST_WaitForFlushMemTable();
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 1), "v11");
|
|
|
|
PutKey(db.get(), write_options, TestKey(1, 3), "v13");
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
reinterpret_cast<DBImpl*>(db.get())->TEST_WaitForFlushMemTable();
|
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(read_options));
|
|
|
|
iter->SeekToLast();
|
|
|
|
ASSERT_EQ(iter->value(), v14);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace rocksdb
|
|
|
|
|
2013-10-23 06:59:44 +02:00
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
2014-05-09 02:25:13 +02:00
|
|
|
ParseCommandLineFlags(&argc, &argv, true);
|
2015-03-17 22:08:00 +01:00
|
|
|
return RUN_ALL_TESTS();
|
2013-10-23 06:59:44 +02:00
|
|
|
}
|
2014-05-09 02:25:13 +02:00
|
|
|
|
|
|
|
#endif // GFLAGS
|
2015-07-20 20:24:54 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-04-16 02:19:57 +02:00
|
|
|
int main(int /*argc*/, char** /*argv*/) {
|
2015-07-20 20:24:54 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"SKIPPED as HashSkipList and HashLinkList are not supported in "
|
|
|
|
"ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|