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 int32_t (*DBOpen)(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db);
57 typedef int32_t (*DBClose)(GRD_DB *db, uint32_t flags);
58 typedef int32_t (*DBCreateCollection)(GRD_DB *db, const char *tableName, const char *optionStr, uint32_t flags);
59 typedef int32_t (*DBDropCollection)(GRD_DB *db, const char *collectionName, uint32_t flags);
60 typedef int32_t (*DBIndexPreload)(GRD_DB *db, const char *tableName);
61 typedef int32_t (*DBKvPut)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key, const GRD_KVItemT *value);
62 typedef int32_t (*DBKvGet)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key, const GRD_KVItemT *value);
63 typedef int32_t (*DBKvDel)(GRD_DB *db, const char *tableName, const GRD_KVItemT *key);
64 typedef int32_t (*DBKvFilter)(GRD_DB *db, const char *tableName, const GRD_FilterOptionT *scanParams,
65     GRD_ResultSet **resultSet);
66 
67 typedef int32_t (*ResultNext)(GRD_ResultSet *resultset);
68 typedef int32_t (*GetValue)(GRD_ResultSet *resultSet, char **value);
69 typedef int32_t (*GetItem)(GRD_ResultSet *resultSet, void *key, void *value);
70 typedef int32_t (*GetItemSize)(GRD_ResultSet *resultSet, uint32_t *keySize, uint32_t *valueSize);
71 typedef int32_t (*Fetch)(GRD_ResultSet *resultSet, GRD_KVItemT *key, GRD_KVItemT *value);
72 typedef int32_t (*KVFreeItem)(GRD_KVItemT *item);
73 typedef int32_t (*FreeResultSet)(GRD_ResultSet *resultSet);
74 typedef int32_t (*DBRepair)(const char *dbFile, const char *configStr);
75 
76 struct GRD_APIInfo {
77     DBOpen DbOpenApi = nullptr;
78     DBClose DbCloseApi = nullptr;
79     DBCreateCollection DbCreateCollectionApi = nullptr;
80     DBDropCollection DbDropCollectionApi = nullptr;
81     DBIndexPreload DbIndexPreloadApi = nullptr;
82     DBKvPut DbKvPutApi = nullptr;
83     DBKvGet DbKvGetApi = nullptr;
84     DBKvDel DbKvDelApi = nullptr;
85     DBKvFilter DbKvFilterApi = nullptr;
86     ResultNext NextApi = nullptr;
87     GetValue GetValueApi = nullptr;
88     GetItem GetItemApi = nullptr;
89     GetItemSize GetItemSizeApi = nullptr;
90     Fetch FetchApi = nullptr;
91     KVFreeItem FreeItemApi = nullptr;
92     FreeResultSet FreeResultSetApi = nullptr;
93     DBRepair DbRepairApi = nullptr;
94 };
95 
96 class PreferenceDbAdapter {
97 public:
98     static bool IsEnhandceDbEnable();
99     static GRD_APIInfo& GetApiInstance();
100     static void ApiInit();
101 
102     static void *gLibrary_;
103     static std::mutex apiMutex_;
104     static GRD_APIInfo api_;
105     static std::atomic<bool> isInit_;
106 };
107 
108 class PreferencesDb {
109 public:
110     PreferencesDb();
111     ~PreferencesDb();
112     int Init(const std::string &dbPath, const std::string &bundleName);
113     int Put(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value);
114     int Delete(const std::vector<uint8_t> &key);
115     int Get(const std::vector<uint8_t> &key, std::vector<uint8_t> &value);
116     int GetAll(std::list<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> &data);
117     int DropCollection();
118     int CreateCollection();
119     int GetAllInner(std::list<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> &data, GRD_ResultSet *resultSet);
120     int OpenDb(bool isNeedRebuild);
121     int CloseDb();
122     int RepairDb();
123     int TryRepairAndRebuild(int openCode);
124 private:
125     GRD_KVItemT BlobToKvItem(const std::vector<uint8_t> &blob);
126     std::vector<uint8_t> KvItemToBlob(GRD_KVItemT &item);
127     ReportParam GetReportParam(const std::string &info, uint32_t errCode);
128     GRD_DB *db_ = nullptr;
129     std::string dbPath_ = "";
130     std::string bundleName_ = "";
131 };
132 
133 // grd errcode
134 #define GRD_OK 0
135 #define GRD_NOT_SUPPORT (-1000)
136 #define GRD_OVER_LIMIT (-2000)
137 #define GRD_INVALID_ARGS (-3000)
138 #define GRD_FAILED_FILE_OPERATION (-5000)
139 #define GRD_INNER_ERR (-8000)
140 #define GRD_NO_DATA (-11000)
141 #define GRD_FAILED_MEMORY_ALLOCATE (-13000)
142 #define GRD_FAILED_MEMORY_RELEASE (-14000)
143 #define GRD_UNDEFINED_TABLE (-23000)
144 #define GRD_REBUILD_DATABASE (-38000)
145 #define GRD_PERMISSION_DENIED (-43000)
146 #define GRD_DATA_CORRUPTED (-45000)
147 #define GRD_DB_BUSY (-46000)
148 
149 } // End of namespace NativePreferences
150 } // End of namespace OHOS
151 #endif // End of #ifndef PREFERENCES_THREAD_H