1 /* 2 * Copyright (c) 2023-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 #ifndef HIVIEW_BASE_EVENT_STORE_SYS_EVENT_DATABASE_H 17 #define HIVIEW_BASE_EVENT_STORE_SYS_EVENT_DATABASE_H 18 19 #include <queue> 20 #include <memory> 21 #include <shared_mutex> 22 #include <string> 23 24 #include "base_def.h" 25 #include "doc_query.h" 26 #include "singleton.h" 27 #include "sys_event_query.h" 28 #include "sys_event_doc_lru_cache.h" 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 namespace EventStore { 33 class SysEventDatabase : public OHOS::DelayedRefSingleton<SysEventDatabase> { 34 public: 35 SysEventDatabase(); 36 ~SysEventDatabase(); 37 int Insert(const std::shared_ptr<SysEvent>& sysEvent); 38 void Clear(); 39 int Query(SysEventQuery& query, EntryQueue& entries); 40 void CheckRepeat(SysEvent& event); 41 std::string GetDatabaseDir(); 42 bool Backup(const std::string& zipFilePath); 43 bool Restore(const std::string& zipFilePath, const std::string& restoreDir); 44 45 private: 46 using FileQueue = std::priority_queue<std::string, std::vector<std::string>, 47 bool(*)(const std::string&, const std::string&)>; 48 // <eventType, <maxSize, maxFileNum>> 49 using EventQuotaMap = std::unordered_map<int, std::pair<uint64_t, uint32_t>>; 50 // <eventType, <totalFileSize, fileQueue that is normal, fileQueue that is over limit>> 51 using ClearFilesMap = std::unordered_map<int, std::tuple<uint64_t, FileQueue, FileQueue>>; 52 53 void InitQuotaMap(); 54 void UpdateClearMap(); 55 void ClearCache(); 56 uint32_t GetMaxFileNum(int type); 57 uint64_t GetMaxSize(int type); 58 void GetQueryFiles(const SysEventQueryArg& queryArg, FileQueue& queryFiles); 59 void GetQueryDirsByDomain(const std::string& domain, std::vector<std::string>& queryDirs); 60 bool IsContainQueryArg(const std::string& file, const SysEventQueryArg& queryArg, 61 std::unordered_map<std::string, long long>& nameSeqMap); 62 int QueryByFiles(SysEventQuery& query, EntryQueue& entries, FileQueue& queryFiles); 63 64 EventQuotaMap quotaMap_; 65 ClearFilesMap clearMap_; 66 std::unique_ptr<SysEventDocLruCache> lruCache_; 67 mutable std::shared_mutex mutex_; 68 }; // SysEventDatabase 69 } // EventStore 70 } // HiviewDFX 71 } // OHOS 72 #endif // HIVIEW_BASE_EVENT_STORE_SYS_EVENT_DATABASE_H 73