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
16 #include "sqlite_single_ver_storage_executor.h"
17
18 #include <algorithm>
19
20 #include "cloud/cloud_store_types.h"
21 #include "data_transformer.h"
22 #include "db_constant.h"
23 #include "db_common.h"
24 #include "db_errno.h"
25 #include "parcel.h"
26 #include "platform_specific.h"
27 #include "res_finalizer.h"
28 #include "runtime_context.h"
29 #include "sqlite_meta_executor.h"
30 #include "sqlite_single_ver_storage_executor_sql.h"
31 #include "log_print.h"
32 #include "log_table_manager_factory.h"
33
34 namespace DistributedDB {
35 namespace {
36 constexpr const char *HWM_HEAD = "naturalbase_cloud_meta_sync_data_";
37 }
38
CloudExcuteRemoveOrUpdate(const std::string & sql,const std::string & deviceName,const std::string & user,bool isUserBlobType)39 int SQLiteSingleVerStorageExecutor::CloudExcuteRemoveOrUpdate(const std::string &sql, const std::string &deviceName,
40 const std::string &user, bool isUserBlobType)
41 {
42 int errCode = E_OK;
43 sqlite3_stmt *statement = nullptr;
44 errCode = SQLiteUtils::GetStatement(dbHandle_, sql, statement);
45 if (errCode != E_OK) {
46 return errCode;
47 }
48 // device name always hash string.
49 int bindIndex = 1; // 1 is the first index for blob to bind.
50 int ret = E_OK;
51 if (!user.empty()) {
52 if (isUserBlobType) {
53 std::vector<uint8_t> useVect(user.begin(), user.end());
54 errCode = SQLiteUtils::BindBlobToStatement(statement, bindIndex, useVect, true);
55 } else {
56 errCode = SQLiteUtils::BindTextToStatement(statement, bindIndex, user);
57 }
58 if (errCode != E_OK) {
59 LOGE("Failed to bind the removed device:%d", errCode);
60 SQLiteUtils::ResetStatement(statement, true, ret);
61 return errCode;
62 }
63 bindIndex++;
64 }
65 if (!deviceName.empty()) {
66 std::vector<uint8_t> devVect(deviceName.begin(), deviceName.end());
67 errCode = SQLiteUtils::BindBlobToStatement(statement, bindIndex, devVect, true); // only one arg.
68 if (errCode != E_OK) {
69 LOGE("Failed to bind the removed device:%d", errCode);
70 SQLiteUtils::ResetStatement(statement, true, ret);
71 return errCode;
72 }
73 }
74 errCode = SQLiteUtils::StepWithRetry(statement, isMemDb_);
75 if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) {
76 LOGE("Failed to execute rm the device synced data:%d", errCode);
77 } else {
78 errCode = E_OK;
79 }
80 SQLiteUtils::ResetStatement(statement, true, ret);
81 return errCode != E_OK ? errCode : ret;
82 }
83
CloudCheckDataExist(const std::string & sql,const std::string & deviceName,const std::string & user,bool & isExist)84 int SQLiteSingleVerStorageExecutor::CloudCheckDataExist(const std::string &sql, const std::string &deviceName,
85 const std::string &user, bool &isExist)
86 {
87 int errCode = E_OK;
88 sqlite3_stmt *statement = nullptr;
89 errCode = SQLiteUtils::GetStatement(dbHandle_, sql, statement);
90 if (errCode != E_OK) {
91 return errCode;
92 }
93 int bindIndex = 1; // 1 is the first index for blob to bind.
94 int ret = E_OK;
95 if (!user.empty()) {
96 errCode = SQLiteUtils::BindTextToStatement(statement, bindIndex, user); // only one arg.
97 if (errCode != E_OK) {
98 LOGE("Failed to bind the removed device:%d", errCode);
99 SQLiteUtils::ResetStatement(statement, true, ret);
100 return errCode;
101 }
102 bindIndex++;
103 if (sql == SELECT_CLOUD_LOG_DATA_BY_USERID_HASHKEY_SQL) { // the second argument is also userid.
104 errCode = SQLiteUtils::BindTextToStatement(statement, bindIndex, user); // only one arg.
105 if (errCode != E_OK) {
106 LOGE("Failed to bind the removed device:%d", errCode);
107 SQLiteUtils::ResetStatement(statement, true, ret);
108 return errCode;
109 }
110 }
111 }
112 if (!deviceName.empty()) {
113 std::vector<uint8_t> devVect(deviceName.begin(), deviceName.end());
114 errCode = SQLiteUtils::BindBlobToStatement(statement, bindIndex, devVect, true); // only one arg.
115 if (errCode != E_OK) {
116 LOGE("Failed to bind the removed device:%d", errCode);
117 SQLiteUtils::ResetStatement(statement, true, ret);
118 return errCode;
119 }
120 }
121 errCode = SQLiteUtils::StepWithRetry(statement, isMemDb_);
122 if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_DONE) && errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
123 LOGE("Failed to execute find the device synced data:%d", errCode);
124 } else {
125 if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { // means deviceId can be matched in log table
126 isExist = true;
127 }
128 SQLiteUtils::ResetStatement(statement, true, ret);
129 return E_OK;
130 }
131 SQLiteUtils::ResetStatement(statement, true, ret);
132 return errCode != E_OK ? errCode : ret;
133 }
134
RemoveDeviceDataInner(ClearMode mode)135 int SQLiteSingleVerStorageExecutor::RemoveDeviceDataInner(ClearMode mode)
136 {
137 if (mode == ClearMode::DEFAULT) {
138 return CloudExcuteRemoveOrUpdate(REMOVE_ALL_DEV_SYNC_DATA_SQL, "", "");
139 }
140 int errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_ALL_HWM_DATA_SQL, "", "", true);
141 if (errCode != E_OK) {
142 return errCode;
143 }
144 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_ALL_LOG_DATA_SQL, "", "");
145 if (errCode != E_OK) {
146 return errCode;
147 }
148 if (mode == FLAG_AND_DATA) {
149 return CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_ALL_DEV_DATA_SQL, "", "");
150 } else if (mode == FLAG_ONLY) {
151 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_ALL_DEV_DATA_VERSION_SQL, "", "");
152 if (errCode != E_OK) {
153 return errCode;
154 }
155 }
156 return CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_ALL_DEV_DATA_SQL, "", "");
157 }
158
RemoveDeviceDataInner(const std::string & deviceName,ClearMode mode)159 int SQLiteSingleVerStorageExecutor::RemoveDeviceDataInner(const std::string &deviceName, ClearMode mode)
160 {
161 if (mode == ClearMode::DEFAULT) {
162 return CloudExcuteRemoveOrUpdate(REMOVE_DEV_SYNC_DATA_BY_DEV_ID_SQL, deviceName, "");
163 }
164 int errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_ALL_HWM_DATA_SQL, "", "", true);
165 if (errCode != E_OK) {
166 return errCode;
167 }
168 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_LOG_DATA_BY_DEVID_SQL, deviceName, "");
169 if (errCode != E_OK) {
170 return errCode;
171 }
172 if (mode == FLAG_AND_DATA) {
173 return CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_BY_DEVID_SQL, deviceName, "");
174 } else if (mode == FLAG_ONLY) {
175 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_VERSION_BY_DEVID_SQL, deviceName, "");
176 if (errCode != E_OK) {
177 return errCode;
178 }
179 }
180 return CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_DEV_DATA_BY_DEVID_SQL, deviceName, "");
181 }
182
RemoveDeviceDataWithUserInner(const std::string & user,ClearMode mode)183 int SQLiteSingleVerStorageExecutor::RemoveDeviceDataWithUserInner(const std::string &user, ClearMode mode)
184 {
185 int errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_HWM_DATA_BY_USERID_SQL, "",
186 std::string(HWM_HEAD) + user, true);
187 if (errCode != E_OK) {
188 return errCode;
189 }
190 if (mode == FLAG_AND_DATA) {
191 bool isMultiHashExistInLog = false;
192 errCode = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_USERID_HASHKEY_SQL, "", user, isMultiHashExistInLog);
193 if (errCode != E_OK) {
194 return errCode;
195 }
196 if (!isMultiHashExistInLog) { // means the hashKey is unique in the log table.
197 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_BY_USERID_SQL, "", user);
198 if (errCode != E_OK) {
199 return errCode;
200 }
201 }
202 }
203 errCode = CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_DEV_DATA_BY_USERID_SQL, "", user); // delete synclog table.
204 if (errCode != E_OK) {
205 return errCode;
206 }
207 return CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_LOG_DATA_BY_USERID_SQL, "", user);
208 }
209
RemoveDeviceDataWithUserInner(const std::string & deviceName,const std::string & user,ClearMode mode)210 int SQLiteSingleVerStorageExecutor::RemoveDeviceDataWithUserInner(const std::string &deviceName,
211 const std::string &user, ClearMode mode)
212 {
213 int errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_HWM_DATA_BY_USERID_SQL, "",
214 std::string(HWM_HEAD) + user, true);
215 if (errCode != E_OK) {
216 return errCode;
217 }
218 bool isMultiHashExistInLog = false;
219 int ret = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_USERID_HASHKEY_SQL, "", user, isMultiHashExistInLog);
220 if (ret != E_OK) {
221 return ret;
222 }
223 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_LOG_DATA_BY_USERID_DEVID_SQL, deviceName, user);
224 if (errCode != E_OK) {
225 return errCode;
226 }
227 if (mode == FLAG_AND_DATA) {
228 if (!isMultiHashExistInLog) { // means the hashKey is unique in the log table.
229 // If the hashKey does not exist in the syncLog table and type is cloud data, the data should be deleted
230 return CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_BY_DEVID_HASHKEY_NOTIN_SQL, deviceName, "");
231 }
232 }
233 return CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_DEV_DATA_BY_DEVID_HASHKEY_NOTIN_SQL, deviceName, "");
234 }
235
RemoveDeviceData(const std::string & deviceName,ClearMode mode)236 int SQLiteSingleVerStorageExecutor::RemoveDeviceData(const std::string &deviceName, ClearMode mode)
237 {
238 if (deviceName.empty()) {
239 return CheckCorruptedStatus(RemoveDeviceDataInner(mode));
240 }
241 bool isDataExist = false;
242 int errCode = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_DEVID_SQL, deviceName, "", isDataExist);
243 // means deviceId can not be matched in log table
244 if (mode != ClearMode::DEFAULT && (!isDataExist || errCode != E_OK)) {
245 return CheckCorruptedStatus(errCode);
246 }
247 return CheckCorruptedStatus(RemoveDeviceDataInner(deviceName, mode));
248 }
249
RemoveDeviceData(const std::string & deviceName,const std::string & user,ClearMode mode)250 int SQLiteSingleVerStorageExecutor::RemoveDeviceData(const std::string &deviceName, const std::string &user,
251 ClearMode mode)
252 {
253 if (mode == ClearMode::DEFAULT) {
254 return CheckCorruptedStatus(deviceName.empty() ?
255 RemoveDeviceDataInner(mode) : RemoveDeviceDataInner(deviceName, mode));
256 }
257 int errCode = E_OK;
258 bool isDataExist = false;
259 if (deviceName.empty()) {
260 errCode = CloudCheckDataExist(SELECT_CLOUD_DEV_DATA_BY_USERID_SQL, "", user, isDataExist);
261 if (errCode != E_OK || !isDataExist) {
262 return CheckCorruptedStatus(errCode);
263 }
264 return CheckCorruptedStatus(RemoveDeviceDataWithUserInner(user, mode));
265 }
266 errCode = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_USERID_DEVID_SQL, deviceName, user, isDataExist);
267 if (errCode != E_OK || !isDataExist) {
268 return CheckCorruptedStatus(errCode);
269 }
270 return CheckCorruptedStatus(RemoveDeviceDataWithUserInner(deviceName, user, mode));
271 }
272
GetEntries(const std::string & device,std::vector<Entry> & entries) const273 int SQLiteSingleVerStorageExecutor::GetEntries(const std::string &device, std::vector<Entry> &entries) const
274 {
275 const std::string sql = SELECT_SYNC_ENTRIES_BY_DEVICE_SQL;
276 sqlite3_stmt *stmt = nullptr;
277 int errCode = SQLiteUtils::GetStatement(dbHandle_, sql, stmt);
278 if (errCode != E_OK) {
279 LOGE("[SQLiteSingleVerStorageExecutor] Get entries by device statement failed:%d", errCode);
280 return errCode;
281 }
282 ResFinalizer finalizer([stmt]() {
283 sqlite3_stmt *statement = stmt;
284 int ret = E_OK;
285 SQLiteUtils::ResetStatement(statement, true, ret);
286 if (ret != E_OK) {
287 LOGW("[SQLiteSingleVerStorageExecutor] Reset get entries statement failed:%d", ret);
288 }
289 });
290 auto hashDev = DBCommon::TransferHashString(device);
291 Value blobDev;
292 DBCommon::StringToVector(hashDev, blobDev);
293 errCode = SQLiteUtils::BindBlobToStatement(stmt, BIND_GET_ENTRIES_DEVICE_INDEX, blobDev);
294 if (errCode != E_OK) {
295 LOGE("[SQLiteSingleVerStorageExecutor] Bind hash device to statement failed:%d", errCode);
296 return errCode;
297 }
298 errCode = StepForResultEntries(true, stmt, entries);
299 if (errCode != E_OK) {
300 return errCode;
301 }
302 LOGD("[SQLiteSingleVerStorageExecutor] Get %zu entries by device", entries.size());
303 return errCode;
304 }
305
RemoveCloudUploadFlag(const std::vector<uint8_t> & hashKey)306 int SQLiteSingleVerStorageExecutor::RemoveCloudUploadFlag(const std::vector<uint8_t> &hashKey)
307 {
308 const std::string tableName = "naturalbase_kv_aux_sync_data_log";
309 bool isCreate = false;
310 int errCode = SQLiteUtils::CheckTableExists(dbHandle_, tableName, isCreate);
311 if (errCode != E_OK) {
312 return errCode;
313 }
314 if (!isCreate) {
315 return E_OK;
316 }
317 std::string removeSql = "UPDATE " + tableName + " SET cloud_flag=0 WHERE hash_key=?";
318 sqlite3_stmt *stmt = nullptr;
319 errCode = SQLiteUtils::GetStatement(dbHandle_, removeSql, stmt);
320 if (errCode != E_OK) {
321 LOGE("[SQLiteSingleVerStorageExecutor] Remove cloud flag get stmt failed %d", errCode);
322 return errCode;
323 }
324 errCode = SQLiteUtils::BindBlobToStatement(stmt, BIND_HASH_KEY_INDEX, hashKey);
325 if (errCode != E_OK) {
326 LOGE("[SQLiteSingleVerStorageExecutor] Remove cloud flag bind hashKey failed %d", errCode);
327 return errCode;
328 }
329 errCode = SQLiteUtils::StepWithRetry(stmt, isMemDb_);
330 if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW) || errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) {
331 errCode = E_OK;
332 }
333 int ret = E_OK;
334 SQLiteUtils::ResetStatement(stmt, true, ret);
335 if (ret != E_OK) {
336 LOGW("[SQLiteSingleVerStorageExecutor] Finalize stmt failed %d", ret);
337 }
338 return errCode == E_OK ? ret : errCode;
339 }
340
IsFromDataOwner(const DataItem & itemGet,const std::string & syncDev)341 bool SQLiteSingleVerStorageExecutor::IsFromDataOwner(const DataItem &itemGet, const std::string &syncDev)
342 {
343 return itemGet.dev == syncDev ||
344 (conflictResolvePolicy_ == DENY_OTHER_DEV_AMEND_CUR_DEV_DATA && itemGet.origDev == syncDev);
345 }
346 } // namespace DistributedDB
347