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_UTILITY_SYS_EVENT_DOC_READER_H
17 #define HIVIEW_BASE_EVENT_STORE_UTILITY_SYS_EVENT_DOC_READER_H
18 
19 #include <fstream>
20 #include <functional>
21 #include <string>
22 
23 #include "content_reader_factory.h"
24 #include "event_doc_reader.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace EventStore {
29 using ReadCallback = std::function<bool(uint8_t* content, uint32_t& contentSize)>;
30 class SysEventDocReader : public EventDocReader {
31 public:
32     SysEventDocReader(const std::string& path);
33     ~SysEventDocReader();
34     int Read(const DocQuery& query, EntryQueue& entries, int& num) override;
35     int ReadFileSize();
36     int ReadPageSize(uint32_t& pageSize);
37     int ReadHeader(DocHeader& header);
38     int ReadHeader(DocHeader& header, HeadExtraInfo& headExtra);
39 
40 private:
41     void Init(const std::string& path);
42     void InitEventInfo(const std::string& path);
43     int Read(ReadCallback callback);
44     int ReadContent(uint8_t** content, uint32_t& contentSize, uint32_t pageIndex);
45     int ReadPages(ReadCallback callback);
46     bool HasReadFileEnd();
47     bool HasReadPageEnd(uint32_t pageIndex);
48     bool IsValidHeader(const DocHeader& header);
49     bool CheckEventInfo(uint8_t* content);
50     int SeekgPage(uint32_t pageIndex);
51     std::shared_ptr<RawData> BuildRawData(uint8_t* content, uint32_t contentSize);
52     void TryToAddEntry(uint8_t* content, uint32_t contentSize, const DocQuery& query,
53         EntryQueue& entries, int& num);
54 
55 private:
56     std::ifstream in_;
57     int fileSize_ = 0;
58     uint32_t pageSize_ = 0;
59     uint8_t dataFmtVersion_ = 0;
60     uint64_t docHeaderSize_ = 0;
61     HeadExtraInfo headExtra_;
62     EventInfo info_;
63 }; // EventDocWriter
64 } // EventStore
65 } // HiviewDFX
66 } // OHOS
67 #endif // HIVIEW_BASE_EVENT_STORE_UTILITY_SYS_EVENT_DOC_READER_H
68