Akumuli
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
nbtree.h
1 #pragma once
2 // C++ headers
3 #include <stack>
4 
5 // App headers
6 #include "blockstore.h"
7 #include "compression.h"
8 
9 namespace Akumuli {
10 namespace StorageEngine {
11 
72 class NBTreeLeaf {
74  LogicAddr prev_;
76  std::vector<uint8_t> buffer_;
78  DataBlockWriter writer_;
79 public:
80  enum class LeafLoadMethod {
81  FULL_PAGE_LOAD, ONLY_HEADER,
82  };
83 
89  NBTreeLeaf(aku_ParamId id, LogicAddr prev);
90 
96  NBTreeLeaf(std::shared_ptr<BlockStore> bstore, LogicAddr curr,
97  LeafLoadMethod load=LeafLoadMethod::FULL_PAGE_LOAD);
98 
100  size_t nelements();
101 
103  std::tuple<aku_Timestamp, aku_Timestamp> get_timestamps() const;
104 
106  LogicAddr get_prev_addr() const;
107 
113  aku_Status read_all(std::vector<aku_Timestamp>* timestamps, std::vector<double>* values);
114 
116  aku_Status append(aku_Timestamp ts, double value);
117 
121  std::tuple<aku_Status, LogicAddr> commit(std::shared_ptr<BlockStore> bstore);
122 };
123 
124 class NBTree;
125 
127  NBTree const& tree_;
128  aku_Timestamp start_;
129  aku_Timestamp stop_;
130  std::vector<LogicAddr> backpath_;
131  bool eof_;
132  aku_ParamId id_;
133 
134  enum {
135  // On average each 4KB page will contain less then 1024 elements.
136  SPACE_RESERVE=1024,
137  };
138  std::vector<aku_Timestamp> ts_;
139  std::vector<double> value_;
140 public:
141  NBTreeCursor(NBTree const& tree, aku_Timestamp start, aku_Timestamp stop);
142 
144  size_t size();
145 
148  bool is_eof();
149 
151  std::tuple<aku_Status, aku_Timestamp, double> at(size_t ix);
152 
153  void proceed();
154 };
155 
162 class NBTree {
163  // NOTE: supernodes not implemented at this point so, generaly speaking,
164  // database is a set of linked lists. Each one of those linked lists is
165  // represented by NBTree instance and a set of NBTreeLeaf objects.
166 
168  std::shared_ptr<BlockStore> bstore_;
169  aku_ParamId id_;
170  LogicAddr last_;
171  std::unique_ptr<NBTreeLeaf> leaf_;
172 
174  void reset_leaf();
175 public:
180  NBTree(aku_ParamId id, std::shared_ptr<BlockStore> bstore);
181 
183  aku_ParamId get_id() const;
184 
186  void append(aku_Timestamp ts, double value);
187 
189  std::vector<LogicAddr> roots() const;
190 
192  std::unique_ptr<NBTreeLeaf> load(LogicAddr addr) const;
193 
201  std::vector<LogicAddr> iter(aku_Timestamp start, aku_Timestamp stop) const;
202 };
203 
204 
205 
206 }} // namespaces
std::tuple< aku_Timestamp, aku_Timestamp > get_timestamps() const
Read timestamps.
Definition: nbtree.cpp:106
std::vector< LogicAddr > iter(aku_Timestamp start, aku_Timestamp stop) const
Definition: nbtree.cpp:214
std::unique_ptr< NBTreeLeaf > load(LogicAddr addr) const
Load Leaf node from block-store.
bool is_eof()
Definition: nbtree.cpp:264
Definition: nbtree.h:162
size_t size()
Returns number of elements in cursor.
Definition: nbtree.cpp:258
aku_Status append(aku_Timestamp ts, double value)
Append values to NBTree.
Definition: nbtree.cpp:140
Definition: compression.h:670
std::tuple< aku_Status, LogicAddr > commit(std::shared_ptr< BlockStore > bstore)
Definition: nbtree.cpp:156
std::vector< LogicAddr > roots() const
Return list of roots starting from leaf node.
Definition: nbtree.cpp:203
aku_ParamId get_id() const
Return series id.
Definition: nbtree.cpp:210
NBTreeLeaf(aku_ParamId id, LogicAddr prev)
Definition: nbtree.cpp:58
size_t nelements()
Returns number of elements.
Definition: nbtree.cpp:100
Definition: nbtree.h:72
LogicAddr get_prev_addr() const
Get logic address of the previous node.
Definition: nbtree.cpp:111
aku_Status read_all(std::vector< aku_Timestamp > *timestamps, std::vector< double > *values)
Definition: nbtree.cpp:117
void append(aku_Timestamp ts, double value)
Append data-point to NBTree.
Definition: nbtree.cpp:181
Definition: nbtree.h:126
std::tuple< aku_Status, aku_Timestamp, double > at(size_t ix)
Read element from cursor (not all elements can be loaded to cursor)
Definition: nbtree.cpp:269
NBTree(aku_ParamId id, std::shared_ptr< BlockStore > bstore)
Definition: nbtree.cpp:169