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