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 "ffrt.h"
28 #include "nocopyable.h"
29 #include "rdb_store.h"
30 #include "singleton.h"
31 #include "user_id_dao.h"
32 #include "user_property_dao.h"
33 
34 namespace OHOS {
35 namespace HiviewDFX {
36 class AppEventPack;
37 
38 class AppEventStore : public NoCopyable {
39 public:
40     static AppEventStore& GetInstance();
41 
42     int InitDbStore();
43     int DestroyDbStore();
44     int64_t InsertEvent(std::shared_ptr<AppEventPack> event);
45     int64_t InsertObserver(const std::string& observer, int64_t hashCode = 0);
46     int64_t InsertEventMapping(int64_t eventSeq, int64_t observerSeq);
47     int64_t InsertUserId(const std::string& name, const std::string& value);
48     int64_t InsertUserProperty(const std::string& name, const std::string& value);
49     int64_t InsertCustomEventParams(std::shared_ptr<AppEventPack> event);
50     int64_t UpdateUserId(const std::string& name, const std::string& value);
51     int64_t UpdateUserProperty(const std::string& name, const std::string& value);
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& observer, int64_t hashCode = 0);
55     int QueryObserverSeqs(const std::string& observer, std::vector<int64_t>& observerSeqs);
56     int QueryUserIds(std::unordered_map<std::string, std::string>& out);
57     int QueryUserId(const std::string& name, std::string& out);
58     int QueryUserProperties(std::unordered_map<std::string, std::string>& out);
59     int QueryUserProperty(const std::string& name, std::string& out);
60     int QueryCustomParamsAdd2EventPack(std::shared_ptr<AppEventPack> event);
61     int DeleteObserver(int64_t observerSeq);
62     int DeleteEventMapping(int64_t observerSeq = 0, const std::vector<int64_t>& eventSeqs = {});
63     int DeleteUserId(const std::string& name = "");
64     int DeleteUserProperty(const std::string& name = "");
65     int DeleteEvent(int64_t eventSeq = 0);
66     int DeleteCustomEventParams();
67     int DeleteEvent(const std::vector<int64_t>& eventSeqs);
68     int DeleteUnusedParamsExceptCurId(const std::string& curRunningId);
69     int DeleteUnusedEventMapping();
70     int DeleteHistoryEvent(int reservedNum, int reservedNumOs);
71     bool DeleteData(int64_t observerSeq, const std::vector<int64_t>& eventSeqs);
72 
73 private:
74     AppEventStore();
75     ~AppEventStore();
76     bool InitDbStoreDir();
77 
78 private:
79     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
80     std::shared_ptr<AppEventDao> appEventDao_;
81     std::shared_ptr<AppEventObserverDao> appEventObserverDao_;
82     std::shared_ptr<AppEventMappingDao> appEventMappingDao_;
83     std::shared_ptr<UserIdDao> userIdDao_;
84     std::shared_ptr<UserPropertyDao> userPropertyDao_;
85     std::shared_ptr<CustomEventParamDao> customEventParamDao_;
86     std::string dirPath_;
87     ffrt::mutex dbMutex_;
88 };
89 } // namespace HiviewDFX
90 } // namespace OHOS
91 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_CACHE_APP_EVENT_STORE_H
92