• 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 "rdb_store.h"
23 
24 namespace OHOS::Media {
25 class CloneRestoreClassify {
26 public:
27     void Init(int32_t sceneCode, const std::string &taskId,
28         std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::shared_ptr<NativeRdb::RdbStore> mediaRdb);
29     void RestoreMaps(std::vector<FileInfo> &fileInfos);
30     void RestoreVideoMaps(std::vector<FileInfo> &fileInfos);
31     void ReportClassifyRestoreTask();
32 
33     template<typename T>
34     static void PutIfPresent(NativeRdb::ValuesBucket& values, const std::string& columnName,
35         const std::optional<T>& optionalValue);
36 
37     template<typename T>
38     static void PutIfInIntersection(NativeRdb::ValuesBucket& values, const std::string& columnName,
39         const std::optional<T>& optionalValue, const std::unordered_set<std::string> &intersection);
40 
41 private:
42     struct ClassifyCloneInfo {
43         std::optional<int64_t> id;
44         std::optional<int64_t> fileIdOld;
45         std::optional<int64_t> fileIdNew;
46         std::optional<int64_t> categoryId;
47         std::optional<std::string> subLabel;
48         std::optional<double> prob;
49         std::optional<std::string> feature;
50         std::optional<std::string> simResult;
51         std::optional<std::string> labelVersion;
52         std::optional<std::string> saliencySubProb;
53         std::optional<std::string> analysisVersion;
54     };
55     struct ClassifyVideoCloneInfo {
56         std::optional<int64_t> id;
57         std::optional<int64_t> fileIdOld;
58         std::optional<int64_t> fileIdNew;
59         std::optional<std::string> categoryId;
60         std::optional<double> confidenceProbability;
61         std::optional<std::string> subCategory;
62         std::optional<double> subConfidenceProb;
63         std::optional<std::string> subLabel;
64         std::optional<double> subLabelProb;
65         std::optional<int64_t> subLabelType;
66         std::optional<std::string> tracks;
67         std::optional<std::vector<uint8_t>> videoPartFeature;
68         std::optional<std::string> filterTag;
69         std::optional<std::string> algoVersion;
70         std::optional<std::string> analysisVersion;
71         std::optional<int64_t> triggerGenerateThumbnail;
72     };
73 
74     void GetClassifyInfos(std::vector<ClassifyCloneInfo> &classifyInfo,
75         std::vector<FileInfo> &fileInfos, int32_t offset);
76     void GetClassifyVideoInfos(std::vector<ClassifyVideoCloneInfo> &classifyVideoInfo,
77         std::vector<FileInfo> &fileInfos, int32_t offset);
78     void DeduplicateClassifyInfos(std::vector<ClassifyCloneInfo> &classifyInfos,
79         std::vector<FileInfo> &fileInfos);
80     void DeduplicateClassifyVideoInfos(std::vector<ClassifyVideoCloneInfo> &classifyVideoInfos,
81         std::vector<FileInfo> &fileInfos);
82     void InsertClassifyAlbums(std::vector<ClassifyCloneInfo> &classifyInfos, std::vector<FileInfo> &fileInfos);
83     void InsertClassifyVideoAlbums(std::vector<ClassifyVideoCloneInfo> &classifyVideoInfos,
84         std::vector<FileInfo> &fileInfos);
85 
86     void GetClassifyInfo(ClassifyCloneInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet);
87     void GetMapInsertValue(NativeRdb::ValuesBucket &value, ClassifyCloneInfo info,
88         const std::unordered_set<std::string> &intersection);
89     void GetClassifyVideoInfo(ClassifyVideoCloneInfo &info, std::shared_ptr<NativeRdb::ResultSet> resultSet);
90     void GetVideoMapInsertValue(NativeRdb::ValuesBucket &value, ClassifyVideoCloneInfo info,
91         const std::unordered_set<std::string> &intersection);
92 
93     bool CheckTableColumns(const std::string& tableName, std::unordered_map<std::string, std::string>& columns);
94     std::unordered_set<std::string> GetCommonColumns(const std::string &tableName);
95     int32_t BatchInsertWithRetry(const std::string &tableName, std::vector<NativeRdb::ValuesBucket> &values,
96         int64_t &rowNum);
97 
98 private:
99     int32_t sceneCode_{-1};
100     std::string taskId_;
101     std::shared_ptr<NativeRdb::RdbStore> mediaRdb_;
102     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
103     std::atomic<int32_t> successInsertLabelCnt_{0};
104     std::atomic<int32_t> successInsertVideoLabelCnt_{0};
105     std::atomic<int32_t> failInsertLabelCnt_{0};
106     std::atomic<int32_t> failInsertVideoLabelCnt_{0};
107 };
108 
109 template<typename T>
PutIfPresent(NativeRdb::ValuesBucket & values,const std::string & columnName,const std::optional<T> & optionalValue)110 void CloneRestoreClassify::PutIfPresent(NativeRdb::ValuesBucket& values, const std::string& columnName,
111     const std::optional<T>& optionalValue)
112 {
113     if (optionalValue.has_value()) {
114         if constexpr (std::is_same_v<std::decay_t<T>, int32_t>) {
115             values.PutInt(columnName, optionalValue.value());
116         } else if constexpr (std::is_same_v<std::decay_t<T>, int64_t>) {
117             values.PutLong(columnName, optionalValue.value());
118         } else if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
119             values.PutString(columnName, optionalValue.value());
120         } else if constexpr (std::is_same_v<std::decay_t<T>, double>) {
121             values.PutDouble(columnName, optionalValue.value());
122         } else if constexpr (std::is_same_v<std::decay_t<T>, std::vector<uint8_t>>) {
123             values.PutBlob(columnName, optionalValue.value());
124         }
125     }
126 }
127 
128 template<typename T>
PutIfInIntersection(NativeRdb::ValuesBucket & values,const std::string & columnName,const std::optional<T> & optionalValue,const std::unordered_set<std::string> & intersection)129 void CloneRestoreClassify::PutIfInIntersection(NativeRdb::ValuesBucket& values, const std::string& columnName,
130     const std::optional<T>& optionalValue, const std::unordered_set<std::string> &intersection)
131 {
132     if (intersection.count(columnName) > 0) {
133         PutIfPresent<T>(values, columnName, optionalValue);
134     }
135 }
136 } // namespace OHOS::Media
137 #endif // CLONE_RESTORE_CLASSIFY_H