2020-06-04 21:14:51 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
//
|
|
|
|
|
2013-05-10 17:40:10 +00:00
|
|
|
/**
|
|
|
|
* An persistent map : key -> (list of strings), using rocksdb merge.
|
|
|
|
* This file is a test-harness / use-case for the StringAppendOperator.
|
|
|
|
*
|
|
|
|
* @author Deon Nicholas (dnicholas@fb.com)
|
|
|
|
* Copyright 2013 Facebook, Inc.
|
2022-10-24 23:38:09 +00:00
|
|
|
*/
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
#include "utilities/merge_operators/string_append/stringappend.h"
|
|
|
|
|
2013-05-10 17:40:10 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2020-06-04 21:14:51 +00:00
|
|
|
#include <tuple>
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
#include "port/stack_trace.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/merge_operator.h"
|
2014-07-23 14:21:38 +00:00
|
|
|
#include "rocksdb/utilities/db_ttl.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "util/random.h"
|
2013-05-10 17:40:10 +00:00
|
|
|
#include "utilities/merge_operators.h"
|
2013-08-20 20:35:28 +00:00
|
|
|
#include "utilities/merge_operators/string_append/stringappend2.h"
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2013-05-10 17:40:10 +00:00
|
|
|
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 03:14:32 +00:00
|
|
|
// Path to the database on file system
|
2018-07-14 00:18:39 +00:00
|
|
|
const std::string kDbName = test::PerThreadDBPath("stringappend_test");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2014-04-10 04:17:14 +00:00
|
|
|
namespace {
|
2013-05-10 17:40:10 +00:00
|
|
|
// OpenDb opens a (possibly new) rocksdb database with a StringAppendOperator
|
2021-08-02 23:49:54 +00:00
|
|
|
std::shared_ptr<DB> OpenNormalDb(const std::string& delim) {
|
2013-08-20 20:35:28 +00:00
|
|
|
DB* db;
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
2021-08-02 23:49:54 +00:00
|
|
|
MergeOperator* mergeOperator;
|
|
|
|
if (delim.size() == 1) {
|
|
|
|
mergeOperator = new StringAppendOperator(delim[0]);
|
|
|
|
} else {
|
|
|
|
mergeOperator = new StringAppendOperator(delim);
|
|
|
|
}
|
|
|
|
options.merge_operator.reset(mergeOperator);
|
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(DB::Open(options, kDbName, &db));
|
2013-08-20 20:35:28 +00:00
|
|
|
return std::shared_ptr<DB>(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a TtlDB with a non-associative StringAppendTESTOperator
|
2021-08-02 23:49:54 +00:00
|
|
|
std::shared_ptr<DB> OpenTtlDb(const std::string& delim) {
|
2014-04-29 03:44:33 +00:00
|
|
|
DBWithTTL* db;
|
2013-08-20 20:35:28 +00:00
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
2021-08-02 23:49:54 +00:00
|
|
|
MergeOperator* mergeOperator;
|
|
|
|
if (delim.size() == 1) {
|
|
|
|
mergeOperator = new StringAppendTESTOperator(delim[0]);
|
|
|
|
} else {
|
|
|
|
mergeOperator = new StringAppendTESTOperator(delim);
|
|
|
|
}
|
|
|
|
options.merge_operator.reset(mergeOperator);
|
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(DBWithTTL::Open(options, kDbName, &db, 123456));
|
2013-08-20 20:35:28 +00:00
|
|
|
return std::shared_ptr<DB>(db);
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
2014-04-10 04:17:14 +00:00
|
|
|
} // namespace
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
/// StringLists represents a set of string-lists, each with a key-index.
|
2013-08-20 20:35:28 +00:00
|
|
|
/// Supports Append(list, string) and Get(list)
|
2013-05-10 17:40:10 +00:00
|
|
|
class StringLists {
|
|
|
|
public:
|
2022-10-24 23:38:09 +00:00
|
|
|
// Constructor: specifies the rocksdb db
|
2013-12-07 00:10:43 +00:00
|
|
|
/* implicit */
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists(std::shared_ptr<DB> db)
|
2022-10-24 23:38:09 +00:00
|
|
|
: db_(db), merge_option_(), get_option_() {
|
2013-05-10 17:40:10 +00:00
|
|
|
assert(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append string val onto the list defined by key; return true on success
|
2022-10-24 23:38:09 +00:00
|
|
|
bool Append(const std::string& key, const std::string& val) {
|
2013-08-20 20:35:28 +00:00
|
|
|
Slice valSlice(val.data(), val.size());
|
|
|
|
auto s = db_->Merge(merge_option_, key, valSlice);
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
std::cerr << "ERROR " << s.ToString() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the list of strings associated with key (or "" if does not exist)
|
2022-10-24 23:38:09 +00:00
|
|
|
bool Get(const std::string& key, std::string* const result) {
|
|
|
|
assert(result != nullptr); // we should have a place to store the result
|
2013-05-10 17:40:10 +00:00
|
|
|
auto s = db_->Get(get_option_, key, result);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either key does not exist, or there is some error.
|
2022-10-24 23:38:09 +00:00
|
|
|
*result = ""; // Always return empty string (just for convention)
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
// NotFound is okay; just return empty (similar to std::map)
|
|
|
|
// But network or db errors, etc, should fail the test (or at least yell)
|
2013-06-14 23:44:39 +00:00
|
|
|
if (!s.IsNotFound()) {
|
2013-05-10 17:40:10 +00:00
|
|
|
std::cerr << "ERROR " << s.ToString() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always return false if s.ok() was not true
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<DB> db_;
|
|
|
|
WriteOptions merge_option_;
|
|
|
|
ReadOptions get_option_;
|
|
|
|
};
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
// The class for unit-testing
|
2020-06-04 21:14:51 +00:00
|
|
|
class StringAppendOperatorTest : public testing::Test,
|
|
|
|
public ::testing::WithParamInterface<bool> {
|
2013-08-20 20:35:28 +00:00
|
|
|
public:
|
|
|
|
StringAppendOperatorTest() {
|
2020-12-24 00:54:05 +00:00
|
|
|
EXPECT_OK(
|
|
|
|
DestroyDB(kDbName, Options())); // Start each test with a fresh DB
|
2013-08-20 20:35:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
void SetUp() override {
|
|
|
|
bool if_use_ttl = GetParam();
|
|
|
|
if (if_use_ttl) {
|
|
|
|
fprintf(stderr, "Running tests with ttl db and generic operator.\n");
|
|
|
|
StringAppendOperatorTest::SetOpenDbFunction(&OpenTtlDb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Running tests with regular db and operator.\n");
|
|
|
|
StringAppendOperatorTest::SetOpenDbFunction(&OpenNormalDb);
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:31:12 +00:00
|
|
|
using OpenFuncPtr = std::shared_ptr<DB> (*)(const std::string&);
|
2013-08-20 20:35:28 +00:00
|
|
|
|
|
|
|
// Allows user to open databases with different configurations.
|
|
|
|
// e.g.: Can open a DB or a TtlDB, etc.
|
2022-10-24 23:38:09 +00:00
|
|
|
static void SetOpenDbFunction(OpenFuncPtr func) { OpenDb = func; }
|
2013-08-20 20:35:28 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static OpenFuncPtr OpenDb;
|
|
|
|
};
|
2022-10-24 23:38:09 +00:00
|
|
|
StringAppendOperatorTest::OpenFuncPtr StringAppendOperatorTest::OpenDb =
|
|
|
|
nullptr;
|
2013-08-20 20:35:28 +00:00
|
|
|
|
2013-05-10 17:40:10 +00:00
|
|
|
// THE TEST CASES BEGIN HERE
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, IteratorTest) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db_ = OpenDb(",");
|
2013-08-19 18:42:47 +00:00
|
|
|
StringLists slists(db_);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("k1", "v1");
|
|
|
|
slists.Append("k1", "v2");
|
|
|
|
slists.Append("k1", "v3");
|
2013-08-19 18:42:47 +00:00
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("k2", "a1");
|
|
|
|
slists.Append("k2", "a2");
|
|
|
|
slists.Append("k2", "a3");
|
2013-08-19 18:42:47 +00:00
|
|
|
|
|
|
|
std::string res;
|
2020-02-20 20:07:53 +00:00
|
|
|
std::unique_ptr<ROCKSDB_NAMESPACE::Iterator> it(
|
|
|
|
db_->NewIterator(ReadOptions()));
|
2013-08-19 18:42:47 +00:00
|
|
|
std::string k1("k1");
|
|
|
|
std::string k2("k2");
|
|
|
|
bool first = true;
|
|
|
|
for (it->Seek(k1); it->Valid(); it->Next()) {
|
|
|
|
res = it->value().ToString();
|
|
|
|
if (first) {
|
|
|
|
ASSERT_EQ(res, "v1,v2,v3");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(res, "a1,a2,a3");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
slists.Append("k2", "a4");
|
|
|
|
slists.Append("k1", "v4");
|
|
|
|
|
|
|
|
// Snapshot should still be the same. Should ignore a4 and v4.
|
|
|
|
first = true;
|
|
|
|
for (it->Seek(k1); it->Valid(); it->Next()) {
|
|
|
|
res = it->value().ToString();
|
|
|
|
if (first) {
|
|
|
|
ASSERT_EQ(res, "v1,v2,v3");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(res, "a1,a2,a3");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should release the snapshot and be aware of the new stuff now
|
|
|
|
it.reset(db_->NewIterator(ReadOptions()));
|
|
|
|
first = true;
|
|
|
|
for (it->Seek(k1); it->Valid(); it->Next()) {
|
|
|
|
res = it->value().ToString();
|
|
|
|
if (first) {
|
|
|
|
ASSERT_EQ(res, "v1,v2,v3,v4");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(res, "a1,a2,a3,a4");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start from k2 this time.
|
|
|
|
for (it->Seek(k2); it->Valid(); it->Next()) {
|
|
|
|
res = it->value().ToString();
|
|
|
|
if (first) {
|
|
|
|
ASSERT_EQ(res, "v1,v2,v3,v4");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(res, "a1,a2,a3,a4");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("k3", "g1");
|
2013-08-19 18:42:47 +00:00
|
|
|
|
|
|
|
it.reset(db_->NewIterator(ReadOptions()));
|
|
|
|
first = true;
|
|
|
|
std::string k3("k3");
|
2022-10-24 23:38:09 +00:00
|
|
|
for (it->Seek(k2); it->Valid(); it->Next()) {
|
2013-08-19 18:42:47 +00:00
|
|
|
res = it->value().ToString();
|
|
|
|
if (first) {
|
|
|
|
ASSERT_EQ(res, "a1,a2,a3,a4");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(res, "g1");
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 23:38:09 +00:00
|
|
|
for (it->Seek(k3); it->Valid(); it->Next()) {
|
2013-08-19 18:42:47 +00:00
|
|
|
res = it->value().ToString();
|
|
|
|
if (first) {
|
|
|
|
// should not be hit
|
|
|
|
ASSERT_EQ(res, "a1,a2,a3,a4");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(res, "g1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, SimpleTest) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb(",");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("k1", "v1");
|
|
|
|
slists.Append("k1", "v2");
|
|
|
|
slists.Append("k1", "v3");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
std::string res;
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("k1", &res));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(res, "v1,v2,v3");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, SimpleDelimiterTest) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("|");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("k1", "v1");
|
|
|
|
slists.Append("k1", "v2");
|
|
|
|
slists.Append("k1", "v3");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
std::string res;
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("k1", &res));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(res, "v1|v2|v3");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 23:49:54 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, EmptyDelimiterTest) {
|
|
|
|
auto db = OpenDb("");
|
|
|
|
StringLists slists(db);
|
|
|
|
|
|
|
|
slists.Append("k1", "v1");
|
|
|
|
slists.Append("k1", "v2");
|
|
|
|
slists.Append("k1", "v3");
|
|
|
|
|
|
|
|
std::string res;
|
|
|
|
ASSERT_TRUE(slists.Get("k1", &res));
|
|
|
|
ASSERT_EQ(res, "v1v2v3");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(StringAppendOperatorTest, MultiCharDelimiterTest) {
|
|
|
|
auto db = OpenDb("<>");
|
|
|
|
StringLists slists(db);
|
|
|
|
|
|
|
|
slists.Append("k1", "v1");
|
|
|
|
slists.Append("k1", "v2");
|
|
|
|
slists.Append("k1", "v3");
|
|
|
|
|
|
|
|
std::string res;
|
|
|
|
ASSERT_TRUE(slists.Get("k1", &res));
|
|
|
|
ASSERT_EQ(res, "v1<>v2<>v3");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(StringAppendOperatorTest, DelimiterIsDefensivelyCopiedTest) {
|
|
|
|
std::string delimiter = "<>";
|
|
|
|
auto db = OpenDb(delimiter);
|
|
|
|
StringLists slists(db);
|
|
|
|
|
|
|
|
slists.Append("k1", "v1");
|
|
|
|
slists.Append("k1", "v2");
|
|
|
|
delimiter.clear();
|
|
|
|
slists.Append("k1", "v3");
|
|
|
|
|
|
|
|
std::string res;
|
|
|
|
ASSERT_TRUE(slists.Get("k1", &res));
|
|
|
|
ASSERT_EQ(res, "v1<>v2<>v3");
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, OneValueNoDelimiterTest) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("!");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("random_key", "single_val");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
std::string res;
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("random_key", &res));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(res, "single_val");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, VariousKeys) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("\n");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("c", "asdasd");
|
|
|
|
slists.Append("a", "x");
|
|
|
|
slists.Append("b", "y");
|
|
|
|
slists.Append("a", "t");
|
|
|
|
slists.Append("a", "r");
|
|
|
|
slists.Append("b", "2");
|
|
|
|
slists.Append("c", "asdasd");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
std::string a, b, c;
|
|
|
|
bool sa, sb, sc;
|
|
|
|
sa = slists.Get("a", &a);
|
|
|
|
sb = slists.Get("b", &b);
|
|
|
|
sc = slists.Get("c", &c);
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
ASSERT_TRUE(sa && sb && sc); // All three keys should have been found
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(a, "x\nt\nr");
|
|
|
|
ASSERT_EQ(b, "y\n2");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate semi random keys/words from a small distribution.
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, RandomMixGetAppend) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb(" ");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
|
|
|
// Generate a list of random keys and values
|
|
|
|
const int kWordCount = 15;
|
2022-10-24 23:38:09 +00:00
|
|
|
std::string words[] = {"sdasd", "triejf", "fnjsdfn", "dfjisdfsf",
|
|
|
|
"342839", "dsuha", "mabuais", "sadajsid",
|
|
|
|
"jf9834hf", "2d9j89", "dj9823jd", "a",
|
|
|
|
"dk02ed2dh", "$(jd4h984$(*", "mabz"};
|
2013-05-10 17:40:10 +00:00
|
|
|
const int kKeyCount = 6;
|
2022-10-24 23:38:09 +00:00
|
|
|
std::string keys[] = {"dhaiusdhu", "denidw", "daisda",
|
|
|
|
"keykey", "muki", "shzassdianmd"};
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Will store a local copy of all data in order to verify correctness
|
2013-08-20 20:35:28 +00:00
|
|
|
std::map<std::string, std::string> parallel_copy;
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Generate a bunch of random queries (Append and Get)!
|
2022-10-24 23:38:09 +00:00
|
|
|
enum query_t { APPEND_OP, GET_OP, NUM_OPS };
|
|
|
|
Random randomGen(1337); // deterministic seed; always get same results!
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
const int kNumQueries = 30;
|
2022-10-24 23:38:09 +00:00
|
|
|
for (int q = 0; q < kNumQueries; ++q) {
|
2013-05-10 17:40:10 +00:00
|
|
|
// Generate a random query (Append or Get) and random parameters
|
|
|
|
query_t query = (query_t)randomGen.Uniform((int)NUM_OPS);
|
|
|
|
std::string key = keys[randomGen.Uniform((int)kKeyCount)];
|
|
|
|
std::string word = words[randomGen.Uniform((int)kWordCount)];
|
|
|
|
|
|
|
|
// Apply the query and any checks.
|
|
|
|
if (query == APPEND_OP) {
|
|
|
|
// Apply the rocksdb test-harness Append defined above
|
2022-10-24 23:38:09 +00:00
|
|
|
slists.Append(key, word); // apply the rocksdb append
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Apply the similar "Append" to the parallel copy
|
|
|
|
if (parallel_copy[key].size() > 0) {
|
|
|
|
parallel_copy[key] += " " + word;
|
|
|
|
} else {
|
|
|
|
parallel_copy[key] = word;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (query == GET_OP) {
|
|
|
|
// Assumes that a non-existent key just returns <empty>
|
|
|
|
std::string res;
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Get(key, &res);
|
|
|
|
ASSERT_EQ(res, parallel_copy[key]);
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, BIGRandomMixGetAppend) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb(" ");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
|
|
|
// Generate a list of random keys and values
|
|
|
|
const int kWordCount = 15;
|
2022-10-24 23:38:09 +00:00
|
|
|
std::string words[] = {"sdasd", "triejf", "fnjsdfn", "dfjisdfsf",
|
|
|
|
"342839", "dsuha", "mabuais", "sadajsid",
|
|
|
|
"jf9834hf", "2d9j89", "dj9823jd", "a",
|
|
|
|
"dk02ed2dh", "$(jd4h984$(*", "mabz"};
|
2013-05-10 17:40:10 +00:00
|
|
|
const int kKeyCount = 6;
|
2022-10-24 23:38:09 +00:00
|
|
|
std::string keys[] = {"dhaiusdhu", "denidw", "daisda",
|
|
|
|
"keykey", "muki", "shzassdianmd"};
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Will store a local copy of all data in order to verify correctness
|
2013-08-20 20:35:28 +00:00
|
|
|
std::map<std::string, std::string> parallel_copy;
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Generate a bunch of random queries (Append and Get)!
|
2022-10-24 23:38:09 +00:00
|
|
|
enum query_t { APPEND_OP, GET_OP, NUM_OPS };
|
|
|
|
Random randomGen(9138204); // deterministic seed
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
const int kNumQueries = 1000;
|
2022-10-24 23:38:09 +00:00
|
|
|
for (int q = 0; q < kNumQueries; ++q) {
|
2013-05-10 17:40:10 +00:00
|
|
|
// Generate a random query (Append or Get) and random parameters
|
|
|
|
query_t query = (query_t)randomGen.Uniform((int)NUM_OPS);
|
|
|
|
std::string key = keys[randomGen.Uniform((int)kKeyCount)];
|
|
|
|
std::string word = words[randomGen.Uniform((int)kWordCount)];
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
// Apply the query and any checks.
|
2013-05-10 17:40:10 +00:00
|
|
|
if (query == APPEND_OP) {
|
|
|
|
// Apply the rocksdb test-harness Append defined above
|
2022-10-24 23:38:09 +00:00
|
|
|
slists.Append(key, word); // apply the rocksdb append
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Apply the similar "Append" to the parallel copy
|
|
|
|
if (parallel_copy[key].size() > 0) {
|
|
|
|
parallel_copy[key] += " " + word;
|
|
|
|
} else {
|
|
|
|
parallel_copy[key] = word;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (query == GET_OP) {
|
|
|
|
// Assumes that a non-existent key just returns <empty>
|
|
|
|
std::string res;
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Get(key, &res);
|
|
|
|
ASSERT_EQ(res, parallel_copy[key]);
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, PersistentVariousKeys) {
|
2013-05-10 17:40:10 +00:00
|
|
|
// Perform the following operations in limited scope
|
|
|
|
{
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("\n");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("c", "asdasd");
|
|
|
|
slists.Append("a", "x");
|
|
|
|
slists.Append("b", "y");
|
|
|
|
slists.Append("a", "t");
|
|
|
|
slists.Append("a", "r");
|
|
|
|
slists.Append("b", "2");
|
|
|
|
slists.Append("c", "asdasd");
|
|
|
|
|
|
|
|
std::string a, b, c;
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
|
|
|
ASSERT_TRUE(slists.Get("c", &c));
|
2013-08-20 20:35:28 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(a, "x\nt\nr");
|
|
|
|
ASSERT_EQ(b, "y\n2");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reopen the database (the previous changes should persist / be remembered)
|
|
|
|
{
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("\n");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("c", "bbnagnagsx");
|
|
|
|
slists.Append("a", "sa");
|
|
|
|
slists.Append("b", "df");
|
|
|
|
slists.Append("a", "gh");
|
|
|
|
slists.Append("a", "jk");
|
|
|
|
slists.Append("b", "l;");
|
|
|
|
slists.Append("c", "rogosh");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2013-06-26 18:41:13 +00:00
|
|
|
// The previous changes should be on disk (L0)
|
|
|
|
// The most recent changes should be in memory (MemTable)
|
|
|
|
// Hence, this will test both Get() paths.
|
2013-08-20 20:35:28 +00:00
|
|
|
std::string a, b, c;
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
|
|
|
ASSERT_TRUE(slists.Get("c", &c));
|
2013-08-20 20:35:28 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(a, "x\nt\nr\nsa\ngh\njk");
|
|
|
|
ASSERT_EQ(b, "y\n2\ndf\nl;");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd\nbbnagnagsx\nrogosh");
|
2013-06-26 18:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reopen the database (the previous changes should persist / be remembered)
|
|
|
|
{
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("\n");
|
2013-06-26 18:41:13 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
|
|
|
// All changes should be on disk. This will test VersionSet Get()
|
2013-08-20 20:35:28 +00:00
|
|
|
std::string a, b, c;
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
|
|
|
ASSERT_TRUE(slists.Get("c", &c));
|
2013-08-20 20:35:28 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(a, "x\nt\nr\nsa\ngh\njk");
|
|
|
|
ASSERT_EQ(b, "y\n2\ndf\nl;");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd\nbbnagnagsx\nrogosh");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, PersistentFlushAndCompaction) {
|
2013-05-10 17:40:10 +00:00
|
|
|
// Perform the following operations in limited scope
|
|
|
|
{
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("\n");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
2013-08-20 20:35:28 +00:00
|
|
|
std::string a, b, c;
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Append, Flush, Get
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("c", "asdasd");
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_OK(db->Flush(ROCKSDB_NAMESPACE::FlushOptions()));
|
|
|
|
ASSERT_TRUE(slists.Get("c", &c));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(c, "asdasd");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Append, Flush, Append, Get
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("a", "x");
|
|
|
|
slists.Append("b", "y");
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_OK(db->Flush(ROCKSDB_NAMESPACE::FlushOptions()));
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("a", "t");
|
|
|
|
slists.Append("a", "r");
|
|
|
|
slists.Append("b", "2");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(a, "x\nt\nr");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(b, "y\n2");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Append, Get
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Append("c", "asdasd"));
|
|
|
|
ASSERT_TRUE(slists.Append("b", "monkey"));
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
|
|
|
ASSERT_TRUE(slists.Get("c", &c));
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(a, "x\nt\nr");
|
|
|
|
ASSERT_EQ(b, "y\n2\nmonkey");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reopen the database (the previous changes should persist / be remembered)
|
|
|
|
{
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb("\n");
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
2013-08-20 20:35:28 +00:00
|
|
|
std::string a, b, c;
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Get (Quick check for persistence of previous database)
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(a, "x\nt\nr");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
// Append, Compact, Get
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("c", "bbnagnagsx");
|
|
|
|
slists.Append("a", "sa");
|
|
|
|
slists.Append("b", "df");
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
|
|
|
ASSERT_TRUE(slists.Get("c", &c));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(a, "x\nt\nr\nsa");
|
|
|
|
ASSERT_EQ(b, "y\n2\nmonkey\ndf");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd\nbbnagnagsx");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Append, Get
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("a", "gh");
|
|
|
|
slists.Append("a", "jk");
|
|
|
|
slists.Append("b", "l;");
|
|
|
|
slists.Append("c", "rogosh");
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("a", &a));
|
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
|
|
|
ASSERT_TRUE(slists.Get("c", &c));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(a, "x\nt\nr\nsa\ngh\njk");
|
|
|
|
ASSERT_EQ(b, "y\n2\nmonkey\ndf\nl;");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd\nbbnagnagsx\nrogosh");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Compact, Get
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(a, "x\nt\nr\nsa\ngh\njk");
|
|
|
|
ASSERT_EQ(b, "y\n2\nmonkey\ndf\nl;");
|
|
|
|
ASSERT_EQ(c, "asdasd\nasdasd\nbbnagnagsx\nrogosh");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Append, Flush, Compact, Get
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("b", "afcg");
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_OK(db->Flush(ROCKSDB_NAMESPACE::FlushOptions()));
|
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
|
|
|
ASSERT_TRUE(slists.Get("b", &b));
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(b, "y\n2\nmonkey\ndf\nl;\nafcg");
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
TEST_P(StringAppendOperatorTest, SimpleTestNullDelimiter) {
|
2021-08-02 23:49:54 +00:00
|
|
|
auto db = OpenDb(std::string(1, '\0'));
|
2013-05-10 17:40:10 +00:00
|
|
|
StringLists slists(db);
|
|
|
|
|
2013-08-20 20:35:28 +00:00
|
|
|
slists.Append("k1", "v1");
|
|
|
|
slists.Append("k1", "v2");
|
|
|
|
slists.Append("k1", "v3");
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
std::string res;
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_TRUE(slists.Get("k1", &res));
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Construct the desired string. Default constructor doesn't like '\0' chars.
|
2022-10-24 23:38:09 +00:00
|
|
|
std::string checker("v1,v2,v3"); // Verify that the string is right size.
|
|
|
|
checker[2] = '\0'; // Use null delimiter instead of comma.
|
2013-05-10 17:40:10 +00:00
|
|
|
checker[5] = '\0';
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_EQ(checker.size(), 8); // Verify it is still the correct size
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
// Check that the rocksdb result string matches the desired string
|
2020-12-22 23:08:17 +00:00
|
|
|
ASSERT_EQ(res.size(), checker.size());
|
2013-08-20 20:35:28 +00:00
|
|
|
ASSERT_EQ(res, checker);
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:51 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(StringAppendOperatorTest, StringAppendOperatorTest,
|
|
|
|
testing::Bool());
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2013-05-10 17:40:10 +00:00
|
|
|
|
2015-03-17 01:08:59 +00:00
|
|
|
int main(int argc, char** argv) {
|
2020-06-04 21:14:51 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
2020-06-04 21:14:51 +00:00
|
|
|
return RUN_ALL_TESTS();
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|