• 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 OHOS_MEDIALIBRARY_ACCURATE_REFRESH_DATA_MANAGER_H
17 #define OHOS_MEDIALIBRARY_ACCURATE_REFRESH_DATA_MANAGER_H
18 
19 #include <string>
20 #include <map>
21 
22 #include "abs_rdb_predicates.h"
23 #include "result_set.h"
24 #include "accurate_common_data.h"
25 #include "medialibrary_rdb_transaction.h"
26 
27 namespace OHOS {
28 namespace Media::AccurateRefresh {
29 #define EXPORT __attribute__ ((visibility ("default")))
30 
31 template <typename ChangeInfo, typename ChangeData>
32 class EXPORT AccurateRefreshDataManager {
33 public:
AccurateRefreshDataManager(std::shared_ptr<TransactionOperations> trans)34     AccurateRefreshDataManager(std::shared_ptr<TransactionOperations> trans): trans_(trans) {}
35     // init的查询语句,Init只需要执行一次
36     int32_t Init(const NativeRdb::AbsRdbPredicates &predicates);
37     int32_t Init(const std::string sql, const std::vector<NativeRdb::ValueObject> bindArgs);
38     // delete/update场景下初始化数据
39     int32_t Init(const std::vector<int32_t> &keys);
40 
41     virtual int32_t UpdateModifiedDatas() = 0;
42     int32_t UpdateModifiedDatasInner(const std::vector<int32_t> &keys, RdbOperation operation,
43         PendingInfo &pendingInfo);
44     virtual int32_t PostProcessModifiedDatas(const std::vector<int32_t> &keys) = 0;
45     // 根据isCheckUpdate在数据获取时进行一次刷新处理,解决多线程问题
46     std::vector<ChangeData> GetChangeDatas(bool isCheckUpdate = false);
47     virtual std::vector<int32_t> GetInitKeys() = 0;
48     void SetTransaction(std::shared_ptr<TransactionOperations> trans);
49     // 外部接口数据无法获取修改前后数据进行精准计算
50     bool CheckIsForRecheck();
51     bool CanTransOperate();
52 
53 protected:
54     int32_t InsertInitChangeInfos(const std::vector<ChangeInfo> &changeInfos, PendingInfo pendingInfo = PendingInfo());
55     bool CheckIsExceed(bool isLengthChanged = false);
56     bool CheckIsExceed(std::size_t length);
57 
58 private:
59     int32_t CheckAndUpdateOperation(RdbOperation &newOperation, RdbOperation oldOperation);
60     int32_t UpdateModifiedDatasForRemove(const std::vector<int32_t> &keys, PendingInfo &pendingInfo);
61     int32_t UpdateModifiedDatasForUpdate(const std::vector<int32_t> &keys, PendingInfo &pendingInfo);
62     int32_t UpdateModifiedDatasForAdd(const std::vector<int32_t> &keys, PendingInfo &pendingInfo);
63     bool IsValidChangeInfo(const ChangeInfo &changeInfo);
64 
65     virtual int32_t GetChangeInfoKey(const ChangeInfo &changeInfo) = 0;
66     virtual std::vector<ChangeInfo> GetInfoByKeys(const std::vector<int32_t> &keys) = 0;
67     virtual std::vector<ChangeInfo> GetInfosByPredicates(const NativeRdb::AbsRdbPredicates &predicates) = 0;
68     virtual std::vector<ChangeInfo> GetInfosByResult(const std::shared_ptr<NativeRdb::ResultSet> &resultSet) = 0;
69     std::size_t GetCurrentDataLength();
70     // before数据插入后处理
PostInsertBeforeData(ChangeData & changeData,PendingInfo & pendingInfo)71     virtual void PostInsertBeforeData(ChangeData &changeData, PendingInfo &pendingInfo) {}
72     // after数据插入后处理
73     virtual void PostInsertAfterData(ChangeData &changeData, PendingInfo &pendingInfo, bool isAdd = false) {}
74     // 资产数据在更新相册时,可能需要刷新,解决多线程问题
CheckUpdateDataForMultiThread(ChangeData & changeData)75     virtual bool CheckUpdateDataForMultiThread(ChangeData &changeData) { return false; }
76 
77 protected:
78     std::map<int32_t, ChangeData> changeDatas_;
79     std::shared_ptr<TransactionOperations> trans_;
80     bool isExceed_ = false;
81     bool isForRecheck_ = false;
82 };
83 
84 } // namespace Media
85 } // namespace OHOS
86 
87 #endif