1 /*
2  * Copyright (c) 2023 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 RD_UTILS_H
17 #define RD_UTILS_H
18 #include <vector>
19 #include "db_errno.h"
20 #include "grd_db_api.h"
21 #include "grd_error.h"
22 #include "grd_kv_api.h"
23 #include "grd_type_export.h"
24 #include "kv_store_nb_delegate.h"
25 #include "sqlite_single_ver_storage_executor.h"
26 #include "grd_document_api.h"
27 
28 namespace DistributedDB {
29 
30 const uint32_t SQLITE_PAGE_SIZE_MIN = 1; // KByte
31 const uint32_t SQLITE_PAGE_SIZE_MAX = 64; // KByte
32 const uint32_t SQLITE_CACHE_SIZE_PAGE = 2147483647; // Max page number of cache
33 
34 constexpr const char *SYNC_COLLECTION_NAME = "naturalbase_kv_sync_data";
35 
36 std::string InitRdConfig();
37 
38 int TransferGrdErrno(int err);
39 
40 std::vector<uint8_t> KvItemToBlob(GRD_KVItemT &item);
41 
42 int GetCollNameFromType(SingleVerDataType type, std::string &collName);
43 
44 int RdKVPut(GRD_DB *db, const char *collectionName, const Key &key, const Value &value);
45 
46 int RdKVGet(GRD_DB *db, const char *collectionName, const Key &key, Value &value);
47 
48 int RdBackup(GRD_DB *db, const char *backupDbFile, uint8_t *encryptedKey, uint32_t encryptedKeyLen);
49 
50 int RdRestore(const char *dbFile, const char *backupDbFile, uint8_t *decryptedKey, uint32_t decryptedKeyLen);
51 
52 // if delete key is not exist in db, return errCode is OK.
53 int RdKVDel(GRD_DB *db, const char *collectionName, const Key &key);
54 
55 int RdKVScan(GRD_DB *db, const char *collectionName, const Key &key, GRD_KvScanModeE mode,
56     GRD_ResultSet **resultSet);
57 
58 int RdKVRangeScan(GRD_DB *db, const char *collectionName, const Key &beginKey, const Key &endKey,
59     GRD_ResultSet **resultSet);
60 
61 int RdKvFetch(GRD_ResultSet *resultSet, Key &key, Value &value);
62 
63 int RdFreeResultSet(GRD_ResultSet *resultSet);
64 
65 int RdDBClose(GRD_DB *db, uint32_t flags);
66 
67 int RdKVBatchPrepare(uint16_t itemNum, GRD_KVBatchT **batch);
68 
69 int RdKVBatchPushback(GRD_KVBatchT *batch, const Key &key, const Value &value);
70 
71 int RdKVBatchPut(GRD_DB *db, const char *kvTableName, GRD_KVBatchT *batch);
72 
73 int RdFlush(GRD_DB *db, uint32_t flags);
74 
75 // if delete key is not exist in db, return errCode is OK.
76 int RdKVBatchDel(GRD_DB *db, const char *kvTableName, GRD_KVBatchT *batch);
77 
78 int RdKVBatchDestroy(GRD_KVBatchT *batch);
79 
80 int RdDbOpen(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB *&db);
81 
82 int RdIndexPreload(GRD_DB *&db, const char *collectionName);
83 
84 int RdCreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, uint32_t flags);
85 
86 bool CheckParaOption(const KvStoreNbDelegate::Option &option,
87     const std::function<void(DBStatus, KvStoreNbDelegate *)> &callback);
88 } // namespace DistributedDB
89 #endif // RD_UTILS_H