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 }
151 return CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_ALL_DEV_DATA_SQL, "", "");
152 }
153
RemoveDeviceDataInner(const std::string & deviceName,ClearMode mode)154 int SQLiteSingleVerStorageExecutor::RemoveDeviceDataInner(const std::string &deviceName, ClearMode mode)
155 {
156 if (mode == ClearMode::DEFAULT) {
157 return CloudExcuteRemoveOrUpdate(REMOVE_DEV_SYNC_DATA_BY_DEV_ID_SQL, deviceName, "");
158 }
159 int errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_ALL_HWM_DATA_SQL, "", "", true);
160 if (errCode != E_OK) {
161 return errCode;
162 }
163 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_LOG_DATA_BY_DEVID_SQL, deviceName, "");
164 if (errCode != E_OK) {
165 return errCode;
166 }
167 if (mode == FLAG_AND_DATA) {
168 return CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_BY_DEVID_SQL, deviceName, "");
169 } else if (mode == FLAG_ONLY) {
170 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_VERSION_BY_DEVID_SQL, deviceName, "");
171 if (errCode != E_OK) {
172 return errCode;
173 }
174 }
175 return CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_DEV_DATA_BY_DEVID_SQL, deviceName, "");
176 }
177
RemoveDeviceDataWithUserInner(const std::string & user,ClearMode mode)178 int SQLiteSingleVerStorageExecutor::RemoveDeviceDataWithUserInner(const std::string &user, ClearMode mode)
179 {
180 int errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_HWM_DATA_BY_USERID_SQL, "",
181 std::string(HWM_HEAD) + user, true);
182 if (errCode != E_OK) {
183 return errCode;
184 }
185 if (mode == FLAG_AND_DATA) {
186 bool isMultiHashExistInLog = false;
187 errCode = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_USERID_HASHKEY_SQL, "", user, isMultiHashExistInLog);
188 if (errCode != E_OK) {
189 return errCode;
190 }
191 if (!isMultiHashExistInLog) { // means the hashKey is unique in the log table.
192 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_BY_USERID_SQL, "", user);
193 if (errCode != E_OK) {
194 return errCode;
195 }
196 }
197 }
198 errCode = CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_DEV_DATA_BY_USERID_SQL, "", user); // delete synclog table.
199 if (errCode != E_OK) {
200 return errCode;
201 }
202 return CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_LOG_DATA_BY_USERID_SQL, "", user);
203 }
204
RemoveDeviceDataWithUserInner(const std::string & deviceName,const std::string & user,ClearMode mode)205 int SQLiteSingleVerStorageExecutor::RemoveDeviceDataWithUserInner(const std::string &deviceName,
206 const std::string &user, ClearMode mode)
207 {
208 int errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_HWM_DATA_BY_USERID_SQL, "",
209 std::string(HWM_HEAD) + user, true);
210 if (errCode != E_OK) {
211 return errCode;
212 }
213 bool isMultiHashExistInLog = false;
214 int ret = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_USERID_HASHKEY_SQL, "", user, isMultiHashExistInLog);
215 if (ret != E_OK) {
216 return ret;
217 }
218 errCode = CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_LOG_DATA_BY_USERID_DEVID_SQL, deviceName, user);
219 if (errCode != E_OK) {
220 return errCode;
221 }
222 if (mode == FLAG_AND_DATA) {
223 if (!isMultiHashExistInLog) { // means the hashKey is unique in the log table.
224 // If the hashKey does not exist in the syncLog table and type is cloud data, the data should be deleted
225 return CloudExcuteRemoveOrUpdate(REMOVE_CLOUD_DEV_DATA_BY_DEVID_HASHKEY_NOTIN_SQL, deviceName, "");
226 }
227 }
228 return CloudExcuteRemoveOrUpdate(UPDATE_CLOUD_DEV_DATA_BY_DEVID_HASHKEY_NOTIN_SQL, deviceName, "");
229 }
230
RemoveDeviceData(const std::string & deviceName,ClearMode mode)231 int SQLiteSingleVerStorageExecutor::RemoveDeviceData(const std::string &deviceName, ClearMode mode)
232 {
233 if (deviceName.empty()) {
234 return CheckCorruptedStatus(RemoveDeviceDataInner(mode));
235 }
236 bool isDataExist = false;
237 int errCode = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_DEVID_SQL, deviceName, "", isDataExist);
238 // means deviceId can not be matched in log table
239 if (mode != ClearMode::DEFAULT && (!isDataExist || errCode != E_OK)) {
240 return CheckCorruptedStatus(errCode);
241 }
242 return CheckCorruptedStatus(RemoveDeviceDataInner(deviceName, mode));
243 }
244
RemoveDeviceData(const std::string & deviceName,const std::string & user,ClearMode mode)245 int SQLiteSingleVerStorageExecutor::RemoveDeviceData(const std::string &deviceName, const std::string &user,
246 ClearMode mode)
247 {
248 if (mode == ClearMode::DEFAULT) {
249 return CheckCorruptedStatus(deviceName.empty() ?
250 RemoveDeviceDataInner(deviceName, mode) : RemoveDeviceDataInner(mode));
251 }
252 int errCode = E_OK;
253 bool isDataExist = false;
254 if (deviceName.empty()) {
255 errCode = CloudCheckDataExist(SELECT_CLOUD_DEV_DATA_BY_USERID_SQL, "", user, isDataExist);
256 if (errCode != E_OK || !isDataExist) {
257 return CheckCorruptedStatus(errCode);
258 }
259 return CheckCorruptedStatus(RemoveDeviceDataWithUserInner(user, mode));
260 }
261 errCode = CloudCheckDataExist(SELECT_CLOUD_LOG_DATA_BY_USERID_DEVID_SQL, deviceName, user, isDataExist);
262 if (errCode != E_OK || !isDataExist) {
263 return CheckCorruptedStatus(errCode);
264 }
265 return CheckCorruptedStatus(RemoveDeviceDataWithUserInner(deviceName, user, mode));
266 }
267
GetEntries(const std::string & device,std::vector<Entry> & entries) const268 int SQLiteSingleVerStorageExecutor::GetEntries(const std::string &device, std::vector<Entry> &entries) const
269 {
270 const std::string sql = SELECT_SYNC_ENTRIES_BY_DEVICE_SQL;
271 sqlite3_stmt *stmt = nullptr;
272 int errCode = SQLiteUtils::GetStatement(dbHandle_, sql, stmt);
273 if (errCode != E_OK) {
274 LOGE("[SQLiteSingleVerStorageExecutor] Get entries by device statement failed:%d", errCode);
275 return errCode;
276 }
277 ResFinalizer finalizer([stmt]() {
278 sqlite3_stmt *statement = stmt;
279 int ret = E_OK;
280 SQLiteUtils::ResetStatement(statement, true, ret);
281 if (ret != E_OK) {
282 LOGW("[SQLiteSingleVerStorageExecutor] Reset get entries statement failed:%d", ret);
283 }
284 });
285 auto hashDev = DBCommon::TransferHashString(device);
286 Value blobDev;
287 DBCommon::StringToVector(hashDev, blobDev);
288 errCode = SQLiteUtils::BindBlobToStatement(stmt, BIND_GET_ENTRIES_DEVICE_INDEX, blobDev);
289 if (errCode != E_OK) {
290 LOGE("[SQLiteSingleVerStorageExecutor] Bind hash device to statement failed:%d", errCode);
291 return errCode;
292 }
293 errCode = StepForResultEntries(true, stmt, entries);
294 if (errCode != E_OK) {
295 return errCode;
296 }
297 LOGD("[SQLiteSingleVerStorageExecutor] Get %zu entries by device", entries.size());
298 return errCode;
299 }
300
RemoveCloudUploadFlag(const std::vector<uint8_t> & hashKey)301 int SQLiteSingleVerStorageExecutor::RemoveCloudUploadFlag(const std::vector<uint8_t> &hashKey)
302 {
303 const std::string tableName = "naturalbase_kv_aux_sync_data_log";
304 bool isCreate = false;
305 int errCode = SQLiteUtils::CheckTableExists(dbHandle_, tableName, isCreate);
306 if (errCode != E_OK) {
307 return errCode;
308 }
309 if (!isCreate) {
310 return E_OK;
311 }
312 std::string removeSql = "UPDATE " + tableName + " SET cloud_flag=cloud_flag&(~" +
313 std::to_string(static_cast<uint32_t>(LogInfoFlag::FLAG_UPLOAD_FINISHED)) + ") WHERE hash_key=?";
314 sqlite3_stmt *stmt = nullptr;
315 errCode = SQLiteUtils::GetStatement(dbHandle_, removeSql, stmt);
316 if (errCode != E_OK) {
317 LOGE("[SQLiteSingleVerStorageExecutor] Remove cloud flag get stmt failed %d", errCode);
318 return errCode;
319 }
320 errCode = SQLiteUtils::BindBlobToStatement(stmt, BIND_HASH_KEY_INDEX, hashKey);
321 if (errCode != E_OK) {
322 LOGE("[SQLiteSingleVerStorageExecutor] Remove cloud flag bind hashKey failed %d", errCode);
323 return errCode;
324 }
325 errCode = SQLiteUtils::StepWithRetry(stmt, isMemDb_);
326 if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW) || errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) {
327 errCode = E_OK;
328 }
329 int ret = E_OK;
330 SQLiteUtils::ResetStatement(stmt, true, ret);
331 if (ret != E_OK) {
332 LOGW("[SQLiteSingleVerStorageExecutor] Finalize stmt failed %d", ret);
333 }
334 return errCode == E_OK ? ret : errCode;
335 }
336
IsFromDataOwner(const DataItem & itemGet,const std::string & syncDev)337 bool SQLiteSingleVerStorageExecutor::IsFromDataOwner(const DataItem &itemGet, const std::string &syncDev)
338 {
339 return itemGet.dev == syncDev ||
340 (conflictResolvePolicy_ == DENY_OTHER_DEV_AMEND_CUR_DEV_DATA && itemGet.origDev == syncDev);
341 }
342 } // namespace DistributedDB
343