1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MULTI_VER_NATURAL_STORE_CONNECTION_H 17 #define MULTI_VER_NATURAL_STORE_CONNECTION_H 18 19 #ifndef OMIT_MULTI_VER 20 #include <mutex> 21 #include <set> 22 23 #include "macro_utils.h" 24 #include "sync_able_kvdb_connection.h" 25 #include "multi_ver_def.h" 26 #include "multi_ver_kv_entry.h" 27 #include "multi_ver_storage_executor.h" 28 29 namespace DistributedDB { 30 class MultiVerNaturalStore; 31 32 enum class TransactState { 33 TRANSACT_IDLE, 34 TRANSACT_IN_PROGRESS, 35 }; 36 37 class MultiVerNaturalStoreConnection : public SyncAbleKvDBConnection { 38 public: 39 explicit MultiVerNaturalStoreConnection(MultiVerNaturalStore *kvDB); 40 ~MultiVerNaturalStoreConnection() override; 41 42 // Delete the copy and assign constructors 43 DISABLE_COPY_ASSIGN_MOVE(MultiVerNaturalStoreConnection); 44 45 // Get the value from the database 46 int Get(const IOption &option, const Key &key, Value &value) const override; 47 48 // Put the value to the database 49 int Put(const IOption &option, const Key &key, const Value &value) override; 50 51 // Delete the value from the database 52 int Delete(const IOption &option, const Key &key) override; 53 54 // Clear all the data from the database 55 int Clear(const IOption &option) override; 56 57 // Get all the data from the database 58 int GetEntries(const IOption &option, const Key &keyPrefix, std::vector<Entry> &entries) const override; 59 60 // Put the batch values to the database. 61 int PutBatch(const IOption &option, const std::vector<Entry> &entries) override; 62 63 // Put the synced data by commit. 64 int PutCommitData(const MultiVerCommitNode &commit, const std::vector<MultiVerKvEntry *> &entries); 65 66 // Delete the batch values from the database. 67 int DeleteBatch(const IOption &option, const std::vector<Key> &keys) override; 68 69 // Get the snapshot 70 int GetSnapshot(IKvDBSnapshot *&snapshot) const override; 71 72 // Release the created snapshot 73 void ReleaseSnapshot(IKvDBSnapshot *&snapshot) override; 74 75 // Start the transaction 76 int StartTransaction() override; 77 78 // Commit the transaction 79 int Commit() override; 80 81 // Roll back the transaction 82 int RollBack() override; 83 84 // Check if the transaction already started manually 85 bool IsTransactionStarted() const override; 86 87 // Called when close and delete the connection. 88 int PreClose() override; 89 90 // Parse event types(from observer mode). 91 int TranslateObserverModeToEventTypes(unsigned mode, std::list<int> &eventTypes) const override; 92 93 int Rekey(const CipherPassword &passwd) override; 94 95 int Export(const std::string &filePath, const CipherPassword &passwd) override; 96 97 int Import(const std::string &filePath, const CipherPassword &passwd) override; 98 99 private: 100 static bool CheckDeletedKeys(const std::vector<Key> &keys); 101 102 int CheckDataStatus(const Key &key, const Value &value, bool isDeleted) const; 103 104 int StartTransactionInner(bool &isAuto); 105 106 int CommitTransactionInner(); 107 108 int RollBackTransactionInner(); 109 110 int CheckTransactionState(); 111 112 MultiVerStorageExecutor *GetHandle(bool isWrite, int &errCode) const; 113 114 DECLARE_OBJECT_TAG(MultiVerNaturalStoreConnection); 115 116 MultiVerStorageExecutor *writeHandle_; 117 mutable std::set<IKvDBSnapshot *> snapshots_; 118 mutable std::mutex snapshotMutex_; 119 mutable std::mutex writeMutex_; 120 std::mutex rekeyMutex_; 121 std::mutex importMutex_; 122 }; 123 } // namespace DistributedDB 124 125 #endif // MULTI_VER_NATURAL_STORE_CONNECTION_H 126 #endif