From 5dd8c114bb3316686ef8fb99d1e456f4cc193766 Mon Sep 17 00:00:00 2001 From: Yu Zhang Date: Thu, 27 Jul 2023 15:31:22 -0700 Subject: [PATCH] Add a UDT comparator for ReverseBytewiseComparator to object library (#11647) Summary: Add a built-in comparator that supports uint64_t style user-defined timestamps for ReverseBytewiseComparator. Pull Request resolved: https://github.com/facebook/rocksdb/pull/11647 Test Plan: Added a test wrapper for retrieving this comparator from registry and used it in this test: `./udt_util_test` Reviewed By: ltamasi Differential Revision: D47848303 Pulled By: jowlyzhang fbshipit-source-id: 5af5534a8c2d9195997d0308c8e194c1c797548c --- test_util/testutil.cc | 10 ++++++++++ test_util/testutil.h | 3 +++ util/comparator.cc | 30 +++++++++++++++++++++++------- util/udt_util_test.cc | 5 +++-- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/test_util/testutil.cc b/test_util/testutil.cc index d958cb0cd..b128b797a 100644 --- a/test_util/testutil.cc +++ b/test_util/testutil.cc @@ -150,6 +150,16 @@ const Comparator* BytewiseComparatorWithU64TsWrapper() { return user_comparator; } +const Comparator* ReverseBytewiseComparatorWithU64TsWrapper() { + ConfigOptions config_options; + const Comparator* user_comparator = nullptr; + Status s = Comparator::CreateFromString( + config_options, "rocksdb.ReverseBytewiseComparator.u64ts", + &user_comparator); + s.PermitUncheckedError(); + return user_comparator; +} + void CorruptKeyType(InternalKey* ikey) { std::string keystr = ikey->Encode().ToString(); keystr[keystr.size() - 8] = kTypeLogData; diff --git a/test_util/testutil.h b/test_util/testutil.h index c40fcdcb0..eca1ff794 100644 --- a/test_util/testutil.h +++ b/test_util/testutil.h @@ -132,6 +132,9 @@ extern const Comparator* Uint64Comparator(); // A wrapper api for getting the ComparatorWithU64Ts extern const Comparator* BytewiseComparatorWithU64TsWrapper(); +// A wrapper api for getting the ComparatorWithU64Ts +extern const Comparator* ReverseBytewiseComparatorWithU64TsWrapper(); + class StringSink : public FSWritableFile { public: std::string contents_; diff --git a/util/comparator.cc b/util/comparator.cc index 19fd47387..d0ff1f7aa 100644 --- a/util/comparator.cc +++ b/util/comparator.cc @@ -316,24 +316,37 @@ const Comparator* BytewiseComparatorWithU64Ts() { return &comp_with_u64_ts; } +const Comparator* ReverseBytewiseComparatorWithU64Ts() { + STATIC_AVOID_DESTRUCTION( + ComparatorWithU64TsImpl, comp_with_u64_ts); + return &comp_with_u64_ts; +} + static int RegisterBuiltinComparators(ObjectLibrary& library, const std::string& /*arg*/) { library.AddFactory( BytewiseComparatorImpl::kClassName(), [](const std::string& /*uri*/, - std::unique_ptr* /*guard */, - std::string* /* errmsg */) { return BytewiseComparator(); }); + std::unique_ptr* /*guard*/, + std::string* /*errmsg*/) { return BytewiseComparator(); }); library.AddFactory( ReverseBytewiseComparatorImpl::kClassName(), [](const std::string& /*uri*/, - std::unique_ptr* /*guard */, - std::string* /* errmsg */) { return ReverseBytewiseComparator(); }); + std::unique_ptr* /*guard*/, + std::string* /*errmsg*/) { return ReverseBytewiseComparator(); }); library.AddFactory( ComparatorWithU64TsImpl::kClassName(), [](const std::string& /*uri*/, - std::unique_ptr* /*guard */, - std::string* /* errmsg */) { return BytewiseComparatorWithU64Ts(); }); - return 3; + std::unique_ptr* /*guard*/, + std::string* /*errmsg*/) { return BytewiseComparatorWithU64Ts(); }); + library.AddFactory( + ComparatorWithU64TsImpl::kClassName(), + [](const std::string& /*uri*/, + std::unique_ptr* /*guard*/, + std::string* /*errmsg*/) { + return ReverseBytewiseComparatorWithU64Ts(); + }); + return 4; } Status Comparator::CreateFromString(const ConfigOptions& config_options, @@ -357,6 +370,9 @@ Status Comparator::CreateFromString(const ConfigOptions& config_options, } else if (id == ComparatorWithU64TsImpl::kClassName()) { *result = BytewiseComparatorWithU64Ts(); + } else if (id == ComparatorWithU64TsImpl< + ReverseBytewiseComparatorImpl>::kClassName()) { + *result = ReverseBytewiseComparatorWithU64Ts(); } else if (value.empty()) { // No Id and no options. Clear the object *result = nullptr; diff --git a/util/udt_util_test.cc b/util/udt_util_test.cc index 9fcbc9bc7..47e1edf34 100644 --- a/util/udt_util_test.cc +++ b/util/udt_util_test.cc @@ -356,8 +356,9 @@ TEST(ValidateUserDefinedTimestampsOptionsTest, TEST(ValidateUserDefinedTimestampsOptionsTest, DisableUserDefinedTimestamps) { bool mark_sst_files = false; - const Comparator* new_comparator = BytewiseComparator(); - const Comparator* old_comparator = test::BytewiseComparatorWithU64TsWrapper(); + const Comparator* new_comparator = ReverseBytewiseComparator(); + const Comparator* old_comparator = + test::ReverseBytewiseComparatorWithU64TsWrapper(); ASSERT_OK(ValidateUserDefinedTimestampsOptions( new_comparator, std::string(old_comparator->Name()), false /*new_persist_udt*/, false /*old_persist_udt*/, &mark_sst_files));