1 /*
2  * Copyright (c) 2021-2024 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 
31 namespace OHOS {
32 namespace DistributedHardware {
33 class DBAdapter : public std::enable_shared_from_this<DBAdapter>,
34     public DistributedKv::KvStoreDeathRecipient {
35 public:
36     DBAdapter(const std::string &appId, const std::string &storeId,
37         const std::shared_ptr<DistributedKv::KvStoreObserver> changeListener);
38 
39     virtual ~DBAdapter();
40     // default init auto-sync kv store
41     int32_t Init(bool isAutoSync, DistributedKv::DataType dataType);
42     // init local kv store
43     int32_t InitLocal();
44     void UnInit();
45     int32_t ReInit(bool isAutoSync);
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     std::vector<DistributedKv::Entry> GetEntriesByKeys(const std::vector<std::string> &keys);
56     bool SyncDataByNetworkId(const std::string &networkId);
57 
58 private:
59     int32_t RegisterChangeListener();
60     int32_t UnRegisterChangeListener();
61     void RegisterKvStoreDeathListener();
62     void UnRegisterKvStoreDeathListener();
63     // get default kv store with auto sync
64     DistributedKv::Status GetKvStorePtr(bool isAutoSync, DistributedKv::DataType dataType);
65     // get local kv store with no sync with other devices
66     DistributedKv::Status GetLocalKvStorePtr();
67     bool DBDiedOpt(int32_t &times);
68     void SyncByNotFound(const std::string &key);
69     std::string GetNetworkIdByKey(const std::string &key);
70 
71 private:
72     DistributedKv::AppId appId_;
73     DistributedKv::StoreId storeId_;
74     DistributedKv::DistributedKvDataManager kvDataMgr_;
75     std::shared_ptr<DistributedKv::SingleKvStore> kvStoragePtr_;
76     std::shared_ptr<DistributedKv::KvStoreObserver> dataChangeListener_;
77     std::mutex dbAdapterMutex_;
78     bool isAutoSync_ {false};
79     DistributedKv::DataType dataType_ {DistributedKv::DataType::TYPE_DYNAMICAL};
80 };
81 } // namespace DistributedHardware
82 } // namespace OHOS
83 #endif
84