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 #include "medialibrary_tab_asset_and_album_operations.h"
17 #include "media_file_utils.h"
18 #include "media_log.h"
19 #include "medialibrary_unistore_manager.h"
20
21 using namespace OHOS::DataShare;
22 using namespace std;
23 using namespace OHOS::NativeRdb;
24
25 namespace OHOS {
26 namespace Media {
Query(const NativeRdb::RdbPredicates & rdbPredicate,const std::vector<std::string> & columns)27 std::shared_ptr<NativeRdb::ResultSet> MediaLibraryTableAssetAlbumOperations::Query(
28 const NativeRdb::RdbPredicates &rdbPredicate, const std::vector<std::string> &columns)
29 {
30 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
31 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, nullptr, "rdbstore is nullptr");
32 return rdbStore->QueryWithFilter(rdbPredicate, columns);
33 }
34
Delete(NativeRdb::RdbPredicates & rdbPredicate)35 int32_t MediaLibraryTableAssetAlbumOperations::Delete(NativeRdb::RdbPredicates &rdbPredicate)
36 {
37 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
38 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_HAS_DB_ERROR, "rdbstore is nullptr");
39 return rdbStore->Delete(rdbPredicate);
40 }
41
OprnTableOversizeChecker(void)42 int32_t MediaLibraryTableAssetAlbumOperations::OprnTableOversizeChecker(void)
43 {
44 static int64_t lastcheckTime {0};
45 if ((MediaFileUtils::UTCTimeMilliSeconds() - lastcheckTime) >= OPRN_TABLE_OVERSIZE_CHECK_INTERVAL) {
46 MEDIA_INFO_LOG("operation table oversize check");
47 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
48 if (rdbStore == nullptr) {
49 MEDIA_ERR_LOG("rdbstore is nullptr");
50 return E_HAS_DB_ERROR;
51 }
52 NativeRdb::RdbPredicates predicates(OPRN_TABLE_NAME);
53 std::vector<std::string> columns;
54
55 auto resultSet = rdbStore->QuerySql(GET_COUNT_FROM_OPERATION_TABLE);
56 if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
57 MEDIA_ERR_LOG("Query not match data fails");
58 return E_DB_FAIL;
59 }
60 int rowCount = 0;
61 if (resultSet->GetInt(0, rowCount) != NativeRdb::E_OK) {
62 MEDIA_ERR_LOG("rdb failed");
63 return E_DB_FAIL;
64 }
65 if (rowCount > OPRN_TABLE_OVERSIZE_LIMIT) {
66 int ret = rdbStore->ExecuteSql(DELETE_FROM_OPERATION_TABLE);
67 CHECK_AND_RETURN_RET_LOG(ret == NativeRdb::E_OK, E_HAS_DB_ERROR, "Query not match data fails");
68 MEDIA_INFO_LOG("oprn table aging delete");
69 }
70 lastcheckTime = MediaFileUtils::UTCTimeMilliSeconds();
71 MEDIA_INFO_LOG("oprn table row count:%{public}d", rowCount);
72 }
73 return E_OK;
74 }
75 } // namespace Media
76 } // namespace OHOS