• 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 namespace {
35 constexpr int32_t MAX_INIT_RETRY_TIMES = 20;
36 constexpr int32_t INIT_RETRY_SLEEP_INTERVAL = 200 * 1000; // 200ms
37 constexpr int32_t MANUAL_SYNC_TIMES = 6;
38 constexpr int32_t MANUAL_SYNC_INTERVAL = 100 * 1000; // 100ms
39 constexpr int32_t DIED_CHECK_MAX_TIMES = 300;
40 constexpr int32_t DIED_CHECK_INTERVAL = 100 * 1000; // 100ms
41 }
42 class DBAdapter : public std::enable_shared_from_this<DBAdapter>,
43     public EventSender,
44     public DistributedKv::KvStoreSyncCallback,
45     public DistributedKv::KvStoreDeathRecipient {
46 public:
47     DBAdapter(const std::string &appId, const std::string &storeId,
48         const std::shared_ptr<DistributedKv::KvStoreObserver> &changeListener);
49 
50     virtual ~DBAdapter();
51 
52     int32_t Init();
53     void UnInit();
54     int32_t ReInit();
55     void SyncCompleted(const std::map<std::string, DistributedKv::Status> &results) override;
56     int32_t GetDataByKey(const std::string &key, std::string &data);
57     int32_t GetDataByKeyPrefix(const std::string &keyPrefix, std::vector<std::string> &values);
58     int32_t PutData(const std::string &key, const std::string &value);
59     int32_t PutDataBatch(const std::vector<std::string> &keys, const std::vector<std::string> &values);
60     void CreateManualSyncCount(const std::string &deviceId);
61     void RemoveManualSyncCount(const std::string &deviceId);
62     int32_t ManualSync(const std::string &networkId);
63     void SyncDBForRecover();
64     virtual void OnRemoteDied() override;
65     void DeleteKvStore();
66     int32_t RemoveDeviceData(const std::string &deviceId);
67     int32_t RemoveDataByKey(const std::string &key);
68 
69 private:
70     int32_t RegisterChangeListener();
71     int32_t UnRegisterChangeListener();
72     void RegisterKvStoreDeathListener();
73     void UnRegisterKvStoreDeathListener();
74     void RegisterManualSyncListener();
75     void UnRegisterManualSyncListener();
76     DistributedKv::Status GetKvStorePtr();
77 
78 private:
79     DistributedKv::AppId appId_;
80     DistributedKv::StoreId storeId_;
81     DistributedKv::DistributedKvDataManager kvDataMgr_;
82     std::shared_ptr<DistributedKv::SingleKvStore> kvStoragePtr_;
83     std::shared_ptr<DistributedKv::KvStoreObserver> dataChangeListener_;
84     std::mutex dbAdapterMutex_;
85     std::unordered_map<std::string, int32_t> manualSyncCountMap_;
86 };
87 } // namespace DistributedHardware
88 } // namespace OHOS
89 #endif
90