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_MEDIA_FOREGROUND_ANALYSIS_META_H 17 #define OHOS_MEDIA_FOREGROUND_ANALYSIS_META_H 18 19 #include <string> 20 21 #include "abs_shared_result_set.h" 22 #include "datashare_predicates.h" 23 #include "medialibrary_async_worker.h" 24 #include "medialibrary_command.h" 25 26 namespace OHOS::Media { 27 enum ForegroundAnalysisOpType : uint32_t { 28 FOREGROUND_NOT_HANDLE = 0, 29 OCR_AND_LABEL = 0X01, 30 SEARCH_INDEX = 0X02 31 }; 32 33 static constexpr const char *FOREGROUND_ANALYSIS_TYPE = "foreground_analysis_type"; 34 static constexpr const char *FOREGROUND_ANALYSIS_TASK_ID = "foreground_analysis_task_id"; 35 static const int FRONT_INDEX_MAX_LIMIT = 2000; 36 static const int FRONT_CV_MAX_LIMIT = 20; 37 static const int FRONT_THREAD_NUM = 4; 38 static const int DELAY_BATCH_TRIGGER_TIME_MS = 2000; 39 class ForegroundAnalysisMeta { 40 public: 41 ForegroundAnalysisMeta() = default; 42 ForegroundAnalysisMeta(std::shared_ptr<NativeRdb::ResultSet> result); 43 ~ForegroundAnalysisMeta(); 44 int32_t GenerateOpType(MediaLibraryCommand &cmd); 45 void StartAnalysisService(); 46 GetIncTaskId()47 static int32_t GetIncTaskId() 48 { 49 static std::atomic<int32_t> incTaskId {1}; 50 return incTaskId.fetch_add(1, std::memory_order_relaxed); 51 } 52 53 static std::shared_ptr<NativeRdb::ResultSet> QueryByErrorCode(int32_t errCode); 54 static void StartServiceByOpType(uint32_t opType, const std::vector<std::string> &fileIds, int32_t taskId); 55 private: 56 bool IsMetaDirtyed(); 57 int32_t RefreshMeta(); 58 int32_t CheckCvAnalysisCondition(MediaLibraryCommand &cmd); 59 int32_t CheckIndexAnalysisCondition(MediaLibraryCommand &cmd); 60 int32_t QueryPendingAnalyzeFileIds(MediaLibraryCommand &cmd, std::vector<std::string> &fileIds); 61 int32_t QueryPendingIndexCount(MediaLibraryCommand &cmd, int32_t &count); 62 void AppendAnalysisTypeOnWhereClause(int32_t type, std::string &whereClause); 63 int32_t GetCurTaskId(MediaLibraryCommand &cmd); 64 65 int frontIndexLimit_ = FRONT_INDEX_MAX_LIMIT; 66 int64_t frontIndexModified_ = 0L; 67 int frontIndexCount_ = 0; 68 int64_t frontCvModified_ = 0L; 69 int frontCvCount_ = 0; 70 bool isInit_ = false; 71 uint32_t opType_ = ForegroundAnalysisOpType::FOREGROUND_NOT_HANDLE; 72 std::vector<std::string> fileIds_; 73 int32_t taskId_ = -1; 74 }; 75 76 class DelayBatchTrigger { 77 public: 78 static DelayBatchTrigger &GetTrigger(); 79 DelayBatchTrigger(std::function<void(const std::map<int32_t, std::set<std::string>> &)> callback, int delayMs); 80 void Push(const std::vector<std::string> &fileIds, int32_t analysisType); 81 private: 82 void StartTimer(); 83 84 std::mutex mutex_; 85 std::map<int32_t, std::set<std::string>> requestMap_; 86 std::function<void(const std::map<int32_t, std::set<std::string>> &)> callback_; 87 int delayMs_; 88 std::atomic<bool> timerRunning_; 89 }; 90 } // namespace OHOS::Media 91 #endif // OHOS_MEDIA_FOREGROUND_ANALYSIS_META_H