Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
tracked_storage.hpp
Go to the documentation of this file.
1#pragma once
3#include <fc/io/cfile.hpp>
5#include <fc/io/fstream.hpp>
6#include <fc/io/raw.hpp>
7#include <fstream>
8
9namespace fc {
10
14 namespace tracked {
15 template<typename T>
16 size_t memory_size( const T& obj ) {
17 return obj.memory_size();
18 }
19 }
20
34 template <typename ContainerType>
36 private:
37 size_t _memory_size = 0;
38 ContainerType _index;
39 public:
40 typedef typename ContainerType::template nth_index<0>::type primary_index_type;
41
42 tracked_storage() = default;
43
44 // read in the contents of a persisted tracked_storage and prevent reading in
45 // more stored objects once max_memory is reached.
46 // returns true if entire persisted tracked_storage was read
47 bool read(fc::cfile_datastream& ds, size_t max_memory) {
48 auto container_size = _index.size();
49 fc::raw::unpack(ds, container_size);
50 for (size_t i = 0; i < container_size; ++i) {
51 if (memory_size() >= max_memory) {
52 return false;
53 }
54 typename ContainerType::value_type v;
55 fc::raw::unpack(ds, v);
56 insert(std::move(v));
57 }
58
59 return true;
60 }
61
62 void write(fc::cfile& dat_content) const {
63 const auto container_size = _index.size();
64 dat_content.write( reinterpret_cast<const char*>(&container_size), sizeof(container_size) );
65
66 for (const auto& item : _index) {
67 auto data = fc::raw::pack(item);
68 dat_content.write(data.data(), data.size());
69 }
70 }
71
72 std::pair<typename primary_index_type::iterator, bool> insert(typename ContainerType::value_type obj) {
73 const auto size = tracked::memory_size(obj);
74 auto result = _index.insert(std::move(obj));
75 if (result.second) {
76 _memory_size += size;
77 }
78 return result;
79 }
80
81 template<typename Key>
82 typename primary_index_type::iterator find(const Key& key) {
83 primary_index_type& primary_idx = _index.template get<0>();
84 return primary_idx.find(key);
85 }
86
87 template<typename Key>
88 typename primary_index_type::const_iterator find(const Key& key) const {
89 const primary_index_type& primary_idx = _index.template get<0>();
90 return primary_idx.find(key);
91 }
92
93 template<typename Lam>
94 void modify(typename primary_index_type::iterator itr, Lam lam) {
95 _memory_size -= tracked::memory_size(*itr);
96 if (_index.modify( itr, std::move(lam))) {
97 _memory_size += tracked::memory_size(*itr);
98 }
99 }
100
101 template<typename Key>
102 void erase(const Key& key) {
103 auto itr = _index.find(key);
104 if (itr == _index.end())
105 return;
106
107 _memory_size -= tracked::memory_size(*itr);
108 _index.erase(itr);
109 }
110
111 void erase(typename primary_index_type::iterator itr) {
112 _memory_size -= tracked::memory_size(*itr);
113 _index.erase(itr);
114 }
115
116 size_t memory_size() const {
117 return _memory_size;
118 }
119
120 const ContainerType& index() const {
121 return _index;
122 }
123 };
124}
void write(const char *d, size_t n)
Definition cfile.hpp:127
tracks the size of storage allocated to its underlying multi_index
tracked_storage()=default
ContainerType::template nth_index< 0 >::type primary_index_type
void erase(const Key &key)
size_t memory_size() const
std::pair< typename primary_index_type::iterator, bool > insert(typename ContainerType::value_type obj)
void modify(typename primary_index_type::iterator itr, Lam lam)
const ContainerType & index() const
bool read(fc::cfile_datastream &ds, size_t max_memory)
void erase(typename primary_index_type::iterator itr)
primary_index_type::iterator find(const Key &key)
void write(fc::cfile &dat_content) const
primary_index_type::const_iterator find(const Key &key) const
Defines exception's used by fc.
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
size_t memory_size(const T &obj)
namespace sysio::chain
Definition authority.cpp:3
#define T(meth, val, expected)