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 all entries in this store which key start with prefixKey. 53 * @param prefix The prefix to be searched. 54 * @param entries Entries will be returned in this parameter. 55 * @return Return SUCCESS for success, others for failure. 56 */ 57 virtual Status GetEntries(const Key &prefix, std::vector<Entry> &entries) const = 0; 58 59 /** 60 * @brief Get all entries in this store by query. 61 * @param query The query object. 62 * @param entries Entries will be returned in this parameter. 63 * @return Return SUCCESS for success, others for failure. 64 */ 65 virtual Status GetEntries(const DataQuery &query, std::vector<Entry> &entries) const = 0; 66 67 /** 68 * @brief Get resultSet in this store which key start with prefixKey. 69 * @param prefix The prefix to be searched. 70 * @param resultSet ResultSet will be returned in this parameter. 71 * @return Return SUCCESS for success, others for failure. 72 */ 73 virtual Status GetResultSet(const Key &prefix, std::shared_ptr<KvStoreResultSet> &resultSet) const = 0; 74 75 /** 76 * @brief Get resultSet in this store by query. 77 * @param query The query object. 78 * @param resultSet ResultSet will be returned in this parameter. 79 * @return Return SUCCESS for success, others for failure. 80 */ 81 virtual Status GetResultSet(const DataQuery &query, std::shared_ptr<KvStoreResultSet> &resultSet) const = 0; 82 83 /** 84 * @brief Close the resultSet returned by GetResultSet. 85 * @param resultSet ResultSet will be returned in this parameter. 86 * @return Return SUCCESS for success, others for failure. 87 */ 88 virtual Status CloseResultSet(std::shared_ptr<KvStoreResultSet> &resultSet) = 0; 89 90 /** 91 * @brief Get the number of result by query. 92 * @param query The query object. 93 * @param count Result will be returned in this parameter. 94 * @return Return SUCCESS for success, others for failure. 95 */ 96 virtual Status GetCount(const DataQuery &query, int &count) const = 0; 97 98 /** 99 * @brief Remove the device data synced from remote. 100 * 101 * Remove all the other devices data synced from remote if device is empty. 102 * 103 * @param device Device id. 104 * @return Return SUCCESS for success, others for failure. 105 */ 106 virtual Status RemoveDeviceData(const std::string &device) = 0; 107 108 /** 109 * @brief Get the kvstore security level. 110 * 111 * The security level is set when create store by options parameter. 112 * 113 * @param secLevel The security level will be returned. 114 * @return Return SUCCESS for success, others for failure. 115 */ 116 virtual Status GetSecurityLevel(SecurityLevel &secLevel) const = 0; 117 118 /** 119 * @brief Sync store with other devices. 120 * 121 * This is an asynchronous method. 122 * sync will fail if there is a syncing operation in progress. 123 * 124 * @param devices Device list to sync. 125 * @param mode Mode can be set to SyncMode::PUSH, SyncMode::PULL and SyncMode::PUTH_PULL. 126 * PUSH_PULL will firstly push all not-local store to listed devices, 127 * then pull these stores back. 128 * @param delay Allowed delay milli-second to sync. 129 * @return Return SUCCESS for success, others for failure. 130 */ 131 virtual Status Sync(const std::vector<std::string> &devices, SyncMode mode, uint32_t delay) = 0; 132 133 /** 134 * @brief Sync store with other devices only syncing the data which is satisfied with the condition. 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::PUSH_PULL. 141 * PUSH_PULL will firstly push all not-local store to listed devices, 142 * then pull these stores back. 143 * @param query The query condition. 144 * @param syncCallback The callback will be called when sync finished. 145 * @return Return SUCCESS for success, others for failure. 146 */ 147 virtual Status Sync(const std::vector<std::string> &devices, SyncMode mode, const DataQuery &query, 148 std::shared_ptr<KvStoreSyncCallback> syncCallback) = 0; 149 150 /** 151 * @brief Sync store with other device, while delay is 0. 152 */ Sync(const std::vector<std::string> & devices,SyncMode mode)153 API_EXPORT inline Status Sync(const std::vector<std::string> &devices, SyncMode mode) 154 { 155 return Sync(devices, mode, 0); 156 } 157 158 /** 159 * @brief Sync store with other device. 160 * 161 * which is satisfied with the condition. 162 * the callback pointer is nullptr. 163 */ Sync(const std::vector<std::string> & devices,SyncMode mode,const DataQuery & query)164 API_EXPORT inline Status Sync(const std::vector<std::string> &devices, SyncMode mode, const DataQuery &query) 165 { 166 return Sync(devices, mode, query, nullptr); 167 } 168 169 /** 170 * @brief Register message for sync operation. 171 * @param callback Callback to register. 172 * @return Return SUCCESS for success, others for failure. 173 */ 174 virtual Status RegisterSyncCallback(std::shared_ptr<KvStoreSyncCallback> callback) = 0; 175 176 /** 177 * @brief Unregister all message for sync operation. 178 * @return Return SUCCESS for success, others for failure. 179 */ 180 virtual Status UnRegisterSyncCallback() = 0; 181 182 /** 183 * @brief Set synchronization parameters of this store. 184 * @param syncParam Sync policy parameter. 185 * @return Return SUCCESS for success, others for failure. 186 */ 187 virtual Status SetSyncParam(const KvSyncParam &syncParam) = 0; 188 189 /** 190 * @brief Get synchronization parameters of this store. 191 * @param syncParam Sync policy parameter. 192 * @return Return SUCCESS for success, others for failure. 193 */ 194 virtual Status GetSyncParam(KvSyncParam &syncParam) = 0; 195 196 /** 197 * @brief Set capability available for sync. 198 * 199 * If enabled is true, it will check permission before sync operation. 200 * only local labels and remote labels overlapped syncing works. 201 * 202 * @param enabled Bool paramater 203 * @return Return SUCCESS for success, others for failure. 204 */ 205 virtual Status SetCapabilityEnabled(bool enabled) const = 0; 206 207 /** 208 * @brief Set capability range for syncing. 209 * 210 * Should set capability available firstly. 211 * 212 * @param localLabels Local labels defined by a list of string value. 213 * @param remoteLabels Remote labels defined by a list of string value. 214 * @return Return SUCCESS for success, others for failure. 215 */ 216 virtual Status SetCapabilityRange(const std::vector<std::string> &localLabels, 217 const std::vector<std::string> &remoteLabels) const = 0; 218 219 /** 220 * @brief Subscribe store with other devices consistently Synchronize the data 221 * which is satisfied with the condition. 222 * @param devices Device list to sync. 223 * @param query The query condition. 224 * @return Return SUCCESS for success, others for failure. 225 */ 226 virtual Status SubscribeWithQuery(const std::vector<std::string> &devices, const DataQuery &query) = 0; 227 228 /** 229 * @brief Unsubscribe store with other devices which is satisfied with the condition. 230 * @param devices Device list to sync. 231 * @param query The query condition. 232 * @return Return SUCCESS for success, others for failure. 233 */ 234 virtual Status UnsubscribeWithQuery(const std::vector<std::string> &devices, const DataQuery &query) = 0; 235 }; 236 } // namespace DistributedKv 237 } // namespace OHOS 238 #endif // SINGLE_KV_STORE_H 239