1 /* 2 * Copyright (c) 2024 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 DISTRIBUTED_KV_STORE_IMPL_H 17 #define DISTRIBUTED_KV_STORE_IMPL_H 18 19 #include <string> 20 #include <mutex> 21 #include "distributed_kv_data_manager.h" 22 #include "kvstore_death_recipient.h" 23 #include "distributed_kv_store_log.h" 24 #include "ability_context_impl.h" 25 #include "kvstore_result_set.h" 26 27 namespace OHOS { 28 namespace DistributedKVStore { 29 using namespace OHOS::DistributedKv; 30 enum { 31 /* exported cj SubscribeType is (DistributedKv::SubscribeType-1) */ 32 SUBSCRIBE_LOCAL = 0, /* i.e. SubscribeType::SUBSCRIBE_TYPE_LOCAL-1 */ 33 SUBSCRIBE_REMOTE = 1, /* i.e. SubscribeType::SUBSCRIBE_TYPE_REMOTE-1 */ 34 SUBSCRIBE_LOCAL_REMOTE = 2, /* i.e. SubscribeType::SUBSCRIBE_TYPE_ALL-1 */ 35 SUBSCRIBE_COUNT = 3 36 }; 37 38 struct ContextParam { 39 std::string baseDir = ""; 40 std::string hapName = ""; 41 int32_t area = DistributedKv::Area::EL1; 42 }; 43 44 struct CJFieldNode { 45 bool nullable; 46 char* defaultString; 47 int32_t type; 48 }; 49 50 struct CJSchema { 51 CJFieldNode root; 52 char** indexes; 53 int32_t indexesSize; 54 int32_t mode; 55 int32_t skip; 56 }; 57 58 struct CJOptions { 59 bool createIfMissing; 60 bool encrypt; 61 bool backup; 62 bool autoSync; 63 int32_t kvStoreType; 64 int32_t securityLevel; 65 CJSchema schema; 66 }; 67 68 struct CArrByte { 69 u_int8_t* head; 70 int64_t size; 71 }; 72 73 struct CArrStr { 74 char** head; 75 int64_t size; 76 }; 77 78 struct CArrNumber { 79 int32_t* intList; 80 float* floatList; 81 double* doubleList; 82 int64_t size; 83 uint8_t tag; 84 }; 85 86 struct ValueType { 87 char* string; 88 int32_t integer; 89 float flo; 90 CArrByte byteArray; 91 bool boolean; 92 double dou; 93 uint8_t tag; 94 }; 95 96 struct CStringNum { 97 char** headChar; 98 int32_t* headNum; 99 int64_t size; 100 }; 101 102 struct CEntry { 103 char *key; 104 ValueType value; 105 }; 106 107 struct CArrEntry { 108 CEntry* head; 109 int64_t size; 110 }; 111 112 struct CChangeNotification { 113 CArrEntry insertEntries; 114 CArrEntry updateEntries; 115 CArrEntry deleteEntries; 116 char* deviceId; 117 }; 118 119 class CKvStoreResultSet : public OHOS::FFI::FFIData { 120 public: GetRuntimeType()121 OHOS::FFI::RuntimeType* GetRuntimeType() override 122 { 123 return GetClassType(); 124 } 125 126 explicit CKvStoreResultSet(std::shared_ptr<DistributedKv::KvStoreResultSet> kvResultSet); 127 128 std::shared_ptr<DistributedKv::KvStoreResultSet> GetKvStoreResultSet(); 129 130 int32_t GetCount(); 131 132 int32_t GetPosition(); 133 134 bool MoveToFirst(); 135 136 bool MoveToLast(); 137 138 bool MoveToNext(); 139 140 bool MoveToPrevious(); 141 142 bool Move(int32_t offset); 143 144 bool MoveToPosition(int32_t position); 145 146 bool IsFirst(); 147 148 bool IsLast(); 149 150 bool IsBeforeFirst(); 151 152 bool IsAfterLast(); 153 154 CEntry GetEntry(); 155 156 private: 157 std::shared_ptr<DistributedKv::KvStoreResultSet> kvResultSet; 158 friend class OHOS::FFI::RuntimeType; 159 friend class OHOS::FFI::TypeBase; GetClassType()160 static OHOS::FFI::RuntimeType* GetClassType() 161 { 162 static OHOS::FFI::RuntimeType runtimeType = 163 OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CKvStoreResultSet"); 164 return &runtimeType; 165 } 166 }; 167 168 class CJKVManager : public OHOS::FFI::FFIData { 169 public: GetRuntimeType()170 OHOS::FFI::RuntimeType* GetRuntimeType() override 171 { 172 return GetClassType(); 173 } 174 175 CJKVManager(); 176 CJKVManager(const char* boudleName, OHOS::AbilityRuntime::Context* context); 177 178 uint64_t GetKVStore(const char* cStoreId, const CJOptions cjOptions, int32_t& errCode); 179 int32_t CloseKVStore(const char* appId, const char* storeId); 180 int32_t DeleteKVStore(const char* appId, const char* storeId); 181 CArrStr GetAllKVStoreId(const char* appId, int32_t& errCode); 182 int32_t OnDistributedDataServiceDie(void (*callbackId)()); 183 int32_t OffDistributedDataServiceDie(void (*callbackId)()); 184 int32_t OffAllDistributedDataServiceDie(); 185 186 private: 187 class DeathRecipient : public DistributedKv::KvStoreDeathRecipient { 188 public: 189 DeathRecipient(void (*callbackId)(), const std::function<void()>& callbackRef); 190 virtual ~DeathRecipient() = default; 191 void OnRemoteDied() override; 192 void (*m_callbackId)(); 193 std::function<void()> m_callbackRef; 194 }; 195 DistributedKv::DistributedKvDataManager kvDataManager_ {}; 196 std::mutex deathMutex_ {}; 197 std::list<std::shared_ptr<DeathRecipient>> deathRecipient_; 198 std::string bundleName_ {}; 199 std::shared_ptr<ContextParam> param_; 200 friend class OHOS::FFI::RuntimeType; 201 friend class OHOS::FFI::TypeBase; GetClassType()202 static OHOS::FFI::RuntimeType* GetClassType() 203 { 204 static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CJKVManager"); 205 return &runtimeType; 206 } 207 }; 208 class CQuery : public OHOS::FFI::FFIData { 209 public: GetRuntimeType()210 OHOS::FFI::RuntimeType* GetRuntimeType() override 211 { 212 return GetClassType(); 213 } 214 CQuery()215 CQuery() {}; 216 217 const DistributedKv::DataQuery& GetDataQuery() const; 218 219 void Reset(); 220 221 void EqualTo(const std::string &field, ValueType &value); 222 223 void NotEqualTo(const std::string &field, ValueType &value); 224 225 void GreaterThan(const std::string &field, ValueType &value); 226 227 void LessThan(const std::string &field, ValueType &value); 228 229 void GreaterThanOrEqualTo(const std::string &field, ValueType &value); 230 231 void LessThanOrEqualTo(const std::string &field, ValueType &value); 232 233 void IsNull(const std::string &field); 234 235 void InNumber(const std::string &field, const CArrNumber &valueList); 236 237 void InString(const std::string &field, const CArrStr &valueList); 238 239 void NotInNumber(const std::string &field, const CArrNumber &valueList); 240 241 void NotInString(const std::string &field, const CArrStr &valueList); 242 243 void Like(const std::string &field, const std::string &value); 244 245 void Unlike(const std::string &field, const std::string &value); 246 247 void And(); 248 249 void Or(); 250 251 void OrderByAsc(const std::string &field); 252 253 void OrderByDesc(const std::string &field); 254 255 void Limit(int32_t total, int32_t offset); 256 257 void IsNotNull(const std::string &field); 258 259 void BeginGroup(); 260 261 void EndGroup(); 262 263 void PrefixKey(const std::string &prefix); 264 265 void SetSuggestIndex(const std::string &index); 266 267 void DeviceId(const std::string &deviceId); 268 269 const std::string GetSqlLike(); 270 271 private: 272 DistributedKv::DataQuery query_; 273 friend class OHOS::FFI::RuntimeType; 274 friend class OHOS::FFI::TypeBase; GetClassType()275 static OHOS::FFI::RuntimeType* GetClassType() 276 { 277 static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CQuery"); 278 return &runtimeType; 279 } 280 }; 281 282 class CJSingleKVStore : public OHOS::FFI::FFIData { 283 public: GetRuntimeType()284 OHOS::FFI::RuntimeType* GetRuntimeType() override 285 { 286 return GetClassType(); 287 } 288 289 explicit CJSingleKVStore(const std::string& storeId); 290 291 std::shared_ptr<DistributedKv::SingleKvStore> GetKvStorePtr(); 292 void SetKvStorePtr(std::shared_ptr<DistributedKv::SingleKvStore> kvStore); 293 void SetContextParam(std::shared_ptr<ContextParam> param); 294 int32_t Put(const std::string &key, const ValueType &value); 295 int32_t PutBatch(const CArrEntry &cArrEntry); 296 int32_t Delete(const std::string &key); 297 int32_t DeleteBatch(const CArrStr &cArrStr); 298 int32_t RemoveDeviceData(const std::string &deviceId); 299 ValueType Get(const std::string &key, int32_t& errCode); 300 CArrEntry GetEntries(OHOS::sptr<CQuery> query, int32_t& errCode); 301 CArrEntry GetEntries(const std::string &keyPrefix, int32_t& errCode); 302 int64_t GetResultSetByString(const std::string &keyPrefix, int32_t& errCode); 303 int64_t GetResultSetByQuery(OHOS::sptr<CQuery> query, int32_t& errCode); 304 int32_t CloseResultSet(OHOS::sptr<CKvStoreResultSet> resultSet); 305 int32_t GetResultSize(OHOS::sptr<CQuery> query, int32_t& errCode); 306 int32_t Backup(const std::string &file); 307 int32_t Restore(const std::string &file); 308 CStringNum DeleteBackup(const CArrStr &cArrStr, int32_t& errCode); 309 int32_t StartTransaction(); 310 int32_t Commit(); 311 int32_t Rollback(); 312 int32_t EnableSync(bool enabled); 313 int32_t SetSyncRange(const CArrStr &localLabels, const CArrStr &remoteSupportLabels); 314 int32_t SetSyncParam(uint32_t defaultAllowedDelayMs); 315 int32_t Sync(const CArrStr deviceIds, uint8_t mode, uint32_t delayMs); 316 int32_t SyncByQuery(const CArrStr deviceIds, OHOS::sptr<CQuery> query, uint8_t mode, uint32_t delayMs); 317 int32_t OnDataChange(uint8_t type, void (*callbackId)(const CChangeNotification valueRef)); 318 int32_t OffDataChange(void (*callbackId)(const CChangeNotification valueRef)); 319 int32_t OffAllDataChange(); 320 int32_t OnSyncComplete(void (*callbackId)(const CStringNum valueRef)); 321 int32_t OffAllSyncComplete(); 322 int32_t GetSecurityLevel(int32_t& errCode); 323 324 private: 325 class DataObserver : public DistributedKv::KvStoreObserver { 326 public: 327 DataObserver(void (*callbackId)(const CChangeNotification), 328 const std::function<void(DistributedKv::ChangeNotification)>& callbackRef); 329 virtual ~DataObserver() = default; 330 void OnChange(const DistributedKv::ChangeNotification& notification) override; 331 void (*m_callbackId)(const CChangeNotification); 332 std::function<void(DistributedKv::ChangeNotification)> m_callbackRef; 333 }; 334 class SyncObserver : public DistributedKv::KvStoreSyncCallback { 335 public: 336 SyncObserver(void (*callbackId)(const CStringNum), 337 const std::function<void(std::map<std::string, DistributedKv::Status>)>& callbackRef); 338 virtual ~SyncObserver() = default; 339 void SyncCompleted(const std::map<std::string, DistributedKv::Status>& results) override; 340 void (*m_callbackId)(const CStringNum); 341 std::function<void(std::map<std::string, DistributedKv::Status>)> m_callbackRef; 342 }; 343 std::shared_ptr<DistributedKv::SingleKvStore> kvStore_ = nullptr; 344 std::string storeId_; 345 std::shared_ptr<ContextParam> param_ = nullptr; 346 friend class OHOS::FFI::RuntimeType; 347 friend class OHOS::FFI::TypeBase; GetClassType()348 static OHOS::FFI::RuntimeType* GetClassType() 349 { 350 static OHOS::FFI::RuntimeType runtimeType = 351 OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CJSingleKVStore"); 352 return &runtimeType; 353 } 354 std::mutex listMutex_ {}; 355 std::list<std::shared_ptr<DataObserver>> dataObserver_[SUBSCRIBE_COUNT]; 356 std::list<std::shared_ptr<SyncObserver>> syncObservers_; 357 }; 358 class CJDeviceKVStore : public CJSingleKVStore { 359 public: GetRuntimeType()360 OHOS::FFI::RuntimeType* GetRuntimeType() override 361 { 362 return GetClassType(); 363 } 364 365 explicit CJDeviceKVStore(const std::string& storeId); 366 367 ValueType Get(const std::string &deviceId, const std::string &key, int32_t& errCode); 368 369 CArrEntry GetEntriesByDataQuery(DistributedKVStore::DataQuery dataQuery, int32_t& errCode); 370 371 CArrEntry GetEntries(const std::string &deviceId, const std::string &keyPrefix, int32_t& errCode); 372 373 CArrEntry GetEntries(const std::string &deviceId, OHOS::sptr<CQuery> query, int32_t& errCode); 374 375 int64_t GetResultSet(const std::string &deviceId, const std::string &keyPrefix, int32_t& errCode); 376 377 int64_t GetResultSetQuery(const std::string &deviceId, OHOS::sptr<CQuery> query, int32_t& errCode); 378 379 int32_t GetResultSize(const std::string &deviceId, OHOS::sptr<CQuery> query, int32_t& errCode); 380 381 private: 382 friend class OHOS::FFI::RuntimeType; 383 friend class OHOS::FFI::TypeBase; GetClassType()384 static OHOS::FFI::RuntimeType* GetClassType() 385 { 386 static OHOS::FFI::RuntimeType runtimeType = 387 OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CJDeviceKVStore"); 388 return &runtimeType; 389 } 390 }; 391 } 392 } // namespace OHOS::DistributedKVStore 393 394 #endif