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
This commit is contained in:
Yu Zhang 2023-07-27 15:31:22 -07:00 committed by Facebook GitHub Bot
parent 63a5125a52
commit 5dd8c114bb
4 changed files with 39 additions and 9 deletions

View File

@ -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;

View File

@ -132,6 +132,9 @@ extern const Comparator* Uint64Comparator();
// A wrapper api for getting the ComparatorWithU64Ts<BytewiseComparator>
extern const Comparator* BytewiseComparatorWithU64TsWrapper();
// A wrapper api for getting the ComparatorWithU64Ts<ReverseBytewiseComparator>
extern const Comparator* ReverseBytewiseComparatorWithU64TsWrapper();
class StringSink : public FSWritableFile {
public:
std::string contents_;

View File

@ -316,24 +316,37 @@ const Comparator* BytewiseComparatorWithU64Ts() {
return &comp_with_u64_ts;
}
const Comparator* ReverseBytewiseComparatorWithU64Ts() {
STATIC_AVOID_DESTRUCTION(
ComparatorWithU64TsImpl<ReverseBytewiseComparatorImpl>, comp_with_u64_ts);
return &comp_with_u64_ts;
}
static int RegisterBuiltinComparators(ObjectLibrary& library,
const std::string& /*arg*/) {
library.AddFactory<const Comparator>(
BytewiseComparatorImpl::kClassName(),
[](const std::string& /*uri*/,
std::unique_ptr<const Comparator>* /*guard */,
std::string* /* errmsg */) { return BytewiseComparator(); });
std::unique_ptr<const Comparator>* /*guard*/,
std::string* /*errmsg*/) { return BytewiseComparator(); });
library.AddFactory<const Comparator>(
ReverseBytewiseComparatorImpl::kClassName(),
[](const std::string& /*uri*/,
std::unique_ptr<const Comparator>* /*guard */,
std::string* /* errmsg */) { return ReverseBytewiseComparator(); });
std::unique_ptr<const Comparator>* /*guard*/,
std::string* /*errmsg*/) { return ReverseBytewiseComparator(); });
library.AddFactory<const Comparator>(
ComparatorWithU64TsImpl<BytewiseComparatorImpl>::kClassName(),
[](const std::string& /*uri*/,
std::unique_ptr<const Comparator>* /*guard */,
std::string* /* errmsg */) { return BytewiseComparatorWithU64Ts(); });
return 3;
std::unique_ptr<const Comparator>* /*guard*/,
std::string* /*errmsg*/) { return BytewiseComparatorWithU64Ts(); });
library.AddFactory<const Comparator>(
ComparatorWithU64TsImpl<ReverseBytewiseComparatorImpl>::kClassName(),
[](const std::string& /*uri*/,
std::unique_ptr<const Comparator>* /*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<BytewiseComparatorImpl>::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;

View File

@ -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));