Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
snapshot.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <boost/core/demangle.hpp>
7#include <ostream>
8
9namespace sysio { namespace chain {
14 static const uint32_t current_snapshot_version = 1;
15
16 namespace detail {
17 template<typename T>
19 static std::string section_name() {
20 return boost::core::demangle(typeid(T).name());
21 }
22 };
23
24 template<typename T>
26 using value_type = std::decay_t<T>;
28
30 return value;
31 };
32 };
33
43 explicit ostream_wrapper(std::ostream& s)
44 :inner(s) {
45
46 }
47
49 ostream_wrapper(const ostream_wrapper& ) = default;
50
51 auto& write( const char* d, size_t s ) {
52 return inner.write(d, s);
53 }
54
55 auto& put(char c) {
56 return inner.put(c);
57 }
58
59 auto tellp() const {
60 return inner.tellp();
61 }
62
63 auto& seekp(std::ostream::pos_type p) {
64 return inner.seekp(p);
65 }
66
67 std::ostream& inner;
68 };
69
70
72 virtual void write(ostream_wrapper& out) const = 0;
73 virtual void write(fc::sha256::encoder& out) const = 0;
74 virtual fc::variant to_variant() const = 0;
75 virtual std::string row_type_name() const = 0;
76 };
77
78 template<typename T>
80 explicit snapshot_row_writer( const T& data )
81 :data(data) {}
82
83 template<typename DataStream>
84 void write_stream(DataStream& out) const {
85 fc::raw::pack(out, data);
86 }
87
88 void write(ostream_wrapper& out) const override {
89 write_stream(out);
90 }
91
92 void write(fc::sha256::encoder& out) const override {
93 write_stream(out);
94 }
95
96 fc::variant to_variant() const override {
97 fc::variant var;
98 fc::to_variant(data, var);
99 return var;
100 }
101
102 std::string row_type_name() const override {
103 return boost::core::demangle( typeid( T ).name() );
104 }
105
106 const T& data;
107 };
108
109 template<typename T>
111 return snapshot_row_writer<T>(data);
112 }
113 }
114
116 public:
118 public:
119 template<typename T>
123
124 private:
125 friend class snapshot_writer;
127 :_writer(writer)
128 {
129
130 }
131 snapshot_writer& _writer;
132 };
133
134 template<typename F>
135 void write_section(const std::string section_name, F f) {
136 write_start_section(section_name);
137 auto section = section_writer(*this);
138 f(section);
140 }
141
142 template<typename T, typename F>
146
147 virtual ~snapshot_writer(){};
148
149 protected:
150 virtual void write_start_section( const std::string& section_name ) = 0;
151 virtual void write_row( const detail::abstract_snapshot_row_writer& row_writer ) = 0;
152 virtual void write_end_section() = 0;
153 };
154
155 using snapshot_writer_ptr = std::shared_ptr<snapshot_writer>;
156
157 namespace detail {
159 virtual void provide(std::istream& in) const = 0;
160 virtual void provide(const fc::variant&) const = 0;
161 virtual std::string row_type_name() const = 0;
162 };
163
164 template<typename T>
166 static constexpr bool value = false;
167 };
168
169 template<uint16_t TypeNumber, typename Derived>
170 struct is_chainbase_object<chainbase::object<TypeNumber, Derived>> {
171 static constexpr bool value = true;
172 };
173
174 template<typename T>
176
178 template<typename T, typename F>
179 static auto apply(const T& data, F f) -> std::enable_if_t<is_chainbase_object_v<T>> {
180 auto orig = data.id;
181 f();
182 SYS_ASSERT(orig == data.id, snapshot_exception,
183 "Snapshot for ${type} mutates row member \"id\" which is illegal",
184 ("type",boost::core::demangle( typeid( T ).name() )));
185 }
186
187 template<typename T, typename F>
188 static auto apply(const T&, F f) -> std::enable_if_t<!is_chainbase_object_v<T>> {
189 f();
190 }
191 };
192
193 template<typename T>
195 explicit snapshot_row_reader( T& data )
196 :data(data) {}
197
198
199 void provide(std::istream& in) const override {
200 row_validation_helper::apply(data, [&in,this](){
201 fc::raw::unpack(in, data);
202 });
203 }
204
205 void provide(const fc::variant& var) const override {
206 row_validation_helper::apply(data, [&var,this]() {
207 fc::from_variant(var, data);
208 });
209 }
210
211 std::string row_type_name() const override {
212 return boost::core::demangle( typeid( T ).name() );
213 }
214
216 };
217
218 template<typename T>
222 }
223
225 public:
227 public:
228 template<typename T>
229 auto read_row( T& out ) -> std::enable_if_t<std::is_same<std::decay_t<T>, typename detail::snapshot_row_traits<T>::snapshot_type>::value,bool> {
230 auto reader = detail::make_row_reader(out);
231 return _reader.read_row(reader);
232 }
233
234 template<typename T>
235 auto read_row( T& out, chainbase::database& ) -> std::enable_if_t<std::is_same<std::decay_t<T>, typename detail::snapshot_row_traits<T>::snapshot_type>::value,bool> {
236 return read_row(out);
237 }
238
239 template<typename T>
240 auto read_row( T& out, chainbase::database& db ) -> std::enable_if_t<!std::is_same<std::decay_t<T>, typename detail::snapshot_row_traits<T>::snapshot_type>::value,bool> {
242 auto reader = detail::make_row_reader(temp);
243 bool result = _reader.read_row(reader);
245 return result;
246 }
247
248 bool empty() {
249 return _reader.empty();
250 }
251
252 private:
253 friend class snapshot_reader;
255 :_reader(_reader)
256 {}
257
258 snapshot_reader& _reader;
259
260 };
261
262 template<typename F>
263 void read_section(const std::string& section_name, F f) {
264 set_section(section_name);
265 auto section = section_reader(*this);
266 f(section);
268 }
269
270 template<typename T, typename F>
274
275 template<typename T>
276 bool has_section(const std::string& suffix = std::string()) {
278 }
279
280 virtual void validate() const = 0;
281
282 virtual void return_to_header() = 0;
283
284 virtual ~snapshot_reader(){};
285
286 protected:
287 virtual bool has_section( const std::string& section_name ) = 0;
288 virtual void set_section( const std::string& section_name ) = 0;
289 virtual bool read_row( detail::abstract_snapshot_row_reader& row_reader ) = 0;
290 virtual bool empty( ) = 0;
291 virtual void clear_section() = 0;
292 };
293
294 using snapshot_reader_ptr = std::shared_ptr<snapshot_reader>;
295
297 public:
299
300 void write_start_section( const std::string& section_name ) override;
301 void write_row( const detail::abstract_snapshot_row_writer& row_writer ) override;
302 void write_end_section( ) override;
303 void finalize();
304
305 private:
307 std::string current_section_name;
308 fc::variants current_rows;
309 };
310
312 public:
313 explicit variant_snapshot_reader(const fc::variant& snapshot);
314
315 void validate() const override;
316 bool has_section( const string& section_name ) override;
317 void set_section( const string& section_name ) override;
318 bool read_row( detail::abstract_snapshot_row_reader& row_reader ) override;
319 bool empty ( ) override;
320 void clear_section() override;
321 void return_to_header() override;
322
323 private:
324 const fc::variant& snapshot;
325 const fc::variant_object* cur_section;
326 uint64_t cur_row;
327 };
328
330 public:
331 explicit ostream_snapshot_writer(std::ostream& snapshot);
332
333 void write_start_section( const std::string& section_name ) override;
334 void write_row( const detail::abstract_snapshot_row_writer& row_writer ) override;
335 void write_end_section( ) override;
336 void finalize();
337
338 static const uint32_t magic_number = 0x30510550;
339
340 private:
342 std::streampos header_pos;
343 std::streampos section_pos;
344 uint64_t row_count;
345
346 };
347
349 public:
350 explicit istream_snapshot_reader(std::istream& snapshot);
351
352 void validate() const override;
353 bool has_section( const string& section_name ) override;
354 void set_section( const string& section_name ) override;
355 bool read_row( detail::abstract_snapshot_row_reader& row_reader ) override;
356 bool empty ( ) override;
357 void clear_section() override;
358 void return_to_header() override;
359
360 private:
361 bool validate_section() const;
362
363 std::istream& snapshot;
364 std::streampos header_pos;
365 uint64_t num_rows;
366 uint64_t cur_row;
367 };
368
370 public:
372
373 void write_start_section( const std::string& section_name ) override;
374 void write_row( const detail::abstract_snapshot_row_writer& row_writer ) override;
375 void write_end_section( ) override;
376 void finalize();
377
378 private:
380
381 };
382
383}}
const mie::Vuint & p
Definition bn.cpp:27
std::string name
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
An order-preserving dictionary of variants.
An order-preserving dictionary of variants.
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
void write_row(const detail::abstract_snapshot_row_writer &row_writer) override
Definition snapshot.cpp:359
integrity_hash_snapshot_writer(fc::sha256::encoder &enc)
Definition snapshot.cpp:349
void write_start_section(const std::string &section_name) override
Definition snapshot.cpp:354
void set_section(const string &section_name) override
Definition snapshot.cpp:287
istream_snapshot_reader(std::istream &snapshot)
Definition snapshot.cpp:193
bool read_row(detail::abstract_snapshot_row_reader &row_reader) override
Definition snapshot.cpp:330
bool has_section(const string &section_name) override
Definition snapshot.cpp:249
static const uint32_t magic_number
Definition snapshot.hpp:338
void write_row(const detail::abstract_snapshot_row_writer &row_writer) override
Definition snapshot.cpp:156
void write_start_section(const std::string &section_name) override
Definition snapshot.cpp:137
ostream_snapshot_writer(std::ostream &snapshot)
Definition snapshot.cpp:122
auto read_row(T &out, chainbase::database &) -> std::enable_if_t< std::is_same< std::decay_t< T >, typename detail::snapshot_row_traits< T >::snapshot_type >::value, bool >
Definition snapshot.hpp:235
auto read_row(T &out, chainbase::database &db) -> std::enable_if_t<!std::is_same< std::decay_t< T >, typename detail::snapshot_row_traits< T >::snapshot_type >::value, bool >
Definition snapshot.hpp:240
auto read_row(T &out) -> std::enable_if_t< std::is_same< std::decay_t< T >, typename detail::snapshot_row_traits< T >::snapshot_type >::value, bool >
Definition snapshot.hpp:229
bool has_section(const std::string &suffix=std::string())
Definition snapshot.hpp:276
virtual void clear_section()=0
virtual bool has_section(const std::string &section_name)=0
virtual void set_section(const std::string &section_name)=0
virtual void validate() const =0
void read_section(const std::string &section_name, F f)
Definition snapshot.hpp:263
virtual void return_to_header()=0
virtual bool read_row(detail::abstract_snapshot_row_reader &row_reader)=0
void add_row(const T &row, const chainbase::database &db)
Definition snapshot.hpp:120
virtual void write_start_section(const std::string &section_name)=0
virtual void write_end_section()=0
void write_section(const std::string section_name, F f)
Definition snapshot.hpp:135
virtual void write_row(const detail::abstract_snapshot_row_writer &row_writer)=0
bool has_section(const string &section_name) override
Definition snapshot.cpp:79
void set_section(const string &section_name) override
Definition snapshot.cpp:90
variant_snapshot_reader(const fc::variant &snapshot)
Definition snapshot.cpp:31
bool read_row(detail::abstract_snapshot_row_reader &row_reader) override
Definition snapshot.cpp:102
void write_start_section(const std::string &section_name) override
Definition snapshot.cpp:14
void write_row(const detail::abstract_snapshot_row_writer &row_writer) override
Definition snapshot.cpp:19
variant_snapshot_writer(fc::mutable_variant_object &snapshot)
Definition snapshot.cpp:7
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
std::vector< fc::variant > variants
Definition variant.hpp:173
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
snapshot_row_reader< T > make_row_reader(T &data)
Definition snapshot.hpp:219
snapshot_row_writer< T > make_row_writer(const T &data)
Definition snapshot.hpp:110
constexpr bool is_chainbase_object_v
Definition snapshot.hpp:175
std::shared_ptr< snapshot_writer > snapshot_writer_ptr
Definition snapshot.hpp:155
std::shared_ptr< snapshot_reader > snapshot_reader_ptr
Definition snapshot.hpp:294
#define value
Definition pkcs11.h:157
#define T(meth, val, expected)
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
virtual void provide(std::istream &in) const =0
virtual std::string row_type_name() const =0
virtual void provide(const fc::variant &) const =0
virtual void write(ostream_wrapper &out) const =0
virtual void write(fc::sha256::encoder &out) const =0
virtual std::string row_type_name() const =0
auto & write(const char *d, size_t s)
Definition snapshot.hpp:51
ostream_wrapper(ostream_wrapper &&)=default
ostream_wrapper(const ostream_wrapper &)=default
auto & seekp(std::ostream::pos_type p)
Definition snapshot.hpp:63
static auto apply(const T &, F f) -> std::enable_if_t<!is_chainbase_object_v< T > >
Definition snapshot.hpp:188
static auto apply(const T &data, F f) -> std::enable_if_t< is_chainbase_object_v< T > >
Definition snapshot.hpp:179
std::string row_type_name() const override
Definition snapshot.hpp:211
void provide(const fc::variant &var) const override
Definition snapshot.hpp:205
void provide(std::istream &in) const override
Definition snapshot.hpp:199
static const snapshot_type & to_snapshot_row(const value_type &value, const chainbase::database &)
Definition snapshot.hpp:29
void write(fc::sha256::encoder &out) const override
Definition snapshot.hpp:92
void write(ostream_wrapper &out) const override
Definition snapshot.hpp:88
std::string row_type_name() const override
Definition snapshot.hpp:102
void write_stream(DataStream &out) const
Definition snapshot.hpp:84
fc::variant to_variant() const override
Definition snapshot.hpp:96
CK_ULONG d
char * s
yh_object_descriptor object