roo_monitoring
API Documentation for roo_monitoring
Loading...
Searching...
No Matches
vault.h
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4
5#include "common.h"
6#include "log.h" // for LogCursor.
7#include "roo_io/data/multipass_input_stream_reader.h"
8#include "roo_logging.h"
9#include "sample.h"
10
11namespace roo_monitoring {
12
13class Collection;
14
15/// Identifies a specific file in the monitoring vault.
17 public:
18 /// Creates a reference that encloses the timestamp at the given resolution.
20
21 VaultFileRef() : timestamp_(0), resolution_(kResolution_1024_ms) {}
22 VaultFileRef(const VaultFileRef& other) = default;
24
25 /// Returns the start timestamp for this vault file.
26 int64_t timestamp() const { return timestamp_; }
27 /// Returns the timestamp for the entry at the given position.
28 int64_t timestamp_at(int position) const {
29 return timestamp_ + time_steps(position);
30 }
31 /// Returns the resolution for this vault file.
32 Resolution resolution() const { return resolution_; }
33
34 /// Returns the time step between entries.
35 int64_t time_step() const { return 1LL << (resolution_ << 1); }
36 /// Returns the time step delta for the specified count.
38 return (int64_t)count << (resolution_ << 1);
39 }
40 /// Returns the total time span covered by the file.
42 return 1LL << ((resolution_ + kRangeLength) << 1);
43 }
44
45 /// Returns the parent vault file at the next coarser resolution.
47 return Lookup(timestamp_, Resolution(resolution_ + 1));
48 }
49
50 /// Returns the child vault file at the next finer resolution.
51 VaultFileRef child(int index) const {
52 return VaultFileRef(timestamp_, Resolution(resolution_ - 1)).advance(index);
53 }
54
55 /// Returns the previous vault file at the same resolution.
57 return VaultFileRef(timestamp_ - time_span(), resolution_);
58 }
59
60 /// Returns the next vault file at the same resolution.
62 return VaultFileRef(timestamp_ + time_span(), resolution_);
63 }
64
65 /// Returns the vault file advanced by n spans.
66 VaultFileRef advance(int n) const {
67 return VaultFileRef(timestamp_ + n * time_span(), resolution_);
68 }
69
70 /// Returns the index of this file within its parent range.
71 int sibling_index() const {
72 return (timestamp_ >> ((resolution_ + kRangeLength) << 1)) & 0x3;
73 }
74
75 private:
77 : timestamp_(timestamp), resolution_(resolution) {}
78
79 int64_t timestamp_;
80 Resolution resolution_;
81};
82
83/// Writes a human-readable representation of the vault file reference.
84roo_logging::Stream& operator<<(roo_logging::Stream& os,
85 const VaultFileRef& file_ref);
86
87/// Sequential reader for a single vault file.
88///
89/// A single vault file has the following format:
90///
91/// header:
92/// major version (uint8): currently always 1
93/// minor version (uint8): currently always 1
94/// entry[]:
95/// sample count (varint)
96/// sample[]:
97/// stream ID (varint)
98/// avg (uint16)
99/// min (uint16)
100/// max (uint16)
101/// fill (uint16)
102///
103/// The file name of the vault file implies the start timestamp.
104/// The level implies the time resolution.
105/// The finished vault always has 256 entries.
107 public:
108 /// Creates a reader bound to the specified collection.
109 VaultFileReader(const Collection* collection);
110 // VaultFileReader(VaultFileReader&& other) = default;
111 // VaultFileReader(const VaultFileReader& other) = delete;
112 // VaultFileReader& operator=(VaultFileReader&& other);
113
114 /// Opens the file and seeks to the specified index and byte offset.
115 bool open(const VaultFileRef& ref, int index, int64_t offset);
116 /// Returns true if a file is currently open.
117 bool is_open() const { return reader_.isOpen(); }
118
119 /// Closes the reader.
120 void close() { reader_.close(); }
121
122 /// Advances the cursor to the first entry at or after the timestamp.
123 void seekForward(int64_t timestamp);
124 /// Reads the next entry and fills the sample vector.
125 bool next(std::vector<Sample>* sample);
126 /// Returns the current entry index.
127 int index() const { return index_; }
128 /// Returns true if the reader has passed the end of file.
129 bool past_eof() const;
130
131 /// Returns true if the file is either good or does not exist.
132 ///
133 /// If open fails for any reason other than not found, or if read fails,
134 /// this returns false.
135 bool ok() const {
136 return reader_.status() == roo_io::kOk ||
137 reader_.status() == roo_io::kNotFound;
138 }
139
140 /// Returns the current reader status.
141 roo_io::Status status() const { return reader_.status(); }
142
143 /// Returns the vault file reference for this reader.
144 const VaultFileRef& vault_ref() const { return ref_; }
145
146 /// Returns the current log cursor.
147 LogCursor tell();
148
150
151 private:
152 const Collection* collection_;
153 VaultFileRef ref_;
154 roo_io::Mount fs_;
155 roo_io::MultipassInputStreamReader reader_;
156 int index_;
157 int position_;
158};
159
160} // namespace roo_monitoring
Collection of timeseries sharing transform and source resolution.
Cursor used when seeking through multiple log files.
Definition log.h:95
Sequential reader for a single vault file.
Definition vault.h:106
bool ok() const
Returns true if the file is either good or does not exist.
Definition vault.h:135
void seekForward(int64_t timestamp)
Advances the cursor to the first entry at or after the timestamp.
Definition vault.cpp:181
void close()
Closes the reader.
Definition vault.h:120
LogCursor tell()
Returns the current log cursor.
Definition vault.cpp:126
int index() const
Returns the current entry index.
Definition vault.h:127
bool past_eof() const
Returns true if the reader has passed the end of file.
Definition vault.cpp:201
bool next(std::vector< Sample > *sample)
Reads the next entry and fills the sample vector.
Definition vault.cpp:145
bool is_open() const
Returns true if a file is currently open.
Definition vault.h:117
bool open(const VaultFileRef &ref, int index, int64_t offset)
Opens the file and seeks to the specified index and byte offset.
Definition vault.cpp:77
const VaultFileRef & vault_ref() const
Returns the vault file reference for this reader.
Definition vault.h:144
roo_io::Status status() const
Returns the current reader status.
Definition vault.h:141
Identifies a specific file in the monitoring vault.
Definition vault.h:16
int64_t timestamp() const
Returns the start timestamp for this vault file.
Definition vault.h:26
int64_t time_span() const
Returns the total time span covered by the file.
Definition vault.h:41
int64_t timestamp_at(int position) const
Returns the timestamp for the entry at the given position.
Definition vault.h:28
VaultFileRef advance(int n) const
Returns the vault file advanced by n spans.
Definition vault.h:66
VaultFileRef parent() const
Returns the parent vault file at the next coarser resolution.
Definition vault.h:46
int sibling_index() const
Returns the index of this file within its parent range.
Definition vault.h:71
VaultFileRef next() const
Returns the next vault file at the same resolution.
Definition vault.h:61
VaultFileRef(const VaultFileRef &other)=default
Resolution resolution() const
Returns the resolution for this vault file.
Definition vault.h:32
int64_t time_steps(int count) const
Returns the time step delta for the specified count.
Definition vault.h:37
VaultFileRef child(int index) const
Returns the child vault file at the next finer resolution.
Definition vault.h:51
VaultFileRef & operator=(const VaultFileRef &other)=default
int64_t time_step() const
Returns the time step between entries.
Definition vault.h:35
VaultFileRef prev() const
Returns the previous vault file at the same resolution.
Definition vault.h:56
static VaultFileRef Lookup(int64_t timestamp, Resolution resolution)
Creates a reference that encloses the timestamp at the given resolution.
Umbrella header for the roo_monitoring module.
Resolution
Time resolution used for log and vault files.
Definition resolution.h:8
static const int kRangeLength
Number of base-4 digits used per range.
Definition common.h:19
roo_logging::Stream & operator<<(roo_logging::Stream &os, const VaultFileRef &file_ref)
Writes a human-readable representation of the vault file reference.
Definition vault.cpp:203