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 OHOS_DISTRIBUTED_HARDWARE_DB_ADAPTER_H 17 #define OHOS_DISTRIBUTED_HARDWARE_DB_ADAPTER_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "distributed_kv_data_manager.h" 27 #include "kvstore_observer.h" 28 29 #include "capability_info.h" 30 #include "event_sender.h" 31 32 namespace OHOS { 33 namespace DistributedHardware { 34 class DBAdapter : public std::enable_shared_from_this<DBAdapter>, 35 public EventSender, 36 public DistributedKv::KvStoreDeathRecipient { 37 public: 38 DBAdapter(const std::string &appId, const std::string &storeId, 39 const std::shared_ptr<DistributedKv::KvStoreObserver> &changeListener); 40 41 virtual ~DBAdapter(); 42 43 int32_t Init(); 44 void UnInit(); 45 int32_t ReInit(); 46 int32_t GetDataByKey(const std::string &key, std::string &data); 47 int32_t GetDataByKeyPrefix(const std::string &keyPrefix, std::vector<std::string> &values); 48 int32_t PutData(const std::string &key, const std::string &value); 49 int32_t PutDataBatch(const std::vector<std::string> &keys, const std::vector<std::string> &values); 50 void SyncDBForRecover(); 51 virtual void OnRemoteDied() override; 52 void DeleteKvStore(); 53 int32_t RemoveDeviceData(const std::string &deviceId); 54 int32_t RemoveDataByKey(const std::string &key); 55 56 private: 57 int32_t RegisterChangeListener(); 58 int32_t UnRegisterChangeListener(); 59 void RegisterKvStoreDeathListener(); 60 void UnRegisterKvStoreDeathListener(); 61 DistributedKv::Status GetKvStorePtr(); 62 63 private: 64 DistributedKv::AppId appId_; 65 DistributedKv::StoreId storeId_; 66 DistributedKv::DistributedKvDataManager kvDataMgr_; 67 std::shared_ptr<DistributedKv::SingleKvStore> kvStoragePtr_; 68 std::shared_ptr<DistributedKv::KvStoreObserver> dataChangeListener_; 69 std::mutex dbAdapterMutex_; 70 }; 71 } // namespace DistributedHardware 72 } // namespace OHOS 73 #endif 74