• 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 
27 namespace OHOS {
28 namespace DistributedKv {
29 enum class CHANGE_FLAG {
30     INSERT,
31     UPDATE,
32     DELETE
33 };
34 
35 class KvStoreMetaManager {
36 public:
37     static constexpr uint32_t META_STORE_VERSION = 0x03000001;
38     using ChangeObserver = std::function<void(const std::vector<uint8_t> &, const std::vector<uint8_t> &, CHANGE_FLAG)>;
39 
40     class MetaDeviceChangeListenerImpl : public AppDistributedKv::AppDeviceChangeListener {
41         void OnDeviceChanged(const AppDistributedKv::DeviceInfo &info,
42                              const AppDistributedKv::DeviceChangeType &type) const override;
43 
44         AppDistributedKv::ChangeLevelType GetChangeLevelType() const override;
45     };
46 
47     ~KvStoreMetaManager();
48 
49     static KvStoreMetaManager &GetInstance();
50 
51     void InitMetaParameter();
52     void InitMetaListener();
53     void InitBroadcast();
54     void InitDeviceOnline();
55     void SubscribeMeta(const std::string &keyPrefix, const ChangeObserver &observer);
56     size_t GetSyncDataSize(const std::string &deviceId);
57     void BindExecutor(std::shared_ptr<ExecutorPool> executors);
58 private:
59     using NbDelegate = std::shared_ptr<DistributedDB::KvStoreNbDelegate>;
60     NbDelegate GetMetaKvStore();
61 
62     NbDelegate CreateMetaKvStore();
63 
64     void ConfigMetaDataManager();
65 
66     KvStoreMetaManager();
67 
68     void InitMetaData();
69 
70     void SubscribeMetaKvStore();
71 
72     void SyncMeta();
73 
74     std::string GetBackupPath() const;
75 
76     ExecutorPool::Task GetTask(uint32_t retry);
77 
78     class KvStoreMetaObserver : public DistributedDB::KvStoreObserver {
79     public:
80         virtual ~KvStoreMetaObserver();
81 
82         // Database change callback
83         void OnChange(const DistributedDB::KvStoreChangedData &data) override;
84         std::map<std::string, ChangeObserver> handlerMap_;
85     private:
86         void HandleChanges(CHANGE_FLAG flag, const std::list<DistributedDB::Entry> &entries);
87     };
88 
89     static constexpr int32_t RETRY_MAX_TIMES = 100;
90     static constexpr int32_t RETRY_INTERVAL = 1;
91     NbDelegate metaDelegate_;
92     std::string metaDBDirectory_;
93     const std::string label_;
94     DistributedDB::KvStoreDelegateManager delegateManager_;
95     static MetaDeviceChangeListenerImpl listener_;
96     KvStoreMetaObserver metaObserver_;
97     std::recursive_mutex mutex_;
98     std::shared_ptr<ExecutorPool> executors_;
99 };
100 }  // namespace DistributedKv
101 }  // namespace OHOS
102 #endif // KVSTORE_META_MANAGER_H
103