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 KV_STORE_DELEGATE_IMPL_H 17 #define KV_STORE_DELEGATE_IMPL_H 18 19 #ifndef OMIT_MULTI_VER 20 #include <functional> 21 #include <map> 22 #include <mutex> 23 #include <string> 24 25 #include "ikvdb_connection.h" 26 #include "ikvdb_factory.h" 27 #include "kv_store_delegate.h" 28 #include "store_types.h" 29 30 namespace DistributedDB { 31 class KvStoreDelegateImpl final : public KvStoreDelegate { 32 public: 33 KvStoreDelegateImpl(IKvDBConnection *conn, const std::string &storeId); 34 ~KvStoreDelegateImpl(); 35 36 DISABLE_COPY_ASSIGN_MOVE(KvStoreDelegateImpl); 37 38 // Used to Put a k-v pair to the kvstore. 39 // Return OK if the operation is successful. 40 DBStatus Put(const Key &key, const Value &value) override; 41 42 // Used to Put a vector<Entry> contains k-v pairs to the kvstore. 43 // Return OK if the operation is successful. 44 DBStatus PutBatch(const std::vector<Entry> &entries) override; 45 46 // Delete a record with the given key. 47 // Return OK if the operation is successful. 48 DBStatus Delete(const Key &key) override; 49 50 // Batch delete records with the given keys. 51 // Return OK if the operation is successful. 52 DBStatus DeleteBatch(const std::vector<Key> &keys) override; 53 54 // Delete all record of th kvstore. 55 // Return OK if the operation is successful. 56 DBStatus Clear() override; 57 58 // Return a storeId of the KvStore instance 59 std::string GetStoreId() const override; 60 61 // Get a snapshot of the kvstore. the observer is used to notify data changed, it can be null. 62 // return value is DBStatus and KvStoreSnapshotDelegate*, these values will be passed to the callback. 63 void GetKvStoreSnapshot(KvStoreObserver *observer, 64 const std::function<void(DBStatus, KvStoreSnapshotDelegate *)> &callback) override; 65 66 // Release a snapshot, it will return OK if the operation is successful. 67 DBStatus ReleaseKvStoreSnapshot(KvStoreSnapshotDelegate *&snapshotDelegate) override; 68 69 // Register a data change observer 70 DBStatus RegisterObserver(KvStoreObserver *observer) override; 71 72 // Unregister a data change observer 73 DBStatus UnRegisterObserver(const KvStoreObserver *observer) override; 74 75 // Start a transaction 76 DBStatus StartTransaction() override; 77 78 // Commit a transaction 79 DBStatus Commit() override; 80 81 // Rollback a transaction 82 DBStatus Rollback() override; 83 84 // Used to set the resolution policy for conflicts. 85 // Return OK if operation is successful. 86 DBStatus SetConflictResolutionPolicy(ResolutionPolicyType type, const ConflictResolution &resolution) override; 87 88 // Rekey the database. 89 DBStatus Rekey(const CipherPassword &password) override; 90 91 // Empty passwords represent non-encrypted files. 92 // Export existing database files to a specified database file in the specified directory. 93 DBStatus Export(const std::string &filePath, const CipherPassword &passwd) override; 94 95 // Import the existing database files to the specified database file in the specified directory. 96 DBStatus Import(const std::string &filePath, const CipherPassword &passwd) override; 97 98 // Set release flag, KvStoreManagerDelegate will set when release the kvstore 99 void SetReleaseFlag(bool flag); 100 101 // Close the KvStoreDelegateImpl 102 DBStatus Close(); 103 104 // Special pragma interface, see PragmaCmd and PragmaData, 105 DBStatus Pragma(PragmaCmd cmd, PragmaData ¶mData) override; 106 107 private: 108 IKvDBConnection *conn_; 109 std::string storeId_; 110 bool releaseFlag_; 111 std::mutex observerMapLock_; 112 std::map<const KvStoreObserver*, const KvDBObserverHandle*> observerMap_; 113 }; 114 } // namespace DistributedDB 115 116 #endif // KV_STORE_DELEGATE_IMPL_H 117 #endif