Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::chain::istream_snapshot_reader Class Reference

#include <snapshot.hpp>

Inheritance diagram for sysio::chain::istream_snapshot_reader:
Collaboration diagram for sysio::chain::istream_snapshot_reader:

Public Member Functions

 istream_snapshot_reader (std::istream &snapshot)
 
void validate () const override
 
bool has_section (const string &section_name) override
 
void set_section (const string &section_name) override
 
bool read_row (detail::abstract_snapshot_row_reader &row_reader) override
 
bool empty () override
 
void clear_section () override
 
void return_to_header () override
 
- Public Member Functions inherited from sysio::chain::snapshot_reader
template<typename F >
void read_section (const std::string &section_name, F f)
 
template<typename T , typename F >
void read_section (F f)
 
template<typename T >
bool has_section (const std::string &suffix=std::string())
 
virtual ~snapshot_reader ()
 

Additional Inherited Members

- Protected Member Functions inherited from sysio::chain::snapshot_reader

Detailed Description

Definition at line 348 of file snapshot.hpp.

Constructor & Destructor Documentation

◆ istream_snapshot_reader()

sysio::chain::istream_snapshot_reader::istream_snapshot_reader ( std::istream & snapshot)
explicit

Definition at line 193 of file snapshot.cpp.

194:snapshot(snapshot)
195,header_pos(snapshot.tellg())
196,num_rows(0)
197,cur_row(0)
198{
199
200}

Member Function Documentation

◆ clear_section()

void sysio::chain::istream_snapshot_reader::clear_section ( )
overridevirtual

Implements sysio::chain::snapshot_reader.

Definition at line 339 of file snapshot.cpp.

339 {
340 num_rows = 0;
341 cur_row = 0;
342}
Here is the caller graph for this function:

◆ empty()

bool sysio::chain::istream_snapshot_reader::empty ( )
overridevirtual

Implements sysio::chain::snapshot_reader.

Definition at line 335 of file snapshot.cpp.

335 {
336 return num_rows == 0;
337}

◆ has_section()

bool sysio::chain::istream_snapshot_reader::has_section ( const string & section_name)
overridevirtual

Implements sysio::chain::snapshot_reader.

Definition at line 249 of file snapshot.cpp.

249 {
250 auto restore_pos = fc::make_scoped_exit([this,pos=snapshot.tellg()](){
251 snapshot.seekg(pos);
252 });
253
254 const std::streamoff header_size = sizeof(ostream_snapshot_writer::magic_number) + sizeof(current_snapshot_version);
255
256 auto next_section_pos = header_pos + header_size;
257
258 while (true) {
259 snapshot.seekg(next_section_pos);
260 uint64_t section_size = 0;
261 snapshot.read((char*)&section_size,sizeof(section_size));
262 if (section_size == std::numeric_limits<uint64_t>::max()) {
263 break;
264 }
265
266 next_section_pos = snapshot.tellg() + std::streamoff(section_size);
267
268 uint64_t ignore = 0;
269 snapshot.read((char*)&ignore,sizeof(ignore));
270
271 bool match = true;
272 for(auto c : section_name) {
273 if(snapshot.get() != c) {
274 match = false;
275 break;
276 }
277 }
278
279 if (match && snapshot.get() == 0) {
280 return true;
281 }
282 }
283
284 return false;
285}
static const uint32_t magic_number
Definition snapshot.hpp:338
constexpr size_t header_size
scoped_exit< Callback > make_scoped_exit(Callback &&c)
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the call graph for this function:

◆ read_row()

bool sysio::chain::istream_snapshot_reader::read_row ( detail::abstract_snapshot_row_reader & row_reader)
overridevirtual

Implements sysio::chain::snapshot_reader.

Definition at line 330 of file snapshot.cpp.

330 {
331 row_reader.provide(snapshot);
332 return ++cur_row < num_rows;
333}
Here is the call graph for this function:

◆ return_to_header()

void sysio::chain::istream_snapshot_reader::return_to_header ( )
overridevirtual

Implements sysio::chain::snapshot_reader.

Definition at line 344 of file snapshot.cpp.

344 {
345 snapshot.seekg( header_pos );
347}
Here is the call graph for this function:

◆ set_section()

void sysio::chain::istream_snapshot_reader::set_section ( const string & section_name)
overridevirtual

Implements sysio::chain::snapshot_reader.

Definition at line 287 of file snapshot.cpp.

287 {
288 auto restore_pos = fc::make_scoped_exit([this,pos=snapshot.tellg()](){
289 snapshot.seekg(pos);
290 });
291
292 const std::streamoff header_size = sizeof(ostream_snapshot_writer::magic_number) + sizeof(current_snapshot_version);
293
294 auto next_section_pos = header_pos + header_size;
295
296 while (true) {
297 snapshot.seekg(next_section_pos);
298 uint64_t section_size = 0;
299 snapshot.read((char*)&section_size,sizeof(section_size));
300 if (section_size == std::numeric_limits<uint64_t>::max()) {
301 break;
302 }
303
304 next_section_pos = snapshot.tellg() + std::streamoff(section_size);
305
306 uint64_t row_count = 0;
307 snapshot.read((char*)&row_count,sizeof(row_count));
308
309 bool match = true;
310 for(auto c : section_name) {
311 if(snapshot.get() != c) {
312 match = false;
313 break;
314 }
315 }
316
317 if (match && snapshot.get() == 0) {
318 cur_row = 0;
319 num_rows = row_count;
320
321 // leave the stream at the right point
322 restore_pos.cancel();
323 return;
324 }
325 }
326
327 SYS_THROW(snapshot_exception, "Binary snapshot has no section named ${n}", ("n", section_name));
328}
#define SYS_THROW(exc_type, FORMAT,...)
Here is the call graph for this function:

◆ validate()

void sysio::chain::istream_snapshot_reader::validate ( ) const
overridevirtual

Implements sysio::chain::snapshot_reader.

Definition at line 202 of file snapshot.cpp.

202 {
203 // make sure to restore the read pos
204 auto restore_pos = fc::make_scoped_exit([this,pos=snapshot.tellg(),ex=snapshot.exceptions()](){
205 snapshot.seekg(pos);
206 snapshot.exceptions(ex);
207 });
208
209 snapshot.exceptions(std::istream::failbit|std::istream::eofbit);
210
211 try {
212 // validate totem
213 auto expected_totem = ostream_snapshot_writer::magic_number;
214 decltype(expected_totem) actual_totem;
215 snapshot.read((char*)&actual_totem, sizeof(actual_totem));
216 SYS_ASSERT(actual_totem == expected_totem, snapshot_exception,
217 "Binary snapshot has unexpected magic number!");
218
219 // validate version
220 auto expected_version = current_snapshot_version;
221 decltype(expected_version) actual_version;
222 snapshot.read((char*)&actual_version, sizeof(actual_version));
223 SYS_ASSERT(actual_version == expected_version, snapshot_exception,
224 "Binary snapshot is an unsuppored version. Expected : ${expected}, Got: ${actual}",
225 ("expected", expected_version)("actual", actual_version));
226
227 while (validate_section()) {}
228 } catch( const std::exception& e ) { \
229 snapshot_exception fce(FC_LOG_MESSAGE( warn, "Binary snapshot validation threw IO exception (${what})",("what",e.what())));
230 throw fce;
231 }
232}
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
#define FC_LOG_MESSAGE(LOG_LEVEL, FORMAT,...)
A helper method for generating log messages.
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: