diff --git a/utilities/secondary_index/secondary_index_mixin.h b/utilities/secondary_index/secondary_index_mixin.h index 02c5d4c14a9..a2c23a57b1c 100644 --- a/utilities/secondary_index/secondary_index_mixin.h +++ b/utilities/secondary_index/secondary_index_mixin.h @@ -73,30 +73,35 @@ class SecondaryIndexMixin : public Txn { } using Txn::Delete; - Status Delete(ColumnFamilyHandle* /* column_family */, const Slice& /* key */, - const bool /* assume_tracked */ = false) override { - return Status::NotSupported( - "Delete with secondary indices not yet supported"); + Status Delete(ColumnFamilyHandle* column_family, const Slice& key, + const bool assume_tracked = false) override { + return PerformWithSavePoint([&]() { + const bool do_validate = !assume_tracked; + return DeleteWithSecondaryIndices(column_family, key, do_validate); + }); } - Status Delete(ColumnFamilyHandle* /* column_family */, - const SliceParts& /* key */, - const bool /* assume_tracked */ = false) override { - return Status::NotSupported( - "Delete with secondary indices not yet supported"); + Status Delete(ColumnFamilyHandle* column_family, const SliceParts& key, + const bool assume_tracked = false) override { + std::string key_str; + const Slice key_slice(key, &key_str); + + return Delete(column_family, key_slice, assume_tracked); } using Txn::SingleDelete; - Status SingleDelete(ColumnFamilyHandle* /* column_family */, - const Slice& /* key */, - const bool /* assume_tracked */ = false) override { - return Status::NotSupported( - "SingleDelete with secondary indices not yet supported"); + Status SingleDelete(ColumnFamilyHandle* column_family, const Slice& key, + const bool assume_tracked = false) override { + return PerformWithSavePoint([&]() { + const bool do_validate = !assume_tracked; + return SingleDeleteWithSecondaryIndices(column_family, key, do_validate); + }); } - Status SingleDelete(ColumnFamilyHandle* /* column_family */, - const SliceParts& /* key */, - const bool /* assume_tracked */ = false) override { - return Status::NotSupported( - "SingleDelete with secondary indices not yet supported"); + Status SingleDelete(ColumnFamilyHandle* column_family, const SliceParts& key, + const bool assume_tracked = false) override { + std::string key_str; + const Slice key_slice(key, &key_str); + + return SingleDelete(column_family, key_slice, assume_tracked); } using Txn::PutUntracked; @@ -136,22 +141,28 @@ class SecondaryIndexMixin : public Txn { } using Txn::DeleteUntracked; - Status DeleteUntracked(ColumnFamilyHandle* /* column_family */, - const Slice& /* key */) override { - return Status::NotSupported( - "DeleteUntracked with secondary indices not yet supported"); + Status DeleteUntracked(ColumnFamilyHandle* column_family, + const Slice& key) override { + return PerformWithSavePoint([&]() { + constexpr bool do_validate = false; + return DeleteWithSecondaryIndices(column_family, key, do_validate); + }); } - Status DeleteUntracked(ColumnFamilyHandle* /* column_family */, - const SliceParts& /* key */) override { - return Status::NotSupported( - "DeleteUntracked with secondary indices not yet supported"); + Status DeleteUntracked(ColumnFamilyHandle* column_family, + const SliceParts& key) override { + std::string key_str; + const Slice key_slice(key, &key_str); + + return DeleteUntracked(column_family, key_slice); } using Txn::SingleDeleteUntracked; - Status SingleDeleteUntracked(ColumnFamilyHandle* /* column_family */, - const Slice& /* key */) override { - return Status::NotSupported( - "SingleDeleteUntracked with secondary indices not yet supported"); + Status SingleDeleteUntracked(ColumnFamilyHandle* column_family, + const Slice& key) override { + return PerformWithSavePoint([&]() { + constexpr bool do_validate = false; + return SingleDeleteWithSecondaryIndices(column_family, key, do_validate); + }); } private: @@ -510,6 +521,57 @@ class SecondaryIndexMixin : public Txn { do_validate); } + template + Status DeleteWithSecondaryIndicesImpl(ColumnFamilyHandle* column_family, + const Slice& key, bool do_validate, + Operation&& operation) { + if (!column_family) { + column_family = Txn::DefaultColumnFamily(); + } + + { + const Status s = RemoveSecondaryEntries(column_family, key, do_validate); + if (!s.ok()) { + return s; + } + } + + { + const Status s = operation(column_family, key); + if (!s.ok()) { + return s; + } + } + + return Status::OK(); + } + + Status DeleteWithSecondaryIndices(ColumnFamilyHandle* column_family, + const Slice& key, bool do_validate) { + return DeleteWithSecondaryIndicesImpl( + column_family, key, do_validate, + [&](ColumnFamilyHandle* cfh, const Slice& primary_key) { + assert(cfh); + + constexpr bool assume_tracked = true; + + return Txn::Delete(cfh, primary_key, assume_tracked); + }); + } + + Status SingleDeleteWithSecondaryIndices(ColumnFamilyHandle* column_family, + const Slice& key, bool do_validate) { + return DeleteWithSecondaryIndicesImpl( + column_family, key, do_validate, + [&](ColumnFamilyHandle* cfh, const Slice& primary_key) { + assert(cfh); + + constexpr bool assume_tracked = true; + + return Txn::SingleDelete(cfh, primary_key, assume_tracked); + }); + } + const std::vector>* secondary_indices_; };