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 16 #ifndef HIGHLIGHT_RESTORE_H 17 #define HIGHLIGHT_RESTORE_H 18 19 #include <mutex> 20 #include <sstream> 21 #include <string> 22 23 #include "backup_const.h" 24 #include "nlohmann/json.hpp" 25 #include "rdb_store.h" 26 27 namespace OHOS::Media { 28 class HighlightRestore { 29 public: 30 void Init(int32_t sceneCode, std::string taskId, 31 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::shared_ptr<NativeRdb::RdbStore> galleryRdb); 32 void RestoreHighlight(const std::string &albumOdid, const std::unordered_map<int32_t, PhotoInfo> &photoInfoMap); 33 34 private: 35 struct HighlightAlbumInfo { 36 int32_t albumIdOld {-1}; 37 int32_t albumIdNew {-1}; 38 int32_t aiAlbumIdNew {-1}; 39 std::string subTitle; 40 std::string albumName; 41 int64_t minDateAdded {0}; 42 int64_t maxDateAdded {0}; 43 int32_t coverId {-1}; 44 std::string coverUri; 45 int64_t generateTime {0}; 46 std::string clusterType; 47 std::string clusterSubType; 48 std::string clusterCondition; 49 int32_t highlightStatus {-1}; 50 int32_t id {-1}; 51 std::string albumOdid; 52 std::vector<nlohmann::json> effectline; 53 ToStringHighlightAlbumInfo54 std::string ToString() const 55 { 56 std::stringstream ss; 57 ss << "HighlightAlbumInfo[" 58 << "albumIdOld: " << albumIdOld << ", albumIdNew: " << albumIdNew << ", aiAlbumIdNew: " << aiAlbumIdNew 59 << ", subTitle: " << subTitle << ", albumName: " << albumName 60 << ", minDateAdded: " << minDateAdded << ", maxDateAdded: " << maxDateAdded 61 << ", coverId: " << coverId << ", coverUri: " << coverUri << ", generateTime: " << generateTime 62 << ", clusterType: " << clusterType << ", clusterSubType: " << clusterSubType 63 << ", clusterCondition: " << clusterCondition 64 << ", highlightStatus: " << highlightStatus << ", id: " << id 65 << "]"; 66 return ss.str(); 67 } 68 }; 69 70 struct HighlightPhotoInfo { 71 int32_t fileIdOld {-1}; 72 PhotoInfo photoInfo; 73 std::string storyIds; 74 std::string portraitIds; 75 std::string hashCode; 76 int64_t dateModified {0}; 77 }; 78 79 void RestoreAlbums(const std::string &albumOdid); 80 void RestoreMaps(const std::unordered_map<int32_t, PhotoInfo> &photoInfoMap); 81 void UpdateAlbums(); 82 void GetAlbumInfos(const std::string &albumOdid); 83 bool HasSameHighlightAlbum(HighlightAlbumInfo &info); 84 void TransferClusterInfo(HighlightAlbumInfo &info); 85 void InsertIntoAnalysisAlbum(); 86 NativeRdb::ValuesBucket GetAnalysisAlbumValuesBucket(const HighlightAlbumInfo &info, int32_t subType); 87 void UpdateAlbumIds(); 88 void InsertIntoHighlightTables(); 89 void InsertIntoHighlightAlbum(); 90 void InsertIntoHighlightCoverAndPlayInfo(); 91 void UpdateHighlightIds(); 92 void UpdateMapInsertValues(std::vector<NativeRdb::ValuesBucket> &values, 93 const HighlightPhotoInfo &highlightPhoto); 94 void UpdateAlbumInfoCoverUris(const HighlightPhotoInfo &highlightPhoto); 95 void UpdateMapInsertValuesByStoryId(std::vector<NativeRdb::ValuesBucket> &values, 96 const HighlightPhotoInfo &highlightPhoto, const std::string &storyId); 97 nlohmann::json GetEffectline(const HighlightPhotoInfo &highlightPhoto); 98 nlohmann::json GetEffectVideoTrack(const std::string &hashCode); 99 NativeRdb::ValuesBucket GetMapInsertValue(int32_t albumId, int32_t fileId); 100 int32_t BatchInsertWithRetry(const std::string &tableName, std::vector<NativeRdb::ValuesBucket> &values, 101 int64_t &rowNum); 102 void ReportHighlightRestoreTask(); 103 104 private: 105 int32_t sceneCode_ {-1}; 106 std::mutex counterMutex_; 107 std::string taskId_; 108 std::shared_ptr<NativeRdb::RdbStore> galleryRdb_; 109 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_; 110 std::vector<HighlightAlbumInfo> albumInfos_; 111 std::unordered_map<std::string, int32_t> albumPhotoCounter_; 112 std::atomic<int32_t> successCnt_ {0}; 113 std::atomic<int32_t> duplicateCnt_ {0}; 114 std::atomic<int32_t> failCnt_ {0}; 115 std::atomic<int32_t> tracksParseFailCnt_ {0}; 116 }; 117 } // namespace OHOS::Media 118 119 #endif // HIGHLIGHT_RESTORE_H