• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::KvStoreSyncCallback,
37     public DistributedKv::KvStoreDeathRecipient {
38 public:
39     DBAdapter(const std::string &appId, const std::string &storeId,
40         const std::shared_ptr<DistributedKv::KvStoreObserver> &changeListener);
41 
42     virtual ~DBAdapter();
43 
44     int32_t Init();
45     void UnInit();
46     int32_t ReInit();
47     void SyncCompleted(const std::map<std::string, DistributedKv::Status> &results) override;
48     int32_t GetDataByKey(const std::string &key, std::string &data);
49     int32_t GetDataByKeyPrefix(const std::string &keyPrefix, std::vector<std::string> &values);
50     int32_t PutData(const std::string &key, const std::string &value);
51     int32_t PutDataBatch(const std::vector<std::string> &keys, const std::vector<std::string> &values);
52     void CreateManualSyncCount(const std::string &deviceId);
53     void RemoveManualSyncCount(const std::string &deviceId);
54     int32_t ManualSync(const std::string &networkId);
55     void SyncDBForRecover();
56     virtual void OnRemoteDied() override;
57     void DeleteKvStore();
58     int32_t RemoveDeviceData(const std::string &deviceId);
59     int32_t RemoveDataByKey(const std::string &key);
60 
61 private:
62     int32_t RegisterChangeListener();
63     int32_t UnRegisterChangeListener();
64     void RegisterKvStoreDeathListener();
65     void UnRegisterKvStoreDeathListener();
66     void RegisterManualSyncListener();
67     void UnRegisterManualSyncListener();
68     DistributedKv::Status GetKvStorePtr();
69 
70 private:
71     DistributedKv::AppId appId_;
72     DistributedKv::StoreId storeId_;
73     DistributedKv::DistributedKvDataManager kvDataMgr_;
74     std::shared_ptr<DistributedKv::SingleKvStore> kvStoragePtr_;
75     std::shared_ptr<DistributedKv::KvStoreObserver> dataChangeListener_;
76     std::mutex dbAdapterMutex_;
77     std::unordered_map<std::string, int32_t> manualSyncCountMap_;
78 };
79 } // namespace DistributedHardware
80 } // namespace OHOS
81 #endif
82