From 6935eb24e04846a77f81529234eebe80a646aaaf Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang Date: Wed, 6 Jan 2016 18:14:01 -0800 Subject: [PATCH] Add ColumnFamilyHandle::GetDescriptor() Summary: This patch addes ColumnFamilyHandle::GetDescriptor(), which allows developers to obtain the CF options and names of the associated column family given its handle. // Returns the up-to-date descriptor used by the current handle. Since it // returns the up-to-date information, this call might internally locks // and releases DB mutex to access the up-to-date CF options. virtual ColumnFamilyDescriptor GetDescriptor() = 0; Test Plan: augment column_family_test Reviewers: sdong, yoshinorim, IslamAbdelRahman, rven, kradhakrishnan, anthony Reviewed By: anthony Subscribers: dhruba, leveldb Differential Revision: https://reviews.facebook.net/D51543 --- db/column_family.cc | 14 ++++++++++++++ db/column_family.h | 1 + db/column_family_test.cc | 19 ++++++++++++++----- include/rocksdb/db.h | 24 +++++++++++++++++------- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/db/column_family.cc b/db/column_family.cc index 474fc91d9..408f53831 100644 --- a/db/column_family.cc +++ b/db/column_family.cc @@ -68,6 +68,20 @@ const std::string& ColumnFamilyHandleImpl::GetName() const { return cfd()->GetName(); } +Status ColumnFamilyHandleImpl::GetDescriptor(ColumnFamilyDescriptor* desc) { +#ifndef ROCKSDB_LITE + // accessing mutable cf-options requires db mutex. + InstrumentedMutexLock l(mutex_); + *desc = ColumnFamilyDescriptor( + cfd()->GetName(), + BuildColumnFamilyOptions(*cfd()->options(), + *cfd()->GetLatestMutableCFOptions())); + return Status::OK(); +#else + return Status::NotSupported(); +#endif // !ROCKSDB_LITE +} + const Comparator* ColumnFamilyHandleImpl::user_comparator() const { return cfd()->user_comparator(); } diff --git a/db/column_family.h b/db/column_family.h index 25d5c2a15..6266d40a2 100644 --- a/db/column_family.h +++ b/db/column_family.h @@ -59,6 +59,7 @@ class ColumnFamilyHandleImpl : public ColumnFamilyHandle { virtual uint32_t GetID() const override; virtual const std::string& GetName() const override; + virtual Status GetDescriptor(ColumnFamilyDescriptor* desc) override; private: ColumnFamilyData* cfd_; diff --git a/db/column_family_test.cc b/db/column_family_test.cc index 70035a939..d86735c2e 100644 --- a/db/column_family_test.cc +++ b/db/column_family_test.cc @@ -16,11 +16,12 @@ #include "rocksdb/db.h" #include "rocksdb/env.h" #include "rocksdb/iterator.h" +#include "util/coding.h" +#include "util/options_parser.h" #include "util/string_util.h" +#include "util/sync_point.h" #include "util/testharness.h" #include "util/testutil.h" -#include "util/coding.h" -#include "util/sync_point.h" #include "utilities/merge_operators.h" namespace rocksdb { @@ -146,10 +147,18 @@ class ColumnFamilyTest : public testing::Test { handles_.resize(cfi + cfs.size()); names_.resize(cfi + cfs.size()); for (size_t i = 0; i < cfs.size(); ++i) { - ASSERT_OK(db_->CreateColumnFamily( - options.size() == 0 ? column_family_options_ : options[i], cfs[i], - &handles_[cfi])); + const auto& current_cf_opt = + options.size() == 0 ? column_family_options_ : options[i]; + ASSERT_OK( + db_->CreateColumnFamily(current_cf_opt, cfs[i], &handles_[cfi])); names_[cfi] = cfs[i]; + +#ifndef ROCKSDB_LITE // RocksDBLite does not support GetDescriptor + // Verify the CF options of the returned CF handle. + ColumnFamilyDescriptor desc; + ASSERT_OK(handles_[cfi]->GetDescriptor(&desc)); + RocksDBOptionsParser::VerifyCFOptions(desc.options, current_cf_opt); +#endif // !ROCKSDB_LITE cfi++; } } diff --git a/include/rocksdb/db.h b/include/rocksdb/db.h index eeb31f924..9bc0993c5 100644 --- a/include/rocksdb/db.h +++ b/include/rocksdb/db.h @@ -50,14 +50,7 @@ class EventListener; using std::unique_ptr; -class ColumnFamilyHandle { - public: - virtual ~ColumnFamilyHandle() {} - virtual const std::string& GetName() const = 0; - virtual uint32_t GetID() const = 0; -}; extern const std::string kDefaultColumnFamilyName; - struct ColumnFamilyDescriptor { std::string name; ColumnFamilyOptions options; @@ -68,6 +61,23 @@ struct ColumnFamilyDescriptor { : name(_name), options(_options) {} }; +class ColumnFamilyHandle { + public: + virtual ~ColumnFamilyHandle() {} + // Returns the name of the column family associated with the current handle. + virtual const std::string& GetName() const = 0; + // Returns the ID of the column family associated with the current handle. + virtual uint32_t GetID() const = 0; + // Fills "*desc" with the up-to-date descriptor of the column family + // associated with this handle. Since it fills "*desc" with the up-to-date + // information, this call might internally lock and release DB mutex to + // access the up-to-date CF options. In addition, all the pointer-typed + // options cannot be referenced any longer than the original options exist. + // + // Note that this function is not supported in RocksDBLite. + virtual Status GetDescriptor(ColumnFamilyDescriptor* desc) = 0; +}; + static const int kMajorVersion = __ROCKSDB_MAJOR__; static const int kMinorVersion = __ROCKSDB_MINOR__;