• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include <libxml/parser.h>
16 #include <libxml/tree.h>
17 
18 #include "clone_restore_geo.h"
19 
20 #include "backup_database_utils.h"
21 #include "medialibrary_data_manager_utils.h"
22 #include "media_file_utils.h"
23 #include "media_log.h"
24 #include "result_set_utils.h"
25 #include "locale_config.h"
26 #include "upgrade_restore_task_report.h"
27 #include "medialibrary_unistore_manager.h"
28 
29 namespace OHOS::Media {
30 const int32_t PAGE_SIZE = 200;
31 const int32_t UPDATE_GEO = 3;
32 const string NOT_MATCH = "NOT MATCH";
33 const string LATITUDE = "latitude";
34 const string LONGITUDE = "longitude";
35 const string LOCATION_KEY = "location_key";
36 const string CITY_ID = "city_id";
37 const string LANGUAGE = "language";
38 const string COUNTRY = "country";
39 const string ADMIN_AREA = "admin_area";
40 const string SUB_ADMIN_AREA = "sub_admin_area";
41 const string LOCALITY = "locality";
42 const string SUB_LOCALITY = "sub_locality";
43 const string THOROUGHFARE = "thoroughfare";
44 const string SUB_THOROUGHFARE = "sub_thoroughfare";
45 const string FEATURE_NAME = "feature_name";
46 const string CITY_NAME = "city_name";
47 const string ADDRESS_DESCRIPTION = "address_description";
48 const string AOI = "aoi";
49 const string POI = "poi";
50 const string FIRST_AOI = "first_aoi";
51 const string FIRST_POI = "first_poi";
52 const string LOCATION_VERSION = "location_version";
53 const string FIRST_AOI_CATEGORY = "first_aoi_category";
54 const string FIRST_POI_CATEGORY = "first_poi_category";
55 const string LOCATION_TYPE = "location_type";
56 const string FILE_ID = "file_id";
57 const string DATA = "data";
58 const string SINGLE_CH = "zh-Hans";
59 const string GEO = "geo";
60 const string GEO_KNOWLEDGE_TABLE = "tab_analysis_geo_knowledge";
61 const string ANA_TOTAL_TABLE = "tab_analysis_total";
62 const string INTEGER = "INTEGER";
63 const int32_t GEO_STATUS_SUCCESS = 1;
64 const int32_t ANALYSISED_STATUS = 2;
65 constexpr double DOUBLE_EPSILON = 1e-15;
66 
67 const unordered_map<string, unordered_set<string>> COMPARED_COLUMNS_MAP = {
68     { "tab_analysis_geo_knowledge",
69         {
70             "latitude",
71             "longitude",
72             "location_key",
73             "city_id",
74             "language",
75             "country",
76             "admin_area",
77             "sub_admin_area",
78             "locality",
79             "sub_locality",
80             "thoroughfare",
81             "sub_thoroughfare",
82             "feature_name",
83             "city_name",
84             "address_description",
85             "aoi",
86             "poi",
87             "first_aoi",
88             "first_poi",
89             "location_version",
90             "first_aoi_category",
91             "first_poi_category",
92             "file_id",
93             "location_type"
94         }
95     }
96 };
97 
98 template<typename Key, typename Value>
GetValueFromMap(const unordered_map<Key,Value> & map,const Key & key,const Value & defaultValue=Value ())99 Value GetValueFromMap(const unordered_map<Key, Value> &map, const Key &key, const Value &defaultValue = Value())
100 {
101     auto it = map.find(key);
102     CHECK_AND_RETURN_RET(it != map.end(), defaultValue);
103     return it->second;
104 }
105 
Init(int32_t sceneCode,const std::string & taskId,std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,std::shared_ptr<NativeRdb::RdbStore> mediaRdb)106 void CloneRestoreGeo::Init(int32_t sceneCode, const std::string &taskId,
107     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::shared_ptr<NativeRdb::RdbStore> mediaRdb)
108 {
109     sceneCode_ = sceneCode;
110     taskId_ = taskId;
111     mediaLibraryRdb_ = mediaLibraryRdb;
112     mediaRdb_ = mediaRdb;
113     successInsertCnt_ = 0;
114     successUpdateCnt_ = 0;
115     failInsertCnt_ = 0;
116     failUpdateCnt_ = 0;
117     systemLanguage_ = Global::I18n::LocaleConfig::GetSystemLanguage();
118 }
119 
RestoreGeoKnowledgeInfos()120 void CloneRestoreGeo::RestoreGeoKnowledgeInfos()
121 {
122     bool cond = (mediaRdb_ == nullptr || mediaLibraryRdb_ == nullptr);
123     CHECK_AND_RETURN_LOG(!cond, "rdbStore is nullptr");
124     GetGeoKnowledgeInfos();
125     GetAnalysisGeoInfos();
126 }
127 
GetGeoKnowledgeInfos()128 void CloneRestoreGeo::GetGeoKnowledgeInfos()
129 {
130     std::unordered_map<std::string, std::string> columns;
131     columns[FILE_ID] = INTEGER;
132     bool hasRequiredColumns = CheckTableColumns(GEO_KNOWLEDGE_TABLE, columns);
133     if (!hasRequiredColumns) {
134         MEDIA_ERR_LOG("The tab_analysis_geo_knowledge does not contain the required columns.");
135         ErrorInfo errorInfo(RestoreError::TABLE_LACK_OF_COLUMN, static_cast<int32_t>(columns.size()),
136             "", "The tab_analysis_geo_knowledge does not contain file_id");
137         UpgradeRestoreTaskReport().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).ReportError(errorInfo);
138         return;
139     }
140     const std::string QUERY_SQL_GEO = "SELECT * FROM " + GEO_KNOWLEDGE_TABLE + " WHERE " + FILE_ID +
141         " in (SELECT " + FILE_ID + " FROM Photos) LIMIT ?, ?";
142     int32_t rowCount = 0;
143     int32_t offset = 0;
144     do {
145         std::vector<NativeRdb::ValueObject> params = {offset, PAGE_SIZE};
146         auto resultSet = BackupDatabaseUtils::QuerySql(mediaRdb_, QUERY_SQL_GEO, params);
147         if (resultSet == nullptr) {
148             MEDIA_ERR_LOG("Query resultSql is null.");
149             break;
150         }
151         while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
152             GeoCloneInfo info;
153             GetGeoKnowledgeInfo(info, resultSet);
154             geoInfos_.emplace_back(info);
155         }
156         resultSet->GetRowCount(rowCount);
157         offset += PAGE_SIZE;
158         resultSet->Close();
159     } while (rowCount > 0);
160     MEDIA_INFO_LOG("query tab_analysis_geo_knowledge nums: %{public}zu", geoInfos_.size());
161 }
162 
GetAnalysisGeoInfos()163 void CloneRestoreGeo::GetAnalysisGeoInfos()
164 {
165     std::unordered_map<std::string, std::string> columns;
166     columns[FILE_ID] = INTEGER;
167     columns[GEO] = INTEGER;
168     bool hasRequiredColumns = CheckTableColumns(ANA_TOTAL_TABLE, columns);
169     if (!hasRequiredColumns) {
170         MEDIA_ERR_LOG("The tab_analysis_total does not contain the required columns.");
171         ErrorInfo errorInfo(RestoreError::TABLE_LACK_OF_COLUMN, static_cast<int32_t>(columns.size()),
172             "", "The tab_analysis_total does not contain file_id or geo");
173         UpgradeRestoreTaskReport().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).ReportError(errorInfo);
174         return;
175     }
176     const std::string QUERY_SQL_ANA = "SELECT " + FILE_ID + ", " + GEO + " FROM " + ANA_TOTAL_TABLE +
177         " WHERE " + FILE_ID + " in (SELECT " + FILE_ID + " FROM Photos) LIMIT ?, ?";
178     int32_t rowCount = 0;
179     int32_t offset = 0;
180     do {
181         std::vector<NativeRdb::ValueObject> params = {offset, PAGE_SIZE};
182         auto resultSet = BackupDatabaseUtils::QuerySql(mediaRdb_, QUERY_SQL_ANA, params);
183         if (resultSet == nullptr) {
184             MEDIA_ERR_LOG("Query resultSql is null.");
185             break;
186         }
187         while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
188             AnaTotalInfo info;
189             info.fileId = GetInt32Val(FILE_ID, resultSet);
190             info.geo = GetInt32Val(GEO, resultSet);
191             anaTotalfos_.emplace_back(info);
192         }
193         resultSet->GetRowCount(rowCount);
194         offset += PAGE_SIZE;
195         resultSet->Close();
196     } while (rowCount > 0);
197     MEDIA_INFO_LOG("query tab_analysis_total nums: %{public}zu", anaTotalfos_.size());
198 }
199 
CheckTableColumns(const std::string & tableName,std::unordered_map<std::string,std::string> & columns)200 bool CloneRestoreGeo::CheckTableColumns(const std::string& tableName,
201     std::unordered_map<std::string, std::string>& columns)
202 {
203     std::unordered_map<std::string, std::string> srcColumnInfoMap =
204         BackupDatabaseUtils::GetColumnInfoMap(mediaRdb_, tableName);
205     std::unordered_set<std::string> result;
206     for (auto it = columns.begin(); it != columns.end(); ++it) {
207         if (srcColumnInfoMap.find(it->first) != srcColumnInfoMap.end()) {
208             result.insert(it->first);
209             continue;
210         }
211         return false;
212     }
213     return true;
214 }
215 
GetGeoKnowledgeInfo(GeoCloneInfo & info,std::shared_ptr<NativeRdb::ResultSet> resultSet)216 void CloneRestoreGeo::GetGeoKnowledgeInfo(GeoCloneInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet)
217 {
218     info.locationKey = BackupDatabaseUtils::GetOptionalValue<int64_t>(resultSet, LOCATION_KEY);
219     info.cityId = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, CITY_ID);
220     info.language = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, LANGUAGE);
221     info.country = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, COUNTRY);
222     info.adminArea = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, ADMIN_AREA);
223     info.subAdminArea = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, SUB_ADMIN_AREA);
224     info.locality = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, LOCALITY);
225     info.subLocality = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, SUB_LOCALITY);
226     info.thoroughfare = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, THOROUGHFARE);
227     info.subThoroughfare = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, SUB_THOROUGHFARE);
228     info.featureName = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, FEATURE_NAME);
229     info.cityName = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, CITY_NAME);
230     info.addressDescription = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, ADDRESS_DESCRIPTION);
231     info.aoi = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, AOI);
232     info.poi = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, POI);
233     info.firstAoi = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, FIRST_AOI);
234     info.firstPoi = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, FIRST_POI);
235     info.locationVersion = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, LOCATION_VERSION);
236     info.firstAoiCategory = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, FIRST_AOI_CATEGORY);
237     info.firstPoiCategory = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, FIRST_POI_CATEGORY);
238     info.locationType = BackupDatabaseUtils::GetOptionalValue<std::string>(resultSet, LOCATION_TYPE);
239     info.latitude = BackupDatabaseUtils::GetOptionalValue<double>(resultSet, LATITUDE);
240     info.longitude = BackupDatabaseUtils::GetOptionalValue<double>(resultSet, LONGITUDE);
241 }
242 
RestoreMaps(std::vector<FileInfo> & fileInfos)243 void CloneRestoreGeo::RestoreMaps(std::vector<FileInfo> &fileInfos)
244 {
245     MEDIA_INFO_LOG("CloneRestoreGeo RestoreMaps");
246     bool cond = (mediaRdb_ == nullptr || mediaLibraryRdb_ == nullptr);
247     CHECK_AND_RETURN_LOG(!cond, "rdbStore is nullptr");
248     CHECK_AND_RETURN_INFO_LOG(!geoInfos_.empty(), "geoInfos_ is empty");
249     int32_t batchCnt = 0;
250     int32_t batchAnaCnt = 0;
251     std::vector<std::string> fileIds;
252     std::vector<NativeRdb::ValuesBucket> values;
253     std::vector<std::string> analysisIds;
254     BatchQueryPhoto(fileInfos);
255     for (const auto &fileInfo : fileInfos) {
256         std::string fileIdString = UpdateMapInsertValues(values, analysisIds, fileInfo, batchCnt, batchAnaCnt);
257         if (fileIdString != NOT_MATCH) {
258             fileIds.emplace_back(fileIdString);
259         }
260     }
261     int64_t rowNum = 0;
262     int32_t errCodeGeo = BatchInsertWithRetry(GEO_KNOWLEDGE_TABLE, values, rowNum);
263     if (errCodeGeo != E_OK) {
264         MEDIA_ERR_LOG("GeoKnowledge: RestoreMaps insert fail");
265         ErrorInfo errorInfo(RestoreError::INSERT_FAILED, static_cast<int32_t>(values.size()),
266             "errCodeGeo: " + std::to_string(errCodeGeo), "GeoKnowledge: RestoreMaps insert fail");
267         UpgradeRestoreTaskReport().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).ReportError(errorInfo);
268         failInsertCnt_ += batchCnt;
269         return;
270     }
271     successInsertCnt_ += batchCnt;
272     int32_t errCodeUpdate = BatchUpdate(ANA_TOTAL_TABLE, fileIds);
273     if (errCodeUpdate != E_OK) {
274         FailUpdate(errCodeUpdate, batchCnt, batchAnaCnt);
275         return;
276     }
277     int32_t errCodeUpdateAna = BatchUpdateAna(ANA_TOTAL_TABLE, analysisIds);
278     if (errCodeUpdateAna != E_OK) {
279         FailUpdateAna(errCodeUpdateAna, batchAnaCnt);
280         return;
281     }
282     successUpdateCnt_ += batchCnt;
283 }
284 
BatchQueryPhoto(std::vector<FileInfo> & fileInfos)285 void CloneRestoreGeo::BatchQueryPhoto(std::vector<FileInfo> &fileInfos)
286 {
287     std::stringstream querySql;
288     querySql << "SELECT " + FILE_ID + ", " + DATA + " FROM Photos WHERE " + DATA + " IN (";
289     std::vector<NativeRdb::ValueObject> params;
290     int32_t count = 0;
291     for (const auto &fileInfo : fileInfos) {
292         // no need query or alreay queried
293         if ((fabs(fileInfo.latitude) < DOUBLE_EPSILON && fabs(fileInfo.longitude) < DOUBLE_EPSILON)
294             || fileInfo.fileIdNew > 0) {
295             continue;
296         }
297         querySql << (count++ > 0 ? "," : "");
298         querySql << "?";
299         params.emplace_back(fileInfo.cloudPath);
300     }
301     querySql << ")";
302     auto resultSet = BackupDatabaseUtils::QuerySql(mediaLibraryRdb_, querySql.str(), params);
303     CHECK_AND_RETURN_LOG(resultSet != nullptr, "resultSet is nullptr");
304 
305     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
306         int32_t fileId = GetInt32Val(FILE_ID, resultSet);
307         std::string data = GetStringVal(DATA, resultSet);
308         auto it = std::find_if(fileInfos.begin(), fileInfos.end(),
309             [data](const FileInfo& info) {
310                 return info.cloudPath == data;
311             });
312         if (it == fileInfos.end()) {
313             continue;
314         }
315         it->fileIdNew = fileId;
316     }
317     resultSet->Close();
318 }
319 
UpdateMapInsertValues(std::vector<NativeRdb::ValuesBucket> & values,std::vector<std::string> & analysisIds,const FileInfo & fileInfo,int32_t & batchCnt,int32_t & batchAnaCnt)320 std::string CloneRestoreGeo::UpdateMapInsertValues(std::vector<NativeRdb::ValuesBucket> &values,
321     std::vector<std::string> &analysisIds, const FileInfo &fileInfo, int32_t &batchCnt, int32_t &batchAnaCnt)
322 {
323     // no need restore or info missing
324     bool cond = ((fabs(fileInfo.latitude) < DOUBLE_EPSILON && fabs(fileInfo.longitude) < DOUBLE_EPSILON)
325         || fileInfo.fileIdNew <= 0);
326     CHECK_AND_RETURN_RET(!cond, NOT_MATCH);
327     return UpdateByGeoLocation(values, analysisIds, fileInfo, batchCnt, batchAnaCnt);
328 }
329 
UpdateByGeoLocation(std::vector<NativeRdb::ValuesBucket> & values,std::vector<std::string> & analysisIds,const FileInfo & fileInfo,int32_t & batchCnt,int32_t & batchAnaCnt)330 std::string CloneRestoreGeo::UpdateByGeoLocation(std::vector<NativeRdb::ValuesBucket> &values,
331     std::vector<std::string> &analysisIds, const FileInfo &fileInfo, int32_t &batchCnt, int32_t &batchAnaCnt)
332 {
333     std::string language = systemLanguage_;
334     double latitude = fileInfo.latitude;
335     double longitude = fileInfo.longitude;
336     int32_t fileIdOld = fileInfo.fileIdOld;
337     if (language.empty()) {
338         language = SINGLE_CH;
339     }
340     auto itGeo = std::find_if(geoInfos_.begin(), geoInfos_.end(),
341         [latitude, longitude, language](const GeoCloneInfo& info) {
342             bool cond = (!info.latitude.has_value() || !info.longitude.has_value() || !info.language.has_value());
343             CHECK_AND_RETURN_RET(!cond, false);
344             return fabs(info.latitude.value() - latitude) < 0.0001
345                 && fabs(info.longitude.value() - longitude) < 0.0001 && info.language.value() == language;
346         });
347     if (itGeo == geoInfos_.end()) {
348         MEDIA_INFO_LOG("not match fileId: %{public}d", fileInfo.fileIdNew);
349         return NOT_MATCH;
350     }
351 
352     std::unordered_set<std::string> intersection = GetCommonColumns(GEO_KNOWLEDGE_TABLE);
353     NativeRdb::ValuesBucket value;
354     GetMapInsertValue(value, itGeo, intersection, fileInfo.fileIdNew);
355     values.emplace_back(value);
356     batchCnt++;
357     auto comparedColumns = GetValueFromMap(COMPARED_COLUMNS_MAP, GEO_KNOWLEDGE_TABLE);
358     if (intersection.size() == comparedColumns.size()) {
359         auto itAna = std::find_if(anaTotalfos_.begin(), anaTotalfos_.end(),
360             [fileIdOld](const AnaTotalInfo& info) {
361             return info.fileId == fileIdOld && info.geo == ANALYSISED_STATUS;
362         });
363         if (itAna != anaTotalfos_.end()) {
364             analysisIds.emplace_back(std::to_string(fileInfo.fileIdNew));
365             batchAnaCnt++;
366             return NOT_MATCH;
367         }
368     }
369     return std::to_string(fileInfo.fileIdNew);
370 }
371 
GetCommonColumns(const string & tableName)372 std::unordered_set<std::string> CloneRestoreGeo::GetCommonColumns(const string &tableName)
373 {
374     std::unordered_map<std::string, std::string> srcColumnInfoMap =
375         BackupDatabaseUtils::GetColumnInfoMap(mediaRdb_, tableName);
376     std::unordered_map<std::string, std::string> dstColumnInfoMap =
377         BackupDatabaseUtils::GetColumnInfoMap(mediaLibraryRdb_, tableName);
378     std::unordered_set<std::string> result;
379     auto comparedColumns = GetValueFromMap(COMPARED_COLUMNS_MAP, tableName);
380     for (auto it = dstColumnInfoMap.begin(); it != dstColumnInfoMap.end(); ++it) {
381         if (srcColumnInfoMap.find(it->first) != srcColumnInfoMap.end() && comparedColumns.count(it->first) > 0) {
382             result.insert(it->first);
383         }
384     }
385     return result;
386 }
387 
GetMapInsertValue(NativeRdb::ValuesBucket & value,std::vector<GeoCloneInfo>::iterator it,const std::unordered_set<std::string> & intersection,int32_t fileId)388 void CloneRestoreGeo::GetMapInsertValue(NativeRdb::ValuesBucket &value, std::vector<GeoCloneInfo>::iterator it,
389     const std::unordered_set<std::string> &intersection, int32_t fileId)
390 {
391     value.PutInt(FILE_ID, fileId);
392     PutIfInIntersection(value, LOCATION_KEY, it->locationKey, intersection);
393     PutIfInIntersection(value, CITY_ID, it->cityId, intersection);
394     PutIfInIntersection(value, LANGUAGE, it->language, intersection);
395     PutIfInIntersection(value, COUNTRY, it->country, intersection);
396     PutIfInIntersection(value, ADMIN_AREA, it->adminArea, intersection);
397     PutIfInIntersection(value, SUB_ADMIN_AREA, it->subAdminArea, intersection);
398     PutIfInIntersection(value, LOCALITY, it->locality, intersection);
399     PutIfInIntersection(value, SUB_LOCALITY, it->subLocality, intersection);
400     PutIfInIntersection(value, THOROUGHFARE, it->thoroughfare, intersection);
401     PutIfInIntersection(value, SUB_THOROUGHFARE, it->subThoroughfare, intersection);
402     PutIfInIntersection(value, FEATURE_NAME, it->featureName, intersection);
403     PutIfInIntersection(value, CITY_NAME, it->cityName, intersection);
404     PutIfInIntersection(value, ADDRESS_DESCRIPTION, it->addressDescription, intersection);
405     PutIfInIntersection(value, AOI, it->aoi, intersection);
406     PutIfInIntersection(value, POI, it->poi, intersection);
407     PutIfInIntersection(value, FIRST_AOI, it->firstAoi, intersection);
408     PutIfInIntersection(value, FIRST_POI, it->firstPoi, intersection);
409     PutIfInIntersection(value, LOCATION_VERSION, it->locationVersion, intersection);
410     PutIfInIntersection(value, FIRST_AOI_CATEGORY, it->firstAoiCategory, intersection);
411     PutIfInIntersection(value, FIRST_POI_CATEGORY, it->firstPoiCategory, intersection);
412     PutIfInIntersection(value, LOCATION_TYPE, it->locationType, intersection);
413     PutIfInIntersection(value, LATITUDE, it->latitude, intersection);
414     PutIfInIntersection(value, LONGITUDE, it->longitude, intersection);
415 }
416 
BatchInsertWithRetry(const std::string & tableName,std::vector<NativeRdb::ValuesBucket> & values,int64_t & rowNum)417 int32_t CloneRestoreGeo::BatchInsertWithRetry(const std::string &tableName,
418     std::vector<NativeRdb::ValuesBucket> &values, int64_t &rowNum)
419 {
420     CHECK_AND_RETURN_RET(!values.empty(), E_OK);
421     int32_t errCode = E_ERR;
422     TransactionOperations trans{ __func__ };
423     trans.SetBackupRdbStore(mediaLibraryRdb_);
424     std::function<int(void)> func = [&]()->int {
425         errCode = trans.BatchInsert(rowNum, tableName, values);
426         CHECK_AND_PRINT_LOG(errCode == E_OK, "InsertSql failed, errCode: %{public}d, rowNum: %{public}ld.",
427             errCode, (long)rowNum);
428         return errCode;
429     };
430 
431     errCode = trans.RetryTrans(func, true);
432     CHECK_AND_PRINT_LOG(errCode == E_OK, "BatchInsertWithRetry: tans finish fail!, ret:%{public}d", errCode);
433     return errCode;
434 }
435 
BatchUpdate(const std::string & tableName,std::vector<std::string> & fileIds)436 int32_t CloneRestoreGeo::BatchUpdate(const std::string &tableName, std::vector<std::string> &fileIds)
437 {
438     std::unique_ptr<NativeRdb::AbsRdbPredicates> updatePredicates =
439         make_unique<NativeRdb::AbsRdbPredicates>(tableName);
440     updatePredicates->In(FILE_ID, fileIds);
441     NativeRdb::ValuesBucket rdbValues;
442     rdbValues.PutInt(GEO, UPDATE_GEO);
443     int32_t changedRows = -1;
444     int32_t errCode = E_OK;
445 
446     errCode = BackupDatabaseUtils::Update(mediaLibraryRdb_, changedRows, rdbValues, updatePredicates);
447     CHECK_AND_PRINT_LOG(errCode == E_OK, "Database update failed, errCode = %{public}d", errCode);
448     return errCode;
449 }
450 
BatchUpdateAna(const std::string & tableName,std::vector<std::string> & analysisIds)451 int32_t CloneRestoreGeo::BatchUpdateAna(const std::string &tableName, std::vector<std::string> &analysisIds)
452 {
453     std::unique_ptr<NativeRdb::AbsRdbPredicates> updatePredicates =
454         make_unique<NativeRdb::AbsRdbPredicates>(tableName);
455     updatePredicates->In(FILE_ID, analysisIds);
456     NativeRdb::ValuesBucket rdbValues;
457     rdbValues.PutInt(GEO, ANALYSISED_STATUS);
458     int32_t changedRows = -1;
459     int32_t errCode = E_OK;
460     errCode = BackupDatabaseUtils::Update(mediaLibraryRdb_, changedRows, rdbValues, updatePredicates);
461     CHECK_AND_PRINT_LOG(errCode == E_OK, "Database update failed, errCode = %{public}d", errCode);
462     return errCode;
463 }
464 
FailUpdate(int32_t errCodeUpdate,int32_t & batchCnt,int32_t & batchAnaCnt)465 void CloneRestoreGeo::FailUpdate(int32_t errCodeUpdate, int32_t &batchCnt, int32_t &batchAnaCnt)
466 {
467     batchCnt -= batchAnaCnt;
468     failUpdateCnt_ += batchCnt;
469     MEDIA_ERR_LOG("AnalysisTotal: RestoreMaps update fail");
470     ErrorInfo errorInfo(RestoreError::UPDATE_FAILED, batchCnt,
471         "errCodeUpdate: " + std::to_string(errCodeUpdate), "AnalysisTotal: RestoreMaps update fail");
472     UpgradeRestoreTaskReport().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).ReportError(errorInfo);
473 }
474 
FailUpdateAna(int32_t errCodeUpdateAna,int32_t & batchAnaCnt)475 void CloneRestoreGeo::FailUpdateAna(int32_t errCodeUpdateAna, int32_t &batchAnaCnt)
476 {
477     failUpdateCnt_ += batchAnaCnt;
478     MEDIA_ERR_LOG("AnalysisTotal: RestoreMaps updateAna fail");
479     ErrorInfo errorInfo(RestoreError::UPDATE_FAILED, batchAnaCnt,
480         "errCodeUpdateAna: " + std::to_string(errCodeUpdateAna), "AnalysisTotal: RestoreMaps updateAna fail");
481     UpgradeRestoreTaskReport().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).ReportError(errorInfo);
482 }
483 
ReportGeoRestoreTask()484 void CloneRestoreGeo::ReportGeoRestoreTask()
485 {
486     MEDIA_INFO_LOG("GeoKnowledge Insert successInsertCnt_: %{public}d, failInsertCnt_: %{public}d, "
487         "AnalysisTotal Update successUpdateCnt_: %{public}d, failUpdateCnt_: %{public}d, "
488         "Current System Language: %{public}s",
489         successInsertCnt_.load(), failInsertCnt_.load(), successUpdateCnt_.load(),
490         failUpdateCnt_.load(), systemLanguage_.c_str());
491     UpgradeRestoreTaskReport().SetSceneCode(sceneCode_).SetTaskId(taskId_)
492         .Report("GeoKnowledge Restore", std::to_string(GEO_STATUS_SUCCESS),
493         "successInsertCnt_: " + std::to_string(successInsertCnt_) +
494         ", failInsertCnt_: " + std::to_string(failInsertCnt_) +
495         ", successUpdateCnt_: " + std::to_string(successUpdateCnt_) +
496         ", failUpdateCnt_: " + std::to_string(failUpdateCnt_) +
497         ", Current System Language: " + systemLanguage_);
498 }
499 } // namespace OHOS::Media