• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #define MLOG_TAG "Thumbnail"
16 
17 #include "thumbnail_aging_helper.h"
18 
19 #include "medialibrary_errno.h"
20 #include "media_log.h"
21 #include "thumbnail_const.h"
22 
23 using namespace std;
24 using namespace OHOS::DistributedKv;
25 using namespace OHOS::NativeRdb;
26 
27 namespace OHOS {
28 namespace Media {
AgingLcd(AsyncTaskData * data)29 static void AgingLcd(AsyncTaskData *data)
30 {
31     if (data == nullptr) {
32         return;
33     }
34     AgingAsyncTaskData* taskData = static_cast<AgingAsyncTaskData*>(data);
35     int32_t err = ThumbnailAgingHelper::ClearLcdFromFileTable(taskData->opts);
36     if (err != E_OK) {
37         MEDIA_ERR_LOG("Failed to ClearLcdFormFileTable %{public}d", err);
38     }
39 }
40 
AgingDistributeLcd(AsyncTaskData * data)41 static void AgingDistributeLcd(AsyncTaskData* data)
42 {
43     if (data == nullptr) {
44         return;
45     }
46     AgingAsyncTaskData* taskData = static_cast<AgingAsyncTaskData*>(data);
47     int32_t err = ThumbnailAgingHelper::ClearRemoteLcdFromFileTable(taskData->opts);
48     if (err != E_OK) {
49         MEDIA_ERR_LOG("Failed to ClearRemoteLcdFormFileTable %{public}d", err);
50     }
51 }
52 
ClearThumbnailRecordTask(AsyncTaskData * data)53 static void ClearThumbnailRecordTask(AsyncTaskData* data)
54 {
55     if (data == nullptr) {
56         return;
57     }
58     AgingAsyncTaskData* taskData = static_cast<AgingAsyncTaskData*>(data);
59     int32_t err = ThumbnailAgingHelper::ClearKeyAndRecordFromMap(taskData->opts);
60     if (err != E_OK) {
61         MEDIA_ERR_LOG("Failed to ClearKeyAndRecordFromMap %{public}d", err);
62     }
63 }
64 
AgingLcdBatch(ThumbRdbOpt & opts)65 int32_t ThumbnailAgingHelper::AgingLcdBatch(ThumbRdbOpt &opts)
66 {
67     MEDIA_INFO_LOG("ThumbnailAgingHelper::AgingLcdBatch IN %{public}s", opts.table.c_str());
68     if (opts.store == nullptr) {
69         MEDIA_ERR_LOG("opts.store is not init");
70         return E_HAS_DB_ERROR;
71     }
72 
73     shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
74     if (asyncWorker == nullptr) {
75         return E_ERR;
76     }
77     AgingAsyncTaskData* taskData = new (std::nothrow)AgingAsyncTaskData();
78     if (taskData == nullptr) {
79         return E_ERR;
80     }
81     taskData->opts = opts;
82     shared_ptr<MediaLibraryAsyncTask> agingAsyncTask = make_shared<MediaLibraryAsyncTask>(AgingLcd, taskData);
83     if (agingAsyncTask != nullptr) {
84         asyncWorker->AddTask(agingAsyncTask, false);
85     }
86     return E_OK;
87 }
88 
ClearLcdFromFileTable(ThumbRdbOpt & opts)89 int32_t ThumbnailAgingHelper::ClearLcdFromFileTable(ThumbRdbOpt &opts)
90 {
91     int lcdCount = 0;
92     int32_t err = GetLcdCount(opts, lcdCount);
93     if (err != E_OK) {
94         MEDIA_ERR_LOG("Failed to GetLcdCount %{public}d", err);
95         return err;
96     }
97     MEDIA_DEBUG_LOG("lcdCount %{public}d", lcdCount);
98     if (lcdCount <= THUMBNAIL_LCD_AGING_THRESHOLD) {
99         MEDIA_INFO_LOG("Not need aging Lcd. lcdCount: %{lcdCount}d", lcdCount);
100         return E_OK;
101     }
102     vector<ThumbnailRdbData> infos;
103     err = GetAgingLcdData(opts, lcdCount - THUMBNAIL_LCD_GENERATE_THRESHOLD, infos);
104     if ((err != E_OK) || infos.empty()) {
105         MEDIA_ERR_LOG("Failed to GetAgingLcdData %{public}d", err);
106         return err;
107     }
108 
109     shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
110     if (asyncWorker == nullptr) {
111         return E_ERR;
112     }
113     ThumbnailData data;
114     for (uint32_t i = 0; i < infos.size(); i++) {
115         opts.row = infos[i].id;
116         ThumbnailUtils::ThumbnailDataCopy(data, infos[i]);
117         ThumbnailUtils::DeleteLcdData(opts, data);
118     }
119 
120     return E_OK;
121 }
122 
AgingDistributeLcdBatch(ThumbRdbOpt & opts)123 int32_t ThumbnailAgingHelper::AgingDistributeLcdBatch(ThumbRdbOpt &opts)
124 {
125     if (opts.store == nullptr) {
126         MEDIA_ERR_LOG("opts.store is not init");
127         return E_ERR;
128     }
129 
130     shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
131     if (asyncWorker == nullptr) {
132         return E_ERR;
133     }
134     AgingAsyncTaskData* taskData = new AgingAsyncTaskData();
135     if (taskData == nullptr) {
136         return E_ERR;
137     }
138     taskData->opts = opts;
139     shared_ptr<MediaLibraryAsyncTask> agingAsyncTask = make_shared<MediaLibraryAsyncTask>(AgingDistributeLcd, taskData);
140     if (agingAsyncTask != nullptr) {
141         asyncWorker->AddTask(agingAsyncTask, false);
142     }
143     return E_OK;
144 }
145 
ClearRemoteLcdFromFileTable(ThumbRdbOpt & opts)146 int32_t ThumbnailAgingHelper::ClearRemoteLcdFromFileTable(ThumbRdbOpt &opts)
147 {
148     int lcdCount = 0;
149     int32_t err = GetDistributeLcdCount(opts, lcdCount);
150     if (err != E_OK) {
151         MEDIA_ERR_LOG("Failed to GetDistributeLcdCount %{public}d", err);
152         return err;
153     }
154     MEDIA_DEBUG_LOG("GetDistributeLcdCount %{public}d", lcdCount);
155     if (lcdCount <= THUMBNAIL_LCD_AGING_THRESHOLD) {
156         MEDIA_INFO_LOG("Not need aging Lcd. GetDistributeLcdCount: %{lcdCount}d", lcdCount);
157         return E_OK;
158     }
159     vector<ThumbnailRdbData> infos;
160     err = GetAgingDistributeLcdData(opts, lcdCount - THUMBNAIL_LCD_GENERATE_THRESHOLD, infos);
161     if ((err != E_OK) || infos.empty()) {
162         MEDIA_ERR_LOG("Failed to GetAgingDistributeLcdData %{public}d", err);
163         return err;
164     }
165 
166     ThumbnailData data;
167     for (uint32_t i = 0; i < infos.size(); i++) {
168         opts.row = infos[i].id;
169         ThumbnailUtils::ThumbnailDataCopy(data, infos[i]);
170         ThumbnailUtils::DeleteDistributeLcdData(opts, data);
171     }
172 
173     return E_OK;
174 }
175 
GetLcdCount(ThumbRdbOpt & opts,int & outLcdCount)176 int32_t ThumbnailAgingHelper::GetLcdCount(ThumbRdbOpt &opts, int &outLcdCount)
177 {
178     int32_t err = E_ERR;
179     if (!ThumbnailUtils::QueryLcdCount(opts, outLcdCount, err)) {
180         MEDIA_ERR_LOG("Failed to QueryLcdCount %{public}d", err);
181         return err;
182     }
183     return E_OK;
184 }
185 
GetDistributeLcdCount(ThumbRdbOpt & opts,int & outLcdCount)186 int32_t ThumbnailAgingHelper::GetDistributeLcdCount(ThumbRdbOpt &opts, int &outLcdCount)
187 {
188     int32_t err = E_ERR;
189     if (!ThumbnailUtils::QueryDistributeLcdCount(opts, outLcdCount, err)) {
190         MEDIA_ERR_LOG("Failed to QueryLcdCount %{public}d", err);
191         return err;
192     }
193     return E_OK;
194 }
195 
GetAgingLcdData(ThumbRdbOpt & opts,int lcdLimit,vector<ThumbnailRdbData> & outDatas)196 int32_t ThumbnailAgingHelper::GetAgingLcdData(ThumbRdbOpt &opts, int lcdLimit, vector<ThumbnailRdbData> &outDatas)
197 {
198     int32_t err = E_ERR;
199     if (!ThumbnailUtils::QueryAgingLcdInfos(opts, lcdLimit, outDatas, err)) {
200         MEDIA_ERR_LOG("Failed to QueryAgingLcdInfos %{public}d", err);
201         return err;
202     }
203     return E_OK;
204 }
205 
GetAgingDistributeLcdData(ThumbRdbOpt & opts,int lcdLimit,vector<ThumbnailRdbData> & outDatas)206 int32_t ThumbnailAgingHelper::GetAgingDistributeLcdData(ThumbRdbOpt &opts,
207     int lcdLimit, vector<ThumbnailRdbData> &outDatas)
208 {
209     int32_t err = E_ERR;
210     if (!ThumbnailUtils::QueryAgingDistributeLcdInfos(opts, lcdLimit, outDatas, err)) {
211         MEDIA_ERR_LOG("Failed to QueryAgingLcdInfos %{public}d", err);
212         return err;
213     }
214     return E_OK;
215 }
216 
InvalidateDistributeBatch(ThumbRdbOpt & opts)217 int32_t ThumbnailAgingHelper::InvalidateDistributeBatch(ThumbRdbOpt &opts)
218 {
219     if (opts.store == nullptr) {
220         MEDIA_ERR_LOG("opts.store is not init");
221         return E_ERR;
222     }
223 
224     shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
225     if (asyncWorker == nullptr) {
226         return E_ERR;
227     }
228     AgingAsyncTaskData* taskData = new (std::nothrow)AgingAsyncTaskData();
229     if (taskData == nullptr) {
230         return E_ERR;
231     }
232     taskData->opts = opts;
233     shared_ptr<MediaLibraryAsyncTask> agingAsyncTask = make_shared<MediaLibraryAsyncTask>(
234         ClearThumbnailRecordTask, taskData);
235     if (agingAsyncTask != nullptr) {
236         asyncWorker->AddTask(agingAsyncTask, true);
237     }
238     return E_OK;
239 }
240 
AgingLcdKeysFromFiles(ThumbRdbOpt & opts)241 int32_t ThumbnailAgingHelper::AgingLcdKeysFromFiles(ThumbRdbOpt &opts)
242 {
243     int32_t err = E_ERR;
244     vector<ThumbnailRdbData> infos;
245     if (!ThumbnailUtils::QueryHasLcdFiles(opts, infos, err)) {
246         MEDIA_ERR_LOG("Failed to QueryHasLcdFiles %{public}d", err);
247         return err;
248     }
249 
250     for (uint32_t i = 0; i < infos.size(); i++) {
251         string key = infos[i].lcdKey;
252         if (!ThumbnailUtils::RemoveDataFromKv(opts.kvStore, key)) {
253             MEDIA_ERR_LOG("RemoveDataFromKv faild key: %{public}s", key.c_str());
254         }
255     }
256 
257     return E_OK;
258 }
259 
AgingThumbnailKeysFromFiles(ThumbRdbOpt & opts)260 int32_t ThumbnailAgingHelper::AgingThumbnailKeysFromFiles(ThumbRdbOpt &opts)
261 {
262     int32_t err = E_ERR;
263     vector<ThumbnailRdbData> infos;
264     if (!ThumbnailUtils::QueryHasThumbnailFiles(opts, infos, err)) {
265         MEDIA_ERR_LOG("Failed to QueryHasThumbnailFiles %{public}d", err);
266         return err;
267     }
268 
269     for (uint32_t i = 0; i < infos.size(); i++) {
270         string key = infos[i].thumbnailKey;
271         if (!ThumbnailUtils::RemoveDataFromKv(opts.kvStore, key)) {
272             MEDIA_ERR_LOG("RemoveDataFromKv faild key: %{public}s", key.c_str());
273         }
274     }
275 
276     return E_OK;
277 }
278 
ClearKeyAndRecordFromMap(ThumbRdbOpt & opts)279 int32_t ThumbnailAgingHelper::ClearKeyAndRecordFromMap(ThumbRdbOpt &opts)
280 {
281     int32_t err = E_ERR;
282     vector<ThumbnailRdbData> infos;
283     if (!ThumbnailUtils::QueryDeviceThumbnailRecords(opts, infos, err)) {
284         MEDIA_ERR_LOG("Failed to QueryDeviceThumbnailRecords %{public}d", err);
285         return err;
286     }
287 
288     ThumbnailData data;
289     for (uint32_t i = 0; i < infos.size(); i++) {
290         opts.row = infos[i].id;
291         ThumbnailUtils::ThumbnailDataCopy(data, infos[i]);
292         ThumbnailUtils::ClearThumbnailAllRecord(opts, data);
293     }
294     return E_OK;
295 }
296 } // namespace Media
297 } // namespace OHOS
298