/workspaces/astro/sol3-sdk/cpp/sol3/core/size_delimited_log_reader.h Source File

Space-ng SDK: /workspaces/astro/sol3-sdk/cpp/sol3/core/size_delimited_log_reader.h Source File
Space-ng SDK
size_delimited_log_reader.h
Go to the documentation of this file.
1 // Copyright (c) Space-ng, inc. All rights reserved.
2 
3 #pragma once
4 
6 #include "sol3/cpp/filesystem.h"
7 
8 #include <flatbuffers/flatbuffers.h>
9 #include <tl/expected.hpp>
10 
11 #include <cstddef>
12 #include <memory>
13 #include <optional>
14 #include <stdexcept>
15 #include <string>
16 #include <string_view>
17 
18 namespace sol3::core {
23  : public std::enable_shared_from_this<SizeDelimitedLogReader> {
24  public:
26  struct ReadError {
27  enum class Code {
29  kEndOfFile,
31  kNotOpen,
38  };
39 
43  size_t offset = 0;
45  size_t size = 0;
47  std::string path;
48  };
49 
51  class ReadErrorException : public std::runtime_error {
52  public:
54  ReadErrorException(ReadError err, std::string_view context);
55 
57  std::string context;
58  };
59 
60  class Iterator;
61 
65  class FrameView {
66  public:
68 
73  flatbuffers::span<uint8_t const> span() const {
74  return flatbuffers::span<uint8_t const>(span_data_, span_size_);
75  }
78  flatbuffers::span<uint8_t const> spanNoPrefix() const;
80  size_t offsetBytes() const { return offset_; }
81 
82  private:
83  friend class SizeDelimitedLogReader;
84  friend class Iterator;
85  FrameView(
86  std::shared_ptr<SizeDelimitedLogReader const> reader,
87  flatbuffers::span<uint8_t const> span,
88  size_t offset);
89 
90  std::shared_ptr<SizeDelimitedLogReader const> reader_;
91  uint8_t const* span_data_ = nullptr;
92  size_t span_size_ = 0;
93  size_t offset_ = 0;
94  };
95 
100  class Iterator {
101  public:
103  using reference = FrameView const&;
104  using pointer = FrameView const*;
105 
107  explicit Iterator(tl::expected<FrameView, ReadError> current);
108  Iterator(Iterator const&) = default;
109  Iterator& operator=(Iterator const& other) = default;
110  Iterator(Iterator&&) = default;
111  Iterator& operator=(Iterator&& other) = default;
112  ~Iterator() = default;
113 
121  bool operator==(Iterator const& other) const;
122  bool operator!=(Iterator const& other) const;
123 
125  tl::expected<FrameView, ReadError> const& frameView() const;
126 
127  private:
128  void advance();
129  reference frame() const;
130 
131  tl::expected<FrameView, ReadError> current_;
132  };
133 
138 
143  static std::shared_ptr<SizeDelimitedLogReader> open(
144  cpp::fs::path const& log_path);
145 
150  tl::expected<FrameView, ReadError> readAtOffset(size_t offset) const;
151 
155  Iterator begin() const;
157  Iterator const& end() const;
158 
160  cpp::fs::path const& logPath() const { return log_path_; }
161 
162  private:
163  explicit SizeDelimitedLogReader(cpp::fs::path const& log_path);
164  void openFile(cpp::fs::path const& log_path);
165  void resetFile();
166 
167  cpp::fs::path log_path_;
169  impl::Mmap mmap_;
170  uint8_t const* data_ = nullptr;
171  size_t size_ = 0;
172 
173  Iterator end_it_;
174 };
175 
179 std::string formatReadError(
180  SizeDelimitedLogReader::ReadError const& err, std::string_view context);
181 // NOLINTBEGIN(readability-identifier-naming)
186 // NOLINTEND(readability-identifier-naming)
187 } // namespace sol3::core
Definition: size_delimited_log_reader.h:65
size_t offsetBytes() const
Byte offset of the frame within the file.
Definition: size_delimited_log_reader.h:80
flatbuffers::span< uint8_t const > spanNoPrefix() const
flatbuffers::span< uint8_t const > span() const
Definition: size_delimited_log_reader.h:73
Definition: size_delimited_log_reader.h:100
Iterator(tl::expected< FrameView, ReadError > current)
bool operator==(Iterator const &other) const
FrameView const & reference
Definition: size_delimited_log_reader.h:103
reference operator*() const
Access the current frame; throws ReadErrorException if in error state.
tl::expected< FrameView, ReadError > const & frameView() const
Access the current frame or the read error, if any.
Iterator & operator=(Iterator const &other)=default
Iterator & operator=(Iterator &&other)=default
pointer operator->() const
Access the current frame; throws ReadErrorException if in error state.
FrameView const * pointer
Definition: size_delimited_log_reader.h:104
bool operator!=(Iterator const &other) const
Exception wrapper that exposes the ReadError and context string.
Definition: size_delimited_log_reader.h:51
ReadErrorException(ReadError err, std::string_view context)
Builds a formatted error message for the provided ReadError.
std::string context
Definition: size_delimited_log_reader.h:57
ReadError error
Definition: size_delimited_log_reader.h:56
Definition: size_delimited_log_reader.h:23
SizeDelimitedLogReader & operator=(SizeDelimitedLogReader const &)=delete
SizeDelimitedLogReader(SizeDelimitedLogReader const &)=delete
SizeDelimitedLogReader & operator=(SizeDelimitedLogReader &&)=delete
tl::expected< FrameView, ReadError > readAtOffset(size_t offset) const
Iterator const & end() const
Sentinel iterator representing end-of-file.
SizeDelimitedLogReader(SizeDelimitedLogReader &&)=delete
static std::shared_ptr< SizeDelimitedLogReader > open(cpp::fs::path const &log_path)
cpp::fs::path const & logPath() const
Path for the currently opened log file.
Definition: size_delimited_log_reader.h:160
Definition: shmem_buffer_impl.h:13
Definition: shmem_buffer_impl.h:36
Definition: any_message_input.h:15
std::string format_as(AnyMessageView &view)
Definition: any_message_view.h:111
std::string formatReadError(SizeDelimitedLogReader::ReadError const &err, std::string_view context)
Format a ReadError for logs or exceptions.
char const * readErrorCodeToString(SizeDelimitedLogReader::ReadError::Code code)
String conversion for error codes (stable, human-readable strings).
Read error details returned from readAtOffset() and iteration.
Definition: size_delimited_log_reader.h:26
Code
Definition: size_delimited_log_reader.h:27
@ kZeroSizeFrame
Size prefix declares a zero-length payload.
@ kNotOpen
File is not open or has been reset.
@ kTruncatedPrefix
Size prefix is truncated (not enough bytes for uint32_t).
@ kTruncatedBuffer
Payload or padding extends past the mapped file size.
@ kEndOfFile
Requested offset is at or past the logical end of file.
size_t offset
Byte offset used for the read attempt.
Definition: size_delimited_log_reader.h:43
size_t size
Total mapped file size at the time of the read.
Definition: size_delimited_log_reader.h:45
std::string path
Log file path associated with the read, when available.
Definition: size_delimited_log_reader.h:47
Code code
Error code describing the failure mode.
Definition: size_delimited_log_reader.h:41