farm-ng-core
event_log_reader.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
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 // Copyright (c) farm-ng, inc. All rights reserved.
16 
17 #pragma once
18 
19 #include <farm_ng/core/event.pb.h>
20 
21 #include <exception>
22 #include <filesystem>
23 #include <memory>
24 #include <string>
25 
26 namespace farm_ng {
27 class EventLogEof : public std::runtime_error {
28  public:
29  explicit EventLogEof(std::string const& what) : std::runtime_error(what) {}
30 };
31 class EventLogExist : public std::runtime_error {
32  public:
33  explicit EventLogExist(std::string const& what) : std::runtime_error(what) {}
34 };
35 
36 class EventLogReaderImpl;
37 class EventLogPos {
38  public:
40  core::proto::Event event,
41  std::streampos pos,
42  std::weak_ptr<EventLogReaderImpl> log);
43 
44  core::proto::Event const& event() const;
45  std::string readPayload() const;
46 
47  private:
48  core::proto::Event event_;
49  std::streampos pos_;
50  std::weak_ptr<EventLogReaderImpl> log_;
51 };
52 
53 /// Implementation of the `EventLogReader` class
54 ///
56  : public std::enable_shared_from_this<EventLogReaderImpl> {
57  public:
58  /// https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dtor
59  virtual ~EventLogReaderImpl() {}
60 
61  virtual void reset() = 0;
62 
63  /// Returns next event.
64  virtual EventLogPos readNextEvent(std::string* payload = nullptr) = 0;
65 
66  virtual std::string readPayload(
67  core::proto::Event const& event, std::streampos pos) = 0;
68 
69  /// Returns the path including the fileaname
70  virtual std::filesystem::path getPath() const = 0;
71 
72  std::vector<EventLogPos> const& getIndex();
73 
74  private:
75  std::vector<EventLogPos> index_;
76 };
77 
78 /// Reader to deserialize data written by the EventLogWriter.
79 ///
81  public:
82  /// Open's log file to read.
83  ///
84  /// Throws runtime-error if files could not be opened or if logfile does not
85  /// contain a valid header.
86  explicit EventLogReader(std::filesystem::path const& log_path);
87 
88  /// https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dtor
89  virtual ~EventLogReader();
90 
91  // This function throws EventLogEof if the end of file is reached or a message
92  // can not be decoded (typically due to an interrupted process).
93  EventLogPos readNextEvent(std::string* payload = nullptr);
94 
95  // Returns an index of all the events contained in the file. This function
96  // caches the index, so its only computed once; the index requires seeking
97  // through the entire file and decoding the events. It should be fast as
98  // payloads are skipped with seekg and not decoded.
99  std::vector<EventLogPos> const& getIndex();
100 
101  /// Returns the path including the fileaname
102  [[nodiscard]] std::filesystem::path getPath() const;
103 
104  /// Reset the writer to the beginning of the file
105  void reset();
106 
107  private:
108  std::shared_ptr<EventLogReaderImpl> impl_;
109 };
110 
111 // finds the first matching stamp in the event. If no stamp is found this
112 // returns a nullptr. Note that this does not allocate the stamp, its a
113 // reference to the underlying Timestamp owned by the event.
114 core::proto::Timestamp const* getStamp(
115  core::proto::Event const& event,
116  std::string const& clock_name,
117  std::string const& semantics);
118 
120  std::string clock_name;
121  std::string semantics;
122  // precondition:
123  // lhs and rhs both contain a stamp from the target clock_name and
124  // semantics.
125  bool operator()(EventLogPos const& lhs, EventLogPos const& rhs) const;
126 };
127 
128 // Returns events in time order, using the given reference clock and semantics
129 // If two timestamps are exactly the same, the uri().path() of the event is used
130 // to disambiguate the sorting.
131 std::vector<EventLogPos> eventLogTimeOrderedIndex(
132  std::string const& clock_name,
133  std::string const& semantics,
134  std::vector<EventLogReader> const& readers);
135 
136 } // namespace farm_ng
farm_ng::EventLogPos::readPayload
std::string readPayload() const
Definition: event_log_reader.cpp:110
farm_ng::EventLogPos::event
core::proto::Event const & event() const
Definition: event_log_reader.cpp:108
farm_ng
Definition: backtrace.cpp:102
farm_ng::EventLogReaderImpl::readPayload
virtual std::string readPayload(core::proto::Event const &event, std::streampos pos)=0
farm_ng::EventLogReaderImpl::~EventLogReaderImpl
virtual ~EventLogReaderImpl()
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dtor
Definition: event_log_reader.h:59
farm_ng::EventLogReader::EventLogReader
EventLogReader(std::filesystem::path const &log_path)
Open's log file to read.
Definition: event_log_reader.cpp:131
farm_ng::EventTimeCompareClockAndSemantics::semantics
std::string semantics
Definition: event_log_reader.h:121
farm_ng::EventLogEof
Definition: event_log_reader.h:27
farm_ng::EventTimeCompareClockAndSemantics::clock_name
std::string clock_name
Definition: event_log_reader.h:120
farm_ng::EventLogReaderImpl::getIndex
std::vector< EventLogPos > const & getIndex()
Definition: event_log_reader.cpp:116
farm_ng::EventLogReaderImpl::getPath
virtual std::filesystem::path getPath() const =0
Returns the path including the fileaname.
farm_ng::EventLogExist::EventLogExist
EventLogExist(std::string const &what)
Definition: event_log_reader.h:33
farm_ng::EventLogReader::~EventLogReader
virtual ~EventLogReader()
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dtor
Definition: event_log_reader.cpp:134
farm_ng::EventLogReader::getPath
std::filesystem::path getPath() const
Returns the path including the fileaname.
Definition: event_log_reader.cpp:144
farm_ng::EventLogReaderImpl::reset
virtual void reset()=0
farm_ng::eventLogTimeOrderedIndex
auto eventLogTimeOrderedIndex(std::string const &clock_name, std::string const &semantics, std::vector< EventLogReader > const &readers) -> std::vector< EventLogPos >
Definition: event_log_reader.cpp:187
farm_ng::EventLogReaderImpl::readNextEvent
virtual EventLogPos readNextEvent(std::string *payload=nullptr)=0
Returns next event.
farm_ng::EventLogPos
Definition: event_log_reader.h:37
farm_ng::EventLogExist
Definition: event_log_reader.h:31
farm_ng::getStamp
auto getStamp(core::proto::Event const &event, std::string const &clock_name, std::string const &semantics) -> core::proto::Timestamp const *
Definition: event_log_reader.cpp:152
farm_ng::EventLogEof::EventLogEof
EventLogEof(std::string const &what)
Definition: event_log_reader.h:29
farm_ng::EventLogReader::getIndex
std::vector< EventLogPos > const & getIndex()
Definition: event_log_reader.cpp:140
farm_ng::EventLogPos::EventLogPos
EventLogPos(core::proto::Event event, std::streampos pos, std::weak_ptr< EventLogReaderImpl > log)
Definition: event_log_reader.cpp:102
farm_ng::EventLogReaderImpl
Implementation of the EventLogReader class.
Definition: event_log_reader.h:55
farm_ng::EventLogReader::readNextEvent
EventLogPos readNextEvent(std::string *payload=nullptr)
Definition: event_log_reader.cpp:136
farm_ng::EventTimeCompareClockAndSemantics::operator()
bool operator()(EventLogPos const &lhs, EventLogPos const &rhs) const
Definition: event_log_reader.cpp:164
farm_ng::EventTimeCompareClockAndSemantics
Definition: event_log_reader.h:119
farm_ng::EventLogReader::reset
void reset()
Reset the writer to the beginning of the file.
Definition: event_log_reader.cpp:148
farm_ng::EventLogReader
Reader to deserialize data written by the EventLogWriter.
Definition: event_log_reader.h:80