diff --git a/db/column_family.cc b/db/column_family.cc index 732a4cf29..482b7111e 100644 --- a/db/column_family.cc +++ b/db/column_family.cc @@ -81,7 +81,7 @@ Status ColumnFamilyHandleImpl::GetDescriptor(ColumnFamilyDescriptor* desc) { #endif // !ROCKSDB_LITE } -const Comparator* ColumnFamilyHandleImpl::user_comparator() const { +const Comparator* ColumnFamilyHandleImpl::GetComparator() const { return cfd()->user_comparator(); } @@ -1007,8 +1007,7 @@ uint32_t GetColumnFamilyID(ColumnFamilyHandle* column_family) { const Comparator* GetColumnFamilyUserComparator( ColumnFamilyHandle* column_family) { if (column_family != nullptr) { - auto cfh = reinterpret_cast(column_family); - return cfh->user_comparator(); + return column_family->GetComparator(); } return nullptr; } diff --git a/db/column_family.h b/db/column_family.h index 43f249b25..8fb4d6a96 100644 --- a/db/column_family.h +++ b/db/column_family.h @@ -55,11 +55,11 @@ class ColumnFamilyHandleImpl : public ColumnFamilyHandle { // destroy without mutex virtual ~ColumnFamilyHandleImpl(); virtual ColumnFamilyData* cfd() const { return cfd_; } - virtual const Comparator* user_comparator() const; virtual uint32_t GetID() const override; virtual const std::string& GetName() const override; virtual Status GetDescriptor(ColumnFamilyDescriptor* desc) override; + virtual const Comparator* GetComparator() const override; private: ColumnFamilyData* cfd_; diff --git a/db/column_family_test.cc b/db/column_family_test.cc index c3f273739..aa00d59e5 100644 --- a/db/column_family_test.cc +++ b/db/column_family_test.cc @@ -1072,6 +1072,39 @@ TEST_F(ColumnFamilyTest, MemtableNotSupportSnapshot) { } #endif // !ROCKSDB_LITE +class TestComparator : public Comparator { + int Compare(const rocksdb::Slice& a, const rocksdb::Slice& b) const override { + return 0; + } + const char* Name() const override { return "Test"; } + void FindShortestSeparator(std::string* start, + const rocksdb::Slice& limit) const override {} + void FindShortSuccessor(std::string* key) const override {} +}; + +static TestComparator third_comparator; +static TestComparator fourth_comparator; + +// Test that we can retrieve the comparator from a created CF +TEST_F(ColumnFamilyTest, GetComparator) { + Open(); + // Add a column family with no comparator specified + CreateColumnFamilies({"first"}); + const Comparator* comp = handles_[0]->GetComparator(); + ASSERT_EQ(comp, BytewiseComparator()); + + // Add three column families - one with no comparator and two + // with comparators specified + ColumnFamilyOptions second, third, fourth; + second.comparator = &third_comparator; + third.comparator = &fourth_comparator; + CreateColumnFamilies({"second", "third", "fourth"}, {second, third, fourth}); + ASSERT_EQ(handles_[1]->GetComparator(), BytewiseComparator()); + ASSERT_EQ(handles_[2]->GetComparator(), &third_comparator); + ASSERT_EQ(handles_[3]->GetComparator(), &fourth_comparator); + Close(); +} + TEST_F(ColumnFamilyTest, DifferentMergeOperators) { Open(); CreateColumnFamilies({"first", "second"}); diff --git a/db/write_batch_test.cc b/db/write_batch_test.cc index cc351a5a6..32ed5089c 100644 --- a/db/write_batch_test.cc +++ b/db/write_batch_test.cc @@ -546,7 +546,7 @@ class ColumnFamilyHandleImplDummy : public ColumnFamilyHandleImpl { explicit ColumnFamilyHandleImplDummy(int id) : ColumnFamilyHandleImpl(nullptr, nullptr, nullptr), id_(id) {} uint32_t GetID() const override { return id_; } - const Comparator* user_comparator() const override { + const Comparator* GetComparator() const override { return BytewiseComparator(); } diff --git a/include/rocksdb/db.h b/include/rocksdb/db.h index 3a1d6b33c..60ce7984a 100644 --- a/include/rocksdb/db.h +++ b/include/rocksdb/db.h @@ -77,6 +77,9 @@ class ColumnFamilyHandle { // // Note that this function is not supported in RocksDBLite. virtual Status GetDescriptor(ColumnFamilyDescriptor* desc) = 0; + // Returns the comparator of the column family associated with the + // current handle. + virtual const Comparator* GetComparator() const = 0; }; static const int kMajorVersion = __ROCKSDB_MAJOR__; diff --git a/utilities/write_batch_with_index/write_batch_with_index_test.cc b/utilities/write_batch_with_index/write_batch_with_index_test.cc index d91482db4..c04650d5d 100644 --- a/utilities/write_batch_with_index/write_batch_with_index_test.cc +++ b/utilities/write_batch_with_index/write_batch_with_index_test.cc @@ -29,7 +29,7 @@ class ColumnFamilyHandleImplDummy : public ColumnFamilyHandleImpl { id_(id), comparator_(comparator) {} uint32_t GetID() const override { return id_; } - const Comparator* user_comparator() const override { return comparator_; } + const Comparator* GetComparator() const override { return comparator_; } private: uint32_t id_;