1 /* 2 * Copyright (c) 2022 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_DATA_SERVICES_FRAMEWORK_METADATA_META_DATA_MANAGER_H 17 #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_METADATA_META_DATA_MANAGER_H 18 #include <functional> 19 #include <memory> 20 #include <mutex> 21 22 #include "concurrent_map.h" 23 #include "serializable/serializable.h" 24 namespace DistributedDB { 25 class KvStoreNbDelegate; 26 } 27 namespace OHOS::DistributedData { 28 class MetaObserver; 29 class MetaDataManager { 30 public: 31 enum Action : int32_t { 32 INSERT, 33 UPDATE, 34 DELETE, 35 }; 36 class API_EXPORT Filter { 37 public: 38 Filter() = default; 39 Filter(const std::string &pattern); 40 virtual ~Filter() = default; 41 virtual bool operator()(const std::string &key) const; 42 virtual std::vector<uint8_t> GetKey() const; 43 44 private: 45 std::string pattern_; 46 }; 47 using MetaStore = DistributedDB::KvStoreNbDelegate; 48 using Observer = std::function<bool(const std::string &, const std::string &, int32_t)>; 49 using Syncer = std::function<void(const std::shared_ptr<MetaStore> &, int32_t)>; 50 using CloudSyncer = std::function<void()>; 51 using Backup = std::function<int32_t(const std::shared_ptr<MetaStore> &)>; 52 using Bytes = std::vector<uint8_t>; 53 using OnComplete = std::function<void(const std::map<std::string, int32_t> &)>; 54 API_EXPORT static MetaDataManager &GetInstance(); 55 API_EXPORT void Initialize(std::shared_ptr<MetaStore> metaStore, const Backup &backup); 56 API_EXPORT void SetSyncer(const Syncer &syncer); 57 API_EXPORT void SetCloudSyncer(const CloudSyncer &cloudSyncer); 58 API_EXPORT bool SaveMeta(const std::string &key, const Serializable &value, bool isLocal = false); 59 API_EXPORT bool LoadMeta(const std::string &key, Serializable &value, bool isLocal = false); 60 template<class T> 61 API_EXPORT bool LoadMeta(const std::string &prefix, std::vector<T> &values, bool isLocal = false) 62 { 63 if (!inited_) { 64 return false; 65 } 66 std::vector<Bytes> entries; 67 if (!GetEntries(prefix, entries, isLocal)) { 68 return false; 69 } 70 values.resize(entries.size()); 71 for (size_t i = 0; i < entries.size(); ++i) { 72 Serializable::Unmarshall({ entries[i].begin(), entries[i].end() }, values[i]); 73 } 74 return true; 75 } 76 77 API_EXPORT bool DelMeta(const std::string &key, bool isLocal = false); 78 API_EXPORT bool Subscribe(std::shared_ptr<Filter> filter, Observer observer); 79 API_EXPORT bool Subscribe(std::string prefix, Observer observer, bool isLocal = false); 80 API_EXPORT bool Unsubscribe(std::string filter); 81 API_EXPORT bool Sync(const std::vector<std::string> &devices, OnComplete complete); 82 83 private: 84 MetaDataManager(); 85 ~MetaDataManager(); 86 87 API_EXPORT bool GetEntries(const std::string &prefix, std::vector<Bytes> &entries, bool isLocal); 88 void StopSA(); 89 90 bool inited_ = false; 91 std::mutex mutex_; 92 std::shared_ptr<MetaStore> metaStore_; 93 ConcurrentMap<std::string, std::shared_ptr<MetaObserver>> metaObservers_; 94 Backup backup_; 95 Syncer syncer_; 96 CloudSyncer cloudSyncer_; 97 }; 98 } // namespace OHOS::DistributedData 99 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_METADATA_META_DATA_MANAGER_H