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.cc
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 
9 
10 
11 namespace SWC { namespace DB { namespace Specs {
12 
13 
15  : range_begin(), range_end(), key_intervals(),
16  values(col_type), ts_start(), ts_finish(),
17  flags(), offset_key(), offset_rev(0), options(0),
18  updating(nullptr) {
19 }
20 
21 Interval::Interval(const Cell::Key& a_range_begin,
22  const Cell::Key& a_range_end)
23  : range_begin(a_range_begin),
24  range_end(a_range_end),
25  key_intervals(),
26  values(),
27  ts_start(), ts_finish(),
28  flags(), offset_key(),
29  offset_rev(0), options(0),
30  updating(nullptr) {
31 }
32 
33 Interval::Interval(const uint8_t** bufp, size_t* remainp)
34  : range_begin(bufp, remainp, true),
35  range_end(bufp, remainp, true),
36  key_intervals(bufp, remainp),
37  values(bufp, remainp, true),
38  ts_start(bufp, remainp),
39  ts_finish(bufp, remainp),
40  flags(bufp, remainp),
41  offset_key(bufp, remainp, true),
42  offset_rev(Serialization::decode_vi64(bufp, remainp)),
43  options(Serialization::decode_i8(bufp, remainp)),
44  updating(nullptr) {
45  if(has_opt__updating()) {
46  updating.reset(new IntervalUpdate(bufp, remainp));
48  }
49 }
50 
52  : range_begin(other.range_begin),
53  range_end(other.range_end),
54  key_intervals(other.key_intervals),
55  values(other.values),
56  ts_start(other.ts_start),
57  ts_finish(other.ts_finish),
58  flags(other.flags),
59  offset_key(other.offset_key),
60  offset_rev(other.offset_rev),
61  options(other.options),
62  updating(
63  other.is_updating()
64  ? new IntervalUpdate(*other.updating.get())
65  : nullptr) {
66 }
67 
68 Interval::Interval(Interval&& other) noexcept
69  : range_begin(std::move(other.range_begin)),
70  range_end(std::move(other.range_end)),
71  key_intervals(std::move(other.key_intervals)),
72  values(std::move(other.values)),
73  ts_start(std::move(other.ts_start)),
74  ts_finish(std::move(other.ts_finish)),
75  flags(std::move(other.flags)),
76  offset_key(std::move(other.offset_key)),
77  offset_rev(other.offset_rev),
78  options(other.options),
79  updating(std::move(other.updating)) {
80 }
81 
82 Interval::~Interval() noexcept { }
83 
84 void Interval::copy(const Interval& other) {
86  range_end.copy(other.range_end);
87 
89 
90  values.copy(other.values);
91 
92  ts_start.copy(other.ts_start);
93  ts_finish.copy(other.ts_finish);
94 
95  flags.copy(other.flags);
96 
97  offset_key.copy(other.offset_key);
98  offset_rev = other.offset_rev;
99 
100  options = other.options;
101 
102  if(other.is_updating())
103  updating.reset(new IntervalUpdate(*other.updating.get()));
104  else
105  updating = nullptr;
106 }
107 
108 void Interval::move(Interval& other) noexcept {
109  range_begin.move(other.range_begin);
110  range_end.move(other.range_end);
111 
112  key_intervals.move(other.key_intervals);
113 
114  values.move(other.values);
115 
116  ts_start.copy(other.ts_start);
117  ts_finish.copy(other.ts_finish);
118 
119  flags.copy(other.flags);
120 
121  offset_key.move(other.offset_key);
122  offset_rev = other.offset_rev;
123 
124  options = other.options;
125 
126  updating = std::move(other.updating);
127 }
128 
129 void Interval::free() noexcept {
130  range_begin.free();
131  range_end.free();
133  values.clear();
134  offset_key.free();
135  updating = nullptr;
136 }
137 
138 bool Interval::equal(const Interval& other) const noexcept {
139  return ts_start.equal(other.ts_start) &&
140  ts_finish.equal(other.ts_finish) &&
141  flags.equal(other.flags) &&
142  options == other.options &&
143  range_begin.equal(other.range_begin) &&
144  range_end.equal(other.range_end) &&
145  key_intervals.equal(other.key_intervals) &&
146  values.equal(other.values) &&
147  offset_key.equal(other.offset_key) &&
148  offset_rev == other.offset_rev &&
149  is_updating() == other.is_updating() &&
150  (!is_updating() || updating->equal(*other.updating.get()));
151 }
152 
153 
154 size_t Interval::encoded_length() const noexcept {
162  + 1
163  + (is_updating() ? updating->encoded_length() : 0);
164 }
165 
166 void Interval::encode(uint8_t** bufp) const {
167  range_begin.encode(bufp);
168  range_end.encode(bufp);
169 
170  key_intervals.encode(bufp);
171 
172  values.encode(bufp);
173 
174  ts_start.encode(bufp);
175  ts_finish.encode(bufp);
176 
177  flags.encode(bufp);
178 
179  offset_key.encode(bufp);
181 
182  bool w_updating = is_updating();
183  uint8_t opts = options;
184  if(w_updating)
185  opts |= OPT_UPDATING;
186  Serialization::encode_i8(bufp, opts);
187 
188  if(w_updating)
189  updating->encode(bufp);
190 }
191 
192 void Interval::decode(const uint8_t** bufp, size_t* remainp, bool owner) {
193  range_begin.decode(bufp, remainp, owner);
194  range_end.decode(bufp, remainp, owner);
195 
196  key_intervals.decode(bufp, remainp);
197 
198  values.decode(bufp, remainp, owner);
199 
200  ts_start.decode(bufp, remainp);
201  ts_finish.decode(bufp, remainp);
202 
203  flags.decode(bufp, remainp);
204 
205  offset_key.decode(bufp, remainp, owner);
206  offset_rev = Serialization::decode_vi64(bufp, remainp);
207  options = Serialization::decode_i8(bufp, remainp);
208 
209  if(has_opt__updating()) {
210  updating.reset(new IntervalUpdate(bufp, remainp));
212  } else {
213  updating = nullptr;
214  }
215 }
216 
218  if(!offset_key.empty()) {
219  begin.copy(offset_key);
220 
221  } else if(!range_begin.empty()) {
222  if(&begin != &range_begin)
223  begin.copy(range_begin);
224 
225  } else if(!key_intervals.empty()) {
226  begin.free();
227  apply_possible_range(begin, false, false, false);
228  }
229 }
230 
232  bool* restp) const {
233  if(!range_end.empty()) {
234  if(&end != &range_end)
235  end.copy(range_end);
236 
237  } else if(!key_intervals.empty()) {
238  end.free();
239  apply_possible_range(end, true, restp, false);
240  if(restp && !end.empty())
241  *restp = true;
242  }
243 }
244 
246  bool rest, bool no_stepping) const {
247  size_t sz = 0;
248  for(const auto& intval : key_intervals) {
249  if(sz < intval.start.size())
250  sz = intval.start.size();
251  if(sz < intval.finish.size())
252  sz = intval.finish.size();
253  }
254  Core::Vector<std::string> key_range(sz + (ending && !rest));
255  bool initial = true;
256  bool found = false;
257  do_:
258  for(const auto& intval : key_intervals) {
259  const auto* keyp = ending
260  ? (initial ? &intval.finish : &intval.start )
261  : (initial ? &intval.start : &intval.finish);
262  for(size_t idx=0; idx < keyp->size(); ++idx) {
263  auto& key_f = key_range[idx];
264  if(!key_f.empty())
265  continue;
266  const Fraction& f = (*keyp)[idx];
267  if(f.empty())
268  continue;
269  switch(f.comp) {
270  case Condition::EQ:
271  break;
272  case Condition::LE:
273  case Condition::LT:
274  if(!ending)
275  continue;
276  break;
277  case Condition::PF:
278  case Condition::GT:
279  case Condition::GE:
280  case Condition::FIP:
281  case Condition::FI:
282  if(ending)
283  continue;
284  break;
285  default:
286  continue;
287  }
288  key_f.append(f.data(), f.size());
289  found = true;
290  }
291  }
292  if(initial) {
293  initial = false;
294  goto do_;
295  }
296  if(!found)
297  return;
298 
299  if(no_stepping) {
300  for(auto it = key_range.cbegin(); it != key_range.cend(); ++it) {
301  if(it->empty()) {
302  key.add(key_range.cbegin(), it);
303  return;
304  }
305  }
306  key.add(key_range.cbegin(), key_range.cend());
307 
308  } else {
309  auto it = key_range.cend();
310  do {
311  --it;
312  if(!it->empty()) {
313  if(ending && !rest)
314  ++it;
315  key.add(key_range.cbegin(), ++it);
316  break;
317  }
318  } while(it != key_range.cbegin());
319  }
320 }
321 
322 void Interval::print(std::ostream& out) const {
323  range_begin.print(out << "Interval(Begin");
324  range_end.print(out << " End");
325 
326  key_intervals.print(out << ' ');
327 
328  ts_start.print(out << " Start");
329  ts_finish.print(out << " Finish");
330 
331  offset_key.print(out << " Offset");
332  if(!offset_key.empty())
333  out << " OffsetRev=" << offset_rev;
334 
335  values.print(out << ' ');
336 
337  flags.print(out << ' ');
338 
339  if(is_updating())
340  updating->print(out << " Update");
341 
342  out << " Options(range-end-rest=" << has_opt__range_end_rest()
343  << " key-eq=" << has_opt__key_equal();
344  if(has_opt__updating())
345  out << " UPDATING";
346  if(has_opt__deleting())
347  out << " DELETING";
348  out << "))";
349 }
350 
351 void Interval::display(std::ostream& out, bool pretty,
352  const std::string& offset) const {
353  out << offset << "Interval(\n";
354 
355  out << offset << " Range(\n"
356  << offset << " begin(";
357  range_begin.display_details(out, pretty);
358  out << ")\n"
359  << offset << " end(";
360  range_end.display_details(out, pretty);
361  out << ")\n"
362  << offset << " )\n";
363 
364  key_intervals.display(out, pretty, offset + ' ');
365 
366  out << offset << " Timestamp(\n"
367  << offset << " start(";
368  ts_start.display(out);
369  out << ")\n"
370  << offset << " finish(";
371  ts_finish.display(out);
372  out << ")\n"
373  << offset << " )\n";
374 
375  out << offset << " Offset(\n"
376  << offset << " key(";
377  offset_key.display_details(out, pretty);
378  out << ")\n"
379  << offset << " rev(" << offset_rev << " )\n"
380  << offset << " )\n";
381 
382  values.display(out, pretty, offset + ' ');
383 
384  out << offset << " Flags(";
385  flags.display(out);
386  out << ")\n";
387 
388  if(is_updating()) {
389  out << offset << " Update(";
390  updating->display(out, pretty);
391  out << ")\n";
392  }
393 
394  out << offset << " Options("
395  << "range-end-rest=" << has_opt__range_end_rest()
396  << " key-eq=" << has_opt__key_equal();
397  if(has_opt__updating())
398  out << " UPDATING";
399  if(has_opt__deleting())
400  out << " DELETING";
401  out << ")\n";
402 
403  out << offset << ")\n";
404 }
405 
406 
407 
408 }}}
SWC::Condition::LE
@ LE
Definition: Comparators.h:33
SWC::DB::Specs::Timestamp::encoded_length
constexpr SWC_CAN_INLINE size_t encoded_length() const noexcept
Definition: SpecsTimestamp.h:78
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::Specs::Interval::key_intervals
KeyIntervals key_intervals
Definition: SpecsInterval.h:239
SWC::DB::Specs::Interval::values
Values values
Definition: SpecsInterval.h:240
SWC::Condition::GE
@ GE
Definition: Comparators.h:31
SWC::Core::Vector::clear
SWC_CAN_INLINE void clear() noexcept(_NoExceptDestructor)
Definition: Vector.h:120
SWC::DB::Specs::Interval::~Interval
~Interval() noexcept
Definition: SpecsInterval.cc:82
SWC::DB::Specs::Interval::has_opt__deleting
constexpr SWC_CAN_INLINE bool has_opt__deleting() const noexcept
Definition: SpecsInterval.h:183
SWC::DB::Specs::Values::print
void print(std::ostream &out) const
Definition: SpecsValues.cc:42
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::Timestamp::copy
constexpr SWC_CAN_INLINE void copy(const Timestamp &other) noexcept
Definition: SpecsTimestamp.h:49
SWC::DB::Specs::Interval::encoded_length
size_t SWC_PURE_FUNC encoded_length() const noexcept
Definition: SpecsInterval.cc:154
SWC::DB::Types::Column
Column
Definition: Column.h:18
SWC::DB::Specs::Interval::range_begin
Cell::Key range_begin
Definition: SpecsInterval.h:238
SWC::DB::Specs::Values::display
void display(std::ostream &out, bool pretty, const std::string &offset) const
Definition: SpecsValues.cc:55
SWC::Condition::LT
@ LT
Definition: Comparators.h:34
SWC::DB::Specs::Interval::options
uint8_t options
Definition: SpecsInterval.h:247
SWC::DB::Specs::Interval::print
void print(std::ostream &out) const
Definition: SpecsInterval.cc:322
SWC::DB::Specs::Timestamp::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsTimestamp.h:86
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::Serialization::encode_i8
constexpr SWC_CAN_INLINE void encode_i8(uint8_t **bufp, uint8_t val) noexcept
Definition: Serialization.h:85
SWC::DB::Specs::Timestamp::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp)
Definition: SpecsTimestamp.h:93
SWC::DB::Specs::KeyIntervals::copy
void copy(const KeyIntervals &other)
Definition: SpecsKeyIntervals.cc:24
SWC::DB::Cell::Key::decode
void decode(const uint8_t **bufp, size_t *remainp, bool owner)
Definition: CellKey.h:337
SWC::DB::Cell::Key::add
SWC_CAN_INLINE void add(const std::string_view &fraction)
Definition: CellKey.h:86
SWC::DB::Specs::Fraction
Definition: SpecsKey.h:18
SWC::DB::Cell::Key::display_details
void display_details(std::ostream &out, bool pretty=true) const
Definition: CellKey.cc:147
SWC::DB::Specs::Interval::free
void free() noexcept
Definition: SpecsInterval.cc:129
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::Values::copy
void copy(const Values &other)
Definition: SpecsValues.cc:19
SWC::Core::Vector::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: Vector.h:168
SWC::DB::Specs::KeyIntervals::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp)
Definition: SpecsKeyIntervals.h:173
SWC::Condition::FIP
@ FIP
Definition: Comparators.h:75
SWC::DB::Specs::IntervalUpdate
Definition: SpecsIntervalUpdate.h:17
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
SWC::DB::Specs::KeyIntervals::encoded_length
SWC_CAN_INLINE size_t encoded_length() const noexcept
Definition: SpecsKeyIntervals.h:158
SWC::Condition::EQ
@ EQ
Definition: Comparators.h:32
SWC::DB::Specs::Flags::display
void display(std::ostream &out) const
Definition: SpecsFlags.cc:20
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::Cell::Key::copy
void copy(const Key &other)
Definition: CellKey.h:314
SWC::Condition::PF
@ PF
Definition: Comparators.h:29
Serialization.h
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::KeyIntervals::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsKeyIntervals.h:166
SWC::DB::Specs::Interval::decode
void decode(const uint8_t **bufp, size_t *remainp, bool owner)
Definition: SpecsInterval.cc:192
SWC::DB::Specs::Flags::encoded_length
constexpr SWC_CAN_INLINE size_t encoded_length() const noexcept
Definition: SpecsFlags.h:101
SWC::Serialization::encoded_length_vi64
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi64(uint64_t val) noexcept
Definition: Serialization.h:272
SWC::DB::Cell::Key::encoded_length
constexpr uint32_t encoded_length() const noexcept
Definition: CellKey.h:323
SWC::DB::Specs::Interval::flags
Flags flags
Definition: SpecsInterval.h:242
SWC::DB::Specs::Flags::copy
constexpr SWC_CAN_INLINE void copy(const Flags &other) noexcept
Definition: SpecsFlags.h:53
SWC::DB::Specs::Timestamp::display
void display(std::ostream &out) const
Definition: SpecsTimestamp.cc:15
SWC::DB::Specs::Fraction::comp
Condition::Comp comp
Definition: SpecsKey.h:20
SWC::DB::Specs::Interval::range_end
Cell::Key range_end
Definition: SpecsInterval.h:238
SpecsInterval.h
SWC::Core::Vector< std::string >
SWC::DB::Cell::Key::free
SWC_CAN_INLINE void free() noexcept
Definition: CellKey.h:73
SWC::DB::Specs::KeyIntervals::display
void display(std::ostream &out, bool pretty, const std::string &offset) const
Definition: SpecsKeyIntervals.cc:72
SWC::DB::Specs::Interval::updating
IntervalUpdate::Ptr updating
Definition: SpecsInterval.h:249
SWC::DB::Specs::Interval
Definition: SpecsInterval.h:25
SWC::Core::Vector::cend
constexpr SWC_CAN_INLINE const_iterator cend() const noexcept
Definition: Vector.h:232
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::Serialization::decode_vi64
constexpr SWC_CAN_INLINE uint64_t decode_vi64(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:302
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::Values::encoded_length
SWC_CAN_INLINE size_t encoded_length() const noexcept
Definition: SpecsValues.h:79
SWC::Condition::FI
@ FI
Definition: Comparators.h:76
SWC::DB::Specs::Flags::print
void print(std::ostream &out) const
Definition: SpecsFlags.cc:15
SWC::DB::Specs::Flags::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsFlags.h:110
SWC::DB::Specs::Values::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsValues.h:92
SWC::DB::Specs::KeyIntervals::print
void print(std::ostream &out) const
Definition: SpecsKeyIntervals.cc:59
SWC::DB::Specs::Interval::is_updating
SWC_CAN_INLINE bool is_updating() const noexcept
Definition: SpecsInterval.h:188
SWC::DB::Cell::Key::print
void print(std::ostream &out) const
Definition: CellKey.cc:182
SWC::Serialization::encode_vi64
constexpr SWC_CAN_INLINE void encode_vi64(uint8_t **bufp, uint64_t val)
Definition: Serialization.h:286
SWC::DB::Specs::Flags::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp)
Definition: SpecsFlags.h:119
flags
uint8_t flags
Flags.
Definition: Header.h:55
SWC::DB::Specs::Flags::clear_only_deletes
constexpr SWC_CAN_INLINE void clear_only_deletes() noexcept
Definition: SpecsFlags.h:85
SWC::DB::Specs::Values::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp, bool owner)
Definition: SpecsValues.h:106
SWC::Core::Vector::cbegin
constexpr SWC_CAN_INLINE const_iterator cbegin() const noexcept
Definition: Vector.h:216
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::Timestamp::print
void print(std::ostream &out) const
Definition: SpecsTimestamp.cc:19
SWC::DB::Cell::Key::encode
void encode(uint8_t **bufp) const
Definition: CellKey.h:328