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[%s] to statement failed:%d",
296 DBCommon::TransferStringToHex(hashDev).c_str(), errCode);
297 return errCode;
298 }
299 errCode = StepForResultEntries(true, stmt, entries);
300 if (errCode != E_OK) {
301 LOGE("[SQLiteSingleVerStorageExecutor] Get device[%s] entries failed:%d",
302 DBCommon::TransferStringToHex(hashDev).c_str(), errCode);
303 return errCode;
304 }
305 LOGD("[SQLiteSingleVerStorageExecutor] Get %zu entries by device", entries.size());
306 return errCode;
307 }
308
PrepareForUnSyncTotalByTime(Timestamp begin,Timestamp end,sqlite3_stmt * & statement,bool getDeletedData) const309 int SQLiteSingleVerStorageExecutor::PrepareForUnSyncTotalByTime(Timestamp begin, Timestamp end,
310 sqlite3_stmt *&statement, bool getDeletedData) const
311 {
312 if (dbHandle_ == nullptr) {
313 return -E_INVALID_DB;
314 }
315
316 const std::string sql = (getDeletedData ? COUNT_SYNC_DELETED_ENTRIES_SQL : COUNT_SYNC_ENTRIES_SQL);
317 int errCode = SQLiteUtils::GetStatement(dbHandle_, sql, statement);
318 if (errCode != E_OK) {
319 LOGE("Prepare the sync num entries statement error:%d", errCode);
320 return errCode;
321 }
322
323 errCode = SQLiteUtils::BindInt64ToStatement(statement, BIND_BEGIN_STAMP_INDEX, begin);
324 if (errCode != E_OK) {
325 LOGE("Bind the begin timestamp for getting sync num error:%d", errCode);
326 int ret = E_OK;
327 SQLiteUtils::ResetStatement(statement, true, ret);
328 return CheckCorruptedStatus(errCode);
329 }
330
331 errCode = SQLiteUtils::BindInt64ToStatement(statement, BIND_END_STAMP_INDEX, end);
332 if (errCode != E_OK) {
333 LOGE("Bind the end timestamp for getting sync num error:%d", errCode);
334 int ret = E_OK;
335 SQLiteUtils::ResetStatement(statement, true, ret);
336 }
337 return CheckCorruptedStatus(errCode);
338 }
339
GetCountValue(sqlite3_stmt * & countStatement,uint32_t & total) const340 int SQLiteSingleVerStorageExecutor::GetCountValue(sqlite3_stmt *&countStatement, uint32_t &total) const
341 {
342 int errCode = SQLiteUtils::StepWithRetry(countStatement, isMemDb_);
343 if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
344 uint64_t readCount = static_cast<uint64_t>(sqlite3_column_int64(countStatement, 0));
345 if (readCount > UINT32_MAX) {
346 LOGW("ReadCount is beyond UINT32_MAX");
347 total = 0;
348 errCode = -E_UNEXPECTED_DATA;
349 } else {
350 total = static_cast<uint32_t>(readCount);
351 errCode = E_OK;
352 }
353 LOGD("[GetCountValue]Entry count in this result set is %" PRIu32 "", total);
354 } else {
355 errCode = -E_UNEXPECTED_DATA;
356 }
357 SQLiteUtils::ResetStatement(countStatement, true, errCode);
358 return CheckCorruptedStatus(errCode);
359 }
360
GetUnSyncTotalByTimestamp(Timestamp begin,Timestamp end,uint32_t & total) const361 int SQLiteSingleVerStorageExecutor::GetUnSyncTotalByTimestamp(Timestamp begin, Timestamp end, uint32_t &total) const
362 {
363 sqlite3_stmt *countStatement = nullptr;
364 int errCode = PrepareForUnSyncTotalByTime(begin, end, countStatement);
365 if (errCode != E_OK) {
366 return errCode;
367 }
368 return GetCountValue(countStatement, total);
369 }
370
GetDeletedSyncTotalByTimestamp(Timestamp begin,Timestamp end,uint32_t & total) const371 int SQLiteSingleVerStorageExecutor::GetDeletedSyncTotalByTimestamp(Timestamp begin, Timestamp end,
372 uint32_t &total) const
373 {
374 sqlite3_stmt *countStatement = nullptr;
375 int errCode = PrepareForUnSyncTotalByTime(begin, end, countStatement, true);
376 if (errCode != E_OK) {
377 return errCode;
378 }
379 return GetCountValue(countStatement, total);
380 }
381
GetSyncTotalWithQuery(QueryObject & query,const std::pair<Timestamp,Timestamp> & timeRange,uint32_t & total) const382 int SQLiteSingleVerStorageExecutor::GetSyncTotalWithQuery(QueryObject &query,
383 const std::pair<Timestamp, Timestamp> &timeRange, uint32_t &total) const
384 {
385 int errCode = E_OK;
386 SqliteQueryHelper helper = query.GetQueryHelper(errCode);
387 if (errCode != E_OK) {
388 return errCode;
389 }
390 sqlite3_stmt *queryStmt = nullptr;
391 errCode = helper.GetQuerySyncStatement(dbHandle_, timeRange.first, timeRange.second, queryStmt, true);
392 if (errCode != E_OK) {
393 LOGE("Get query sync num statement failed. %d", errCode);
394 return errCode;
395 }
396 return GetCountValue(queryStmt, total);
397 }
398
RemoveCloudUploadFlag(const std::vector<uint8_t> & hashKey)399 int SQLiteSingleVerStorageExecutor::RemoveCloudUploadFlag(const std::vector<uint8_t> &hashKey)
400 {
401 const std::string tableName = "naturalbase_kv_aux_sync_data_log";
402 bool isCreate = false;
403 int errCode = SQLiteUtils::CheckTableExists(dbHandle_, tableName, isCreate);
404 if (errCode != E_OK) {
405 return errCode;
406 }
407 if (!isCreate) {
408 return E_OK;
409 }
410 std::string removeSql = "UPDATE " + tableName + " SET cloud_flag=0 WHERE hash_key=?";
411 sqlite3_stmt *stmt = nullptr;
412 errCode = SQLiteUtils::GetStatement(dbHandle_, removeSql, stmt);
413 if (errCode != E_OK) {
414 LOGE("[SQLiteSingleVerStorageExecutor] Remove cloud flag get stmt failed %d", errCode);
415 return errCode;
416 }
417 errCode = SQLiteUtils::BindBlobToStatement(stmt, BIND_HASH_KEY_INDEX, hashKey);
418 if (errCode != E_OK) {
419 LOGE("[SQLiteSingleVerStorageExecutor] Remove cloud flag bind hashKey failed %d", errCode);
420 return errCode;
421 }
422 errCode = SQLiteUtils::StepWithRetry(stmt, isMemDb_);
423 if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW) || errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) {
424 errCode = E_OK;
425 }
426 int ret = E_OK;
427 SQLiteUtils::ResetStatement(stmt, true, ret);
428 if (ret != E_OK) {
429 LOGW("[SQLiteSingleVerStorageExecutor] Finalize stmt failed %d", ret);
430 }
431 return errCode == E_OK ? ret : errCode;
432 }
433
IsFromDataOwner(const DataItem & itemGet,const std::string & syncDev)434 bool SQLiteSingleVerStorageExecutor::IsFromDataOwner(const DataItem &itemGet, const std::string &syncDev)
435 {
436 return itemGet.dev == syncDev ||
437 (conflictResolvePolicy_ == DENY_OTHER_DEV_AMEND_CUR_DEV_DATA && itemGet.origDev == syncDev);
438 }
439 } // namespace DistributedDB
440