1 /*
2 * Copyright (c) 2021 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 #ifdef RELATIONAL_STORE
16 #include "relational_store_manager.h"
17
18 #include <thread>
19
20 #include "auto_launch.h"
21 #include "relational_store_instance.h"
22 #include "db_common.h"
23 #include "param_check_utils.h"
24 #include "log_print.h"
25 #include "db_errno.h"
26 #include "kv_store_errno.h"
27 #include "relational_store_changed_data_impl.h"
28 #include "relational_store_delegate_impl.h"
29 #include "runtime_context.h"
30 #include "platform_specific.h"
31
32 namespace DistributedDB {
33 namespace {
34 const int GET_CONNECT_RETRY = 3;
35 const int RETRY_GET_CONN_INTER = 30;
36
InitStoreProp(const std::string & storePath,const std::string & appId,const std::string & userId,const std::string & storeId,RelationalDBProperties & properties)37 void InitStoreProp(const std::string &storePath, const std::string &appId, const std::string &userId,
38 const std::string &storeId, RelationalDBProperties &properties)
39 {
40 properties.SetStringProp(RelationalDBProperties::DATA_DIR, storePath);
41 properties.SetIdentifier(userId, appId, storeId);
42 }
43 }
44
RelationalStoreManager(const std::string & appId,const std::string & userId)45 RelationalStoreManager::RelationalStoreManager(const std::string &appId, const std::string &userId)
46 : appId_(appId),
47 userId_(userId)
48 {}
49
GetOneConnectionWithRetry(const RelationalDBProperties & properties,int & errCode)50 static RelationalStoreConnection *GetOneConnectionWithRetry(const RelationalDBProperties &properties, int &errCode)
51 {
52 for (int i = 0; i < GET_CONNECT_RETRY; i++) {
53 auto conn = RelationalStoreInstance::GetDatabaseConnection(properties, errCode);
54 if (conn != nullptr) {
55 return conn;
56 }
57 if (errCode == -E_STALE) {
58 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_GET_CONN_INTER));
59 } else {
60 return nullptr;
61 }
62 }
63 return nullptr;
64 }
65
OpenStore(const std::string & path,const std::string & storeId,const RelationalStoreDelegate::Option & option,RelationalStoreDelegate * & delegate)66 DB_API DBStatus RelationalStoreManager::OpenStore(const std::string &path, const std::string &storeId,
67 const RelationalStoreDelegate::Option &option, RelationalStoreDelegate *&delegate)
68 {
69 if (delegate != nullptr) {
70 LOGE("[RelationalStoreMgr] Invalid delegate!");
71 return INVALID_ARGS;
72 }
73
74 std::string canonicalDir;
75 if (!ParamCheckUtils::CheckDataDir(path, canonicalDir)) {
76 return INVALID_ARGS;
77 }
78
79 if (!ParamCheckUtils::CheckStoreParameter(storeId, appId_, userId_) || path.empty()) {
80 return INVALID_ARGS;
81 }
82
83 RelationalDBProperties properties;
84 InitStoreProp(canonicalDir, appId_, userId_, storeId, properties);
85
86 int errCode = E_OK;
87 auto *conn = GetOneConnectionWithRetry(properties, errCode);
88 if (conn == nullptr) {
89 return TransferDBErrno(errCode);
90 }
91
92 delegate = new (std::nothrow) RelationalStoreDelegateImpl(conn, path);
93 if (delegate == nullptr) {
94 conn->Close();
95 return DB_ERROR;
96 }
97 conn->RegisterObserverAction([option, storeId, this](const std::string &changedDevice) {
98 RelationalStoreChangedDataImpl data(changedDevice);
99 data.SetStoreProperty({userId_, appId_, storeId});
100 if (option.observer) {
101 LOGD("begin to observer on changed, changedDevice=%s", STR_MASK(changedDevice));
102 option.observer->OnChange(data);
103 }
104 });
105 return OK;
106 }
107
CloseStore(RelationalStoreDelegate * store)108 DBStatus RelationalStoreManager::CloseStore(RelationalStoreDelegate *store)
109 {
110 if (store == nullptr) {
111 return INVALID_ARGS;
112 }
113
114 auto storeImpl = static_cast<RelationalStoreDelegateImpl *>(store);
115 DBStatus status = storeImpl->Close();
116 if (status == BUSY) {
117 LOGD("NbDelegateImpl is busy now.");
118 return BUSY;
119 }
120 storeImpl->SetReleaseFlag(true);
121 delete store;
122 store = nullptr;
123 return OK;
124 }
125
GetDistributedTableName(const std::string & device,const std::string & tableName)126 std::string RelationalStoreManager::GetDistributedTableName(const std::string &device, const std::string &tableName)
127 {
128 if (device.empty() || tableName.empty()) {
129 return {};
130 }
131 return DBCommon::GetDistributedTableName(device, tableName);
132 }
133
SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback & callback)134 void RelationalStoreManager::SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback)
135 {
136 RuntimeContext::GetInstance()->SetAutoLaunchRequestCallback(callback, DBType::DB_RELATION);
137 }
138
GetRelationalStoreIdentifier(const std::string & userId,const std::string & appId,const std::string & storeId)139 std::string RelationalStoreManager::GetRelationalStoreIdentifier(const std::string &userId, const std::string &appId,
140 const std::string &storeId)
141 {
142 if (!ParamCheckUtils::CheckStoreParameter(storeId, appId, userId)) {
143 return "";
144 }
145 return DBCommon::TransferHashString(DBCommon::GenerateIdentifierId(storeId, appId, userId));
146 }
147 } // namespace DistributedDB
148 #endif