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 PREFERENCES_DB_ADAPTER_H 17 #define PREFERENCES_DB_ADAPTER_H 18 19 #include <vector> 20 #include <shared_mutex> 21 #include <list> 22 #include <string> 23 #include "preferences_dfx_adapter.h" 24 25 namespace OHOS { 26 namespace NativePreferences { 27 28 typedef struct GRD_DB GRD_DB; 29 30 #define GRD_DB_OPEN_CREATE 0x01 31 #define GRD_DB_OPEN_CHECK 0x04 32 #define GRD_DB_CLOSE_IGNORE_ERROR 0x01 33 34 typedef struct GRD_KVItem { 35 void *data; 36 uint32_t dataLen; 37 } GRD_KVItemT; 38 39 typedef enum GRD_KvScanMode { 40 KV_SCAN_PREFIX = 0, 41 KV_SCAN_EQUAL_OR_LESS_KEY = 1, 42 KV_SCAN_EQUAL_OR_GREATER_KEY = 2, 43 KV_SCAN_RANGE = 3, 44 KV_SCAN_ALL = 4, 45 KV_SCAN_BUTT // INVALID SCAN 46 } GRD_KvScanModeE; 47 48 typedef struct GRD_FilterOption { 49 GRD_KvScanModeE mode; 50 GRD_KVItemT begin; 51 GRD_KVItemT end; 52 } GRD_FilterOptionT; 53 54 typedef struct GRD_ResultSet GRD_ResultSet; 55 56 typedef enum GRD_ConfigType { 57 GRD_CONFIG_USER_VERSION, 58 GRD_CONFIG_DATA_VERSION, 59 GRD_CONFIG_BOTTOM, 60 } GRD_ConfigTypeE; 61 62 typedef enum GRD_DbDataType { 63 GRD_DB_DATATYPE_INTEGER = 0, 64 GRD_DB_DATATYPE_FLOAT, 65 GRD_DB_DATATYPE_TEXT, 66 GRD_DB_DATATYPE_BLOB, 67 GRD_DB_DATATYPE_FLOATVECTOR, 68 GRD_DB_DATATYPE_NULL, 69 } GRD_DbDataTypeE; 70 71 typedef struct GRD_DbValueT { 72 GRD_DbDataTypeE type; 73 union { 74 int64_t longValue; 75 double doubleValue; 76 struct { 77 union { 78 const void *strAddr; 79 void *freeAddr; 80 }; 81 uint32_t length; 82 }; 83 } value; 84 } GRD_DbValueT; 85 86 typedef enum { 87 GRD_EVENT_CORRUPTION, 88 GRD_EVENT_BOTTOM, 89 } GRD_EventTypeE; 90 91 typedef void (*GRD_EventCallbackT)(void *callbackContext, const char *eventInfo); 92 93 typedef int32_t (*DBOpen)(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db); 94 typedef int32_t (*DBClose)(GRD_DB *db, uint32_t flags); 95 typedef int32_t (*DBCreateCollection)(GRD_DB *db, const char *tableName, const char *optionStr, uint32_t flags); 96 typedef int32_t (*DBDropCollection)(GRD_DB *db, const char *collectionName, uint32_t flags); 97 typedef int32_t (*DBIndexPreload)(GRD_DB *db, const char *tableName); 98 typedef int32_t (*DBKvPut)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key, const GRD_KVItemT *value); 99 typedef int32_t (*DBKvGet)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key, const GRD_KVItemT *value); 100 typedef int32_t (*DBKvDel)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key); 101 typedef int32_t (*DBKvFilter)(GRD_DB *db, const char *tableName, const GRD_FilterOptionT *scanParams, 102 GRD_ResultSet **resultSet); 103 104 typedef int32_t (*ResultNext)(GRD_ResultSet *resultset); 105 typedef int32_t (*GetValue)(GRD_ResultSet *resultSet, char **value); 106 typedef int32_t (*GetItem)(GRD_ResultSet *resultSet, void *key, void *value); 107 typedef int32_t (*GetItemSize)(GRD_ResultSet *resultSet, uint32_t *keySize, uint32_t *valueSize); 108 typedef int32_t (*Fetch)(GRD_ResultSet *resultSet, GRD_KVItemT *key, GRD_KVItemT *value); 109 typedef int32_t (*KVFreeItem)(GRD_KVItemT *item); 110 typedef int32_t (*FreeResultSet)(GRD_ResultSet *resultSet); 111 typedef int32_t (*DBRepair)(const char *dbFile, const char *configStr); 112 typedef GRD_DbValueT (*DBGetConfig)(GRD_DB *db, GRD_ConfigTypeE type); 113 typedef int32_t (*DBSetEventCallback)(void *callbackContext, GRD_EventTypeE type, GRD_EventCallbackT callback); 114 115 struct GRD_APIInfo { 116 DBOpen DbOpenApi = nullptr; 117 DBClose DbCloseApi = nullptr; 118 DBCreateCollection DbCreateCollectionApi = nullptr; 119 DBDropCollection DbDropCollectionApi = nullptr; 120 DBIndexPreload DbIndexPreloadApi = nullptr; 121 DBKvPut DbKvPutApi = nullptr; 122 DBKvGet DbKvGetApi = nullptr; 123 DBKvDel DbKvDelApi = nullptr; 124 DBKvFilter DbKvFilterApi = nullptr; 125 ResultNext NextApi = nullptr; 126 GetValue GetValueApi = nullptr; 127 GetItem GetItemApi = nullptr; 128 GetItemSize GetItemSizeApi = nullptr; 129 Fetch FetchApi = nullptr; 130 KVFreeItem FreeItemApi = nullptr; 131 FreeResultSet FreeResultSetApi = nullptr; 132 DBRepair DbRepairApi = nullptr; 133 DBGetConfig DbGetConfigApi = nullptr; 134 DBSetEventCallback DbSetEventCallbackApi = nullptr; 135 }; 136 137 class PreferenceDbAdapter { 138 public: 139 static bool IsEnhandceDbEnable(); 140 static GRD_APIInfo& GetApiInstance(); 141 static void ApiInit(); 142 static std::string GetDbEventInfo(); 143 static void DbEventCallback(void *callbackContext, const char *eventInfo); 144 static void RegisterDbEventCallback(); 145 146 static void *gLibrary_; 147 static std::mutex apiMutex_; 148 static GRD_APIInfo api_; 149 static std::atomic<bool> isInit_; 150 static thread_local std::string eventInfo_; 151 }; 152 153 class PreferencesDb { 154 public: 155 PreferencesDb(); 156 ~PreferencesDb(); 157 int Init(const std::string &dbPath, const std::string &bundleName); 158 int Put(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value); 159 int Delete(const std::vector<uint8_t> &key); 160 int Get(const std::vector<uint8_t> &key, std::vector<uint8_t> &value); 161 int GetAll(std::list<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> &data); 162 int DropCollection(); 163 int CreateCollection(); 164 int GetAllInner(std::list<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> &data, GRD_ResultSet *resultSet); 165 int OpenDb(bool isNeedRebuild); 166 int CloseDb(); 167 int RepairDb(); 168 int TryRepairAndRebuild(int openCode); 169 int GetKernelDataVersion(int64_t &dataVersion); 170 private: 171 GRD_KVItemT BlobToKvItem(const std::vector<uint8_t> &blob); 172 std::vector<uint8_t> KvItemToBlob(GRD_KVItemT &item); 173 ReportParam GetReportParam(const std::string &info, uint32_t errCode); 174 GRD_DB *db_ = nullptr; 175 std::string dbPath_ = ""; 176 std::string bundleName_ = ""; 177 }; 178 179 // grd errcode 180 #define GRD_OK 0 181 #define GRD_NOT_SUPPORT (-1000) 182 #define GRD_OVER_LIMIT (-2000) 183 #define GRD_INVALID_ARGS (-3000) 184 #define GRD_FAILED_FILE_OPERATION (-5000) 185 #define GRD_INNER_ERR (-8000) 186 #define GRD_NO_DATA (-11000) 187 #define GRD_FAILED_MEMORY_ALLOCATE (-13000) 188 #define GRD_FAILED_MEMORY_RELEASE (-14000) 189 #define GRD_UNDEFINED_TABLE (-23000) 190 #define GRD_REBUILD_DATABASE (-38000) 191 #define GRD_PERMISSION_DENIED (-43000) 192 #define GRD_DATA_CORRUPTED (-45000) 193 #define GRD_DB_BUSY (-46000) 194 195 } // End of namespace NativePreferences 196 } // End of namespace OHOS 197 #endif // End of #ifndef PREFERENCES_THREAD_H