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 #include "thumbnail_file_utils.h"
23
24 using namespace std;
25 using namespace OHOS::DistributedKv;
26 using namespace OHOS::NativeRdb;
27
28 namespace OHOS {
29 namespace Media {
AgingLcd(AsyncTaskData * data)30 static void AgingLcd(AsyncTaskData *data)
31 {
32 if (data == nullptr) {
33 return;
34 }
35 AgingAsyncTaskData* taskData = static_cast<AgingAsyncTaskData*>(data);
36 int32_t err = ThumbnailAgingHelper::ClearLcdFromFileTable(taskData->opts);
37 if (err != E_OK) {
38 MEDIA_ERR_LOG("Failed to ClearLcdFormFileTable %{public}d", err);
39 }
40 }
41
AgingLcdBatch(ThumbRdbOpt & opts)42 int32_t ThumbnailAgingHelper::AgingLcdBatch(ThumbRdbOpt &opts)
43 {
44 MEDIA_INFO_LOG("IN %{private}s", opts.table.c_str());
45 if (opts.store == nullptr) {
46 return E_HAS_DB_ERROR;
47 }
48
49 shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
50 if (asyncWorker == nullptr) {
51 return E_ERR;
52 }
53 AgingAsyncTaskData* taskData = new (std::nothrow)AgingAsyncTaskData();
54 if (taskData == nullptr) {
55 return E_ERR;
56 }
57 taskData->opts = opts;
58 shared_ptr<MediaLibraryAsyncTask> agingAsyncTask = make_shared<MediaLibraryAsyncTask>(AgingLcd, taskData);
59 if (agingAsyncTask != nullptr) {
60 asyncWorker->AddTask(agingAsyncTask, false);
61 }
62 return E_OK;
63 }
64
GetAgingDataCount(const int64_t & time,const bool & before,ThumbRdbOpt & opts,int & count)65 int32_t ThumbnailAgingHelper::GetAgingDataCount(const int64_t &time, const bool &before, ThumbRdbOpt &opts, int &count)
66 {
67 int err = GetLcdCountByTime(time, before, opts, count);
68 if (err != E_OK) {
69 MEDIA_ERR_LOG("Failed to GetAgingDataCount %{public}d", err);
70 return err;
71 }
72 return E_OK;
73 }
74
ClearLcdFromFileTable(ThumbRdbOpt & opts)75 int32_t ThumbnailAgingHelper::ClearLcdFromFileTable(ThumbRdbOpt &opts)
76 {
77 int lcdCount = 0;
78 int32_t err = GetLcdCount(opts, lcdCount);
79 if (err != E_OK) {
80 MEDIA_ERR_LOG("Failed to GetLcdCount %{public}d", err);
81 return err;
82 }
83 MEDIA_DEBUG_LOG("lcdCount %{public}d", lcdCount);
84 if (lcdCount <= THUMBNAIL_LCD_AGING_THRESHOLD) {
85 MEDIA_INFO_LOG("Not need aging Lcd. lcdCount: %{public}d", lcdCount);
86 return E_OK;
87 }
88 vector<ThumbnailData> infos;
89 err = GetAgingLcdData(opts, lcdCount - THUMBNAIL_LCD_AGING_THRESHOLD, infos);
90 if ((err != E_OK) || infos.empty()) {
91 MEDIA_ERR_LOG("Failed to GetAgingLcdData %{public}d", err);
92 return err;
93 }
94
95 shared_ptr<MediaLibraryAsyncWorker> asyncWorker = MediaLibraryAsyncWorker::GetInstance();
96 if (asyncWorker == nullptr) {
97 return E_ERR;
98 }
99 for (uint32_t i = 0; i < infos.size(); i++) {
100 opts.row = infos[i].id;
101 if (ThumbnailFileUtils::DeleteThumbFile(infos[i], ThumbnailType::LCD)) {
102 ThumbnailUtils::CleanThumbnailInfo(opts, false, true);
103 }
104 }
105 MEDIA_INFO_LOG("Clear Lcd completed");
106 return E_OK;
107 }
108
GetLcdCount(ThumbRdbOpt & opts,int & outLcdCount)109 int32_t ThumbnailAgingHelper::GetLcdCount(ThumbRdbOpt &opts, int &outLcdCount)
110 {
111 int32_t err = E_ERR;
112 if (!ThumbnailUtils::QueryLcdCount(opts, outLcdCount, err)) {
113 MEDIA_ERR_LOG("Failed to QueryLcdCount %{public}d", err);
114 return err;
115 }
116 return E_OK;
117 }
118
GetLcdCountByTime(const int64_t & time,const bool & before,ThumbRdbOpt & opts,int & outLcdCount)119 int32_t ThumbnailAgingHelper::GetLcdCountByTime(const int64_t &time, const bool &before, ThumbRdbOpt &opts,
120 int &outLcdCount)
121 {
122 int32_t err = E_ERR;
123 if (!ThumbnailUtils::QueryLcdCountByTime(time, before, opts, outLcdCount, err)) {
124 MEDIA_ERR_LOG("Failed to QueryLcdCountByTime %{public}d", err);
125 return err;
126 }
127 return E_OK;
128 }
129
GetAgingLcdData(ThumbRdbOpt & opts,int lcdLimit,vector<ThumbnailData> & outDatas)130 int32_t ThumbnailAgingHelper::GetAgingLcdData(ThumbRdbOpt &opts, int lcdLimit, vector<ThumbnailData> &outDatas)
131 {
132 int32_t err = E_ERR;
133 if (!ThumbnailUtils::QueryAgingLcdInfos(opts, lcdLimit, outDatas, err)) {
134 MEDIA_ERR_LOG("Failed to QueryAgingLcdInfos %{public}d", err);
135 return err;
136 }
137 return E_OK;
138 }
139 } // namespace Media
140 } // namespace OHOS
141