SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
SpecsValue.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_db_cells_SpecsValue_h
8 #define swcdb_db_cells_SpecsValue_h
9 
10 
11 #include "swcdb/core/Comparators.h"
12 #include "swcdb/db/Cells/Cell.h"
13 
14 
15 namespace SWC { namespace DB { namespace Specs {
16 
17 class Value {
18  public:
19 
20  constexpr SWC_CAN_INLINE
21  explicit Value(Condition::Comp a_comp=Condition::NONE) noexcept
22  : own(false), comp(a_comp),
23  size(0), data(nullptr), matcher(nullptr) {
24  }
25 
27  explicit Value(const char* data_n, Condition::Comp comp_n,
28  bool owner)
29  : own(false),
30  comp(), size(), data(),
31  matcher(nullptr) {
32  set(data_n, strlen(data_n), comp_n, owner);
33  }
34 
36  explicit Value(const char* data_n, const uint32_t size_n,
37  Condition::Comp comp_n, bool owner)
38  : own(false),
39  comp(), size(), data(),
40  matcher(nullptr) {
41  set(data_n, size_n, comp_n, owner);
42  }
43 
45  explicit Value(const uint8_t* data_n, const uint32_t size_n,
46  Condition::Comp comp_n, bool owner)
47  : own(false),
48  comp(), size(), data(),
49  matcher(nullptr) {
50  set(data_n, size_n, comp_n, owner);
51  }
52 
54  explicit Value(int64_t count, Condition::Comp comp_n)
55  : own(false),
56  comp(), size(), data(),
57  matcher(nullptr) {
58  set_counter(count, comp_n);
59  }
60 
62  explicit Value(const uint8_t** bufp, size_t* remainp, bool owner)
63  : own(owner),
64  comp(Condition::Comp(Serialization::decode_i8(bufp, remainp))),
65  size(
66  comp == Condition::NONE
67  ? 0
68  : Serialization::decode_vi32(bufp, remainp)
69  ),
70  data(
71  size
72  ? own
73  ? static_cast<uint8_t*>(memcpy(new uint8_t[size], *bufp, size))
74  : const_cast<uint8_t*>(*bufp)
75  : nullptr
76  ),
77  matcher(nullptr) {
78  *remainp -= size;
79  *bufp += size;
80  }
81 
83  explicit Value(const Value &other)
84  : own(true),
85  comp(other.comp),
86  size(other.size),
87  data(
88  size
89  ? static_cast<uint8_t*>(memcpy(new uint8_t[size], other.data, size))
90  : nullptr
91  ),
92  matcher(nullptr) {
93  }
94 
95  constexpr SWC_CAN_INLINE
96  explicit Value(Value&& other) noexcept
97  : own(other.own), comp(other.comp),
98  size(other.size), data(other.data),
99  matcher(other.matcher) {
100  other.comp = Condition::NONE;
101  other.size = 0;
102  other.data = nullptr;
103  other.matcher = nullptr;
104  }
105 
107  Value& operator=(const Value& other) {
108  copy(other);
109  return *this;
110  }
111 
113  Value& operator=(Value&& other) noexcept {
114  move(other);
115  return *this;
116  }
117 
119  void copy(const Value &other) {
120  set(other.data, other.size, other.comp, true);
121  }
122 
124  void set(const char* data_n, Condition::Comp comp_n, bool owner) {
125  set(data_n, strlen(data_n), comp_n, owner);
126  }
127 
129  void set(const std::string& data_n, Condition::Comp comp_n) {
130  set(data_n.c_str(), data_n.length(), comp_n, true);
131  }
132 
134  void set(const char* data_n, uint32_t size_n,
135  Condition::Comp comp_n, bool owner) {
136  set(reinterpret_cast<const uint8_t*>(data_n), size_n, comp_n, owner);
137  }
138 
139  void move(Value &other) noexcept;
140 
141  void set_counter(int64_t count, Condition::Comp comp_n);
142 
143  void set(const uint8_t* data_n, const uint32_t size_n,
144  Condition::Comp comp_n, bool owner);
145 
146  ~Value() noexcept {
147  _free();
148  }
149 
151  void _free() noexcept {
152  if(own)
153  delete [] data;
154  delete matcher;
155  }
156 
158  void free() noexcept {
159  _free();
160  data = nullptr;
161  size = 0;
162  matcher = nullptr;
163  }
164 
165  constexpr SWC_CAN_INLINE
166  bool empty() const noexcept {
167  return comp == Condition::NONE;
168  }
169 
170  constexpr
171  bool equal(const Value &other) const noexcept;
172 
173  constexpr SWC_CAN_INLINE
174  size_t encoded_length() const noexcept {
175  size_t sz = 1;
176  if(comp != Condition::NONE) {
178  sz += size;
179  }
180  return sz;
181  }
182 
184  void encode(uint8_t** bufp) const {
186  if(comp != Condition::NONE) {
188  memcpy(*bufp, data, size);
189  *bufp+=size;
190  }
191  }
192 
194  void decode(const uint8_t** bufp, size_t* remainp, bool owner) {
195  free();
196  own = owner;
198  if(comp != Condition::NONE) {
199  if((size = Serialization::decode_vi32(bufp, remainp))) {
200  data = own
201  ? static_cast<uint8_t*>(memcpy(new uint8_t[size], *bufp, size))
202  : const_cast<uint8_t*>(*bufp);
203  *remainp -= size;
204  *bufp += size;
205  }
206  }
207  }
208 
209 
211  bool is_matching(Types::Column col_type, const Cells::Cell& cell) const {
212  switch(col_type) {
213  case Types::Column::PLAIN:
214  return is_matching_plain(cell);
216  return is_matching_serial(cell);
217  case Types::Column::COUNTER_I64:
218  case Types::Column::COUNTER_I32:
219  case Types::Column::COUNTER_I16:
220  case Types::Column::COUNTER_I8:
221  return is_matching_counter(cell);
222  default:
223  return false;
224  }
225  }
226 
227  bool is_matching_plain(const Cells::Cell& cell) const;
228 
229  bool is_matching_serial(const Cells::Cell& cell) const;
230 
231  bool is_matching_counter(const Cells::Cell& cell) const;
232 
234  std::string to_string(Types::Column col_type) const {
235  std::string s;
236  {
237  std::stringstream ss;
238  print(col_type, ss);
239  s = ss.str();
240  }
241  return s;
242  }
243 
244  void print(Types::Column col_type, std::ostream& out) const;
245 
246  void display(Types::Column col_type, std::ostream& out,
247  bool pretty=true) const;
248 
249  bool own;
251  uint32_t size;
252  uint8_t* data;
253 
254  struct TypeMatcher {
255  virtual ~TypeMatcher() noexcept { }
256  };
257  private:
259 };
260 
261 
262 
264 void Value::move(Value &other) noexcept {
265  _free();
266  own = other.own;
267  comp = other.comp;
268  data = other.data;
269  size = other.size;
270  matcher = other.matcher;
271  other.comp = Condition::NONE;
272  other.data = nullptr;
273  other.matcher = nullptr;
274  other.size = 0;
275 }
276 
278 void Value::set_counter(int64_t count, Condition::Comp comp_n) {
279  uint32_t len = Serialization::encoded_length_vi64(count);
280  uint8_t data_n[10];
281  uint8_t* ptr = data_n;
282  Serialization::encode_vi64(&ptr, count);
283  set(data_n, len, comp_n, true);
284 }
285 
287 void Value::set(const uint8_t* data_n, const uint32_t size_n,
288  Condition::Comp comp_n, bool owner) {
289  free();
290  own = owner;
291  comp = comp_n;
292  if((size = size_n))
293  data = own
294  ? static_cast<uint8_t*>(memcpy(new uint8_t[size], data_n, size))
295  : const_cast<uint8_t*>(data_n);
296 }
297 
298 constexpr SWC_CAN_INLINE
299 bool Value::equal(const Value &other) const noexcept {
300  return
301  size == other.size &&
302  ((!data && !other.data) ||
303  (data && other.data && Condition::mem_eq(data, other.data, size)));
304 }
305 
306 
307 
308 }}}
309 
310 #ifdef SWC_IMPL_SOURCE
312 #endif
313 
314 #endif // swcdb_db_cells_SpecsValue_h
SWC::DB::Specs::Value::Value
SWC_CAN_INLINE Value(const Value &other)
Definition: SpecsValue.h:83
SWC::DB::Specs::Value::_free
SWC_CAN_INLINE void _free() noexcept
Definition: SpecsValue.h:151
SWC::DB::Specs::Value::copy
SWC_CAN_INLINE void copy(const Value &other)
Definition: SpecsValue.h:119
Cell.h
SWC::DB::Specs::Value::encoded_length
constexpr SWC_CAN_INLINE size_t encoded_length() const noexcept
Definition: SpecsValue.h:174
SWC::Condition::mem_eq
bool mem_eq(const uint8_t *b1, const uint8_t *b2, size_t count) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:207
SWC::DB::Specs::Value::set
SWC_CAN_INLINE void set(const char *data_n, Condition::Comp comp_n, bool owner)
Definition: SpecsValue.h:124
data
T data
Definition: BitFieldInt.h:1
SWC::Serialization::encoded_length_vi32
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi32(uint32_t val) noexcept
Definition: Serialization.h:234
SWC::DB::Specs::Value::size
uint32_t size
Definition: SpecsValue.h:251
SWC::DB::Specs::Value::to_string
SWC_CAN_INLINE std::string to_string(Types::Column col_type) const
Definition: SpecsValue.h:234
SWC::DB::Types::Column
Column
Definition: Column.h:18
SWC::DB::Specs::Value::print
void print(Types::Column col_type, std::ostream &out) const
Definition: SpecsValue.cc:89
SWC::DB::Specs::Value::Value
constexpr SWC_CAN_INLINE Value(Value &&other) noexcept
Definition: SpecsValue.h:96
SWC::DB::Cells::Cell
Definition: Cell.h:92
SWC::Serialization::encode_i8
constexpr SWC_CAN_INLINE void encode_i8(uint8_t **bufp, uint8_t val) noexcept
Definition: Serialization.h:85
SWC::DB::Specs::Value::free
SWC_CAN_INLINE void free() noexcept
Definition: SpecsValue.h:158
SWC::DB::Specs::Value::Value
SWC_CAN_INLINE Value(int64_t count, Condition::Comp comp_n)
Definition: SpecsValue.h:54
SWC::DB::Specs::Value::Value
SWC_CAN_INLINE Value(const char *data_n, const uint32_t size_n, Condition::Comp comp_n, bool owner)
Definition: SpecsValue.h:36
SWC::Condition::Comp
Comp
Definition: Comparators.h:27
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::DB::Specs::Value::is_matching
SWC_CAN_INLINE bool is_matching(Types::Column col_type, const Cells::Cell &cell) const
Definition: SpecsValue.h:211
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Specs::Value::data
uint8_t * data
Definition: SpecsValue.h:252
SpecsValue.cc
SWC::DB::Specs::Value::Value
SWC_CAN_INLINE Value(const uint8_t **bufp, size_t *remainp, bool owner)
Definition: SpecsValue.h:62
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
SWC::DB::Specs::Value::set
SWC_CAN_INLINE void set(const std::string &data_n, Condition::Comp comp_n)
Definition: SpecsValue.h:129
SWC::DB::Specs::Value::~Value
~Value() noexcept
Definition: SpecsValue.h:146
SWC::DB::Specs::Value::set_counter
void set_counter(int64_t count, Condition::Comp comp_n)
Definition: SpecsValue.h:278
SWC::DB::Specs::Value::operator=
SWC_CAN_INLINE Value & operator=(Value &&other) noexcept
Definition: SpecsValue.h:113
SWC::DB::Specs::Value::Value
SWC_CAN_INLINE Value(const char *data_n, Condition::Comp comp_n, bool owner)
Definition: SpecsValue.h:27
SWC::DB::Specs::Value::equal
constexpr bool equal(const Value &other) const noexcept
Definition: SpecsValue.h:299
Comparators.h
SWC::DB::Specs::Value::set
SWC_CAN_INLINE void set(const char *data_n, uint32_t size_n, Condition::Comp comp_n, bool owner)
Definition: SpecsValue.h:134
SWC::Serialization::encoded_length_vi64
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi64(uint64_t val) noexcept
Definition: Serialization.h:272
SWC::DB::Specs::Value::Value
SWC_CAN_INLINE Value(const uint8_t *data_n, const uint32_t size_n, Condition::Comp comp_n, bool owner)
Definition: SpecsValue.h:45
SWC::Condition::NONE
@ NONE
Definition: Comparators.h:28
SWC::DB::Specs::Value
Definition: SpecsValue.h:17
SWC::DB::Specs::Value::TypeMatcher
Definition: SpecsValue.h:254
SWC::DB::Specs::Value::is_matching_counter
bool is_matching_counter(const Cells::Cell &cell) const
Definition: SpecsValue.cc:79
SWC::Serialization::encode_vi32
constexpr SWC_CAN_INLINE void encode_vi32(uint8_t **bufp, uint32_t val)
Definition: Serialization.h:243
SWC::DB::Specs::Value::TypeMatcher::~TypeMatcher
virtual ~TypeMatcher() noexcept
Definition: SpecsValue.h:255
SWC::DB::Specs::Value::operator=
SWC_CAN_INLINE Value & operator=(const Value &other)
Definition: SpecsValue.h:107
SWC::DB::Specs::Value::is_matching_serial
bool is_matching_serial(const Cells::Cell &cell) const
Definition: SpecsValue.cc:58
SWC::Serialization::encode_vi64
constexpr SWC_CAN_INLINE void encode_vi64(uint8_t **bufp, uint64_t val)
Definition: Serialization.h:286
SWC::DB::Specs::Value::move
void move(Value &other) noexcept
Definition: SpecsValue.h:264
SWC::DB::Specs::Value::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: SpecsValue.h:166
SWC::DB::Specs::Value::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp, bool owner)
Definition: SpecsValue.h:194
SWC::DB::Types::Column::SERIAL
@ SERIAL
SWC::DB::Specs::Value::is_matching_plain
bool is_matching_plain(const Cells::Cell &cell) const
Definition: SpecsValue.cc:26
SWC::DB::Specs::Value::matcher
TypeMatcher * matcher
Definition: SpecsValue.h:258
SWC::DB::Specs::Value::Value
constexpr SWC_CAN_INLINE Value(Condition::Comp a_comp=Condition::NONE) noexcept
Definition: SpecsValue.h:21
SWC::DB::Specs::Value::own
bool own
Definition: SpecsValue.h:249
SWC::Serialization::decode_i8
constexpr SWC_CAN_INLINE uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:91
SWC::DB::Specs::Value::display
void display(Types::Column col_type, std::ostream &out, bool pretty=true) const
Definition: SpecsValue.cc:96
SWC::DB::Specs::Value::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsValue.h:184
SWC::Serialization::decode_vi32
constexpr SWC_CAN_INLINE uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:254
SWC::DB::Specs::Value::comp
Condition::Comp comp
Definition: SpecsValue.h:250