SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
SpecsInterval.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_SpecsInterval_h
8 #define swcdb_db_cells_SpecsInterval_h
9 
10 
12 #include "swcdb/db/Types/Column.h"
19 #include "swcdb/db/Cells/Cell.h"
21 
22 
23 namespace SWC { namespace DB { namespace Specs {
24 
25 class Interval {
26  public:
27 
28  static constexpr const uint8_t OPT_KEY_EQUAL = 0x01;
29  static constexpr const uint8_t OPT_RANGE_END_REST = 0x02;
30  static constexpr const uint8_t OPT_UPDATING = 0x04;
31  static constexpr const uint8_t OPT_DELETING = 0x08;
32 
33  typedef std::shared_ptr<Interval> Ptr;
34 
37  return Ptr(new Interval(col_type));
38  }
39 
41  static Ptr make_ptr(const uint8_t** bufp, size_t* remainp) {
42  return Ptr(new Interval(bufp, remainp));
43  }
44 
46  static Ptr make_ptr(const Interval& other) {
47  return Ptr(new Interval(other));
48  }
49 
51  static Ptr make_ptr(Interval&& other) {
52  return Ptr(new Interval(std::move(other)));
53  }
54 
55 
56  explicit Interval(Types::Column col_type = Types::Column::UNKNOWN) noexcept;
57 
58  explicit Interval(const Cell::Key& range_begin, const Cell::Key& range_end);
59 
60  explicit Interval(const uint8_t** bufp, size_t* remainp);
61 
62  explicit Interval(const Interval& other);
63 
64  explicit Interval(Interval&& other) noexcept;
65 
67  Interval& operator=(const Interval& other) {
68  copy(other);
69  return *this;
70  }
71 
73  Interval& operator=(Interval&& other) noexcept {
74  move(other);
75  return *this;
76  }
77 
78  void copy(const Interval& other);
79 
80  void move(Interval& other) noexcept;
81 
82  ~Interval() noexcept;
83 
84  void free() noexcept;
85 
86  size_t size_of_internal() const noexcept;
87 
88  bool SWC_PURE_FUNC equal(const Interval& other) const noexcept;
89 
91  bool is_matching(const Types::KeySeq key_seq,
92  const Cell::Key& key,
93  int64_t timestamp, bool desc) const {
94  if(offset_key.empty())
95  return true;
96 
97  switch(DB::KeySeq::compare(key_seq, offset_key, key)) {
98  case Condition::LT:
99  return false;
100  case Condition::EQ:
101  return is_matching(timestamp, desc);
102  default:
103  return true;
104  }
105  }
106 
107  constexpr SWC_CAN_INLINE
108  bool is_matching(int64_t timestamp, bool desc) const noexcept {
109  return desc ? offset_rev > timestamp : offset_rev < timestamp;
110  }
111 
113  bool is_matching(const Types::KeySeq key_seq,
114  const Cells::Cell& cell, bool& stop) const {
115  return
116  is_matching(
117  key_seq, cell.key, cell.get_timestamp(), cell.is_time_order_desc())
118  &&
120  &&
122  &&
123  is_matching_begin(key_seq, cell.key)
124  &&
125  !(stop = !is_matching_end(key_seq, cell.key))
126  &&
127  key_intervals.is_matching(key_seq, cell.key)
128  &&
129  values.is_matching(cell);
130  }
131 
132  bool is_matching_begin(const Types::KeySeq key_seq,
133  const DB::Cell::Key& key) const;
134 
135  bool is_matching_end(const Types::KeySeq key_seq,
136  const DB::Cell::Key& key) const;
137 
138  bool is_in_previous(const Types::KeySeq key_seq,
139  const DB::Cell::Key& prev) const;
140 
141  size_t SWC_PURE_FUNC encoded_length() const noexcept;
142 
143  void encode(uint8_t** bufp) const;
144 
145  void decode(const uint8_t** bufp, size_t* remainp, bool owner);
146 
147  constexpr SWC_CAN_INLINE
148  void set_opt__key_equal() noexcept {
150  }
151 
152  constexpr SWC_CAN_INLINE
153  void set_opt__range_end_rest() noexcept {
155  }
156 
157  constexpr SWC_CAN_INLINE
158  void set_opt__updating() noexcept {
160  }
161 
162  constexpr SWC_CAN_INLINE
163  void set_opt__deleting() noexcept {
165  }
166 
167  constexpr SWC_CAN_INLINE
168  bool has_opt__key_equal() const noexcept {
169  return options & OPT_KEY_EQUAL;
170  }
171 
172  constexpr SWC_CAN_INLINE
173  bool has_opt__range_end_rest() const noexcept {
174  return options & OPT_RANGE_END_REST;
175  }
176 
177  constexpr SWC_CAN_INLINE
178  bool has_opt__updating() const noexcept {
179  return options & OPT_UPDATING;
180  }
181 
182  constexpr SWC_CAN_INLINE
183  bool has_opt__deleting() const noexcept {
184  return options & OPT_DELETING;
185  }
186 
188  bool is_updating() const noexcept {
189  return updating.get() != nullptr;
190  }
191 
194  if(key_intervals.empty())
195  return;
196 
197  if(range_begin.empty()) {
198  apply_possible_range(range_begin, false, false, true);
199  }
200  if(range_end.empty()) {
201  apply_possible_range(range_end, true, true, true);
202  if(!range_end.empty() && !has_opt__key_equal())
204  }
205  }
206 
209  bool* end_restp = nullptr) const {
211  apply_possible_range_end(end, end_restp);
212  }
213 
214  void apply_possible_range_begin(DB::Cell::Key& begin) const;
215 
217  bool* restp = nullptr) const;
218 
219  void apply_possible_range(DB::Cell::Key& key, bool ending,
220  bool rest, bool no_stepping) const;
221 
223  std::string to_string() const {
224  std::string s;
225  {
226  std::stringstream ss;
227  print(ss);
228  s = ss.str();
229  }
230  return s;
231  }
232 
233  void print(std::ostream& out) const;
234 
235  void display(std::ostream& out, bool pretty=false,
236  const std::string& offset = "") const;
237 
243 
245  int64_t offset_rev;
246 
247  uint8_t options;
248 
250 
251 };
252 
253 
254 
256 size_t Interval::size_of_internal() const noexcept {
257  return range_begin.size + range_end.size
260  + offset_key.size
261  + (is_updating() ? updating->vlen: 0);
262 }
263 
266  const DB::Cell::Key& key) const {
267  if(!range_begin.empty()) switch(key_seq) {
268 
269  case Types::KeySeq::LEXIC:
270  return
272  range_begin, key, range_begin.count, true
273  ) != Condition::LT;
274 
275  case Types::KeySeq::VOLUME:
276  return
278  range_begin, key, range_begin.count, true
279  ) != Condition::LT;
280 
281  case Types::KeySeq::FC_LEXIC:
282  return
284  range_begin, key, key.count, true
285  ) != Condition::LT;
286 
288  return
290  range_begin, key, key.count, true
291  ) != Condition::LT;
292 
293  default:
294  break;
295  }
296  return true;
297 }
298 
301  const DB::Cell::Key& key) const {
302  if(!range_end.empty()) switch(key_seq) {
303 
304  case Types::KeySeq::LEXIC:
305  return
307  range_end, key,
309  ? range_end.count : key.count,
310  true
311  ) != Condition::GT;
312 
313  case Types::KeySeq::VOLUME:
314  return
316  range_end, key,
318  ? range_end.count : key.count,
319  true
320  ) != Condition::GT;
321 
322  case Types::KeySeq::FC_LEXIC:
323  return
325  ? key.count < range_end.count
328  range_end, key,
330  ? range_end.count : key.count,
331  true
332  ) != Condition::GT;
333 
335  return
337  ? key.count < range_end.count
340  range_end, key,
342  ? range_end.count : key.count,
343  true
344  ) != Condition::GT;
345  default:
346  break;
347  }
348  return true;
349 }
350 
353  const DB::Cell::Key& prev) const {
354  if(!range_end.empty()) switch(key_seq) {
355 
356  case Types::KeySeq::LEXIC:
357  return
359  range_end, prev,
361  ? range_end.count : prev.count,
362  true
363  ) != Condition::GT;
364 
365  case Types::KeySeq::VOLUME:
366  return
368  range_end, prev,
370  ? range_end.count : prev.count,
371  true
372  ) != Condition::GT;
373 
374  case Types::KeySeq::FC_LEXIC:
375  return
377  ? (prev.count >= range_end.count
378  ? true
380  range_end, prev, range_end.count, true
381  ) != Condition::GT)
383  range_end, prev,
385  ? range_end.count : prev.count,
386  true
387  ) != Condition::GT;
388 
390  return
392  ? (prev.count >= range_end.count
393  ? true
395  range_end, prev, range_end.count, true
396  ) != Condition::GT)
398  range_end, prev,
400  ? range_end.count : prev.count,
401  true
402  ) != Condition::GT;
403 
404  default:
405  break;
406  }
407  return true;
408 }
409 
410 
411 }}}
412 
413 
414 #ifdef SWC_IMPL_SOURCE
416 #endif
417 
418 #endif // swcdb_db_cells_SpecsInterval_h
SpecsKeyIntervals.h
SWC::DB::Specs::KeyIntervals::is_matching
bool is_matching(const Types::KeySeq key_seq, const DB::Cell::Key &cellkey) const
Definition: SpecsKeyIntervals.h:201
SWC::DB::Specs::Interval::is_matching_end
bool is_matching_end(const Types::KeySeq key_seq, const DB::Cell::Key &key) const
Definition: SpecsInterval.h:300
Cell.h
SpecsIntervalUpdate.h
SWC::DB::Specs::Interval::apply_possible_range
SWC_CAN_INLINE void apply_possible_range(DB::Cell::Key &begin, DB::Cell::Key &end, bool *end_restp=nullptr) const
Definition: SpecsInterval.h:208
SWC::DB::KeySeq::compare_opt_volume
Condition::Comp compare_opt_volume(const Cell::Key &opt_empty, const Cell::Key &other, uint24_t max, bool empty_eq) SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: KeyComparator.h:389
SWC::DB::Specs::Interval::make_ptr
static SWC_CAN_INLINE Ptr make_ptr(Interval &&other)
Definition: SpecsInterval.h:51
SWC::DB::Specs::Interval::key_intervals
KeyIntervals key_intervals
Definition: SpecsInterval.h:239
SWC::DB::Specs::Interval::OPT_KEY_EQUAL
static constexpr const uint8_t OPT_KEY_EQUAL
Definition: SpecsInterval.h:28
SWC::DB::Specs::KeyIntervals::size_of_internal
size_t size_of_internal() const noexcept
Definition: SpecsKeyIntervals.h:190
SpecsTimestamp.h
SWC::DB::Specs::Interval::values
Values values
Definition: SpecsInterval.h:240
SWC::DB::Specs::KeyIntervals
Definition: SpecsKeyIntervals.h:94
SWC::DB::Specs::Values::is_matching
bool is_matching(const Cells::Cell &cell) const
Definition: SpecsValues.h:133
SWC::DB::Specs::Interval::~Interval
~Interval() noexcept
Definition: SpecsInterval.cc:82
SWC::DB::Specs::Values::size_of_internal
size_t size_of_internal() const noexcept
Definition: SpecsValues.h:125
SWC::DB::Specs::Interval::OPT_RANGE_END_REST
static constexpr const uint8_t OPT_RANGE_END_REST
Definition: SpecsInterval.h:29
SWC::DB::Specs::Interval::has_opt__deleting
constexpr SWC_CAN_INLINE bool has_opt__deleting() const noexcept
Definition: SpecsInterval.h:183
SWC::Condition::GT
@ GT
Definition: Comparators.h:30
SWC::DB::Specs::Interval::OPT_UPDATING
static constexpr const uint8_t OPT_UPDATING
Definition: SpecsInterval.h:30
SWC::DB::Specs::Interval::encoded_length
size_t SWC_PURE_FUNC encoded_length() const noexcept
Definition: SpecsInterval.cc:154
SWC::DB::Specs::Interval::Ptr
std::shared_ptr< Interval > Ptr
Definition: SpecsInterval.h:33
SWC::DB::Types::Column
Column
Definition: Column.h:18
SWC::DB::Specs::Interval::range_begin
Cell::Key range_begin
Definition: SpecsInterval.h:238
SWC::Condition::LT
@ LT
Definition: Comparators.h:34
SWC::DB::Specs::Interval::options
uint8_t options
Definition: SpecsInterval.h:247
SWC::DB::Specs::Interval::operator=
SWC_CAN_INLINE Interval & operator=(const Interval &other)
Definition: SpecsInterval.h:67
SWC::DB::Specs::Interval::print
void print(std::ostream &out) const
Definition: SpecsInterval.cc:322
SWC::DB::Cells::Cell
Definition: Cell.h:92
SWC::DB::KeySeq::compare_opt_fc_lexic
Condition::Comp compare_opt_fc_lexic(const Cell::Key &opt_empty, const Cell::Key &other, uint24_t max, bool empty_eq) SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: KeyComparator.h:397
SWC::DB::Specs::Interval::ts_start
Timestamp ts_start
Definition: SpecsInterval.h:241
SWC::DB::Specs::Interval::encode
void encode(uint8_t **bufp) const
Definition: SpecsInterval.cc:166
SWC::DB::Specs::Interval::apply_possible_range_end
void apply_possible_range_end(DB::Cell::Key &end, bool *restp=nullptr) const
Definition: SpecsInterval.cc:231
SWC::DB::Cell::Key
Definition: CellKey.h:24
SWC::DB::Specs::Key
Definition: SpecsKey.h:136
SWC::DB::Cells::Cell::key
DB::Cell::Key key
Definition: Cell.h:357
SWC::DB::KeySeq::compare_opt_lexic
Condition::Comp compare_opt_lexic(const Cell::Key &opt_empty, const Cell::Key &other, uint24_t max, bool empty_eq) SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: KeyComparator.h:381
SWC::DB::Specs::Interval::is_in_previous
bool is_in_previous(const Types::KeySeq key_seq, const DB::Cell::Key &prev) const
Definition: SpecsInterval.h:352
SWC::DB::Specs::Interval::free
void free() noexcept
Definition: SpecsInterval.cc:129
SWC::DB::Types::KeySeq
KeySeq
Definition: KeySeq.h:13
SpecsInterval.cc
SWC::DB::Types::KeySeq::UNKNOWN
@ UNKNOWN
SWC::DB::Specs::Interval::to_string
SWC_CAN_INLINE std::string to_string() const
Definition: SpecsInterval.h:223
SWC::DB::Specs::Interval::move
void move(Interval &other) noexcept
Definition: SpecsInterval.cc:108
SWC::DB::Cell::Key::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: CellKey.h:186
SWC::DB::Specs::Interval::apply_possible_range_pure
SWC_CAN_INLINE void apply_possible_range_pure()
Definition: SpecsInterval.h:193
SWC::Core::Vector::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::DB::Cells::Cell::is_time_order_desc
constexpr SWC_CAN_INLINE bool is_time_order_desc() const noexcept
Definition: Cell.h:177
SWC::DB::Cell::Serial::Value::UNKNOWN
@ UNKNOWN
Definition: CellValueSerialField.h:34
SWC::DB::Specs::Interval::is_matching
SWC_CAN_INLINE bool is_matching(const Types::KeySeq key_seq, const Cells::Cell &cell, bool &stop) const
Definition: SpecsInterval.h:113
SWC::DB::Specs::Interval::equal
bool SWC_PURE_FUNC equal(const Interval &other) const noexcept
Definition: SpecsInterval.cc:138
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Specs::Interval::copy
void copy(const Interval &other)
Definition: SpecsInterval.cc:84
SWC::DB::Specs::Interval::display
void display(std::ostream &out, bool pretty=false, const std::string &offset="") const
Definition: SpecsInterval.cc:351
Column.h
SWC::DB::Cell::Key::size
uint32_t size
Definition: CellKey.h:256
SWC_PURE_FUNC
#define SWC_PURE_FUNC
Definition: Compat.h:108
SWC::DB::Specs::Timestamp
Definition: SpecsTimestamp.h:18
SWC::Condition::EQ
@ EQ
Definition: Comparators.h:32
SWC::DB::Specs::Interval::ts_finish
Timestamp ts_finish
Definition: SpecsInterval.h:241
SWC::DB::Specs::Interval::has_opt__key_equal
constexpr SWC_CAN_INLINE bool has_opt__key_equal() const noexcept
Definition: SpecsInterval.h:168
SWC::DB::Specs::Interval::has_opt__range_end_rest
constexpr SWC_CAN_INLINE bool has_opt__range_end_rest() const noexcept
Definition: SpecsInterval.h:173
SWC::DB::Specs::Interval::decode
void decode(const uint8_t **bufp, size_t *remainp, bool owner)
Definition: SpecsInterval.cc:192
SWC::DB::Specs::Interval::make_ptr
static SWC_CAN_INLINE Ptr make_ptr(const uint8_t **bufp, size_t *remainp)
Definition: SpecsInterval.h:41
SWC::DB::Specs::Interval::make_ptr
static SWC_CAN_INLINE Ptr make_ptr(const Interval &other)
Definition: SpecsInterval.h:46
Identifiers.h
SWC::DB::Specs::Interval::is_matching_begin
bool is_matching_begin(const Types::KeySeq key_seq, const DB::Cell::Key &key) const
Definition: SpecsInterval.h:265
SWC::DB::Specs::Interval::set_opt__range_end_rest
constexpr SWC_CAN_INLINE void set_opt__range_end_rest() noexcept
Definition: SpecsInterval.h:153
SWC::DB::Specs::Interval::flags
Flags flags
Definition: SpecsInterval.h:242
SWC::DB::Specs::Interval::make_ptr
static SWC_CAN_INLINE Ptr make_ptr(Types::Column col_type=Types::Column::UNKNOWN)
Definition: SpecsInterval.h:36
SpecsKey.h
SWC::DB::Specs::Interval::range_end
Cell::Key range_end
Definition: SpecsInterval.h:238
SWC::DB::Specs::Interval::OPT_DELETING
static constexpr const uint8_t OPT_DELETING
Definition: SpecsInterval.h:31
SWC::DB::Specs::Flags
Definition: SpecsFlags.h:18
SWC::DB::Specs::Interval::set_opt__updating
constexpr SWC_CAN_INLINE void set_opt__updating() noexcept
Definition: SpecsInterval.h:158
SWC::DB::Specs::Interval::updating
IntervalUpdate::Ptr updating
Definition: SpecsInterval.h:249
SWC::DB::Specs::Interval
Definition: SpecsInterval.h:25
SWC::DB::KeySeq::compare_opt_fc_volume
Condition::Comp compare_opt_fc_volume(const Cell::Key &opt_empty, const Cell::Key &other, uint24_t max, bool empty_eq) SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: KeyComparator.h:405
SWC::DB::Cell::Key::count
uint24_t count
Definition: CellKey.h:255
SWC::DB::Cells::Cell::get_timestamp
constexpr SWC_CAN_INLINE int64_t get_timestamp() const noexcept
Definition: Cell.h:317
SWC::DB::Specs::Interval::offset_rev
int64_t offset_rev
Definition: SpecsInterval.h:245
SWC::DB::Specs::Interval::Interval
Interval(Types::Column col_type=Types::Column::UNKNOWN) noexcept
Definition: SpecsInterval.cc:14
SWC::DB::Specs::Interval::operator=
SWC_CAN_INLINE Interval & operator=(Interval &&other) noexcept
Definition: SpecsInterval.h:73
SWC::DB::Specs::Interval::apply_possible_range_begin
void apply_possible_range_begin(DB::Cell::Key &begin) const
Definition: SpecsInterval.cc:217
SWC::DB::Specs::Interval::has_opt__updating
constexpr SWC_CAN_INLINE bool has_opt__updating() const noexcept
Definition: SpecsInterval.h:178
SWC::DB::Specs::Interval::offset_key
Cell::Key offset_key
Definition: SpecsInterval.h:244
SWC::DB::Specs::Interval::is_matching
constexpr SWC_CAN_INLINE bool is_matching(int64_t timestamp, bool desc) const noexcept
Definition: SpecsInterval.h:108
SWC::DB::Specs::Interval::is_updating
SWC_CAN_INLINE bool is_updating() const noexcept
Definition: SpecsInterval.h:188
SWC::DB::Specs::Interval::set_opt__deleting
constexpr SWC_CAN_INLINE void set_opt__deleting() noexcept
Definition: SpecsInterval.h:163
SpecsValues.h
SWC::DB::Specs::Interval::set_opt__key_equal
constexpr SWC_CAN_INLINE void set_opt__key_equal() noexcept
Definition: SpecsInterval.h:148
KeyComparator.h
SWC::DB::Specs::Interval::size_of_internal
size_t size_of_internal() const noexcept
Definition: SpecsInterval.h:256
SWC::DB::Specs::Values
Definition: SpecsValues.h:17
SWC::DB::Specs::Interval::is_matching
SWC_CAN_INLINE bool is_matching(const Types::KeySeq key_seq, const Cell::Key &key, int64_t timestamp, bool desc) const
Definition: SpecsInterval.h:91
SWC::DB::Specs::Timestamp::is_matching
SWC_CAN_INLINE bool is_matching(int64_t other) const noexcept
Definition: SpecsTimestamp.h:100
SWC::DB::Specs::IntervalUpdate::Ptr
std::unique_ptr< IntervalUpdate > Ptr
Definition: SpecsIntervalUpdate.h:34
SWC::DB::KeySeq::compare
Condition::Comp compare(const Types::KeySeq seq, const Cell::Key &key, const Cell::Key &other) SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: KeyComparator.h:294
SpecsFlags.h