2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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).
|
2018-05-18 01:24:20 +00:00
|
|
|
#include <array>
|
2014-10-29 20:49:45 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2015-10-16 21:10:33 +00:00
|
|
|
#include "memtable/stl_wrappers.h"
|
2014-10-29 20:49:45 +00:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2014-10-29 23:17:28 +00:00
|
|
|
#include "util/hash.h"
|
2015-10-16 21:10:33 +00:00
|
|
|
#include "util/kv_map.h"
|
2020-07-09 21:33:42 +00:00
|
|
|
#include "util/random.h"
|
2015-03-20 00:29:37 +00:00
|
|
|
#include "util/string_util.h"
|
2014-10-29 20:49:45 +00:00
|
|
|
#include "utilities/merge_operators.h"
|
|
|
|
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-10-29 20:49:45 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-02-10 23:42:46 +00:00
|
|
|
static const Comparator* kTestComparator = nullptr;
|
2014-10-29 20:49:45 +00:00
|
|
|
|
|
|
|
class KVIter : public Iterator {
|
|
|
|
public:
|
2015-09-02 20:58:22 +00:00
|
|
|
explicit KVIter(const stl_wrappers::KVMap* map)
|
|
|
|
: map_(map), iter_(map_->end()) {}
|
2019-02-14 21:52:47 +00:00
|
|
|
bool Valid() const override { return iter_ != map_->end(); }
|
|
|
|
void SeekToFirst() override { iter_ = map_->begin(); }
|
|
|
|
void SeekToLast() override {
|
2014-10-29 20:49:45 +00:00
|
|
|
if (map_->empty()) {
|
|
|
|
iter_ = map_->end();
|
|
|
|
} else {
|
|
|
|
iter_ = map_->find(map_->rbegin()->first);
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void Seek(const Slice& k) override {
|
2015-02-26 19:28:41 +00:00
|
|
|
iter_ = map_->lower_bound(k.ToString());
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekForPrev(const Slice& k) override {
|
2016-09-28 01:20:57 +00:00
|
|
|
iter_ = map_->upper_bound(k.ToString());
|
|
|
|
Prev();
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void Next() override { ++iter_; }
|
|
|
|
void Prev() override {
|
2014-10-29 20:49:45 +00:00
|
|
|
if (iter_ == map_->begin()) {
|
|
|
|
iter_ = map_->end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
--iter_;
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
Slice key() const override { return iter_->first; }
|
|
|
|
Slice value() const override { return iter_->second; }
|
|
|
|
Status status() const override { return Status::OK(); }
|
2014-10-29 20:49:45 +00:00
|
|
|
|
|
|
|
private:
|
2015-09-02 20:58:22 +00:00
|
|
|
const stl_wrappers::KVMap* const map_;
|
|
|
|
stl_wrappers::KVMap::const_iterator iter_;
|
2014-10-29 20:49:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void AssertItersEqual(Iterator* iter1, Iterator* iter2) {
|
|
|
|
ASSERT_EQ(iter1->Valid(), iter2->Valid());
|
|
|
|
if (iter1->Valid()) {
|
|
|
|
ASSERT_EQ(iter1->key().ToString(), iter2->key().ToString());
|
|
|
|
ASSERT_EQ(iter1->value().ToString(), iter2->value().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measuring operations on DB (expect to be empty).
|
|
|
|
// source_strings are candidate keys
|
|
|
|
void DoRandomIteraratorTest(DB* db, std::vector<std::string> source_strings,
|
|
|
|
Random* rnd, int num_writes, int num_iter_ops,
|
|
|
|
int num_trigger_flush) {
|
2020-02-10 23:42:46 +00:00
|
|
|
stl_wrappers::KVMap map((stl_wrappers::LessOfComparator(kTestComparator)));
|
2014-10-29 20:49:45 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < num_writes; i++) {
|
|
|
|
if (num_trigger_flush > 0 && i != 0 && i % num_trigger_flush == 0) {
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
int type = rnd->Uniform(2);
|
2014-11-11 21:47:22 +00:00
|
|
|
int index = rnd->Uniform(static_cast<int>(source_strings.size()));
|
2014-10-29 20:49:45 +00:00
|
|
|
auto& key = source_strings[index];
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
// put
|
|
|
|
map[key] = key;
|
|
|
|
ASSERT_OK(db->Put(WriteOptions(), key, key));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// delete
|
|
|
|
if (map.find(key) != map.end()) {
|
|
|
|
map.erase(key);
|
|
|
|
}
|
|
|
|
ASSERT_OK(db->Delete(WriteOptions(), key));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Iterator> iter(db->NewIterator(ReadOptions()));
|
|
|
|
std::unique_ptr<Iterator> result_iter(new KVIter(&map));
|
|
|
|
|
|
|
|
bool is_valid = false;
|
|
|
|
for (int i = 0; i < num_iter_ops; i++) {
|
|
|
|
// Random walk and make sure iter and result_iter returns the
|
|
|
|
// same key and value
|
|
|
|
int type = rnd->Uniform(6);
|
|
|
|
ASSERT_OK(iter->status());
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
// Seek to First
|
|
|
|
iter->SeekToFirst();
|
|
|
|
result_iter->SeekToFirst();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Seek to last
|
|
|
|
iter->SeekToLast();
|
|
|
|
result_iter->SeekToLast();
|
|
|
|
break;
|
|
|
|
case 2: {
|
|
|
|
// Seek to random key
|
2014-11-11 21:47:22 +00:00
|
|
|
auto key_idx = rnd->Uniform(static_cast<int>(source_strings.size()));
|
2014-10-29 20:49:45 +00:00
|
|
|
auto key = source_strings[key_idx];
|
|
|
|
iter->Seek(key);
|
|
|
|
result_iter->Seek(key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
// Next
|
|
|
|
if (is_valid) {
|
|
|
|
iter->Next();
|
|
|
|
result_iter->Next();
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
// Prev
|
|
|
|
if (is_valid) {
|
|
|
|
iter->Prev();
|
|
|
|
result_iter->Prev();
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
assert(type == 5);
|
2014-11-11 21:47:22 +00:00
|
|
|
auto key_idx = rnd->Uniform(static_cast<int>(source_strings.size()));
|
2014-10-29 20:49:45 +00:00
|
|
|
auto key = source_strings[key_idx];
|
|
|
|
std::string result;
|
|
|
|
auto status = db->Get(ReadOptions(), key, &result);
|
|
|
|
if (map.find(key) == map.end()) {
|
|
|
|
ASSERT_TRUE(status.IsNotFound());
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(map[key], result);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AssertItersEqual(iter.get(), result_iter.get());
|
|
|
|
is_valid = iter->Valid();
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 23:17:28 +00:00
|
|
|
|
|
|
|
class DoubleComparator : public Comparator {
|
|
|
|
public:
|
|
|
|
DoubleComparator() {}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
const char* Name() const override { return "DoubleComparator"; }
|
2014-10-29 23:17:28 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
int Compare(const Slice& a, const Slice& b) const override {
|
2015-06-08 18:43:55 +00:00
|
|
|
#ifndef CYGWIN
|
2014-10-29 23:17:28 +00:00
|
|
|
double da = std::stod(a.ToString());
|
|
|
|
double db = std::stod(b.ToString());
|
2015-06-08 18:43:55 +00:00
|
|
|
#else
|
|
|
|
double da = std::strtod(a.ToString().c_str(), 0 /* endptr */);
|
|
|
|
double db = std::strtod(a.ToString().c_str(), 0 /* endptr */);
|
|
|
|
#endif
|
2014-10-29 23:17:28 +00:00
|
|
|
if (da == db) {
|
|
|
|
return a.compare(b);
|
|
|
|
} else if (da > db) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void FindShortestSeparator(std::string* /*start*/,
|
|
|
|
const Slice& /*limit*/) const override {}
|
2014-10-29 23:17:28 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
void FindShortSuccessor(std::string* /*key*/) const override {}
|
2014-10-29 23:17:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class HashComparator : public Comparator {
|
|
|
|
public:
|
|
|
|
HashComparator() {}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
const char* Name() const override { return "HashComparator"; }
|
2014-10-29 23:17:28 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
int Compare(const Slice& a, const Slice& b) const override {
|
2014-10-29 23:17:28 +00:00
|
|
|
uint32_t ha = Hash(a.data(), a.size(), 66);
|
|
|
|
uint32_t hb = Hash(b.data(), b.size(), 66);
|
|
|
|
if (ha == hb) {
|
|
|
|
return a.compare(b);
|
|
|
|
} else if (ha > hb) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void FindShortestSeparator(std::string* /*start*/,
|
|
|
|
const Slice& /*limit*/) const override {}
|
2014-10-29 23:17:28 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
void FindShortSuccessor(std::string* /*key*/) const override {}
|
2014-10-29 23:17:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TwoStrComparator : public Comparator {
|
|
|
|
public:
|
|
|
|
TwoStrComparator() {}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
const char* Name() const override { return "TwoStrComparator"; }
|
2014-10-29 23:17:28 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
int Compare(const Slice& a, const Slice& b) const override {
|
2014-10-29 23:17:28 +00:00
|
|
|
assert(a.size() >= 2);
|
|
|
|
assert(b.size() >= 2);
|
|
|
|
size_t size_a1 = static_cast<size_t>(a[0]);
|
|
|
|
size_t size_b1 = static_cast<size_t>(b[0]);
|
|
|
|
size_t size_a2 = static_cast<size_t>(a[1]);
|
|
|
|
size_t size_b2 = static_cast<size_t>(b[1]);
|
|
|
|
assert(size_a1 + size_a2 + 2 == a.size());
|
|
|
|
assert(size_b1 + size_b2 + 2 == b.size());
|
|
|
|
|
|
|
|
Slice a1 = Slice(a.data() + 2, size_a1);
|
|
|
|
Slice b1 = Slice(b.data() + 2, size_b1);
|
|
|
|
Slice a2 = Slice(a.data() + 2 + size_a1, size_a2);
|
|
|
|
Slice b2 = Slice(b.data() + 2 + size_b1, size_b2);
|
|
|
|
|
|
|
|
if (a1 != b1) {
|
|
|
|
return a1.compare(b1);
|
|
|
|
}
|
|
|
|
return a2.compare(b2);
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void FindShortestSeparator(std::string* /*start*/,
|
|
|
|
const Slice& /*limit*/) const override {}
|
2014-10-29 23:17:28 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
void FindShortSuccessor(std::string* /*key*/) const override {}
|
2014-10-29 23:17:28 +00:00
|
|
|
};
|
2014-10-29 20:49:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
class ComparatorDBTest
|
|
|
|
: public testing::Test,
|
|
|
|
virtual public ::testing::WithParamInterface<uint32_t> {
|
2014-10-29 20:49:45 +00:00
|
|
|
private:
|
|
|
|
std::string dbname_;
|
|
|
|
Env* env_;
|
|
|
|
DB* db_;
|
|
|
|
Options last_options_;
|
|
|
|
std::unique_ptr<const Comparator> comparator_guard;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ComparatorDBTest() : env_(Env::Default()), db_(nullptr) {
|
2020-02-10 23:42:46 +00:00
|
|
|
kTestComparator = BytewiseComparator();
|
2018-07-14 00:18:39 +00:00
|
|
|
dbname_ = test::PerThreadDBPath("comparator_db_test");
|
2018-06-05 02:59:44 +00:00
|
|
|
BlockBasedTableOptions toptions;
|
|
|
|
toptions.format_version = GetParam();
|
|
|
|
last_options_.table_factory.reset(
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::NewBlockBasedTableFactory(toptions));
|
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 03:52:32 +00:00
|
|
|
EXPECT_OK(DestroyDB(dbname_, last_options_));
|
2014-10-29 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~ComparatorDBTest() override {
|
2014-10-29 20:49:45 +00:00
|
|
|
delete 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 03:52:32 +00:00
|
|
|
EXPECT_OK(DestroyDB(dbname_, last_options_));
|
2020-02-10 23:42:46 +00:00
|
|
|
kTestComparator = BytewiseComparator();
|
2014-10-29 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DB* GetDB() { return db_; }
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
void SetOwnedComparator(const Comparator* cmp, bool owner = true) {
|
|
|
|
if (owner) {
|
|
|
|
comparator_guard.reset(cmp);
|
|
|
|
} else {
|
|
|
|
comparator_guard.reset();
|
|
|
|
}
|
2020-02-10 23:42:46 +00:00
|
|
|
kTestComparator = cmp;
|
2014-10-29 20:49:45 +00:00
|
|
|
last_options_.comparator = cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current option configuration.
|
|
|
|
Options* GetOptions() { return &last_options_; }
|
|
|
|
|
|
|
|
void DestroyAndReopen() {
|
|
|
|
// Destroy using last options
|
|
|
|
Destroy();
|
|
|
|
ASSERT_OK(TryReopen());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Destroy() {
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
ASSERT_OK(DestroyDB(dbname_, last_options_));
|
|
|
|
}
|
|
|
|
|
|
|
|
Status TryReopen() {
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
last_options_.create_if_missing = true;
|
|
|
|
|
|
|
|
return DB::Open(last_options_, dbname_, &db_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(FormatDef, ComparatorDBTest,
|
|
|
|
testing::Values(test::kDefaultFormatVersion));
|
|
|
|
INSTANTIATE_TEST_CASE_P(FormatLatest, ComparatorDBTest,
|
2021-12-10 16:12:09 +00:00
|
|
|
testing::Values(kLatestFormatVersion));
|
2018-06-05 02:59:44 +00:00
|
|
|
|
|
|
|
TEST_P(ComparatorDBTest, Bytewise) {
|
2014-10-29 20:49:45 +00:00
|
|
|
for (int rand_seed = 301; rand_seed < 306; rand_seed++) {
|
|
|
|
DestroyAndReopen();
|
|
|
|
Random rnd(rand_seed);
|
|
|
|
DoRandomIteraratorTest(GetDB(),
|
|
|
|
{"a", "b", "c", "d", "e", "f", "g", "h", "i"}, &rnd,
|
|
|
|
8, 100, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
TEST_P(ComparatorDBTest, SimpleSuffixReverseComparator) {
|
2014-10-29 20:49:45 +00:00
|
|
|
SetOwnedComparator(new test::SimpleSuffixReverseComparator());
|
|
|
|
|
|
|
|
for (int rnd_seed = 301; rnd_seed < 316; rnd_seed++) {
|
|
|
|
Options* opt = GetOptions();
|
2020-02-10 23:42:46 +00:00
|
|
|
opt->comparator = kTestComparator;
|
2014-10-29 20:49:45 +00:00
|
|
|
DestroyAndReopen();
|
|
|
|
Random rnd(rnd_seed);
|
|
|
|
|
|
|
|
std::vector<std::string> source_strings;
|
|
|
|
std::vector<std::string> source_prefixes;
|
|
|
|
// Randomly generate 5 prefixes
|
|
|
|
for (int i = 0; i < 5; i++) {
|
2020-07-09 21:33:42 +00:00
|
|
|
source_prefixes.push_back(rnd.HumanReadableString(8));
|
2014-10-29 20:49:45 +00:00
|
|
|
}
|
|
|
|
for (int j = 0; j < 20; j++) {
|
2014-11-11 21:47:22 +00:00
|
|
|
int prefix_index = rnd.Uniform(static_cast<int>(source_prefixes.size()));
|
2014-10-29 20:49:45 +00:00
|
|
|
std::string key = source_prefixes[prefix_index] +
|
2020-07-09 21:33:42 +00:00
|
|
|
rnd.HumanReadableString(rnd.Uniform(8));
|
2014-10-29 20:49:45 +00:00
|
|
|
source_strings.push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoRandomIteraratorTest(GetDB(), source_strings, &rnd, 30, 600, 66);
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 23:17:28 +00:00
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
TEST_P(ComparatorDBTest, Uint64Comparator) {
|
|
|
|
SetOwnedComparator(test::Uint64Comparator(), false /* owner */);
|
2014-10-29 23:17:28 +00:00
|
|
|
|
|
|
|
for (int rnd_seed = 301; rnd_seed < 316; rnd_seed++) {
|
|
|
|
Options* opt = GetOptions();
|
2020-02-10 23:42:46 +00:00
|
|
|
opt->comparator = kTestComparator;
|
2014-10-29 23:17:28 +00:00
|
|
|
DestroyAndReopen();
|
|
|
|
Random rnd(rnd_seed);
|
|
|
|
Random64 rnd64(rnd_seed);
|
|
|
|
|
|
|
|
std::vector<std::string> source_strings;
|
|
|
|
// Randomly generate source keys
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
uint64_t r = rnd64.Next();
|
|
|
|
std::string str;
|
|
|
|
str.resize(8);
|
|
|
|
memcpy(&str[0], static_cast<void*>(&r), 8);
|
|
|
|
source_strings.push_back(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoRandomIteraratorTest(GetDB(), source_strings, &rnd, 200, 1000, 66);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
TEST_P(ComparatorDBTest, DoubleComparator) {
|
2014-10-29 23:17:28 +00:00
|
|
|
SetOwnedComparator(new DoubleComparator());
|
|
|
|
|
|
|
|
for (int rnd_seed = 301; rnd_seed < 316; rnd_seed++) {
|
|
|
|
Options* opt = GetOptions();
|
2020-02-10 23:42:46 +00:00
|
|
|
opt->comparator = kTestComparator;
|
2014-10-29 23:17:28 +00:00
|
|
|
DestroyAndReopen();
|
|
|
|
Random rnd(rnd_seed);
|
|
|
|
|
|
|
|
std::vector<std::string> source_strings;
|
|
|
|
// Randomly generate source keys
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
uint32_t r = rnd.Next();
|
|
|
|
uint32_t divide_order = rnd.Uniform(8);
|
|
|
|
double to_divide = 1.0;
|
|
|
|
for (uint32_t j = 0; j < divide_order; j++) {
|
|
|
|
to_divide *= 10.0;
|
|
|
|
}
|
2022-05-06 20:03:58 +00:00
|
|
|
source_strings.push_back(std::to_string(r / to_divide));
|
2014-10-29 23:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DoRandomIteraratorTest(GetDB(), source_strings, &rnd, 200, 1000, 66);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
TEST_P(ComparatorDBTest, HashComparator) {
|
2014-10-29 23:17:28 +00:00
|
|
|
SetOwnedComparator(new HashComparator());
|
|
|
|
|
|
|
|
for (int rnd_seed = 301; rnd_seed < 316; rnd_seed++) {
|
|
|
|
Options* opt = GetOptions();
|
2020-02-10 23:42:46 +00:00
|
|
|
opt->comparator = kTestComparator;
|
2014-10-29 23:17:28 +00:00
|
|
|
DestroyAndReopen();
|
|
|
|
Random rnd(rnd_seed);
|
|
|
|
|
|
|
|
std::vector<std::string> source_strings;
|
|
|
|
// Randomly generate source keys
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
source_strings.push_back(test::RandomKey(&rnd, 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
DoRandomIteraratorTest(GetDB(), source_strings, &rnd, 200, 1000, 66);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
TEST_P(ComparatorDBTest, TwoStrComparator) {
|
2014-10-29 23:17:28 +00:00
|
|
|
SetOwnedComparator(new TwoStrComparator());
|
|
|
|
|
|
|
|
for (int rnd_seed = 301; rnd_seed < 316; rnd_seed++) {
|
|
|
|
Options* opt = GetOptions();
|
2020-02-10 23:42:46 +00:00
|
|
|
opt->comparator = kTestComparator;
|
2014-10-29 23:17:28 +00:00
|
|
|
DestroyAndReopen();
|
|
|
|
Random rnd(rnd_seed);
|
|
|
|
|
|
|
|
std::vector<std::string> source_strings;
|
|
|
|
// Randomly generate source keys
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
std::string str;
|
|
|
|
uint32_t size1 = rnd.Uniform(8);
|
|
|
|
uint32_t size2 = rnd.Uniform(8);
|
|
|
|
str.append(1, static_cast<char>(size1));
|
|
|
|
str.append(1, static_cast<char>(size2));
|
|
|
|
str.append(test::RandomKey(&rnd, size1));
|
|
|
|
str.append(test::RandomKey(&rnd, size2));
|
|
|
|
source_strings.push_back(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoRandomIteraratorTest(GetDB(), source_strings, &rnd, 200, 1000, 66);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
namespace {
|
|
|
|
void VerifyNotSuccessor(const Slice& s, const Slice& t) {
|
|
|
|
auto bc = BytewiseComparator();
|
|
|
|
auto rbc = ReverseBytewiseComparator();
|
|
|
|
ASSERT_FALSE(bc->IsSameLengthImmediateSuccessor(s, t));
|
|
|
|
ASSERT_FALSE(rbc->IsSameLengthImmediateSuccessor(s, t));
|
|
|
|
ASSERT_FALSE(bc->IsSameLengthImmediateSuccessor(t, s));
|
|
|
|
ASSERT_FALSE(rbc->IsSameLengthImmediateSuccessor(t, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerifySuccessor(const Slice& s, const Slice& t) {
|
|
|
|
auto bc = BytewiseComparator();
|
|
|
|
auto rbc = ReverseBytewiseComparator();
|
|
|
|
ASSERT_TRUE(bc->IsSameLengthImmediateSuccessor(s, t));
|
|
|
|
ASSERT_FALSE(rbc->IsSameLengthImmediateSuccessor(s, t));
|
|
|
|
ASSERT_FALSE(bc->IsSameLengthImmediateSuccessor(t, s));
|
|
|
|
// Should be true but that increases exposure to a design bug in
|
|
|
|
// auto_prefix_mode, so currently set to FALSE
|
|
|
|
ASSERT_FALSE(rbc->IsSameLengthImmediateSuccessor(t, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-06-26 22:56:26 +00:00
|
|
|
TEST_P(ComparatorDBTest, IsSameLengthImmediateSuccessor) {
|
|
|
|
{
|
|
|
|
// different length
|
|
|
|
Slice s("abcxy");
|
|
|
|
Slice t("abcxyz");
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifyNotSuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Slice s("abcxyz");
|
|
|
|
Slice t("abcxy");
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifyNotSuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// not last byte different
|
|
|
|
Slice s("abc1xyz");
|
|
|
|
Slice t("abc2xyz");
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifyNotSuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// same string
|
|
|
|
Slice s("abcxyz");
|
|
|
|
Slice t("abcxyz");
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifyNotSuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Slice s("abcxy");
|
|
|
|
Slice t("abcxz");
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifySuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char s_array[] = "\x50\x8a\xac";
|
|
|
|
const char t_array[] = "\x50\x8a\xad";
|
|
|
|
Slice s(s_array);
|
|
|
|
Slice t(t_array);
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifySuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char s_array[] = "\x50\x8a\xff";
|
|
|
|
const char t_array[] = "\x50\x8b\x00";
|
|
|
|
Slice s(s_array, 3);
|
|
|
|
Slice t(t_array, 3);
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifySuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char s_array[] = "\x50\x8a\xff\xff";
|
|
|
|
const char t_array[] = "\x50\x8b\x00\x00";
|
|
|
|
Slice s(s_array, 4);
|
|
|
|
Slice t(t_array, 4);
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifySuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char s_array[] = "\x50\x8a\xff\xff";
|
|
|
|
const char t_array[] = "\x50\x8b\x00\x01";
|
|
|
|
Slice s(s_array, 4);
|
|
|
|
Slice t(t_array, 4);
|
Document design/specification bugs with auto_prefix_mode (#10144)
Summary:
auto_prefix_mode is designed to use prefix filtering in a
particular "safe" set of cases where the upper bound and the seek key
have different prefixes: where the upper bound is the "same length
immediate successor". These conditions are not sufficient to guarantee
the same iteration results as total_order_seek if the DB contains
"short" keys, less than the "full" (maximum) prefix length.
We are not simply disabling the optimization in these successor cases
because it is likely that users are essentially getting what they want
out of existing usage. Especially if users are constructing successor
bounds with the intention of doing a prefix-bounded seek, the existing
behavior is more expected than the total_order_seek behavior.
Consequently, for now we reconcile the bad specification of behavior by
documenting the existing mismatch with total_order_seek.
A closely related issue affects hypothetical comparators like
ReverseBytewiseComparator: if they "correctly" implement
IsSameLengthImmediateSuccessor, auto_prefix_mode could omit more
entries (other than "short" keys noted above). Luckily, the built-in
ReverseBytewiseComparator has an "incorrect" implementation of
IsSameLengthImmediateSuccessor that effectively prevents prefix
optimization and, thus, the bug. This is now documented as a new
constraint on IsSameLengthImmediateSuccessor, and the implementation
tweaked to be simply "safe" rather than "incorrect".
This change also includes unit test updates to demonstrate the above
issues. (Test was cleaned up for readability and simplicity.)
Intended follow-up:
* Tweak documented axioms for prefix_extractor (more details then)
* Consider some sort of fix for this case. I don't know what that would
look like without breaking the performance of existing code. Perhaps
if all keys in an SST file have prefixes that are "full length," we can track
that fact and use it to allow optimization with the "same length
immediate successor", but that would only apply to new files.
* Consider a better system of specifying prefix bounds
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10144
Test Plan: test updates included
Reviewed By: siying
Differential Revision: D37052710
Pulled By: pdillinger
fbshipit-source-id: 5f63b7d65f3f214e4b143e0f9aa1749527c587db
2022-06-13 18:08:50 +00:00
|
|
|
VerifyNotSuccessor(s, t);
|
2018-06-26 22:56:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
TEST_P(ComparatorDBTest, FindShortestSeparator) {
|
2018-05-18 01:24:20 +00:00
|
|
|
std::string s1 = "abc1xyz";
|
|
|
|
std::string s2 = "abc3xy";
|
|
|
|
|
|
|
|
BytewiseComparator()->FindShortestSeparator(&s1, s2);
|
|
|
|
ASSERT_EQ("abc2", s1);
|
|
|
|
|
|
|
|
s1 = "abc5xyztt";
|
|
|
|
|
|
|
|
ReverseBytewiseComparator()->FindShortestSeparator(&s1, s2);
|
|
|
|
ASSERT_EQ("abc5", s1);
|
|
|
|
|
|
|
|
s1 = "abc3";
|
|
|
|
s2 = "abc2xy";
|
|
|
|
ReverseBytewiseComparator()->FindShortestSeparator(&s1, s2);
|
|
|
|
ASSERT_EQ("abc3", s1);
|
|
|
|
|
|
|
|
s1 = "abc3xyz";
|
|
|
|
s2 = "abc2xy";
|
|
|
|
ReverseBytewiseComparator()->FindShortestSeparator(&s1, s2);
|
|
|
|
ASSERT_EQ("abc3", s1);
|
|
|
|
|
|
|
|
s1 = "abc3xyz";
|
|
|
|
s2 = "abc2";
|
|
|
|
ReverseBytewiseComparator()->FindShortestSeparator(&s1, s2);
|
|
|
|
ASSERT_EQ("abc3", s1);
|
|
|
|
|
|
|
|
std::string old_s1 = s1 = "abc2xy";
|
|
|
|
s2 = "abc2";
|
|
|
|
ReverseBytewiseComparator()->FindShortestSeparator(&s1, s2);
|
|
|
|
ASSERT_TRUE(old_s1 >= s1);
|
|
|
|
ASSERT_TRUE(s1 > s2);
|
|
|
|
}
|
|
|
|
|
2018-06-05 02:59:44 +00:00
|
|
|
TEST_P(ComparatorDBTest, SeparatorSuccessorRandomizeTest) {
|
2018-05-18 01:24:20 +00:00
|
|
|
// Char list for boundary cases.
|
|
|
|
std::array<unsigned char, 6> char_list{{0, 1, 2, 253, 254, 255}};
|
|
|
|
Random rnd(301);
|
|
|
|
|
|
|
|
for (int attempts = 0; attempts < 1000; attempts++) {
|
|
|
|
uint32_t size1 = rnd.Skewed(4);
|
|
|
|
uint32_t size2;
|
|
|
|
|
|
|
|
if (rnd.OneIn(2)) {
|
|
|
|
// size2 to be random size
|
|
|
|
size2 = rnd.Skewed(4);
|
|
|
|
} else {
|
|
|
|
// size1 is within [-2, +2] of size1
|
|
|
|
int diff = static_cast<int>(rnd.Uniform(5)) - 2;
|
|
|
|
int tmp_size2 = static_cast<int>(size1) + diff;
|
|
|
|
if (tmp_size2 < 0) {
|
|
|
|
tmp_size2 = 0;
|
|
|
|
}
|
|
|
|
size2 = static_cast<uint32_t>(tmp_size2);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string s1;
|
|
|
|
std::string s2;
|
|
|
|
for (uint32_t i = 0; i < size1; i++) {
|
|
|
|
if (rnd.OneIn(2)) {
|
|
|
|
// Use random byte
|
|
|
|
s1 += static_cast<char>(rnd.Uniform(256));
|
|
|
|
} else {
|
|
|
|
// Use one byte in char_list
|
|
|
|
char c = static_cast<char>(char_list[rnd.Uniform(sizeof(char_list))]);
|
|
|
|
s1 += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First set s2 to be the same as s1, and then modify s2.
|
|
|
|
s2 = s1;
|
|
|
|
s2.resize(size2);
|
|
|
|
// We start from the back of the string
|
|
|
|
if (size2 > 0) {
|
|
|
|
uint32_t pos = size2 - 1;
|
|
|
|
do {
|
|
|
|
if (pos >= size1 || rnd.OneIn(4)) {
|
|
|
|
// For 1/4 chance, use random byte
|
|
|
|
s2[pos] = static_cast<char>(rnd.Uniform(256));
|
|
|
|
} else if (rnd.OneIn(4)) {
|
|
|
|
// In 1/4 chance, stop here.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Create a char within [-2, +2] of the matching char of s1.
|
|
|
|
int diff = static_cast<int>(rnd.Uniform(5)) - 2;
|
|
|
|
// char may be signed or unsigned based on platform.
|
|
|
|
int s1_char = static_cast<int>(static_cast<unsigned char>(s1[pos]));
|
|
|
|
int s2_char = s1_char + diff;
|
|
|
|
if (s2_char < 0) {
|
|
|
|
s2_char = 0;
|
|
|
|
}
|
|
|
|
if (s2_char > 255) {
|
|
|
|
s2_char = 255;
|
|
|
|
}
|
|
|
|
s2[pos] = static_cast<char>(s2_char);
|
|
|
|
}
|
|
|
|
} while (pos-- != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test separators
|
|
|
|
for (int rev = 0; rev < 2; rev++) {
|
|
|
|
if (rev == 1) {
|
|
|
|
// switch s1 and s2
|
|
|
|
std::string t = s1;
|
|
|
|
s1 = s2;
|
|
|
|
s2 = t;
|
|
|
|
}
|
|
|
|
std::string separator = s1;
|
|
|
|
BytewiseComparator()->FindShortestSeparator(&separator, s2);
|
|
|
|
std::string rev_separator = s1;
|
|
|
|
ReverseBytewiseComparator()->FindShortestSeparator(&rev_separator, s2);
|
|
|
|
|
|
|
|
if (s1 == s2) {
|
|
|
|
ASSERT_EQ(s1, separator);
|
|
|
|
ASSERT_EQ(s2, rev_separator);
|
|
|
|
} else if (s1 < s2) {
|
|
|
|
ASSERT_TRUE(s1 <= separator);
|
|
|
|
ASSERT_TRUE(s2 > separator);
|
|
|
|
ASSERT_LE(separator.size(), std::max(s1.size(), s2.size()));
|
|
|
|
ASSERT_EQ(s1, rev_separator);
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(s1 >= rev_separator);
|
|
|
|
ASSERT_TRUE(s2 < rev_separator);
|
|
|
|
ASSERT_LE(rev_separator.size(), std::max(s1.size(), s2.size()));
|
|
|
|
ASSERT_EQ(s1, separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test successors
|
|
|
|
std::string succ = s1;
|
|
|
|
BytewiseComparator()->FindShortSuccessor(&succ);
|
|
|
|
ASSERT_TRUE(succ >= s1);
|
|
|
|
|
|
|
|
succ = s1;
|
|
|
|
ReverseBytewiseComparator()->FindShortSuccessor(&succ);
|
|
|
|
ASSERT_TRUE(succ <= s1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-10-29 20:49:45 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|