• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_delegate_impl.h"
17 
18 #include "db_errno.h"
19 #include "kv_store_errno.h"
20 #include "log_print.h"
21 #include "param_check_utils.h"
22 #include "relational_store_instance.h"
23 #include "sync_operation.h"
24 
25 namespace DistributedDB {
RelationalStoreDelegateImpl(RelationalStoreConnection * conn,const std::string & path)26 RelationalStoreDelegateImpl::RelationalStoreDelegateImpl(RelationalStoreConnection *conn, const std::string &path)
27     : conn_(conn),
28       storePath_(path)
29 {}
30 
~RelationalStoreDelegateImpl()31 RelationalStoreDelegateImpl::~RelationalStoreDelegateImpl()
32 {
33     if (!releaseFlag_) {
34         LOGF("[RelationalStore Delegate] Can't release directly");
35         return;
36     }
37 
38     conn_ = nullptr;
39 };
40 
RemoveDeviceData(const std::string & device)41 DBStatus RelationalStoreDelegateImpl::RemoveDeviceData(const std::string &device)
42 {
43     return RemoveDeviceData(device, {});
44 }
45 
CreateDistributedTable(const std::string & tableName)46 DBStatus RelationalStoreDelegateImpl::CreateDistributedTable(const std::string &tableName)
47 {
48     if (!ParamCheckUtils::CheckRelationalTableName(tableName)) {
49         LOGE("invalid table name.");
50         return INVALID_ARGS;
51     }
52 
53     if (conn_ == nullptr) {
54         LOGE("[RelationalStore Delegate] Invalid connection for operation!");
55         return DB_ERROR;
56     }
57 
58     int errCode = conn_->CreateDistributedTable(tableName);
59     if (errCode != E_OK) {
60         LOGE("[RelationalStore Delegate] Create Distributed table failed:%d", errCode);
61         return TransferDBErrno(errCode);
62     }
63     return OK;
64 }
65 
Sync(const std::vector<std::string> & devices,SyncMode mode,const Query & query,const SyncStatusCallback & onComplete,bool wait)66 DBStatus RelationalStoreDelegateImpl::Sync(const std::vector<std::string> &devices, SyncMode mode,
67     const Query &query, const SyncStatusCallback &onComplete, bool wait)
68 {
69     if (conn_ == nullptr) {
70         LOGE("Invalid connection for operation!");
71         return DB_ERROR;
72     }
73 
74     RelationalStoreConnection::SyncInfo syncInfo{devices, mode,
75         std::bind(&RelationalStoreDelegateImpl::OnSyncComplete, std::placeholders::_1, onComplete), query, wait};
76     int errCode = conn_->SyncToDevice(syncInfo);
77     if (errCode != E_OK) {
78         LOGW("[RelationalStore Delegate] sync data to device failed:%d", errCode);
79         return TransferDBErrno(errCode);
80     }
81     return OK;
82 }
83 
RemoveDeviceData(const std::string & device,const std::string & tableName)84 DBStatus RelationalStoreDelegateImpl::RemoveDeviceData(const std::string &device, const std::string &tableName)
85 {
86     if (conn_ == nullptr) {
87         LOGE("Invalid connection for operation!");
88         return DB_ERROR;
89     }
90 
91     if (device.empty() || device.length() > DBConstant::MAX_DEV_LENGTH ||
92         !ParamCheckUtils::CheckRelationalTableName(tableName)) {
93         LOGE("[RelationalStore Delegate] Remove device data with invalid device name or table name.");
94         return INVALID_ARGS;
95     }
96 
97     int errCode = conn_->RemoveDeviceData(device, tableName);
98     if (errCode != E_OK) {
99         LOGW("[RelationalStore Delegate] remove device data failed:%d", errCode);
100         return TransferDBErrno(errCode);
101     }
102     return OK;
103 }
104 
Close()105 DBStatus RelationalStoreDelegateImpl::Close()
106 {
107     if (conn_ == nullptr) {
108         return OK;
109     }
110 
111     int errCode = RelationalStoreInstance::ReleaseDataBaseConnection(conn_);
112     if (errCode == -E_BUSY) {
113         LOGW("[RelationalStore Delegate] busy for close");
114         return BUSY;
115     }
116     if (errCode != E_OK) {
117         LOGE("Release db connection error:%d", errCode);
118         return TransferDBErrno(errCode);
119     }
120 
121     LOGI("[RelationalStore Delegate] Close");
122     conn_ = nullptr;
123     return OK;
124 }
125 
SetReleaseFlag(bool flag)126 void RelationalStoreDelegateImpl::SetReleaseFlag(bool flag)
127 {
128     releaseFlag_ = flag;
129 }
130 
OnSyncComplete(const std::map<std::string,std::vector<TableStatus>> & devicesStatus,const SyncStatusCallback & onComplete)131 void RelationalStoreDelegateImpl::OnSyncComplete(const std::map<std::string, std::vector<TableStatus>> &devicesStatus,
132     const SyncStatusCallback &onComplete)
133 {
134     const auto &statusMap = SyncOperation::DBStatusTransMap();
135     std::map<std::string, std::vector<TableStatus>> res;
136     for (const auto &[device, tablesStatus] : devicesStatus) {
137         for (const auto &tableStatus : tablesStatus) {
138             TableStatus table;
139             table.tableName = tableStatus.tableName;
140             DBStatus status = DB_ERROR;
141             auto iterator = statusMap.find(tableStatus.status);
142             if (iterator != statusMap.end()) {
143                 status = iterator->second;
144             }
145             table.status = status;
146             res[device].push_back(table);
147         }
148     }
149     if (onComplete) {
150         onComplete(res);
151     }
152 }
153 
RemoteQuery(const std::string & device,const RemoteCondition & condition,uint64_t timeout,std::shared_ptr<ResultSet> & result)154 DBStatus RelationalStoreDelegateImpl::RemoteQuery(const std::string &device, const RemoteCondition &condition,
155     uint64_t timeout, std::shared_ptr<ResultSet> &result)
156 {
157     if (conn_ == nullptr) {
158         LOGE("Invalid connection for operation!");
159         return DB_ERROR;
160     }
161     int errCode = conn_->RemoteQuery(device, condition, timeout, result);
162     if (errCode != E_OK) {
163         LOGW("[RelationalStore Delegate] remote query failed:%d", errCode);
164         result = nullptr;
165         return TransferDBErrno(errCode);
166     }
167     return OK;
168 }
169 
RemoveDeviceData()170 DBStatus RelationalStoreDelegateImpl::RemoveDeviceData()
171 {
172     if (conn_ == nullptr) {
173         LOGE("Invalid connection for operation!");
174         return DB_ERROR;
175     }
176 
177     int errCode = conn_->RemoveDeviceData();
178     if (errCode != E_OK) {
179         LOGW("[RelationalStore Delegate] remove device data failed:%d", errCode);
180         return TransferDBErrno(errCode);
181     }
182     return OK;
183 }
184 } // namespace DistributedDB
185 #endif