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