SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
SpecsKey.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_SpecsKey_h
8 #define swcdb_db_cells_SpecsKey_h
9 
10 
13 
14 
15 namespace SWC { namespace DB { namespace Specs {
16 
17 
18 struct Fraction final : public std::string {
19 
21  mutable void* compiled;
22 
24  Fraction() noexcept : comp(Condition::NONE), compiled(nullptr) { }
25 
27  Fraction(std::string&& fraction, Condition::Comp a_comp) noexcept
28  : std::string(std::move(fraction)), comp(a_comp),
29  compiled(nullptr) {
30  }
31 
33  Fraction(const char* buf, uint32_t len, Condition::Comp a_comp)
34  : std::string(buf, len), comp(a_comp), compiled(nullptr) {
35  }
36 
38  Fraction(const Fraction& other)
39  : std::string(other), comp(other.comp), compiled(nullptr) {
40  }
41 
43  Fraction(Fraction&& other) noexcept
44  : std::string(std::move(other)), comp(other.comp),
45  compiled(other.compiled) {
46  other.compiled = nullptr;
47  }
48 
50  ~Fraction() noexcept {
51  release();
52  }
53 
55  Fraction& operator=(Fraction&& other) noexcept {
56  release();
57  std::string::operator=(std::move(other));
58  comp = other.comp;
59  compiled = other.compiled;
60  other.compiled = nullptr;
61  return *this;
62  }
63 
65  Fraction& operator=(const Fraction& other) {
66  release();
67  std::string::operator=(other);
68  comp = other.comp;
69  return *this;
70  }
71 
73  Fraction& operator=(std::string&& other) noexcept {
74  release();
75  std::string::operator=(std::move(other));
76  return *this;
77  }
78 
80  void release() noexcept {
81  if(compiled) {
82  switch(comp) {
83  case Condition::RE:
84  delete static_cast<re2::RE2*>(compiled);
85  break;
86  default: break;
87  }
88  compiled = nullptr;
89  }
90  }
91 
93  bool operator==(const Fraction &other) const {
94  return other.comp == comp && length() == other.length() &&
96  reinterpret_cast<const uint8_t*>(data()),
97  reinterpret_cast<const uint8_t*>(other.data()),
98  length());
99  }
100 
102  uint32_t encoded_length() const noexcept {
103  return 1 + Serialization::encoded_length_vi32(size()) + size();
104  }
105 
107  void encode(uint8_t** bufp) const {
110  if(!empty()) {
111  memcpy(*bufp, data(), size());
112  *bufp += size();
113  }
114  }
115 
117  void decode(const uint8_t** bufp, size_t* remainp) {
118  clear();
120  if(uint32_t len = Serialization::decode_vi32(bufp, remainp)) {
121  append(reinterpret_cast<const char*>(*bufp), len);
122  *bufp += len;
123  *remainp -= len;
124  }
125  }
126 
127  void print(std::ostream& out, bool pretty=true) const;
128 
129  template<Types::KeySeq T_seq>
130  bool is_matching(const uint8_t* ptr, uint32_t len) const;
131 
132 };
133 
134 
135 
136 class Key final : public Core::Vector<Fraction> {
137  public:
138 
140  typedef std::shared_ptr<Key> Ptr;
141 
143  explicit Key() noexcept { }
144 
146  explicit Key(const uint8_t** bufp, size_t* remainp) {
147  decode(bufp, remainp);
148  }
149 
151  explicit Key(Key&& other) noexcept
152  : Vec(std::move(other)) {
153  }
154 
156  explicit Key(const DB::Cell::Key &cell_key, Condition::Comp comp) {
157  set(cell_key, comp);
158  }
159 
160  explicit Key(const Key& other);
161 
162  ~Key() noexcept;
163 
164  size_t size_of_internal() const noexcept;
165 
166  void copy(const Key &other);
167 
169  Key& operator=(Key&& other) noexcept {
170  move(other);
171  return *this;
172  }
173 
175  void move(Key& other) noexcept {
176  Vec::operator=(std::move(other));
177  }
178 
179  bool SWC_PURE_FUNC equal(const Key &other) const noexcept;
180 
181 
182  void set(const DB::Cell::Key &cell_key, Condition::Comp comp);
183 
185  void set(int32_t idx, Condition::Comp comp) {
186  if(empty())
187  return;
188  (begin()+idx)->comp = comp;
189  }
190 
191 
192  Fraction& add(Fraction&& other);
193 
194  Fraction& add(std::string&& fraction, Condition::Comp comp);
195 
196  Fraction& add(const char* buf, uint32_t len, Condition::Comp comp);
197 
199  Fraction& add(const std::string& fraction, Condition::Comp comp) {
200  return add(fraction.c_str(), fraction.length(), comp);
201  }
202 
204  Fraction& add(const std::string_view& fraction, Condition::Comp comp) {
205  return add(fraction.data(), fraction.length(), comp);
206  }
207 
209  Fraction& add(const char* fraction, Condition::Comp comp) {
210  return add(fraction, strlen(fraction), comp);
211  }
212 
214  Fraction& add(const uint8_t* fraction, uint32_t len, Condition::Comp comp) {
215  return add(reinterpret_cast<const char*>(fraction), len, comp);
216  }
217 
218 
219  Fraction& insert(uint32_t idx, Fraction&& other);
220 
221  Fraction& insert(uint32_t idx, std::string&& fraction,
222  Condition::Comp comp);
223 
224  Fraction& insert(uint32_t idx, const char* buf, uint32_t len,
225  Condition::Comp comp);
226 
228  Fraction& insert(uint32_t idx, const std::string& fraction,
229  Condition::Comp comp) {
230  return insert(idx, fraction.c_str(), fraction.length(), comp);
231  }
232 
234  Fraction& insert(uint32_t idx, const std::string_view& fraction,
235  Condition::Comp comp) {
236  return insert(idx, fraction.data(), fraction.length(), comp);
237  }
238 
240  Fraction& insert(uint32_t idx, const uint8_t* fraction, uint32_t len,
241  Condition::Comp comp) {
242  return insert(idx, reinterpret_cast<const char*>(fraction), len, comp);
243  }
244 
246  Fraction& insert(uint32_t idx, const char* fraction, Condition::Comp comp) {
247  return insert(idx, fraction, strlen(fraction), comp);
248  }
249 
251  std::string_view get(const uint32_t idx, Condition::Comp& comp) const {
252  auto& f = (*this)[idx];
253  comp = f.comp;
254  return f;
255  }
256 
258  std::string_view get(const uint32_t idx) const {
259  return (*this)[idx];
260  }
261 
262  void get(DB::Cell::Key& key) const;
263 
264 
265  void remove(uint32_t idx, bool recursive=false);
266 
267 
269  uint32_t encoded_length() const noexcept {
270  uint32_t len = Serialization::encoded_length_vi32(size());
271  for(auto it = cbegin(); it != cend(); ++it)
272  len += it->encoded_length();
273  return len;
274  }
275 
277  void encode(uint8_t** bufp) const {
279  for(auto it = cbegin(); it != cend(); ++it)
280  it->encode(bufp);
281  }
282 
284  void decode(const uint8_t** bufp, size_t* remainp) {
285  clear();
286  resize(Serialization::decode_vi32(bufp, remainp));
287  for(auto it = begin(); it != cend(); ++it)
288  it->decode(bufp, remainp);
289  }
290 
291 
293  bool is_matching(const Types::KeySeq seq, const Cell::Key &key) const {
294  switch(seq) {
295  case Types::KeySeq::LEXIC:
296  case Types::KeySeq::FC_LEXIC:
297  return is_matching_lexic(key);
298  case Types::KeySeq::VOLUME:
300  return is_matching_volume(key);
301  default:
302  return false;
303  }
304  }
305 
306  bool is_matching_lexic(const Cell::Key &key) const;
307 
308  bool is_matching_volume(const Cell::Key &key) const;
309 
310  template<Types::KeySeq T_seq>
311  bool is_matching(const Cell::Key &key) const;
312 
314  std::string to_string() const {
315  std::string s;
316  {
317  std::stringstream ss;
318  print(ss);
319  s = ss.str();
320  }
321  return s;
322  }
323 
324  void print(std::ostream& out) const;
325 
326  void display(std::ostream& out, bool pretty=true) const;
327 
328 };
329 
330 
331 
332 template<Types::KeySeq T_seq>
334 bool
335 Fraction::is_matching(const uint8_t* ptr, uint32_t len) const {
336  switch(comp) {
337  case Condition::FIP:
338  case Condition::FI:
339  return true;
340  case Condition::RE: {
341  if(empty())
342  return !ptr|| !len;
343  if(!compiled)
344  compiled = new re2::RE2(re2::StringPiece(data(), size()));
345  return Condition::re(
346  *static_cast<re2::RE2*>(compiled),
347  reinterpret_cast<const char*>(ptr), len
348  );
349  }
350  default:
351  return KeySeq::is_matching<T_seq>(
352  comp, reinterpret_cast<const uint8_t*>(c_str()), size(), ptr, len);
353  }
354 }
355 
356 
357 
359 size_t Key::size_of_internal() const noexcept {
360  size_t sz = 0;
361  for(auto& f : *this) {
362  sz += sizeof(f);
363  sz += f.size();
364  }
365  return sz;
366 }
367 
369 bool Key::is_matching_lexic(const Cell::Key &key) const {
370  return is_matching<Types::KeySeq::LEXIC>(key);
371 }
372 
374 bool Key::is_matching_volume(const Cell::Key &key) const {
375  return is_matching<Types::KeySeq::VOLUME>(key);
376 }
377 
378 template<Types::KeySeq T_seq>
380 bool
381 Key::is_matching(const Cell::Key &key) const {
382  if(empty())
383  return true;
384  const uint8_t* ptr = key.data;
385  uint32_t len;
386  auto it = cbegin();
387  for(uint24_t c = key.count; c && it != cend(); ++it, --c, ptr += len) {
388  len = Serialization::decode_vi24(&ptr);
389  if(!it->is_matching<T_seq>(ptr, len))
390  return false;
391  }
392  if(size() == key.count)
393  return true;
394  switch((cend() - 1)->comp) {
395  case Condition::FIP:
396  return size() <= key.count + 1;
397  case Condition::FI:
398  return size() <= key.count;
399  case Condition::NONE:
400  return true;
401  default:
402  return false;
403  }
404 }
405 
406 
407 
408 }}}
409 
410 #ifdef SWC_IMPL_SOURCE
412 #endif
413 
414 #endif // swcdb_db_cells_SpecsKey_h
SWC::DB::Specs::Key::is_matching_lexic
bool is_matching_lexic(const Cell::Key &key) const
Definition: SpecsKey.h:369
SWC::DB::Specs::Key::add
SWC_CAN_INLINE Fraction & add(const std::string &fraction, Condition::Comp comp)
Definition: SpecsKey.h:199
SWC::DB::Specs::Key::insert
SWC_CAN_INLINE Fraction & insert(uint32_t idx, const std::string &fraction, Condition::Comp comp)
Definition: SpecsKey.h:228
SWC::Core::Vector< Fraction >::resize
SWC_CAN_INLINE void resize(size_type sz, ArgsT &&... args)
Definition: Vector.h:308
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::Core::Vector< Fraction >::clear
SWC_CAN_INLINE void clear() noexcept(_NoExceptDestructor)
Definition: Vector.h:120
SWC::DB::Specs::Fraction::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsKey.h:107
SWC::DB::Specs::Key::encoded_length
SWC_CAN_INLINE uint32_t encoded_length() const noexcept
Definition: SpecsKey.h:269
data
T data
Definition: BitFieldInt.h:1
SWC::DB::Specs::Fraction::encoded_length
SWC_CAN_INLINE uint32_t encoded_length() const noexcept
Definition: SpecsKey.h:102
SWC::Core::Vector< Fraction >::operator=
SWC_CAN_INLINE Vector & operator=(Vector &&other) noexcept
Definition: Vector.h:141
SWC::DB::Specs::Fraction::is_matching
bool is_matching(const uint8_t *ptr, uint32_t len) const
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::Fraction::Fraction
SWC_CAN_INLINE Fraction(Fraction &&other) noexcept
Definition: SpecsKey.h:43
SWC::DB::Specs::Key::~Key
~Key() noexcept
Definition: SpecsKey.cc:38
SWC::DB::Specs::Fraction::release
SWC_CAN_INLINE void release() noexcept
Definition: SpecsKey.h:80
SWC::DB::Specs::Key::insert
SWC_CAN_INLINE Fraction & insert(uint32_t idx, const uint8_t *fraction, uint32_t len, Condition::Comp comp)
Definition: SpecsKey.h:240
SWC::DB::Specs::Key::Ptr
std::shared_ptr< Key > Ptr
Definition: SpecsKey.h:140
SWC::DB::Specs::Fraction::operator=
SWC_CAN_INLINE Fraction & operator=(const Fraction &other)
Definition: SpecsKey.h:65
SWC::DB::Specs::Key::insert
SWC_CAN_INLINE Fraction & insert(uint32_t idx, const std::string_view &fraction, Condition::Comp comp)
Definition: SpecsKey.h:234
SWC::DB::Specs::Key::copy
void copy(const Key &other)
Definition: SpecsKey.cc:40
SWC::DB::Cell::Key
Definition: CellKey.h:24
SWC::DB::Specs::Key
Definition: SpecsKey.h:136
SWC::DB::Specs::Fraction::Fraction
SWC_CAN_INLINE Fraction(std::string &&fraction, Condition::Comp a_comp) noexcept
Definition: SpecsKey.h:27
SWC::Serialization::encode_i8
constexpr SWC_CAN_INLINE void encode_i8(uint8_t **bufp, uint8_t val) noexcept
Definition: Serialization.h:85
SWC::Condition::RE
@ RE
Definition: Comparators.h:36
SWC::DB::Specs::Key::remove
void remove(uint32_t idx, bool recursive=false)
Definition: SpecsKey.cc:98
SWC::DB::Specs::Fraction::~Fraction
SWC_CAN_INLINE ~Fraction() noexcept
Definition: SpecsKey.h:50
SWC::DB::Specs::Fraction
Definition: SpecsKey.h:18
SWC::DB::Specs::Key::set
SWC_CAN_INLINE void set(int32_t idx, Condition::Comp comp)
Definition: SpecsKey.h:185
SWC::DB::Specs::Fraction::print
void print(std::ostream &out, bool pretty=true) const
Definition: SpecsKey.cc:15
SWC::DB::Cell::Key::data
uint8_t * data
Definition: CellKey.h:257
SWC::DB::Types::KeySeq
KeySeq
Definition: KeySeq.h:13
SWC::DB::Types::KeySeq::UNKNOWN
@ UNKNOWN
SWC::DB::Specs::Key::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp)
Definition: SpecsKey.h:284
SWC::DB::Specs::Fraction::operator=
SWC_CAN_INLINE Fraction & operator=(Fraction &&other) noexcept
Definition: SpecsKey.h:55
SWC::Condition::Comp
Comp
Definition: Comparators.h:27
SWC::Core::Vector< Fraction >::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: Vector.h:168
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Condition::FIP
@ FIP
Definition: Comparators.h:75
SWC::DB::Specs::Key::is_matching_volume
bool is_matching_volume(const Cell::Key &key) const
Definition: SpecsKey.h:374
SWC::DB::Specs::Key::print
void print(std::ostream &out) const
Definition: SpecsKey.cc:105
SWC::DB::Specs::Key::Key
SWC_CAN_INLINE Key(Key &&other) noexcept
Definition: SpecsKey.h:151
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Serialization::decode_vi24
constexpr SWC_CAN_INLINE uint24_t decode_vi24(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:217
SWC::DB::Specs::Key::equal
bool SWC_PURE_FUNC equal(const Key &other) const noexcept
Definition: SpecsKey.cc:44
SWC::DB::Specs::Key::Key
SWC_CAN_INLINE Key(const DB::Cell::Key &cell_key, Condition::Comp comp)
Definition: SpecsKey.h:156
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
SWC_PURE_FUNC
#define SWC_PURE_FUNC
Definition: Compat.h:108
SWC::DB::Specs::Fraction::compiled
void * compiled
Definition: SpecsKey.h:21
Serialization.h
SWC::Comm::Protocol::FsBroker::Handler::length
void length(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Length.h:17
SWC::DB::Specs::Key::set
void set(const DB::Cell::Key &cell_key, Condition::Comp comp)
Definition: SpecsKey.cc:48
SWC::Condition::NONE
@ NONE
Definition: Comparators.h:28
SWC::Condition::re
bool re(const re2::RE2 &regex, const re2::StringPiece &value)
Definition: Comparators.cc:225
SWC::DB::Specs::Fraction::comp
Condition::Comp comp
Definition: SpecsKey.h:20
SWC::DB::Specs::Key::add
SWC_CAN_INLINE Fraction & add(const char *fraction, Condition::Comp comp)
Definition: SpecsKey.h:209
SWC::DB::Specs::Key::is_matching
bool is_matching(const Cell::Key &key) const
SWC::DB::Specs::Key::is_matching
SWC_CAN_INLINE bool is_matching(const Types::KeySeq seq, const Cell::Key &key) const
Definition: SpecsKey.h:293
SWC::Core::Vector
Definition: Vector.h:14
SWC::DB::Specs::Fraction::Fraction
SWC_CAN_INLINE Fraction(const char *buf, uint32_t len, Condition::Comp a_comp)
Definition: SpecsKey.h:33
SWC::DB::Specs::Fraction::operator==
SWC_CAN_INLINE bool operator==(const Fraction &other) const
Definition: SpecsKey.h:93
SWC::DB::Specs::Key::add
Fraction & add(Fraction &&other)
Definition: SpecsKey.cc:64
SWC::DB::Cell::Key::count
uint24_t count
Definition: CellKey.h:255
SWC::uint24_t
Core::uint24_t uint24_t
Definition: BitFieldInt.h:401
SWC::DB::Specs::Key::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsKey.h:277
SWC::Core::Vector< Fraction >::cend
constexpr SWC_CAN_INLINE const_iterator cend() const noexcept
Definition: Vector.h:232
SWC::DB::Specs::Key::to_string
SWC_CAN_INLINE std::string to_string() const
Definition: SpecsKey.h:314
SWC::Serialization::encode_vi32
constexpr SWC_CAN_INLINE void encode_vi32(uint8_t **bufp, uint32_t val)
Definition: Serialization.h:243
SWC::Condition::FI
@ FI
Definition: Comparators.h:76
SpecsKey.cc
SWC::DB::Specs::Fraction::Fraction
SWC_CAN_INLINE Fraction() noexcept
Definition: SpecsKey.h:24
SWC::DB::Specs::Key::add
SWC_CAN_INLINE Fraction & add(const std::string_view &fraction, Condition::Comp comp)
Definition: SpecsKey.h:204
SWC::DB::Specs::Key::insert
SWC_CAN_INLINE Fraction & insert(uint32_t idx, const char *fraction, Condition::Comp comp)
Definition: SpecsKey.h:246
SWC::DB::Specs::Fraction::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp)
Definition: SpecsKey.h:117
SWC::DB::Specs::Key::Key
SWC_CAN_INLINE Key(const uint8_t **bufp, size_t *remainp)
Definition: SpecsKey.h:146
SWC::DB::Specs::Key::display
void display(std::ostream &out, bool pretty=true) const
Definition: SpecsKey.cc:112
KeyComparator.h
SWC::Comm::Protocol::FsBroker::Handler::append
void append(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Append.h:17
SWC::DB::Specs::Key::Key
SWC_CAN_INLINE Key() noexcept
Definition: SpecsKey.h:143
SWC::DB::Specs::Fraction::operator=
SWC_CAN_INLINE Fraction & operator=(std::string &&other) noexcept
Definition: SpecsKey.h:73
SWC::DB::Specs::Key::size_of_internal
size_t size_of_internal() const noexcept
Definition: SpecsKey.h:359
SWC::Core::Vector< Fraction >::cbegin
constexpr SWC_CAN_INLINE const_iterator cbegin() const noexcept
Definition: Vector.h:216
SWC::DB::Specs::Key::add
SWC_CAN_INLINE Fraction & add(const uint8_t *fraction, uint32_t len, Condition::Comp comp)
Definition: SpecsKey.h:214
SWC::Serialization::decode_i8
constexpr SWC_CAN_INLINE uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:91
SWC::Core::Vector< Fraction >::size
constexpr SWC_CAN_INLINE size_type size() const noexcept
Definition: Vector.h:189
SWC::DB::Specs::Key::move
SWC_CAN_INLINE void move(Key &other) noexcept
Definition: SpecsKey.h:175
SWC::DB::Specs::Key::get
SWC_CAN_INLINE std::string_view get(const uint32_t idx, Condition::Comp &comp) const
Definition: SpecsKey.h:251
SWC::DB::Specs::Key::insert
Fraction & insert(uint32_t idx, Fraction &&other)
Definition: SpecsKey.cc:78
SWC::DB::Specs::Key::Vec
Core::Vector< Fraction > Vec
Definition: SpecsKey.h:139
SWC::DB::Specs::Key::get
SWC_CAN_INLINE std::string_view get(const uint32_t idx) const
Definition: SpecsKey.h:258
SWC::Serialization::decode_vi32
constexpr SWC_CAN_INLINE uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:254
SWC::Core::Vector< Fraction >::begin
constexpr SWC_CAN_INLINE iterator begin() noexcept
Definition: Vector.h:211
SWC::DB::Specs::Fraction::Fraction
SWC_CAN_INLINE Fraction(const Fraction &other)
Definition: SpecsKey.h:38