// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
-// This source code is licensed under the BSD-style license found in the
-// LICENSE file in the root directory of this source tree. An additional grant
-// of patent rights can be found in the PATENTS file in the same directory.
+// 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).
//
#include <assert.h>
#include <memory>
#include "utilities/merge_operators.h"
#include "util/testharness.h"
-using namespace rocksdb;
+namespace rocksdb {
+
+bool use_compression;
+
+class MergeTest : public testing::Test {};
-namespace {
size_t num_merge_operator_calls;
void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; }
size_t num_partial_merge_calls;
void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; }
-}
class CountMergeOperator : public AssociativeMergeOperator {
public:
std::shared_ptr<MergeOperator> mergeOperator_;
};
-namespace {
std::shared_ptr<DB> OpenDb(const std::string& dbname, const bool ttl = false,
const size_t max_successive_merges = 0) {
DB* db;
// DBWithTTL is not supported in ROCKSDB_LITE
#ifndef ROCKSDB_LITE
if (ttl) {
- std::cout << "Opening database with TTL\n";
DBWithTTL* db_with_ttl;
s = DBWithTTL::Open(options, dbname, &db_with_ttl);
db = db_with_ttl;
}
return std::shared_ptr<DB>(db);
}
-} // namespace
// Imagine we are maintaining a set of uint64 counters.
// Each counter has a distinct name. And we would like
}
};
-namespace {
void dumpDb(DB* db) {
auto it = unique_ptr<Iterator>(db->NewIterator(ReadOptions()));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
- uint64_t value = DecodeFixed64(it->value().data());
- std::cout << it->key().ToString() << ": " << value << std::endl;
+ //uint64_t value = DecodeFixed64(it->value().data());
+ //std::cout << it->key().ToString() << ": " << value << std::endl;
}
assert(it->status().ok()); // Check for any errors found during the scan
}
dumpDb(db);
- std::cout << "1\n";
-
// 1+...+49 = ?
uint64_t sum = 0;
for (int i = 1; i < 50; i++) {
}
assert(counters.assert_get("b") == sum);
- std::cout << "2\n";
dumpDb(db);
- std::cout << "3\n";
-
if (test_compaction) {
db->Flush(o);
- std::cout << "Compaction started ...\n";
db->CompactRange(CompactRangeOptions(), nullptr, nullptr);
- std::cout << "Compaction ended\n";
dumpDb(db);
static_cast<size_t>((num_merges % (max_num_merges + 1))));
}
-void runTest(int argc, const std::string& dbname, const bool use_ttl = false) {
- bool compact = false;
- if (argc > 1) {
- compact = true;
- std::cout << "Turn on Compaction\n";
- }
+void runTest(const std::string& dbname, const bool use_ttl = false) {
{
auto db = OpenDb(dbname, use_ttl);
{
- std::cout << "Test read-modify-write counters... \n";
Counters counters(db, 0);
testCounters(counters, db.get(), true);
}
{
- std::cout << "Test merge-based counters... \n";
MergeBasedCounters counters(db, 0);
- testCounters(counters, db.get(), compact);
+ testCounters(counters, db.get(), use_compression);
}
}
DestroyDB(dbname, Options());
{
- std::cout << "Test merge in memtable... \n";
size_t max_merge = 5;
auto db = OpenDb(dbname, use_ttl, max_merge);
MergeBasedCounters counters(db, 0);
- testCounters(counters, db.get(), compact);
+ testCounters(counters, db.get(), use_compression);
testSuccessiveMerge(counters, max_merge, max_merge * 2);
testSingleBatchSuccessiveMerge(db.get(), 5, 7);
DestroyDB(dbname, Options());
}
{
- std::cout << "Test Partial-Merge\n";
size_t max_merge = 100;
// Min merge is hard-coded to 2.
uint32_t min_merge = 2;
}
{
- std::cout << "Test merge-operator not set after reopen\n";
{
auto db = OpenDb(dbname);
MergeBasedCounters counters(db, 0);
}
*/
}
-} // namespace
-int main(int argc, char *argv[]) {
- //TODO: Make this test like a general rocksdb unit-test
- rocksdb::port::InstallStackTraceHandler();
- runTest(argc, test::TmpDir() + "/merge_testdb");
-// DBWithTTL is not supported in ROCKSDB_LITE
+TEST_F(MergeTest, MergeDbTest) {
+ runTest(test::PerThreadDBPath("merge_testdb"));
+}
+
#ifndef ROCKSDB_LITE
- runTest(argc, test::TmpDir() + "/merge_testdbttl", true); // Run test on TTL database
+TEST_F(MergeTest, MergeDbTtlTest) {
+ runTest(test::PerThreadDBPath("merge_testdbttl"),
+ true); // Run test on TTL database
+}
#endif // !ROCKSDB_LITE
- printf("Passed all tests!\n");
- return 0;
+
+} // namespace rocksdb
+
+int main(int argc, char** argv) {
+ rocksdb::use_compression = false;
+ if (argc > 1) {
+ rocksdb::use_compression = true;
+ }
+
+ rocksdb::port::InstallStackTraceHandler();
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
}