1 /* 2 * Copyright (c) 2022-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 #ifndef HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_CACHE_APP_EVENT_STORE_H 16 #define HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_CACHE_APP_EVENT_STORE_H 17 18 #include <memory> 19 #include <mutex> 20 #include <string> 21 #include <vector> 22 23 #include "app_event_dao.h" 24 #include "app_event_mapping_dao.h" 25 #include "app_event_observer_dao.h" 26 #include "custom_event_param_dao.h" 27 #include "nocopyable.h" 28 #include "rdb_store.h" 29 #include "singleton.h" 30 #include "user_id_dao.h" 31 #include "user_property_dao.h" 32 33 namespace OHOS { 34 namespace HiviewDFX { 35 class AppEventPack; 36 37 class AppEventStore : public NoCopyable { 38 public: 39 static AppEventStore& GetInstance(); 40 41 int InitDbStore(); 42 int DestroyDbStore(); 43 int64_t InsertEvent(std::shared_ptr<AppEventPack> event); 44 int64_t InsertObserver(const AppEventCacheCommon::Observer& observer); 45 int InsertEventMapping(const std::vector<AppEventCacheCommon::EventObserverInfo>& eventObservers); 46 int InsertUserId(const std::string& name, const std::string& value); 47 int InsertUserProperty(const std::string& name, const std::string& value); 48 int InsertCustomEventParams(std::shared_ptr<AppEventPack> event); 49 int UpdateUserId(const std::string& name, const std::string& value); 50 int UpdateUserProperty(const std::string& name, const std::string& value); 51 int UpdateObserver(int64_t seq, const std::string& filters); 52 int TakeEvents(std::vector<std::shared_ptr<AppEventPack>>& events, int64_t observerSeq, uint32_t eventSize = 0); 53 int QueryEvents(std::vector<std::shared_ptr<AppEventPack>>& events, int64_t observerSeq, uint32_t eventSize = 0); 54 int64_t QueryObserverSeq(const std::string& name, int64_t hashCode = 0); 55 int64_t QueryObserverSeqAndFilters(const std::string& name, int64_t hashCode, std::string& filters); 56 int QueryObserverSeqs(const std::string& name, std::vector<int64_t>& observerSeqs); 57 int QueryWatchers(std::vector<AppEventCacheCommon::Observer>& observers); 58 int QueryUserIds(std::unordered_map<std::string, std::string>& out); 59 int QueryUserId(const std::string& name, std::string& out); 60 int QueryUserProperties(std::unordered_map<std::string, std::string>& out); 61 int QueryUserProperty(const std::string& name, std::string& out); 62 int QueryCustomParamsAdd2EventPack(std::shared_ptr<AppEventPack> event); 63 int DeleteObserver(int64_t observerSeq); 64 int DeleteEventMapping(int64_t observerSeq = 0, const std::vector<int64_t>& eventSeqs = {}); 65 int DeleteUserId(const std::string& name = ""); 66 int DeleteUserProperty(const std::string& name = ""); 67 int DeleteEvent(int64_t eventSeq = 0); 68 int DeleteCustomEventParams(); 69 int DeleteEvent(const std::vector<int64_t>& eventSeqs); 70 int DeleteUnusedParamsExceptCurId(const std::string& curRunningId); 71 int DeleteUnusedEventMapping(); 72 int DeleteHistoryEvent(int reservedNum, int reservedNumOs); 73 bool DeleteData(int64_t observerSeq, const std::vector<int64_t>& eventSeqs); 74 75 private: 76 AppEventStore(); 77 ~AppEventStore(); 78 bool InitDbStoreDir(); 79 void CheckAndRepairDbStore(int errCode); 80 81 private: 82 std::shared_ptr<NativeRdb::RdbStore> dbStore_; 83 std::string dirPath_; 84 std::mutex dbMutex_; 85 }; 86 } // namespace HiviewDFX 87 } // namespace OHOS 88 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_CACHE_APP_EVENT_STORE_H 89