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