]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/rocksdb/db/merge_test.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / db / merge_test.cc
index dddbb4e228d7dea6d3236b46f5f0e29b65eab58c..15f6f5125a02610df712fe8b97ee07bda03f1b99 100644 (file)
@@ -1,7 +1,7 @@
 //  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:
@@ -74,7 +76,6 @@ class CountMergeOperator : public AssociativeMergeOperator {
   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;
@@ -87,7 +88,6 @@ std::shared_ptr<DB> OpenDb(const std::string& dbname, const bool ttl = false,
 // 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;
@@ -104,7 +104,6 @@ std::shared_ptr<DB> OpenDb(const std::string& dbname, const bool ttl = false,
   }
   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
@@ -246,12 +245,11 @@ class MergeBasedCounters : public Counters {
   }
 };
 
-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
 }
@@ -281,8 +279,6 @@ void testCounters(Counters& counters, DB* db, bool test_compaction) {
 
   dumpDb(db);
 
-  std::cout << "1\n";
-
   // 1+...+49 = ?
   uint64_t sum = 0;
   for (int i = 1; i < 50; i++) {
@@ -291,17 +287,12 @@ void testCounters(Counters& counters, DB* db, bool test_compaction) {
   }
   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);
 
@@ -411,44 +402,35 @@ void testSingleBatchSuccessiveMerge(DB* db, size_t max_num_merges,
             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;
@@ -468,7 +450,6 @@ void runTest(int argc, const std::string& dbname, const bool use_ttl = false) {
   }
 
   {
-    std::cout << "Test merge-operator not set after reopen\n";
     {
       auto db = OpenDb(dbname);
       MergeBasedCounters counters(db, 0);
@@ -502,16 +483,27 @@ void runTest(int argc, const std::string& dbname, const bool use_ttl = false) {
   }
   */
 }
-}  // 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();
 }