• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Backup = std::function<int32_t(const std::shared_ptr<MetaStore> &)>;
51     using Bytes = std::vector<uint8_t>;
52     API_EXPORT static MetaDataManager &GetInstance();
53     API_EXPORT void Initialize(std::shared_ptr<MetaStore> metaStore, const Backup &backup, const Syncer &syncer);
54     API_EXPORT bool SaveMeta(const std::string &key, const Serializable &value, bool isLocal = false);
55     API_EXPORT bool LoadMeta(const std::string &key, Serializable &value, bool isLocal = false);
56     template<class T>
57     API_EXPORT bool LoadMeta(const std::string &prefix, std::vector<T> &values, bool isLocal = false)
58     {
59         if (!inited_) {
60             return false;
61         }
62         std::vector<Bytes> entries;
63         if (!GetEntries(prefix, entries, isLocal)) {
64             return false;
65         }
66         values.resize(entries.size());
67         for (size_t i = 0; i < entries.size(); ++i) {
68             Serializable::Unmarshall({ entries[i].begin(), entries[i].end() }, values[i]);
69         }
70         return true;
71     }
72 
73     API_EXPORT bool DelMeta(const std::string &key, bool isLocal = false);
74     API_EXPORT bool Subscribe(std::shared_ptr<Filter> filter, Observer observer);
75     API_EXPORT bool Subscribe(std::string prefix, Observer observer);
76     API_EXPORT bool Unsubscribe(std::string filter);
77 private:
78     MetaDataManager();
79     ~MetaDataManager();
80 
81     API_EXPORT bool GetEntries(const std::string &prefix, std::vector<Bytes> &entries, bool isLocal);
82 
83     bool inited_ = false;
84     std::mutex mutex_;
85     std::shared_ptr<MetaStore> metaStore_;
86     ConcurrentMap<std::string, std::shared_ptr<MetaObserver>> metaObservers_;
87     Backup backup_;
88     Syncer syncer_;
89 };
90 } // namespace OHOS::DistributedData
91 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_METADATA_META_DATA_MANAGER_H
92