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 #include "lru_bucket.h" 25 namespace DistributedDB { 26 class KvStoreNbDelegate; 27 } 28 namespace OHOS::DistributedData { 29 class MetaObserver; 30 class MetaDataManager { 31 public: 32 enum Action : int32_t { 33 INSERT, 34 UPDATE, 35 DELETE, 36 }; 37 class API_EXPORT Filter { 38 public: 39 Filter() = default; 40 Filter(const std::string &pattern); 41 virtual ~Filter() = default; 42 virtual bool operator()(const std::string &key) const; 43 virtual std::vector<uint8_t> GetKey() const; 44 45 private: 46 std::string pattern_; 47 }; 48 using MetaStore = DistributedDB::KvStoreNbDelegate; 49 using Observer = std::function<bool(const std::string &, const std::string &, int32_t)>; 50 using Syncer = std::function<void(const std::shared_ptr<MetaStore> &, int32_t)>; 51 using CloudSyncer = std::function<void()>; 52 using Backup = std::function<int32_t(const std::shared_ptr<MetaStore> &)>; 53 using Bytes = std::vector<uint8_t>; 54 using OnComplete = std::function<void(const std::map<std::string, int32_t> &)>; 55 struct Entry { 56 std::string key; 57 std::string value; 58 }; 59 API_EXPORT static MetaDataManager &GetInstance(); 60 API_EXPORT void Initialize(std::shared_ptr<MetaStore> metaStore, const Backup &backup, const std::string &storeId); 61 API_EXPORT void SetSyncer(const Syncer &syncer); 62 API_EXPORT void SetCloudSyncer(const CloudSyncer &cloudSyncer); 63 API_EXPORT bool SaveMeta(const std::string &key, const Serializable &value, bool isLocal = false); 64 API_EXPORT bool SaveMeta(const std::vector<Entry> &values, bool isLocal = false); 65 API_EXPORT bool LoadMeta(const std::string &key, Serializable &value, bool isLocal = false); 66 template<class T> 67 API_EXPORT bool LoadMeta(const std::string &prefix, std::vector<T> &values, bool isLocal = false) 68 { 69 if (!inited_) { 70 return false; 71 } 72 std::vector<Bytes> entries; 73 if (!GetEntries(prefix, entries, isLocal)) { 74 return false; 75 } 76 values.resize(entries.size()); 77 for (size_t i = 0; i < entries.size(); ++i) { 78 Serializable::Unmarshall({ entries[i].begin(), entries[i].end() }, values[i]); 79 } 80 return true; 81 } 82 83 API_EXPORT bool DelMeta(const std::string &key, bool isLocal = false); 84 API_EXPORT bool DelMeta(const std::vector<std::string> &keys, bool isLocal = false); 85 API_EXPORT bool Subscribe(std::shared_ptr<Filter> filter, Observer observer); 86 API_EXPORT bool Subscribe(std::string prefix, Observer observer, bool isLocal = false); 87 API_EXPORT bool Unsubscribe(std::string filter); 88 API_EXPORT bool Sync(const std::vector<std::string> &devices, OnComplete complete, bool wait = false, 89 bool isRetry = true); 90 91 private: 92 MetaDataManager(); 93 ~MetaDataManager(); 94 95 API_EXPORT bool GetEntries(const std::string &prefix, std::vector<Bytes> &entries, bool isLocal); 96 DelCacheMeta(const std::string & key,bool isLocal)97 void DelCacheMeta(const std::string &key, bool isLocal) 98 { 99 if (!isLocal) { 100 return; 101 } 102 localdata_.Delete(key); 103 } 104 LoadCacheMeta(const std::string & key,Serializable & value,bool isLocal)105 bool LoadCacheMeta(const std::string &key, Serializable &value, bool isLocal) 106 { 107 if (!isLocal) { 108 return false; 109 } 110 std::string data; 111 if (!localdata_.Get(key, data)) { 112 return false; 113 } 114 Serializable::Unmarshall(data, value); 115 return true; 116 } 117 SaveCacheMeta(const std::string & key,const std::string & data,bool isLocal)118 void SaveCacheMeta(const std::string &key, const std::string &data, bool isLocal) 119 { 120 if (!isLocal) { 121 return; 122 } 123 localdata_.Set(key, data); 124 } 125 126 void StopSA(); 127 128 bool inited_ = false; 129 std::mutex mutex_; 130 std::shared_ptr<MetaStore> metaStore_; 131 ConcurrentMap<std::string, std::shared_ptr<MetaObserver>> metaObservers_; 132 Backup backup_; 133 Syncer syncer_; 134 CloudSyncer cloudSyncer_; 135 std::string storeId_; 136 LRUBucket<std::string, std::string> localdata_ {64}; 137 }; 138 } // namespace OHOS::DistributedData 139 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_METADATA_META_DATA_MANAGER_H 140