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 31 struct ContextParam { 32 std::string baseDir = ""; 33 std::string hapName = ""; 34 int32_t area = DistributedKv::Area::EL1; 35 }; 36 37 struct CJFieldNode { 38 bool nullable; 39 char* defaultString; 40 int32_t type; 41 }; 42 43 struct CJSchema { 44 CJFieldNode root; 45 char** indexes; 46 int32_t indexesSize; 47 int32_t mode; 48 int32_t skip; 49 }; 50 51 struct CJOptions { 52 bool createIfMissing; 53 bool encrypt; 54 bool backup; 55 bool autoSync; 56 int32_t kvStoreType; 57 int32_t securityLevel; 58 CJSchema schema; 59 }; 60 61 struct CArrByte { 62 u_int8_t* head; 63 int64_t size; 64 }; 65 66 struct CArrStr { 67 char** head; 68 int64_t size; 69 }; 70 71 struct ValueType { 72 char* string; 73 int32_t integer; 74 float flo; 75 CArrByte byteArray; 76 bool boolean; 77 double dou; 78 uint8_t tag; 79 }; 80 81 struct CEntry { 82 char *key; 83 ValueType value; 84 }; 85 86 struct CArrEntry { 87 CEntry* head; 88 int64_t size; 89 }; 90 91 class CJKVManager : public OHOS::FFI::FFIData { 92 public: GetRuntimeType()93 OHOS::FFI::RuntimeType* GetRuntimeType() override 94 { 95 return GetClassType(); 96 } 97 98 CJKVManager(); 99 CJKVManager(const char* boudleName, OHOS::AbilityRuntime::Context* context); 100 101 uint64_t GetKVStore(const char* cStoreId, const CJOptions cjOptions, int32_t& errCode); 102 int32_t CloseKVStore(const char* appId, const char* storeId); 103 int32_t DeleteKVStore(const char* appId, const char* storeId); 104 CArrStr GetAllKVStoreId(const char* appId, int32_t& errCode); 105 private: 106 DistributedKv::DistributedKvDataManager kvDataManager_ {}; 107 std::string bundleName_ {}; 108 std::shared_ptr<ContextParam> param_; 109 friend class OHOS::FFI::RuntimeType; 110 friend class OHOS::FFI::TypeBase; GetClassType()111 static OHOS::FFI::RuntimeType* GetClassType() 112 { 113 static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CJKVManager"); 114 return &runtimeType; 115 } 116 }; 117 118 class CJSingleKVStore : public OHOS::FFI::FFIData { 119 public: GetRuntimeType()120 OHOS::FFI::RuntimeType* GetRuntimeType() override 121 { 122 return GetClassType(); 123 } 124 125 explicit CJSingleKVStore(const std::string& storeId); 126 127 std::shared_ptr<DistributedKv::SingleKvStore> GetKvStorePtr(); 128 void SetKvStorePtr(std::shared_ptr<DistributedKv::SingleKvStore> kvStore); 129 void SetContextParam(std::shared_ptr<ContextParam> param); 130 int32_t Put(const std::string &key, const ValueType &value); 131 int32_t PutBatch(const CArrEntry &cArrEntry); 132 int32_t Delete(const std::string &key); 133 int32_t DeleteBatch(const CArrStr &cArrStr); 134 ValueType Get(const std::string &key, int32_t& errCode); 135 int32_t Backup(const std::string &file); 136 int32_t Restore(const std::string &file); 137 int32_t StartTransaction(); 138 int32_t Commit(); 139 int32_t Rollback(); 140 int32_t EnableSync(bool enabled); 141 int32_t SetSyncParam(uint32_t defaultAllowedDelayMs); 142 143 private: 144 /* private non-static members */ 145 std::shared_ptr<DistributedKv::SingleKvStore> kvStore_ = nullptr; 146 std::string storeId_; 147 std::shared_ptr<ContextParam> param_ = nullptr; 148 friend class OHOS::FFI::RuntimeType; 149 friend class OHOS::FFI::TypeBase; GetClassType()150 static OHOS::FFI::RuntimeType* GetClassType() 151 { 152 static OHOS::FFI::RuntimeType runtimeType = 153 OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CJSingleKVStore"); 154 return &runtimeType; 155 } 156 }; 157 158 class CQuery : public OHOS::FFI::FFIData { 159 public: GetRuntimeType()160 OHOS::FFI::RuntimeType* GetRuntimeType() override 161 { 162 return GetClassType(); 163 } 164 CQuery()165 CQuery() {}; 166 167 const DistributedKv::DataQuery& GetDataQuery() const; 168 169 void Reset(); 170 171 void EqualTo(const std::string &field, ValueType &value); 172 173 private: 174 DistributedKv::DataQuery query_; 175 friend class OHOS::FFI::RuntimeType; 176 friend class OHOS::FFI::TypeBase; GetClassType()177 static OHOS::FFI::RuntimeType* GetClassType() 178 { 179 static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CQuery"); 180 return &runtimeType; 181 } 182 }; 183 184 class CJDeviceKVStore : public CJSingleKVStore { 185 public: GetRuntimeType()186 OHOS::FFI::RuntimeType* GetRuntimeType() override 187 { 188 return GetClassType(); 189 } 190 191 explicit CJDeviceKVStore(const std::string& storeId); 192 193 ValueType Get(const std::string &deviceId, const std::string &key, int32_t& errCode); 194 195 CArrEntry GetEntriesByDataQuery(DistributedKVStore::DataQuery dataQuery, int32_t& errCode); 196 197 CArrEntry GetEntries(const std::string &deviceId, const std::string &keyPrefix, int32_t& errCode); 198 199 CArrEntry GetEntries(const std::string &deviceId, OHOS::sptr<CQuery> query, int32_t& errCode); 200 201 int64_t GetResultSet(const std::string &deviceId, const std::string &keyPrefix, int32_t& errCode); 202 203 int64_t GetResultSetQuery(const std::string &deviceId, OHOS::sptr<CQuery> query, int32_t& errCode); 204 205 int32_t GetResultSize(const std::string &deviceId, OHOS::sptr<CQuery> query, int32_t& errCode); 206 207 private: 208 friend class OHOS::FFI::RuntimeType; 209 friend class OHOS::FFI::TypeBase; GetClassType()210 static OHOS::FFI::RuntimeType* GetClassType() 211 { 212 static OHOS::FFI::RuntimeType runtimeType = 213 OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CJDeviceKVStore"); 214 return &runtimeType; 215 } 216 }; 217 218 class CKvStoreResultSet : public OHOS::FFI::FFIData { 219 public: GetRuntimeType()220 OHOS::FFI::RuntimeType* GetRuntimeType() override 221 { 222 return GetClassType(); 223 } 224 225 explicit CKvStoreResultSet(std::shared_ptr<DistributedKv::KvStoreResultSet> kvResultSet); 226 227 std::shared_ptr<DistributedKv::KvStoreResultSet> GetKvStoreResultSet(); 228 229 int32_t GetCount(); 230 231 private: 232 std::shared_ptr<DistributedKv::KvStoreResultSet> kvResultSet; 233 friend class OHOS::FFI::RuntimeType; 234 friend class OHOS::FFI::TypeBase; GetClassType()235 static OHOS::FFI::RuntimeType* GetClassType() 236 { 237 static OHOS::FFI::RuntimeType runtimeType = 238 OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("CKvStoreResultSet"); 239 return &runtimeType; 240 } 241 }; 242 } 243 } // namespace OHOS::DistributedKVStore 244 245 #endif