1 /* 2 * Copyright (c) 2022-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 #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 std::string& observer, int64_t hashCode = 0); 45 int64_t InsertEventMapping(int64_t eventSeq, int64_t observerSeq); 46 int64_t InsertUserId(const std::string& name, const std::string& value); 47 int64_t InsertUserProperty(const std::string& name, const std::string& value); 48 int64_t InsertCustomEventParams(std::shared_ptr<AppEventPack> event); 49 int64_t UpdateUserId(const std::string& name, const std::string& value); 50 int64_t UpdateUserProperty(const std::string& name, const std::string& value); 51 int TakeEvents(std::vector<std::shared_ptr<AppEventPack>>& events, int64_t observerSeq, uint32_t eventSize = 0); 52 int QueryEvents(std::vector<std::shared_ptr<AppEventPack>>& events, int64_t observerSeq, uint32_t eventSize = 0); 53 int64_t QueryObserverSeq(const std::string& observer, int64_t hashCode = 0); 54 int QueryObserverSeqs(const std::string& observer, std::vector<int64_t>& observerSeqs); 55 int QueryUserIds(std::unordered_map<std::string, std::string>& out); 56 int QueryUserId(const std::string& name, std::string& out); 57 int QueryUserProperties(std::unordered_map<std::string, std::string>& out); 58 int QueryUserProperty(const std::string& name, std::string& out); 59 int QueryCustomParamsAdd2EventPack(std::shared_ptr<AppEventPack> event); 60 int DeleteObserver(int64_t observerSeq); 61 int DeleteEventMapping(int64_t observerSeq = 0, const std::vector<int64_t>& eventSeqs = {}); 62 int DeleteUserId(const std::string& name = ""); 63 int DeleteUserProperty(const std::string& name = ""); 64 int DeleteEvent(int64_t eventSeq = 0); 65 int DeleteCustomEventParams(); 66 int DeleteEvent(const std::vector<int64_t>& eventSeqs); 67 int DeleteUnusedParamsExceptCurId(const std::string& curRunningId); 68 int DeleteUnusedEventMapping(); 69 int DeleteHistoryEvent(int reservedNum, int reservedNumOs); 70 bool DeleteData(int64_t observerSeq, const std::vector<int64_t>& eventSeqs); 71 72 private: 73 AppEventStore(); 74 ~AppEventStore(); 75 bool InitDbStoreDir(); 76 77 private: 78 std::shared_ptr<NativeRdb::RdbStore> dbStore_; 79 std::shared_ptr<AppEventDao> appEventDao_; 80 std::shared_ptr<AppEventObserverDao> appEventObserverDao_; 81 std::shared_ptr<AppEventMappingDao> appEventMappingDao_; 82 std::shared_ptr<UserIdDao> userIdDao_; 83 std::shared_ptr<UserPropertyDao> userPropertyDao_; 84 std::shared_ptr<CustomEventParamDao> customEventParamDao_; 85 std::string dirPath_; 86 std::mutex dbMutex_; 87 }; 88 } // namespace HiviewDFX 89 } // namespace OHOS 90 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_CACHE_APP_EVENT_STORE_H 91