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 SINGLE_KVSTORE_IMPL_H 17 #define SINGLE_KVSTORE_IMPL_H 18 19 #include <mutex> 20 #include <set> 21 #include <memory> 22 #include <shared_mutex> 23 24 #include "auth/auth_delegate.h" 25 #include "flowctrl_manager/kvstore_flowctrl_manager.h" 26 #include "ikvstore_observer.h" 27 #include "ikvstore_single.h" 28 #include "ikvstore_sync_callback.h" 29 #include "kv_store_delegate_manager.h" 30 #include "kv_store_nb_delegate.h" 31 #include "kvstore_observer_impl.h" 32 #include "kvstore_resultset_impl.h" 33 #include "kvstore_sync_manager.h" 34 #include "inner_types.h" 35 36 namespace OHOS::DistributedKv { 37 class SingleKvStoreImpl : public SingleKvStoreStub { 38 public: 39 SingleKvStoreImpl(const Options &options, const std::string &userId, const std::string &bundleName, 40 const std::string &storeId, const std::string &appId, const std::string &directory, 41 DistributedDB::KvStoreNbDelegate *delegate); 42 ~SingleKvStoreImpl(); 43 std::string GetStoreId(); 44 Status Put(const Key &key, const Value &value) override; 45 Status Delete(const Key &key) override; 46 Status Get(const Key &key, Value &value) override; 47 Status SubscribeKvStore(const SubscribeType subscribeType, sptr<IKvStoreObserver> observer) override; 48 Status UnSubscribeKvStore(const SubscribeType subscribeType, sptr<IKvStoreObserver> observer) override; 49 Status GetEntries(const Key &prefixKey, std::vector<Entry> &entries) override; 50 Status GetEntriesWithQuery(const std::string &query, std::vector<Entry> &entries) override; 51 void GetResultSet(const Key &prefixKey, std::function<void(Status, sptr<IKvStoreResultSet>)> callback) override; 52 void GetResultSetWithQuery(const std::string &query, 53 std::function<void(Status, sptr<IKvStoreResultSet>)> callback) override; 54 Status GetCountWithQuery(const std::string &query, int &result) override; 55 Status CloseResultSet(sptr<IKvStoreResultSet> resultSet) override; 56 Status Sync(const std::vector<std::string> &deviceIds, SyncMode mode, uint32_t allowedDelayMs, 57 uint64_t sequenceId) override; 58 Status Sync(const std::vector<std::string> &deviceIds, SyncMode mode, 59 const std::string &query, uint64_t sequenceId) override; 60 Status RemoveDeviceData(const std::string &device) override; 61 Status RegisterSyncCallback(sptr<IKvStoreSyncCallback> callback) override; 62 Status UnRegisterSyncCallback() override; 63 Status ReKey(const std::vector<uint8_t> &key); 64 InnerStatus Close(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager); 65 Status ForceClose(DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager); 66 Status MigrateKvStore(const std::string &harmonyAccountId, const std::string &kvStoreDataDir, 67 DistributedDB::KvStoreDelegateManager *oldDelegateMgr, 68 DistributedDB::KvStoreDelegateManager *&newDelegateMgr); 69 void IncreaseOpenCount(); 70 Status PutBatch(const std::vector<Entry> &entries) override; 71 Status DeleteBatch(const std::vector<Key> &keys) override; 72 Status StartTransaction() override; 73 Status Commit() override; 74 Status Rollback() override; 75 Status Control(KvControlCmd cmd, const KvParam &inputParam, sptr<KvParam> &output) override; 76 Status SetCapabilityEnabled(bool enabled) override; 77 Status SetCapabilityRange(const std::vector<std::string> &localLabels, 78 const std::vector<std::string> &remoteSupportLabels) override; 79 Status GetSecurityLevel(SecurityLevel &securityLevel) override; 80 bool Import(const std::string &bundleName) const; 81 void OnDump(int fd) const; 82 void SetCompatibleIdentify(const std::string &changedDevice); 83 void SetCompatibleIdentify(); 84 85 protected: 86 virtual KvStoreObserverImpl *CreateObserver(const SubscribeType subscribeType, sptr<IKvStoreObserver> observer); 87 virtual KvStoreResultSetImpl *CreateResultSet( 88 DistributedDB::KvStoreResultSet *resultSet, const DistributedDB::Key &prix); 89 90 private: 91 Status ConvertDbStatus(DistributedDB::DBStatus dbStatus); 92 uint32_t GetSyncDelayTime(uint32_t allowedDelayMs) const; 93 Status AddSync(const std::vector<std::string> &deviceIds, SyncMode mode, uint32_t delayMs, 94 uint64_t sequenceId); 95 Status AddSync(const std::vector<std::string> &deviceIds, SyncMode mode, 96 const std::string &query, uint32_t delayMs, uint64_t sequenceId); 97 Status RemoveAllSyncOperation(); 98 void DoSyncComplete(const std::map<std::string, DistributedDB::DBStatus> &devicesSyncResult, 99 const std::string &query, uint64_t sequenceId); 100 Status DoSync(const std::vector<std::string> &deviceIds, SyncMode mode, const KvStoreSyncManager::SyncEnd &syncEnd, 101 uint64_t sequenceId); 102 Status DoQuerySync(const std::vector<std::string> &deviceIds, SyncMode mode, const std::string &query, 103 const KvStoreSyncManager::SyncEnd &syncEnd, uint64_t sequenceId); 104 Status AddAutoSync(); 105 Status DoAutoSync(const KvStoreSyncManager::SyncEnd &); 106 Status RebuildKvStoreObserver(DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate); 107 Status RebuildKvStoreResultSet(); 108 int ConvertToDbObserverMode(SubscribeType subscribeType) const; 109 DistributedDB::SyncMode ConvertToDbSyncMode(SyncMode syncMode) const; 110 Status DoSubscribe(const std::vector<std::string> &deviceIds, 111 const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); 112 Status AddSubscribe(const std::vector<std::string> &deviceIds, 113 const std::string &query, uint32_t delayMs, uint64_t sequenceId); 114 Status Subscribe(const std::vector<std::string> &deviceIds, 115 const std::string &query, uint64_t sequenceId) override; 116 Status DoUnSubscribe(const std::vector<std::string> &deviceIds, 117 const std::string &query, const KvStoreSyncManager::SyncEnd &syncEnd); 118 Status AddUnSubscribe(const std::vector<std::string> &deviceIds, 119 const std::string &query, uint32_t delayMs, uint64_t sequenceId); 120 Status UnSubscribe(const std::vector<std::string> &deviceIds, 121 const std::string &query, uint64_t sequenceId) override; 122 std::vector<std::string> MapNodeIdToUuids(const std::vector<std::string> &deviceIds); 123 124 // kvstore options. 125 const Options options_; 126 // kvstore cipherKey. 127 const std::vector<uint8_t> cipherKey_; 128 // deviceAccount id get from service 129 std::string deviceAccountId_; 130 // appId get from PMS. 131 const std::string bundleName_; 132 // kvstore name. 133 const std::string storeId_; 134 135 const std::string appId_; 136 137 // kvstore absolute path in distributeddatamgr. 138 const std::string storePath_; 139 // for top-app, 0 means synchronization immediately. for others, 0 means 1000ms. 140 uint32_t defaultSyncDelayMs_{ 0 }; 141 std::atomic_uint32_t waitingSyncCount_{ 0 }; 142 std::atomic_uint32_t waitingAutoSyncCount_{ 0 }; 143 std::atomic_uint32_t syncRetries_{ 0 }; 144 std::vector<std::string> lastSyncDeviceIds_{ }; 145 SyncMode lastSyncMode_{ SyncMode::PULL }; 146 uint32_t lastSyncDelayMs_{ 0 }; 147 148 // distributeddb is responsible for free kvStoreNbDelegate_, 149 // (destruct will be done while calling CloseKvStore in KvStoreDelegateManager) 150 // so DO NOT free it in SingleKvStoreImpl's destructor. 151 mutable std::shared_mutex storeNbDelegateMutex_{}; 152 DistributedDB::KvStoreNbDelegate *kvStoreNbDelegate_; 153 std::mutex observerMapMutex_; 154 std::map<IRemoteObject *, KvStoreObserverImpl *> observerMap_; 155 std::mutex storeResultSetMutex_; 156 std::map<IKvStoreResultSet *, sptr<KvStoreResultSetImpl>> storeResultSetMap_; 157 sptr<IKvStoreSyncCallback> syncCallback_; 158 int openCount_; 159 160 // flowControl 161 KvStoreFlowCtrlManager flowCtrl_; 162 static constexpr int BURST_CAPACITY = 1000; 163 static constexpr int SUSTAINED_CAPACITY = 10000; 164 }; 165 } // namespace OHOS::DistributedKv 166 #endif // SINGLE_KVSTORE_IMPL_H 167