• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = GetApiInfo();
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     InitApiInfo(configStr);
31     GetApiInfoInstance();
32     if (GRD_DBApiInfo->DBOpenApi == nullptr) {
33         GLOGE("Fail to dlysm RD api symbol");
34         return GRD_INNER_ERR;
35     }
36     int32_t ret = GRD_DBApiInfo->DBOpenApi(dbPath, configStr, flags, db);
37     if (ret != GRD_OK) {
38         GLOGE("Fail to open db");
39         UnloadApiInfo(GRD_DBApiInfo);
40         return ret;
41     }
42     return ret;
43 }
44 
GRD_DBClose(GRD_DB * db,uint32_t flags)45 GRD_API int32_t GRD_DBClose(GRD_DB *db, uint32_t flags)
46 {
47     if (GRD_DBApiInfo->DBCloseApi == nullptr) {
48         GLOGE("Fail to dlysm RD api symbol");
49         return GRD_INNER_ERR;
50     }
51     int32_t ret = GRD_DBApiInfo->DBCloseApi(db, flags);
52     if (ret != GRD_OK) {
53         GLOGE("Fail to close db");
54     }
55     UnloadApiInfo(GRD_DBApiInfo);
56     return ret;
57 }
58 
GRD_DBBackup(GRD_DB * db,const char * backupDbFile,uint8_t * encryptedKey,uint32_t encryptedKeyLen)59 GRD_API int32_t GRD_DBBackup(GRD_DB *db, const char *backupDbFile, uint8_t *encryptedKey, uint32_t encryptedKeyLen)
60 {
61     if (GRD_DBApiInfo->DBBackupApi == nullptr) {
62         GLOGE("Fail to dlysm RD api symbol");
63         return GRD_INNER_ERR;
64     }
65     GRD_CipherInfoT cipherInfo = {.hexPassword = nullptr};
66     return GRD_DBApiInfo->DBBackupApi(db, backupDbFile, &cipherInfo);
67 }
68 
GRD_DBRestore(const char * dbFile,const char * backupDbFile,uint8_t * decryptedKey,uint32_t decryptedKeyLen)69 GRD_API int32_t GRD_DBRestore(const char *dbFile, const char *backupDbFile, uint8_t *decryptedKey,
70     uint32_t decryptedKeyLen)
71 {
72     // db restore operation will start after dbclose, should reload so to link api func
73     GetApiInfoInstance();
74     if (GRD_DBApiInfo->DBRestoreApi == nullptr) {
75         GLOGE("Fail to dlysm RD api symbol");
76         UnloadApiInfo(GRD_DBApiInfo);
77         return GRD_INNER_ERR;
78     }
79     GRD_CipherInfoT cipherInfo = {.hexPassword = nullptr};
80     int32_t ret = GRD_DBApiInfo->DBRestoreApi(dbFile, backupDbFile, &cipherInfo);
81     if (ret != GRD_OK) {
82         GLOGE("Fail to restore db");
83     }
84     UnloadApiInfo(GRD_DBApiInfo);
85     return ret;
86 }
87 
GRD_Flush(GRD_DB * db,uint32_t flags)88 GRD_API int32_t GRD_Flush(GRD_DB *db, uint32_t flags)
89 {
90     if (GRD_DBApiInfo->FlushApi == nullptr) {
91         GLOGE("Fail to dlysm RD api symbol");
92         return GRD_INNER_ERR;
93     }
94     return GRD_DBApiInfo->FlushApi(db, flags);
95 }
96 
GRD_IndexPreload(GRD_DB * db,const char * collectionName)97 GRD_API int32_t GRD_IndexPreload(GRD_DB *db, const char *collectionName)
98 {
99     if (GRD_DBApiInfo->IndexPreloadApi == nullptr) {
100         GLOGE("Fail to dlysm RD api symbol");
101         return GRD_INNER_ERR;
102     }
103     return GRD_DBApiInfo->IndexPreloadApi(db, collectionName);
104 }
105