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 int32_t (*DBOpen)(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db);
87 typedef int32_t (*DBClose)(GRD_DB *db, uint32_t flags);
88 typedef int32_t (*DBCreateCollection)(GRD_DB *db, const char *tableName, const char *optionStr, uint32_t flags);
89 typedef int32_t (*DBDropCollection)(GRD_DB *db, const char *collectionName, uint32_t flags);
90 typedef int32_t (*DBIndexPreload)(GRD_DB *db, const char *tableName);
91 typedef int32_t (*DBKvPut)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key, const GRD_KVItemT *value);
92 typedef int32_t (*DBKvGet)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key, const GRD_KVItemT *value);
93 typedef int32_t (*DBKvDel)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key);
94 typedef int32_t (*DBKvFilter)(GRD_DB *db, const char *tableName, const GRD_FilterOptionT *scanParams,
95     GRD_ResultSet **resultSet);
96 
97 typedef int32_t (*ResultNext)(GRD_ResultSet *resultset);
98 typedef int32_t (*GetValue)(GRD_ResultSet *resultSet, char **value);
99 typedef int32_t (*GetItem)(GRD_ResultSet *resultSet, void *key, void *value);
100 typedef int32_t (*GetItemSize)(GRD_ResultSet *resultSet, uint32_t *keySize, uint32_t *valueSize);
101 typedef int32_t (*Fetch)(GRD_ResultSet *resultSet, GRD_KVItemT *key, GRD_KVItemT *value);
102 typedef int32_t (*KVFreeItem)(GRD_KVItemT *item);
103 typedef int32_t (*FreeResultSet)(GRD_ResultSet *resultSet);
104 typedef int32_t (*DBRepair)(const char *dbFile, const char *configStr);
105 typedef GRD_DbValueT (*DBGetConfig)(GRD_DB *db, GRD_ConfigTypeE type);
106 
107 struct GRD_APIInfo {
108     DBOpen DbOpenApi = nullptr;
109     DBClose DbCloseApi = nullptr;
110     DBCreateCollection DbCreateCollectionApi = nullptr;
111     DBDropCollection DbDropCollectionApi = nullptr;
112     DBIndexPreload DbIndexPreloadApi = nullptr;
113     DBKvPut DbKvPutApi = nullptr;
114     DBKvGet DbKvGetApi = nullptr;
115     DBKvDel DbKvDelApi = nullptr;
116     DBKvFilter DbKvFilterApi = nullptr;
117     ResultNext NextApi = nullptr;
118     GetValue GetValueApi = nullptr;
119     GetItem GetItemApi = nullptr;
120     GetItemSize GetItemSizeApi = nullptr;
121     Fetch FetchApi = nullptr;
122     KVFreeItem FreeItemApi = nullptr;
123     FreeResultSet FreeResultSetApi = nullptr;
124     DBRepair DbRepairApi = nullptr;
125     DBGetConfig DbGetConfigApi = nullptr;
126 };
127 
128 class PreferenceDbAdapter {
129 public:
130     static bool IsEnhandceDbEnable();
131     static GRD_APIInfo& GetApiInstance();
132     static void ApiInit();
133 
134     static void *gLibrary_;
135     static std::mutex apiMutex_;
136     static GRD_APIInfo api_;
137     static std::atomic<bool> isInit_;
138 };
139 
140 class PreferencesDb {
141 public:
142     PreferencesDb();
143     ~PreferencesDb();
144     int Init(const std::string &dbPath, const std::string &bundleName);
145     int Put(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value);
146     int Delete(const std::vector<uint8_t> &key);
147     int Get(const std::vector<uint8_t> &key, std::vector<uint8_t> &value);
148     int GetAll(std::list<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> &data);
149     int DropCollection();
150     int CreateCollection();
151     int GetAllInner(std::list<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> &data, GRD_ResultSet *resultSet);
152     int OpenDb(bool isNeedRebuild);
153     int CloseDb();
154     int RepairDb();
155     int TryRepairAndRebuild(int openCode);
156     int GetKernelDataVersion(int64_t &dataVersion);
157 private:
158     GRD_KVItemT BlobToKvItem(const std::vector<uint8_t> &blob);
159     std::vector<uint8_t> KvItemToBlob(GRD_KVItemT &item);
160     ReportParam GetReportParam(const std::string &info, uint32_t errCode);
161     GRD_DB *db_ = nullptr;
162     std::string dbPath_ = "";
163     std::string bundleName_ = "";
164 };
165 
166 // grd errcode
167 #define GRD_OK 0
168 #define GRD_NOT_SUPPORT (-1000)
169 #define GRD_OVER_LIMIT (-2000)
170 #define GRD_INVALID_ARGS (-3000)
171 #define GRD_FAILED_FILE_OPERATION (-5000)
172 #define GRD_INNER_ERR (-8000)
173 #define GRD_NO_DATA (-11000)
174 #define GRD_FAILED_MEMORY_ALLOCATE (-13000)
175 #define GRD_FAILED_MEMORY_RELEASE (-14000)
176 #define GRD_UNDEFINED_TABLE (-23000)
177 #define GRD_REBUILD_DATABASE (-38000)
178 #define GRD_PERMISSION_DENIED (-43000)
179 #define GRD_DATA_CORRUPTED (-45000)
180 #define GRD_DB_BUSY (-46000)
181 
182 } // End of namespace NativePreferences
183 } // End of namespace OHOS
184 #endif // End of #ifndef PREFERENCES_THREAD_H