From d6052d381e2d947fdcdd741d26c221990f4f9864 Mon Sep 17 00:00:00 2001 From: Yanqin Jin Date: Mon, 22 Mar 2021 12:27:31 -0700 Subject: [PATCH] Remove duplicate code (#8079) Summary: The implementation of TransactionDB::WrapDB() and TransactionDB::WrapStackableDB() are almost identical, except for the type of the first argument `db`. This PR adds a new template function in anonymous namespace, and calls it in the above two functions. Pull Request resolved: https://github.com/facebook/rocksdb/pull/8079 Test Plan: make check Reviewed By: lth Differential Revision: D27184575 Pulled By: riversand963 fbshipit-source-id: f2855a6db3a7e897d0d611f7050ca4b696c56a7a --- .../pessimistic_transaction_db.cc | 48 +++++++------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/utilities/transactions/pessimistic_transaction_db.cc b/utilities/transactions/pessimistic_transaction_db.cc index 2c69f7359..cb5ba0330 100644 --- a/utilities/transactions/pessimistic_transaction_db.cc +++ b/utilities/transactions/pessimistic_transaction_db.cc @@ -276,10 +276,10 @@ void TransactionDB::PrepareWrap( db_options->allow_2pc = true; } -Status TransactionDB::WrapDB( - // make sure this db is already opened with memtable history enabled, - // auto compaction distabled and 2 phase commit enabled - DB* db, const TransactionDBOptions& txn_db_options, +namespace { +template +Status WrapAnotherDBInternal( + DBType* db, const TransactionDBOptions& txn_db_options, const std::vector& compaction_enabled_cf_indices, const std::vector& handles, TransactionDB** dbptr) { assert(db != nullptr); @@ -309,6 +309,17 @@ Status TransactionDB::WrapDB( } return s; } +} // namespace + +Status TransactionDB::WrapDB( + // make sure this db is already opened with memtable history enabled, + // auto compaction distabled and 2 phase commit enabled + DB* db, const TransactionDBOptions& txn_db_options, + const std::vector& compaction_enabled_cf_indices, + const std::vector& handles, TransactionDB** dbptr) { + return WrapAnotherDBInternal(db, txn_db_options, + compaction_enabled_cf_indices, handles, dbptr); +} Status TransactionDB::WrapStackableDB( // make sure this stackable_db is already opened with memtable history @@ -316,33 +327,8 @@ Status TransactionDB::WrapStackableDB( StackableDB* db, const TransactionDBOptions& txn_db_options, const std::vector& compaction_enabled_cf_indices, const std::vector& handles, TransactionDB** dbptr) { - assert(db != nullptr); - assert(dbptr != nullptr); - *dbptr = nullptr; - std::unique_ptr txn_db; - - switch (txn_db_options.write_policy) { - case WRITE_UNPREPARED: - txn_db.reset(new WriteUnpreparedTxnDB( - db, PessimisticTransactionDB::ValidateTxnDBOptions(txn_db_options))); - break; - case WRITE_PREPARED: - txn_db.reset(new WritePreparedTxnDB( - db, PessimisticTransactionDB::ValidateTxnDBOptions(txn_db_options))); - break; - case WRITE_COMMITTED: - default: - txn_db.reset(new WriteCommittedTxnDB( - db, PessimisticTransactionDB::ValidateTxnDBOptions(txn_db_options))); - } - txn_db->UpdateCFComparatorMap(handles); - Status s = txn_db->Initialize(compaction_enabled_cf_indices, handles); - // In case of a failure at this point, db is deleted via the txn_db destructor - // and set to nullptr. - if (s.ok()) { - *dbptr = txn_db.release(); - } - return s; + return WrapAnotherDBInternal(db, txn_db_options, + compaction_enabled_cf_indices, handles, dbptr); } // Let LockManager know that this column family exists so it can