• 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 #ifndef KVSTORE_META_MANAGER_H
16 #define KVSTORE_META_MANAGER_H
17 
18 #include <mutex>
19 
20 #include "app_device_change_listener.h"
21 #include "executor_pool.h"
22 #include "kv_store_delegate.h"
23 #include "kv_store_delegate_manager.h"
24 #include "system_ability.h"
25 #include "types.h"
26 #include "metadata/store_meta_data.h"
27 
28 namespace OHOS {
29 namespace DistributedKv {
30 enum class CHANGE_FLAG {
31     INSERT,
32     UPDATE,
33     DELETE
34 };
35 
36 class KvStoreMetaManager {
37 public:
38     static constexpr uint32_t META_STORE_VERSION = 0x03000001;
39     using ChangeObserver = std::function<void(const std::vector<uint8_t> &, const std::vector<uint8_t> &, CHANGE_FLAG)>;
40 
41     class MetaDeviceChangeListenerImpl : public AppDistributedKv::AppDeviceChangeListener {
42         void OnDeviceChanged(const AppDistributedKv::DeviceInfo &info,
43                              const AppDistributedKv::DeviceChangeType &type) const override;
44 
45         AppDistributedKv::ChangeLevelType GetChangeLevelType() const override;
46     };
47 
48     ~KvStoreMetaManager();
49 
50     static KvStoreMetaManager &GetInstance();
51 
52     void InitMetaParameter();
53     void InitMetaListener();
54     void InitBroadcast();
55     void InitDeviceOnline();
56     void SubscribeMeta(const std::string &keyPrefix, const ChangeObserver &observer);
57     void BindExecutor(std::shared_ptr<ExecutorPool> executors);
58 private:
59     using NbDelegate = std::shared_ptr<DistributedDB::KvStoreNbDelegate>;
60     using TaskId = ExecutorPool::TaskId;
61     NbDelegate GetMetaKvStore();
62 
63     NbDelegate CreateMetaKvStore();
64 
65     void SetSyncer();
66 
67     KvStoreMetaManager();
68 
69     void InitMetaData();
70 
71     void UpdateMetaData();
72 
73     void SubscribeMetaKvStore();
74 
75     void SyncMeta();
76 
77     void NotifyAllAutoSyncDBInfo();
78 
79     void OnDataChange(CHANGE_FLAG flag, const std::list<DistributedDB::Entry>& changedData);
80 
81     void OnDeviceChange(const std::string& deviceId);
82 
83     void AddDbInfo(const DistributedData::StoreMetaData& metaData, std::vector<DistributedDB::DBInfo>& dbInfos,
84         bool isDeleted = false);
85 
86     void GetDbInfosByDeviceId(const std::string& deviceId, std::vector<DistributedDB::DBInfo>& dbInfos);
87 
88     std::string GetBackupPath() const;
89 
90     ExecutorPool::Task GetTask(uint32_t retry);
91 
92     std::function<void()> SyncTask(const NbDelegate &store, int32_t status);
93 
94     class KvStoreMetaObserver : public DistributedDB::KvStoreObserver {
95     public:
96         virtual ~KvStoreMetaObserver();
97 
98         // Database change callback
99         void OnChange(const DistributedDB::KvStoreChangedData &data) override;
100         std::map<std::string, ChangeObserver> handlerMap_;
101     private:
102         void HandleChanges(CHANGE_FLAG flag, const std::list<DistributedDB::Entry> &entries);
103     };
104 
105     class DBInfoDeviceChangeListenerImpl : public AppDistributedKv::AppDeviceChangeListener {
106         void OnDeviceChanged(const AppDistributedKv::DeviceInfo &info,
107             const AppDistributedKv::DeviceChangeType &type) const override;
108 
109         AppDistributedKv::ChangeLevelType GetChangeLevelType() const override;
110     };
111 
112     static constexpr int32_t RETRY_MAX_TIMES = 100;
113     static constexpr int32_t RETRY_INTERVAL = 1;
114     static constexpr uint8_t COMPRESS_RATE = 10;
115     static constexpr int32_t DELAY_SYNC = 500;
116     NbDelegate metaDelegate_;
117     std::string metaDBDirectory_;
118     const std::string label_;
119     DistributedDB::KvStoreDelegateManager delegateManager_;
120     static MetaDeviceChangeListenerImpl listener_;
121     static DBInfoDeviceChangeListenerImpl dbInfoListener_;
122     KvStoreMetaObserver metaObserver_;
123     std::mutex mutex_;
124     std::shared_ptr<ExecutorPool> executors_;
125     TaskId delaySyncTaskId_ = ExecutorPool::INVALID_TASK_ID;
126     static constexpr int32_t META_VERSION = 2;
127 };
128 }  // namespace DistributedKv
129 }  // namespace OHOS
130 #endif // KVSTORE_META_MANAGER_H