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 #include "grd_base/grd_db_api.h"
16
17 #include "check_common.h"
18 #include "doc_errno.h"
19 #include "document_store_manager.h"
20 #include "grd_api_manager.h"
21 #include "grd_base/grd_error.h"
22 #include "grd_type_inner.h"
23 #include "rd_log_print.h"
24
25 using namespace DocumentDB;
26 static GRD_APIInfo GRD_DBApiInfo;
27
GRD_DBOpen(const char * dbPath,const char * configStr,uint32_t flags,GRD_DB ** db)28 GRD_API int32_t GRD_DBOpen(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db)
29 {
30 if (GRD_DBApiInfo.DBOpenApi == nullptr) {
31 InitApiInfo(configStr);
32 GRD_DBApiInfo = GetApiInfoInstance();
33 }
34 if (GRD_DBApiInfo.DBOpenApi == nullptr) {
35 GLOGE("Fail to dlysm RD api symbol");
36 return GRD_INNER_ERR;
37 }
38 return GRD_DBApiInfo.DBOpenApi(dbPath, configStr, flags, db);
39 }
40
GRD_DBClose(GRD_DB * db,uint32_t flags)41 GRD_API int32_t GRD_DBClose(GRD_DB *db, uint32_t flags)
42 {
43 if (GRD_DBApiInfo.DBCloseApi == nullptr) {
44 GRD_DBApiInfo = GetApiInfoInstance();
45 }
46 if (GRD_DBApiInfo.DBCloseApi == nullptr) {
47 GLOGE("Fail to dlysm RD api symbol");
48 return GRD_INNER_ERR;
49 }
50 return GRD_DBApiInfo.DBCloseApi(db, flags);
51 }
52
GRD_DBBackup(GRD_DB * db,const char * backupDbFile,uint8_t * encryptedKey,uint32_t encryptedKeyLen)53 GRD_API int32_t GRD_DBBackup(GRD_DB *db, const char *backupDbFile, uint8_t *encryptedKey, uint32_t encryptedKeyLen)
54 {
55 if (GRD_DBApiInfo.DBBackupApi == nullptr) {
56 GRD_DBApiInfo = GetApiInfoInstance();
57 }
58 if (GRD_DBApiInfo.DBBackupApi == nullptr) {
59 GLOGE("Fail to dlysm RD api symbol");
60 return GRD_INNER_ERR;
61 }
62 GRD_CipherInfoT cipherInfo = {.hexPassword = nullptr};
63 return GRD_DBApiInfo.DBBackupApi(db, backupDbFile, &cipherInfo);
64 }
65
GRD_DBRestore(const char * dbFile,const char * backupDbFile,uint8_t * decryptedKey,uint32_t decryptedKeyLen)66 GRD_API int32_t GRD_DBRestore(const char *dbFile, const char *backupDbFile, uint8_t *decryptedKey,
67 uint32_t decryptedKeyLen)
68 {
69 if (GRD_DBApiInfo.DBRestoreApi == nullptr) {
70 GRD_DBApiInfo = GetApiInfoInstance();
71 }
72 if (GRD_DBApiInfo.DBRestoreApi == nullptr) {
73 GLOGE("Fail to dlysm RD api symbol");
74 return GRD_INNER_ERR;
75 }
76 GRD_CipherInfoT cipherInfo = {.hexPassword = nullptr};
77 return GRD_DBApiInfo.DBRestoreApi(dbFile, backupDbFile, &cipherInfo);
78 }
79
GRD_Flush(GRD_DB * db,uint32_t flags)80 GRD_API int32_t GRD_Flush(GRD_DB *db, uint32_t flags)
81 {
82 if (GRD_DBApiInfo.FlushApi == nullptr) {
83 GRD_DBApiInfo = GetApiInfoInstance();
84 }
85 if (GRD_DBApiInfo.FlushApi == nullptr) {
86 GLOGE("Fail to dlysm RD api symbol");
87 return GRD_INNER_ERR;
88 }
89 return GRD_DBApiInfo.FlushApi(db, flags);
90 }
91
GRD_IndexPreload(GRD_DB * db,const char * collectionName)92 GRD_API int32_t GRD_IndexPreload(GRD_DB *db, const char *collectionName)
93 {
94 if (GRD_DBApiInfo.IndexPreloadApi == nullptr) {
95 GRD_DBApiInfo = GetApiInfoInstance();
96 }
97 if (GRD_DBApiInfo.IndexPreloadApi == nullptr) {
98 GLOGE("Fail to dlysm RD api symbol");
99 return GRD_INNER_ERR;
100 }
101 return GRD_DBApiInfo.IndexPreloadApi(db, collectionName);
102 }
103