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 "document_store_manager.h"
17
18 #include "db_config.h"
19 #include "doc_errno.h"
20 #include "grd_base/grd_type_export.h"
21 #include "kv_store_manager.h"
22 #include "log_print.h"
23 #include "os_api.h"
24
25 namespace DocumentDB {
26 namespace {
CheckDBOpenFlag(unsigned int flag)27 bool CheckDBOpenFlag(unsigned int flag)
28 {
29 unsigned int mask = ~(GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK);
30 unsigned int invalidOpt = (GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK);
31 return ((flag & mask) == 0x00) && ((flag & invalidOpt) != invalidOpt);
32 }
33
CheckDBCloseFlag(unsigned int flag)34 bool CheckDBCloseFlag(unsigned int flag)
35 {
36 return (flag == GRD_DB_CLOSE) || (flag == GRD_DB_CLOSE_IGNORE_ERROR);
37 }
38
CheckDBCreate(uint32_t flags,const std::string & path)39 bool CheckDBCreate(uint32_t flags, const std::string &path)
40 {
41 if ((flags & GRD_DB_OPEN_CREATE) == 0 && !OSAPI::CheckPathExistence(path)) {
42 return false;
43 }
44 return true;
45 }
46 } // namespace
47
48 std::mutex DocumentStoreManager::openCloseMutex_;
49 std::map<std::string, int> DocumentStoreManager::dbConnCount_;
50
GetDocumentStore(const std::string & path,const std::string & config,uint32_t flags,DocumentStore * & store)51 int DocumentStoreManager::GetDocumentStore(const std::string &path, const std::string &config, uint32_t flags,
52 DocumentStore *&store)
53 {
54 std::string canonicalPath;
55 std::string dbName;
56 int errCode = CheckDBPath(path, canonicalPath, dbName);
57 if (errCode != E_OK) {
58 GLOGE("Check document db file path failed.");
59 return errCode;
60 }
61
62 DBConfig dbConfig = DBConfig::ReadConfig(config, errCode);
63 if (errCode != E_OK) {
64 GLOGE("Read db config str failed. %d", errCode);
65 return errCode;
66 }
67
68 if (!CheckDBOpenFlag(flags)) {
69 GLOGE("Check document db open flags failed.");
70 return -E_INVALID_ARGS;
71 }
72 if (!CheckDBCreate(flags, path)) {
73 GLOGE("Open db failed, file no exists.");
74 return -E_INVALID_ARGS;
75 }
76
77 std::lock_guard<std::mutex> lock(openCloseMutex_);
78
79 std::string dbRealPath = canonicalPath + "/" + dbName;
80 auto it = dbConnCount_.find(dbRealPath);
81 bool isFirstOpen = (it == dbConnCount_.end() || it->second == 0);
82
83 KvStoreExecutor *executor = nullptr;
84 errCode = KvStoreManager::GetKvStore(dbRealPath, dbConfig, isFirstOpen, executor);
85 if (errCode != E_OK) {
86 GLOGE("Open document store failed. %d", errCode);
87 return errCode;
88 }
89
90 store = new (std::nothrow) DocumentStore(executor);
91 if (store == nullptr) {
92 delete executor;
93 GLOGE("Memory allocation failed!");
94 return -E_FAILED_MEMORY_ALLOCATE;
95 }
96
97 store->OnClose([dbRealPath]() {
98 dbConnCount_[dbRealPath]--;
99 });
100
101 if (isFirstOpen) {
102 dbConnCount_[dbRealPath] = 1;
103 } else {
104 dbConnCount_[dbRealPath]++;
105 }
106 return errCode;
107 }
108
CloseDocumentStore(DocumentStore * store,uint32_t flags)109 int DocumentStoreManager::CloseDocumentStore(DocumentStore *store, uint32_t flags)
110 {
111 if (!CheckDBCloseFlag(flags)) {
112 GLOGE("Check document db close flags failed.");
113 return -E_INVALID_ARGS;
114 }
115
116 std::lock_guard<std::mutex> lock(openCloseMutex_);
117 int errCode = store->Close(flags);
118 if (errCode != E_OK) {
119 GLOGE("Close document store failed. %d", errCode);
120 return errCode;
121 }
122
123 delete store;
124 return E_OK;
125 }
126
CheckDBPath(const std::string & path,std::string & canonicalPath,std::string & dbName)127 int DocumentStoreManager::CheckDBPath(const std::string &path, std::string &canonicalPath, std::string &dbName)
128 {
129 if (path.empty()) {
130 GLOGE("Invalid path empty");
131 return -E_INVALID_ARGS;
132 }
133
134 if (path.back() == '/') {
135 GLOGE("Invalid path end with slash");
136 return -E_INVALID_ARGS;
137 }
138
139 std::string dirPath;
140 OSAPI::SplitFilePath(path, dirPath, dbName);
141
142 int errCode = OSAPI::GetRealPath(dirPath, canonicalPath);
143 if (errCode != E_OK) {
144 GLOGE("Get real path failed. %d", errCode);
145 return -E_FILE_OPERATION;
146 }
147
148 if (!OSAPI::CheckPermission(canonicalPath)) {
149 GLOGE("Check path permission failed.");
150 return -E_FILE_OPERATION;
151 }
152
153 return E_OK;
154 }
155 } // namespace DocumentDB