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 #include "grd_base/grd_db_api.h"
17
18 #include "doc_errno.h"
19 #include "document_store_manager.h"
20 #include "grd_base/grd_error.h"
21 #include "grd_type_inner.h"
22 #include "log_print.h"
23
24 using namespace DocumentDB;
25
GRD_DBOpen(const char * dbPath,const char * configStr,uint32_t flags,GRD_DB ** db)26 GRD_API int32_t GRD_DBOpen(const char *dbPath, const char *configStr, uint32_t flags, GRD_DB **db)
27 {
28 if (db == nullptr) {
29 return GRD_INVALID_ARGS;
30 }
31 std::string path = (dbPath == nullptr ? "" : dbPath);
32 std::string config = (configStr == nullptr ? "" : configStr);
33 DocumentStore *store = nullptr;
34 int ret = DocumentStoreManager::GetDocumentStore(path, config, flags, store);
35 if (ret != E_OK || store == nullptr) {
36 return TransferDocErr(ret);
37 }
38
39 *db = new (std::nothrow) GRD_DB();
40 if (*db == nullptr) {
41 (void)DocumentStoreManager::CloseDocumentStore(store, GRD_DB_CLOSE_IGNORE_ERROR);
42 store = nullptr;
43 return GRD_FAILED_MEMORY_ALLOCATE;
44 }
45
46 (*db)->store_ = store;
47 return TransferDocErr(ret);
48 }
49
GRD_DBClose(GRD_DB * db,uint32_t flags)50 GRD_API int32_t GRD_DBClose(GRD_DB *db, uint32_t flags)
51 {
52 if (db == nullptr || db->store_ == nullptr) {
53 return GRD_INVALID_ARGS;
54 }
55
56 int ret = DocumentStoreManager::CloseDocumentStore(db->store_, flags);
57 if (ret != E_OK) {
58 return TransferDocErr(ret);
59 }
60
61 db->store_ = nullptr;
62 delete db;
63 return GRD_OK;
64 }
65
GRD_Flush(GRD_DB * db,uint32_t flags)66 GRD_API int32_t GRD_Flush(GRD_DB *db, uint32_t flags)
67 {
68 if (db == nullptr || db->store_ == nullptr) {
69 return GRD_INVALID_ARGS;
70 }
71 if (flags != GRD_DB_FLUSH_ASYNC && flags != GRD_DB_FLUSH_SYNC) {
72 return GRD_INVALID_ARGS;
73 }
74 return GRD_OK;
75 }