• 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 
16 #ifndef SINGLE_KV_STORE_H
17 #define SINGLE_KV_STORE_H
18 
19 #include <map>
20 #include "kvstore.h"
21 #include "kvstore_observer.h"
22 #include "kvstore_result_set.h"
23 #include "kvstore_sync_callback.h"
24 #include "types.h"
25 #include "data_query.h"
26 
27 namespace OHOS {
28 namespace DistributedKv {
29 // This is a public interface. Implementation of this class is in AppKvStoreImpl.
30 // This class provides put, delete, search, sync and subscribe functions of a key-value store.
31 class API_EXPORT SingleKvStore : public virtual KvStore {
32 public:
33     /**
34      * @brief Constructor.
35      */
36     API_EXPORT SingleKvStore() = default;
37 
38     /**
39      * @brief Destructor.
40      */
~SingleKvStore()41     API_EXPORT virtual ~SingleKvStore() {}
42 
43     /**
44      * @brief Get value from KvStore by its key.
45      * @param key   Key of this entry.
46      * @param value Value will be returned in this parameter.
47      * @return Return SUCCESS for success, others for failure.
48     */
49     virtual Status Get(const Key &key, Value &value) = 0;
50 
51     /**
52      * @brief Get value from KvStore by its key.
53      * If key is not found, a compensation synchronization will be automatically
54      * triggered to the specified device only when the networkId is valid.
55      *
56      * @param key   Key of this entry.
57      * @param networkId The networkId of online device.
58      * @param value Value will be returned in this parameter.
59      * @return Return SUCCESS for success, others for failure.
60     */
Get(const Key & key,const std::string & networkId,Value & value)61     virtual Status Get(const Key &key, const std::string &networkId, Value &value)
62     {
63         return Status::SUCCESS;
64     }
65 
66     /**
67      * @brief Get all entries in this store which key start with prefixKey.
68      * @param prefix  The prefix to be searched.
69      * @param entries Entries will be returned in this parameter.
70      * @return Return SUCCESS for success, others for failure.
71     */
72     virtual Status GetEntries(const Key &prefix, std::vector<Entry> &entries) const = 0;
73 
74     /**
75      * @brief Get all entries in this store by query.
76      * @param query   The query object.
77      * @param entries Entries will be returned in this parameter.
78      * @return Return SUCCESS for success, others for failure.
79     */
80     virtual Status GetEntries(const DataQuery &query, std::vector<Entry> &entries) const = 0;
81 
82     /**
83      * @brief Get resultSet in this store which key start with prefixKey.
84      * @param prefix    The prefix to be searched.
85      * @param resultSet ResultSet will be returned in this parameter.
86      * @return Return SUCCESS for success, others for failure.
87     */
88     virtual Status GetResultSet(const Key &prefix, std::shared_ptr<KvStoreResultSet> &resultSet) const = 0;
89 
90     /**
91      * @brief Get resultSet in this store by query.
92      * @param query     The query object.
93      * @param resultSet ResultSet will be returned in this parameter.
94      * @return Return SUCCESS for success, others for failure.
95     */
96     virtual Status GetResultSet(const DataQuery &query, std::shared_ptr<KvStoreResultSet> &resultSet) const = 0;
97 
98     /**
99      * @brief Close the resultSet returned by GetResultSet.
100      * @param resultSet ResultSet will be returned in this parameter.
101      * @return Return SUCCESS for success, others for failure.
102     */
103     virtual Status CloseResultSet(std::shared_ptr<KvStoreResultSet> &resultSet) = 0;
104 
105     /**
106      * @brief Get the number of result by query.
107      * @param query The query object.
108      * @param count Result will be returned in this parameter.
109      * @return Return SUCCESS for success, others for failure.
110     */
111     virtual Status GetCount(const DataQuery &query, int &count) const = 0;
112 
113     /**
114      * @brief Remove the device data synced from remote.
115      *
116      * Remove all the other devices data synced from remote if device is empty.
117      *
118      * @param device Device id.
119      * @return Return SUCCESS for success, others for failure.
120     */
121     virtual Status RemoveDeviceData(const std::string &device) = 0;
122 
123     /**
124      * @brief Get the kvstore security level.
125      *
126      * The security level is set when create store by options parameter.
127      *
128      * @param secLevel The security level will be returned.
129      * @return Return SUCCESS for success, others for failure.
130     */
131     virtual Status GetSecurityLevel(SecurityLevel &secLevel) const = 0;
132 
133     /**
134      * @brief Sync store with other devices.
135      *
136      * This is an asynchronous method.
137      * sync will fail if there is a syncing operation in progress.
138      *
139      * @param devices Device list to sync.
140      * @param mode    Mode can be set to SyncMode::PUSH, SyncMode::PULL and SyncMode::PUTH_PULL.
141      *                PUSH_PULL will firstly push all not-local store to listed devices,
142      *                then pull these stores back.
143      * @param delay   Allowed delay milli-second to sync.
144      * @return Return SUCCESS for success, others for failure.
145     */
146     virtual Status Sync(const std::vector<std::string> &devices, SyncMode mode, uint32_t delay) = 0;
147 
148     /**
149      * @brief Sync store with other devices only syncing the data which is satisfied with the condition.
150      *
151      * This is an asynchronous method.
152      * sync will fail if there is a syncing operation in progress.
153      *
154      * @param devices      Device list to sync.
155      * @param mode         Mode can be set to SyncMode::PUSH, SyncMode::PULL and SyncMode::PUSH_PULL.
156      *                     PUSH_PULL will firstly push all not-local store to listed devices,
157      *                     then pull these stores back.
158      * @param query        The query condition.
159      * @param syncCallback The callback will be called when sync finished.
160      * @return Return SUCCESS for success, others for failure.
161     */
162     virtual Status Sync(const std::vector<std::string> &devices, SyncMode mode, const DataQuery &query,
163         std::shared_ptr<KvStoreSyncCallback> syncCallback) = 0;
164 
165     /**
166      * @brief Sync store with other device, while delay is 0.
167     */
Sync(const std::vector<std::string> & devices,SyncMode mode)168     API_EXPORT inline Status Sync(const std::vector<std::string> &devices, SyncMode mode)
169     {
170         return Sync(devices, mode, 0);
171     }
172 
173     /**
174      * @brief Sync store with other device.
175      *
176      * which is satisfied with the condition.
177      * the callback pointer is nullptr.
178     */
Sync(const std::vector<std::string> & devices,SyncMode mode,const DataQuery & query)179     API_EXPORT inline Status Sync(const std::vector<std::string> &devices, SyncMode mode, const DataQuery &query)
180     {
181         return Sync(devices, mode, query, nullptr);
182     }
183 
184     /**
185      * @brief Register message for sync operation.
186      * @param callback Callback to register.
187      * @return Return SUCCESS for success, others for failure.
188     */
189     virtual Status RegisterSyncCallback(std::shared_ptr<KvStoreSyncCallback> callback) = 0;
190 
191     /**
192      * @brief Unregister all message for sync operation.
193      * @return Return SUCCESS for success, others for failure.
194     */
195     virtual Status UnRegisterSyncCallback() = 0;
196 
197     /**
198      * @brief Set synchronization parameters of this store.
199      * @param syncParam Sync policy parameter.
200      * @return Return SUCCESS for success, others for failure.
201     */
202     virtual Status SetSyncParam(const KvSyncParam &syncParam) = 0;
203 
204     /**
205      * @brief Get synchronization parameters of this store.
206      * @param syncParam Sync policy parameter.
207      * @return Return SUCCESS for success, others for failure.
208     */
209     virtual Status GetSyncParam(KvSyncParam &syncParam) = 0;
210 
211     /**
212      * @brief Set capability available for sync.
213      *
214      * If enabled is true, it will check permission before sync operation.
215      * only local labels and remote labels overlapped syncing works.
216      *
217      * @param enabled Bool paramater
218      * @return Return SUCCESS for success, others for failure.
219     */
220     virtual Status SetCapabilityEnabled(bool enabled) const = 0;
221 
222     /**
223      * @brief Set capability range for syncing.
224      *
225      * Should set capability available firstly.
226      *
227      * @param localLabels  Local labels defined by a list of string value.
228      * @param remoteLabels Remote labels defined by a list of string value.
229      * @return Return SUCCESS for success, others for failure.
230     */
231     virtual Status SetCapabilityRange(const std::vector<std::string> &localLabels,
232                                       const std::vector<std::string> &remoteLabels) const = 0;
233 
234     /**
235      * @brief Subscribe store with other devices consistently Synchronize the data
236      *        which is satisfied with the condition.
237      * @param devices Device list to sync.
238      * @param query   The query condition.
239      * @return Return SUCCESS for success, others for failure.
240     */
241     virtual Status SubscribeWithQuery(const std::vector<std::string> &devices, const DataQuery &query) = 0;
242 
243     /**
244      * @brief Unsubscribe store with other devices which is satisfied with the condition.
245      * @param devices Device list to sync.
246      * @param query   The query condition.
247      * @return Return SUCCESS for success, others for failure.
248     */
249     virtual Status UnsubscribeWithQuery(const std::vector<std::string> &devices, const DataQuery &query) = 0;
250 
251     /**
252      * @brief Set identifier.
253      * @param accountId The accountId.
254      * @param appId The name of the application.
255      * @param storeId The name of kvstore.
256      * @param tagretDev target device list.
257      * @return Return SUCCESS for success, others for failure.
258     */
SetIdentifier(const std::string & accountId,const std::string & appId,const std::string & storeId,const std::vector<std::string> & tagretDev)259     virtual Status SetIdentifier(const std::string &accountId, const std::string &appId,
260         const std::string &storeId, const std::vector<std::string> &tagretDev)
261     {
262         return Status::SUCCESS;
263     };
264 };
265 }  // namespace DistributedKv
266 }  // namespace OHOS
267 #endif  // SINGLE_KV_STORE_H
268