• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #define MLOG_TAG "AnalysisHandler"
17 
18 #include "analysis_handler.h"
19 
20 #include "medialibrary_errno.h"
21 #include "medialibrary_period_worker.h"
22 #include "medialibrary_unistore_manager.h"
23 #include "medialibrary_rdb_utils.h"
24 #include "photo_album_column.h"
25 #include "photo_map_column.h"
26 #include "power_efficiency_manager.h"
27 #include "result_set_utils.h"
28 #include "vision_column.h"
29 
30 using namespace std;
31 
32 namespace OHOS {
33 namespace Media {
34 
35 using ChangeType = DataShare::DataShareObserver::ChangeType;
36 
37 std::mutex AnalysisHandler::mtx_;
38 queue<CloudSyncHandleData> AnalysisHandler::taskQueue_;
39 std::atomic<uint16_t> AnalysisHandler::counts_(0);
40 static constexpr uint16_t HANDLE_IDLING_TIME = 5;
41 static const string INSERT_REFRESH_ALBUM = "INSERT OR REPLACE INTO RefreshAlbum (refresh_album_id) VALUES ";
42 
~AnalysisHandler()43 AnalysisHandler::~AnalysisHandler() {}
44 
GetFileIds(const CloudSyncHandleData & handleData)45 static vector<string> GetFileIds(const CloudSyncHandleData &handleData)
46 {
47     vector<string> fileIds;
48     for (auto &uri : handleData.orgInfo.uris) {
49         string uriString = uri.ToString();
50         auto index = uriString.rfind('/');
51         if (index == string::npos) {
52             continue;
53         }
54         auto fileIdStr = uriString.substr(index + 1);
55         fileIds.push_back(fileIdStr);
56     }
57     return fileIds;
58 }
59 
GetHandleData(CloudSyncHandleData & handleData)60 static int32_t GetHandleData(CloudSyncHandleData &handleData)
61 {
62     lock_guard<mutex> lockGuard(AnalysisHandler::mtx_);
63     if (AnalysisHandler::taskQueue_.empty()) {
64         ++AnalysisHandler::counts_;
65         if (AnalysisHandler::counts_.load() > HANDLE_IDLING_TIME) {
66             auto periodWorker = MediaLibraryPeriodWorker::GetInstance();
67             CHECK_AND_RETURN_RET_LOG(periodWorker != nullptr, E_ERR, "failed to get period worker instance");
68             periodWorker->StopThread(PeriodTaskType::CLOUD_ANALYSIS_ALBUM);
69         }
70         return E_ERR;
71     } else {
72         AnalysisHandler::counts_.store(0);
73         handleData = AnalysisHandler::taskQueue_.front();
74         AnalysisHandler::taskQueue_.pop();
75     }
76     return E_OK;
77 }
78 
GetAlbumIds(const shared_ptr<MediaLibraryRdbStore> rdbStore,const vector<string> & fileIds)79 static vector<string> GetAlbumIds(const shared_ptr<MediaLibraryRdbStore> rdbStore, const vector<string> &fileIds)
80 {
81     vector<string> albumIds;
82     vector<string> columns = {
83         "DISTINCT (map_album)"
84     };
85     NativeRdb::RdbPredicates predicates(ANALYSIS_PHOTO_MAP_TABLE);
86     predicates.In(PhotoMap::ASSET_ID, fileIds);
87     shared_ptr<NativeRdb::ResultSet> resultSet = rdbStore->Query(predicates, columns);
88     CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, albumIds, "Failed query AnalysisAlbum");
89 
90     while (resultSet->GoToNextRow() == E_OK) {
91         int32_t albumId = ANALYSIS_ALBUM_OFFSET + get<int32_t>(ResultSetUtils::GetValFromColumn(
92             ANALYSIS_PHOTO_MAP_TABLE + "." + PhotoMap::ALBUM_ID, resultSet, TYPE_INT32));
93         albumIds.push_back(to_string(albumId));
94     }
95     resultSet->Close();
96     return albumIds;
97 }
98 
ProcessHandleData(PeriodTaskData * data)99 void AnalysisHandler::ProcessHandleData(PeriodTaskData *data)
100 {
101     CloudSyncHandleData handleData;
102     if (GetHandleData(handleData) != E_OK) {
103         return;
104     }
105 
106     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
107     CHECK_AND_RETURN_LOG(rdbStore != nullptr, "Can not get rdbstore");
108     vector<string> fileIds;
109     std::string insertRefreshAlbum;
110     if (handleData.orgInfo.type == ChangeType::OTHER) {
111         MEDIA_INFO_LOG("ChangeType being OTHER, all analysis albums are refreshed directly.");
112         return;
113     }
114     fileIds = GetFileIds(handleData);
115     if (fileIds.empty()) {
116         return;
117     }
118     vector<string> albumIds = GetAlbumIds(rdbStore, fileIds);
119     if (albumIds.empty()) {
120         return;
121     }
122     insertRefreshAlbum = INSERT_REFRESH_ALBUM;
123     int32_t albumSize = static_cast<int32_t>(albumIds.size());
124     for (string albumId: albumIds) {
125         insertRefreshAlbum.append("(" + albumId + "),");
126     }
127     if (insertRefreshAlbum.back() == ',') {
128         insertRefreshAlbum.pop_back();
129     }
130     MEDIA_INFO_LOG("%{public}d files update %{public}d analysis album", static_cast<int32_t>(fileIds.size()),
131         albumSize);
132 
133     MEDIA_DEBUG_LOG("sql: %{public}s", insertRefreshAlbum.c_str());
134     int32_t ret = rdbStore->ExecuteSql(insertRefreshAlbum);
135     if (ret != NativeRdb::E_OK) {
136         MEDIA_ERR_LOG("insert analysisAlbum fail!");
137     }
138 }
139 
init()140 void AnalysisHandler::init()
141 {
142     auto periodWorker = MediaLibraryPeriodWorker::GetInstance();
143     CHECK_AND_RETURN_LOG(periodWorker != nullptr, "failed to get period worker instance");
144     if (periodWorker->IsThreadRunning(PeriodTaskType::CLOUD_ANALYSIS_ALBUM)) {
145         MEDIA_DEBUG_LOG("cloud analysis album is running");
146         return;
147     }
148 
149     AnalysisHandler::counts_.store(0);
150     periodWorker->StartTask(PeriodTaskType::CLOUD_ANALYSIS_ALBUM, AnalysisHandler::ProcessHandleData, nullptr);
151 }
152 
MergeTask(const CloudSyncHandleData & handleData)153 void AnalysisHandler::MergeTask(const CloudSyncHandleData &handleData)
154 {
155     lock_guard<mutex> lockGuard(AnalysisHandler::mtx_);
156     if (AnalysisHandler::taskQueue_.empty()) {
157         AnalysisHandler::taskQueue_.push(handleData);
158         return;
159     }
160     CloudSyncHandleData &tempHandleData = AnalysisHandler::taskQueue_.front();
161     if (tempHandleData.orgInfo.type == ChangeType::OTHER) {
162         return;
163     } else if (handleData.orgInfo.type == ChangeType::OTHER) {
164         AnalysisHandler::taskQueue_.pop();
165         AnalysisHandler::taskQueue_.push(handleData);
166     } else {
167         tempHandleData.orgInfo.uris.insert(
168             tempHandleData.orgInfo.uris.end(), handleData.orgInfo.uris.begin(), handleData.orgInfo.uris.end());
169     }
170 }
171 
Handle(const CloudSyncHandleData & handleData)172 void AnalysisHandler::Handle(const CloudSyncHandleData &handleData)
173 {
174     MergeTask(handleData);
175 }
176 } //namespace Media
177 } //namespace OHOS
178