• 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 
16 #ifndef CLONE_RESTORE_CLASSIFY_H
17 #define CLONE_RESTORE_CLASSIFY_H
18 
19 #include <string>
20 
21 #include "backup_const.h"
22 #include "clone_restore_analysis_total.h"
23 #include "rdb_store.h"
24 
25 namespace OHOS::Media {
26 class CloneRestoreClassify {
27 public:
28     void Init(int32_t sceneCode, const std::string &taskId,
29         std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::shared_ptr<NativeRdb::RdbStore> mediaRdb);
30     void Restore(const std::unordered_map<int32_t, PhotoInfo> &photoInfoMap);
31 
32     template<typename T>
33     static void PutIfPresent(NativeRdb::ValuesBucket& values, const std::string& columnName,
34         const std::optional<T>& optionalValue);
35 
36     template<typename T>
37     static void PutIfInIntersection(NativeRdb::ValuesBucket& values, const std::string& columnName,
38         const std::optional<T>& optionalValue, const std::unordered_set<std::string> &intersection);
39 
40 private:
41     struct ClassifyCloneInfo {
42         std::optional<int32_t> id;
43         std::optional<int32_t> fileIdOld;
44         std::optional<int32_t> fileIdNew;
45         std::optional<int32_t> categoryId;
46         std::optional<std::string> subLabel;
47         std::optional<double> prob;
48         std::optional<std::vector<uint8_t>> feature;
49         std::optional<std::string> simResult;
50         std::optional<std::string> labelVersion;
51         std::optional<std::string> saliencySubProb;
52         std::optional<std::string> analysisVersion;
53         std::optional<std::string> captionResult;
54         std::optional<std::string> captionVersion;
55     };
56     struct ClassifyVideoCloneInfo {
57         std::optional<int32_t> id;
58         std::optional<int32_t> fileIdOld;
59         std::optional<int32_t> fileIdNew;
60         std::optional<std::string> categoryId;
61         std::optional<std::string> confidenceProbability;
62         std::optional<std::string> subCategory;
63         std::optional<std::string> subConfidenceProb;
64         std::optional<std::string> subLabel;
65         std::optional<std::string> subLabelProb;
66         std::optional<std::string> subLabelType;
67         std::optional<std::string> tracks;
68         std::optional<std::vector<uint8_t>> videoPartFeature;
69         std::optional<std::string> filterTag;
70         std::optional<std::string> algoVersion;
71         std::optional<std::string> analysisVersion;
72         std::optional<int32_t> triggerGenerateThumbnail;
73     };
74 
75     void GetMaxIds();
76     void RestoreBatch(const std::unordered_map<int32_t, PhotoInfo> &photoInfoMap);
77     void RestoreMaps();
78     void RestoreVideoMaps();
79     void ReportRestoreTask();
80     void ReportRestoreTaskOfTotal();
81     void ReportRestoreTaskofImage();
82     void ReportRestoreTaskofVideo();
83 
84     void GetClassifyInfos(std::vector<ClassifyCloneInfo> &classifyInfos);
85     void GetClassifyVideoInfos(std::vector<ClassifyVideoCloneInfo> &classifyVideoInfos);
86     void DeduplicateClassifyInfos(std::vector<ClassifyCloneInfo> &infos);
87     void DeduplicateClassifyVideoInfos(std::vector<ClassifyVideoCloneInfo> &infos);
88     std::unordered_set<int32_t> GetExistingFileIds(const std::string &tableName);
89     void RemoveDuplicateClassifyInfos(std::vector<ClassifyCloneInfo> &infos,
90         const std::unordered_set<int32_t> &existingFileIds);
91     void RemoveDuplicateClassifyVideoInfos(std::vector<ClassifyVideoCloneInfo> &infos,
92         const std::unordered_set<int32_t> &existingFileIds);
93     void InsertClassifyAlbums(std::vector<ClassifyCloneInfo> &classifyInfos);
94     void InsertClassifyVideoAlbums(std::vector<ClassifyVideoCloneInfo> &classifyVideoInfos);
95 
96     void GetClassifyInfo(ClassifyCloneInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet);
97     void GetMapInsertValue(NativeRdb::ValuesBucket &value, ClassifyCloneInfo info,
98         const std::unordered_set<std::string> &intersection);
99     void GetClassifyVideoInfo(ClassifyVideoCloneInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet);
100     void GetVideoMapInsertValue(NativeRdb::ValuesBucket &value, ClassifyVideoCloneInfo info,
101         const std::unordered_set<std::string> &intersection);
102 
103     bool CheckTableColumns(const std::string& tableName, std::unordered_map<std::string, std::string>& columns);
104     std::unordered_set<std::string> GetCommonColumns(const std::string &tableName);
105     int32_t BatchInsertWithRetry(const std::string &tableName, std::vector<NativeRdb::ValuesBucket> &values,
106         int64_t &rowNum);
107 
108     void AddSpecialAlbum();
109     void AddSelfieAlbum();
110     void AddUserCommentAlbum();
111 
112 private:
113     int32_t sceneCode_{-1};
114     std::string taskId_;
115     std::string analysisType_;
116     std::shared_ptr<NativeRdb::RdbStore> mediaRdb_;
117     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
118     int32_t maxIdOfLabel_{0};
119     int32_t maxIdOfVideoLabel_{0};
120     std::atomic<int32_t> successInsertLabelCnt_{0};
121     std::atomic<int32_t> successInsertVideoLabelCnt_{0};
122     std::atomic<int32_t> failInsertLabelCnt_{0};
123     std::atomic<int32_t> failInsertVideoLabelCnt_{0};
124     std::atomic<int32_t> duplicateLabelCnt_{0};
125     std::atomic<int32_t> duplicateVideoLabelCnt_{0};
126     std::atomic<int64_t> restoreTimeCost_{0};
127     std::atomic<int64_t> restoreLabelTimeCost_{0};
128     std::atomic<int64_t> restoreVideoLabelTimeCost_{0};
129     CloneRestoreAnalysisTotal cloneRestoreAnalysisTotal_;
130 };
131 
132 template<typename T>
PutIfPresent(NativeRdb::ValuesBucket & values,const std::string & columnName,const std::optional<T> & optionalValue)133 void CloneRestoreClassify::PutIfPresent(NativeRdb::ValuesBucket& values, const std::string& columnName,
134     const std::optional<T>& optionalValue)
135 {
136     if (optionalValue.has_value()) {
137         if constexpr (std::is_same_v<std::decay_t<T>, int32_t>) {
138             values.PutInt(columnName, optionalValue.value());
139         } else if constexpr (std::is_same_v<std::decay_t<T>, int64_t>) {
140             values.PutLong(columnName, optionalValue.value());
141         } else if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
142             values.PutString(columnName, optionalValue.value());
143         } else if constexpr (std::is_same_v<std::decay_t<T>, double>) {
144             values.PutDouble(columnName, optionalValue.value());
145         } else if constexpr (std::is_same_v<std::decay_t<T>, std::vector<uint8_t>>) {
146             values.PutBlob(columnName, optionalValue.value());
147         }
148     }
149 }
150 
151 template<typename T>
PutIfInIntersection(NativeRdb::ValuesBucket & values,const std::string & columnName,const std::optional<T> & optionalValue,const std::unordered_set<std::string> & intersection)152 void CloneRestoreClassify::PutIfInIntersection(NativeRdb::ValuesBucket& values, const std::string& columnName,
153     const std::optional<T>& optionalValue, const std::unordered_set<std::string> &intersection)
154 {
155     if (intersection.count(columnName) > 0) {
156         PutIfPresent<T>(values, columnName, optionalValue);
157     }
158 }
159 } // namespace OHOS::Media
160 #endif // CLONE_RESTORE_CLASSIFY_H