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-09-09 07:24:40 +02:00
|
|
|
|
|
|
|
#include <string>
|
2020-07-09 23:33:42 +02:00
|
|
|
#include <vector>
|
2014-09-09 07:24:40 +02:00
|
|
|
|
2017-02-03 01:38:40 +01:00
|
|
|
#include "table/merging_iterator.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "util/random.h"
|
2014-09-09 07:24:40 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-09-09 07:24:40 +02:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class MergerTest : public testing::Test {
|
2014-09-09 07:24:40 +02:00
|
|
|
public:
|
|
|
|
MergerTest()
|
2017-09-11 20:53:22 +02:00
|
|
|
: icomp_(BytewiseComparator()),
|
|
|
|
rnd_(3),
|
|
|
|
merging_iterator_(nullptr),
|
|
|
|
single_iterator_(nullptr) {}
|
2019-02-14 22:52:47 +01:00
|
|
|
~MergerTest() override = default;
|
2014-11-11 22:47:22 +01:00
|
|
|
std::vector<std::string> GenerateStrings(size_t len, int string_len) {
|
2014-09-09 07:24:40 +02:00
|
|
|
std::vector<std::string> ret;
|
2017-09-11 20:53:22 +02:00
|
|
|
|
2014-11-11 22:47:22 +01:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2020-07-09 23:33:42 +02:00
|
|
|
InternalKey ik(rnd_.HumanReadableString(string_len), 0,
|
2017-09-11 20:53:22 +02:00
|
|
|
ValueType::kTypeValue);
|
|
|
|
ret.push_back(ik.Encode().ToString(false));
|
2014-09-09 07:24:40 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssertEquivalence() {
|
|
|
|
auto a = merging_iterator_.get();
|
|
|
|
auto b = single_iterator_.get();
|
|
|
|
if (!a->Valid()) {
|
|
|
|
ASSERT_TRUE(!b->Valid());
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(b->Valid());
|
|
|
|
ASSERT_EQ(b->key().ToString(), a->key().ToString());
|
|
|
|
ASSERT_EQ(b->value().ToString(), a->value().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 20:53:22 +02:00
|
|
|
void SeekToRandom() {
|
2020-07-09 23:33:42 +02:00
|
|
|
InternalKey ik(rnd_.HumanReadableString(5), 0, ValueType::kTypeValue);
|
2017-09-11 20:53:22 +02:00
|
|
|
Seek(ik.Encode().ToString(false));
|
|
|
|
}
|
2014-09-09 07:24:40 +02:00
|
|
|
|
|
|
|
void Seek(std::string target) {
|
|
|
|
merging_iterator_->Seek(target);
|
|
|
|
single_iterator_->Seek(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeekToFirst() {
|
|
|
|
merging_iterator_->SeekToFirst();
|
|
|
|
single_iterator_->SeekToFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeekToLast() {
|
|
|
|
merging_iterator_->SeekToLast();
|
|
|
|
single_iterator_->SeekToLast();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Next(int times) {
|
|
|
|
for (int i = 0; i < times && merging_iterator_->Valid(); ++i) {
|
|
|
|
AssertEquivalence();
|
|
|
|
merging_iterator_->Next();
|
|
|
|
single_iterator_->Next();
|
|
|
|
}
|
|
|
|
AssertEquivalence();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prev(int times) {
|
|
|
|
for (int i = 0; i < times && merging_iterator_->Valid(); ++i) {
|
|
|
|
AssertEquivalence();
|
|
|
|
merging_iterator_->Prev();
|
|
|
|
single_iterator_->Prev();
|
|
|
|
}
|
|
|
|
AssertEquivalence();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NextAndPrev(int times) {
|
|
|
|
for (int i = 0; i < times && merging_iterator_->Valid(); ++i) {
|
|
|
|
AssertEquivalence();
|
|
|
|
if (rnd_.OneIn(2)) {
|
|
|
|
merging_iterator_->Prev();
|
|
|
|
single_iterator_->Prev();
|
|
|
|
} else {
|
|
|
|
merging_iterator_->Next();
|
|
|
|
single_iterator_->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AssertEquivalence();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Generate(size_t num_iterators, size_t strings_per_iterator,
|
2014-11-11 22:47:22 +01:00
|
|
|
int letters_per_string) {
|
2015-10-13 00:06:38 +02:00
|
|
|
std::vector<InternalIterator*> small_iterators;
|
2014-09-09 07:24:40 +02:00
|
|
|
for (size_t i = 0; i < num_iterators; ++i) {
|
|
|
|
auto strings = GenerateStrings(strings_per_iterator, letters_per_string);
|
2015-07-17 18:27:24 +02:00
|
|
|
small_iterators.push_back(new test::VectorIterator(strings));
|
2014-09-09 07:24:40 +02:00
|
|
|
all_keys_.insert(all_keys_.end(), strings.begin(), strings.end());
|
|
|
|
}
|
|
|
|
|
2014-11-11 22:47:22 +01:00
|
|
|
merging_iterator_.reset(
|
2017-09-11 20:53:22 +02:00
|
|
|
NewMergingIterator(&icomp_, &small_iterators[0],
|
2014-11-11 22:47:22 +01:00
|
|
|
static_cast<int>(small_iterators.size())));
|
2015-07-17 18:27:24 +02:00
|
|
|
single_iterator_.reset(new test::VectorIterator(all_keys_));
|
2014-09-09 07:24:40 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 20:53:22 +02:00
|
|
|
InternalKeyComparator icomp_;
|
2014-09-09 07:24:40 +02:00
|
|
|
Random rnd_;
|
2015-10-13 00:06:38 +02:00
|
|
|
std::unique_ptr<InternalIterator> merging_iterator_;
|
|
|
|
std::unique_ptr<InternalIterator> single_iterator_;
|
2014-09-09 07:24:40 +02:00
|
|
|
std::vector<std::string> all_keys_;
|
|
|
|
};
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(MergerTest, SeekToRandomNextTest) {
|
2014-09-09 07:24:40 +02:00
|
|
|
Generate(1000, 50, 50);
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
SeekToRandom();
|
|
|
|
AssertEquivalence();
|
|
|
|
Next(50000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(MergerTest, SeekToRandomNextSmallStringsTest) {
|
2014-09-09 07:24:40 +02:00
|
|
|
Generate(1000, 50, 2);
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
SeekToRandom();
|
|
|
|
AssertEquivalence();
|
|
|
|
Next(50000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(MergerTest, SeekToRandomPrevTest) {
|
2014-09-09 07:24:40 +02:00
|
|
|
Generate(1000, 50, 50);
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
SeekToRandom();
|
|
|
|
AssertEquivalence();
|
|
|
|
Prev(50000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(MergerTest, SeekToRandomRandomTest) {
|
2014-09-09 07:24:40 +02:00
|
|
|
Generate(200, 50, 50);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
SeekToRandom();
|
|
|
|
AssertEquivalence();
|
|
|
|
NextAndPrev(5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(MergerTest, SeekToFirstTest) {
|
2014-09-09 07:24:40 +02:00
|
|
|
Generate(1000, 50, 50);
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
SeekToFirst();
|
|
|
|
AssertEquivalence();
|
|
|
|
Next(50000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(MergerTest, SeekToLastTest) {
|
2014-09-09 07:24:40 +02:00
|
|
|
Generate(1000, 50, 50);
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
SeekToLast();
|
|
|
|
AssertEquivalence();
|
|
|
|
Prev(50000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-09-09 07:24:40 +02:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|