SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Cell.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 
7 
8 #include "swcdb/db/Cells/Cell.h"
10 
11 
12 
13 namespace SWC { namespace DB { namespace Cells {
14 
15 
16 
17 const char* to_string(Flag flag) noexcept {
18  switch(flag) {
19  case Flag::INSERT:
20  return "INSERT";
21  case Flag::DELETE_LE:
22  return "DELETE_LE";
23  case Flag::DELETE_EQ:
24  return "DELETE_EQ";
25  case Flag::NONE:
26  return "NONE";
27  default:
28  return "UNKNOWN";
29  }
30 }
31 
32 Flag flag_from(const uint8_t* rptr, uint32_t len) noexcept {
33  const char* ptr = reinterpret_cast<const char*>(rptr);
34  if(len >= 9) {
35  if(Condition::str_case_eq(ptr, "DELETE_LE", 9))
36  return Flag::DELETE_LE;
37  if(Condition::str_case_eq(ptr, "DELETE_EQ", 9))
38  return Flag::DELETE_EQ;
39  }
40  if(len >= 6) {
41  if(Condition::str_case_eq(ptr, "insert", 6))
42  return Flag::INSERT;
43  }
44  return Flag::NONE;
45 }
46 
47 
48 void Cell::set_value(Types::Encoder encoder, const uint8_t* v, uint32_t len) {
49  _free();
50  if(!len) {
51  vlen = 0;
52  value = nullptr;
53  return;
54  }
55 
56  int err = Error::OK;
57  size_t len_enc = 0;
58  DynamicBuffer output;
59  Core::Encoder::encode(err, encoder, v, len, &len_enc, output,
60  Serialization::encoded_length_vi32(len) + 1, false);
61  if(len_enc) {
63  uint8_t* ptr = output.base;
64  Serialization::encode_vi32(&ptr, len);
65  Serialization::encode_i8(&ptr, uint8_t(encoder));
66  vlen = output.fill();
67  value = _value(output.base);
68  // or keep as - value = output.base, output.own = false;
69  } else {
71  vlen = len;
72  value = _value(v);
73  }
74  own = true;
75 }
76 
79  if(have_encoder()) {
80  const uint8_t* ptr = value;
81  size_t remain = vlen;
82  v.reallocate(Serialization::decode_vi32(&ptr, &remain));
84  int err = Error::OK;
85  Core::Encoder::decode(err, encoder, ptr, remain, v.base, v.size);
86  if(err) {
87  v.free();
88  SWC_THROWF(Error::ENCODER_DECODE, "Cell(key=%s) value-encoder(%s)",
90  }
91  } else {
93  if(vlen)
94  v.set(value, vlen, owner);
95  }
96  return encoder;
97 }
98 
99 
100 bool Cell::equal(const Cell& other) const noexcept {
101  return flag == other.flag &&
102  control == other.control &&
103  vlen == other.vlen &&
104  (!(control & HAVE_TIMESTAMP) || timestamp == other.timestamp) &&
105  (!(control & HAVE_REVISION) || revision == other.revision) &&
106  key.equal(other.key) &&
107  Condition::mem_eq(value, other.value, vlen);
108 }
109 
110 
111 void Cell::display(std::ostream& out,
112  Types::Column typ, uint8_t flags, bool meta) const {
113 
115  out << Time::fmt_ns(timestamp) << '\t';
116 
118  out << timestamp << '\t';
119 
120  bool bin = flags & DisplayFlag::BINARY;
121  key.display(out, !bin);
122  out << '\t';
123 
124  if(flag != Flag::INSERT) {
125  out << '(' << Cells::to_string(Flag(flag)) << ')';
126  return;
127  }
128 
129  if(!vlen)
130  return;
131 
132  if(Types::is_counter(typ)) {
133  if(bin) {
134  uint8_t op;
135  int64_t eq_rev = TIMESTAMP_NULL;
136  out << get_counter(op, eq_rev);
137  if(op & OP_EQUAL && !(op & HAVE_REVISION))
138  eq_rev = get_timestamp();
139  if(eq_rev != TIMESTAMP_NULL)
140  out << " EQ-SINCE(" << Time::fmt_ns(eq_rev) << ")";
141  } else {
142  out << get_counter();
143  }
144 
145  } else if(meta && !bin) {
146  StaticBuffer v;
147  get_value(v, false);
148  const uint8_t* ptr = v.base;
149  size_t remain = v.size;
150  DB::Cell::Key de_key;
152  de_key.decode(&ptr, &remain, false);
153  de_key.display(out << "end=", true);
155  out << " rid=" << Serialization::decode_vi64(&ptr, &remain);
157  de_key.decode(&ptr, &remain, false);
158  de_key.display(out << " min=", true);
160  de_key.decode(&ptr, &remain, false);
161  de_key.display(out << " max=", true);
162 
163  } else {
164  StaticBuffer v;
165  get_value(v, false);
166 
167  if(typ == Types::Column::SERIAL) {
169 
170  } else {
171  const uint8_t* ptr = v.base;
172  char hex[5];
173  hex[4] = 0;
174 
175  for(uint32_t i=v.size; i; --i, ++ptr) {
176  if(!bin && (*ptr < 32 || *ptr > 126)) {
177  sprintf(hex, "0x%X", *ptr);
178  out << hex;
179  } else {
180  out << *ptr;
181  }
182  }
183  }
184  }
185 }
186 
187 void Cell::print(std::ostream& out, Types::Column typ) const {
188  out << "Cell(flag=" << Cells::to_string(Flag(flag));
189  key.print(out << " key=");
190  out << " control=" << int(control)
191  << " ts=" << get_timestamp()
192  << " rev=" << get_revision()
193  << " enc=" << have_encoder()
194  << " value=(len=" << vlen;
195 
196  if(flag != Flag::INSERT || !vlen) {
197  out << ')' << ')';
198  return;
199  }
200 
201  if(Types::is_counter(typ)) {
202  out << " count=";
203  uint8_t op;
204  int64_t eq_rev = TIMESTAMP_NULL;
205  out << get_counter(op, eq_rev);
206  if(op & OP_EQUAL && !(op & HAVE_REVISION))
207  eq_rev = get_timestamp();
208  if(eq_rev != TIMESTAMP_NULL)
209  out << " eq-since=" << Time::fmt_ns(eq_rev);
210 
211  } else {
212  out << " data=\"";
213  StaticBuffer v;
214  get_value(v, false);
215 
216  if(typ == Types::Column::SERIAL) {
218 
219  } else {
220  char hex[5];
221  hex[4] = 0;
222  const uint8_t* ptr = v.base;
223  for(uint32_t len = v.size; len; --len, ++ptr) {
224  if(*ptr == '"')
225  out << '\\';
226  if(31 < *ptr && *ptr < 127) {
227  out << *ptr;
228  } else {
229  sprintf(hex, "0x%X", *ptr);
230  out << hex;
231  }
232  }
233  }
234  out << "\")";
235  }
236  out << ')';
237 }
238 
239 
240 
241 }}}
SWC::DB::Cells::Cell::set_value
SWC_CAN_INLINE void set_value(uint8_t *v, uint32_t len, bool owner)
Definition: Cell.h:223
Cell.h
SWC::DB::Cells::DELETE_EQ
@ DELETE_EQ
Definition: Cell.h:64
SWC::Core::Buffer::reallocate
SWC_CAN_INLINE void reallocate(size_t len)
Definition: Buffer.h:92
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::Cells::Flag
Flag
Definition: Cell.h:60
SWC::DB::Cells::to_string
const char *SWC_CONST_FUNC to_string(Flag flag) noexcept
Definition: Cell.cc:17
SWC::Core::Encoder::Type
Type
Definition: Encoder.h:28
SWC::Serialization::encoded_length_vi32
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi32(uint32_t val) noexcept
Definition: Serialization.h:234
SWC::DB::Cells::Cell::timestamp
int64_t timestamp
Definition: Cell.h:373
SWC::DB::Types::Column
Column
Definition: Column.h:18
SWC::DB::Cells::DELETE_LE
@ DELETE_LE
Definition: Cell.h:63
SWC::DB::BINARY
@ BINARY
Definition: Cell.h:44
SWC::DB::Cells::Cell
Definition: Cell.h:92
SWC::DB::Cell::Key
Definition: CellKey.h:24
SWC::DB::Cells::Cell::own
bool own
Definition: Cell.h:358
SWC::Error::ENCODER_DECODE
@ ENCODER_DECODE
Definition: Error.h:77
SWC::Serialization::encode_i8
constexpr SWC_CAN_INLINE void encode_i8(uint8_t **bufp, uint8_t val) noexcept
Definition: Serialization.h:85
SWC::DB::Cells::Cell::key
DB::Cell::Key key
Definition: Cell.h:357
SWC::Core::Encoder::Type::DEFAULT
@ DEFAULT
SWC::DB::Cell::Key::decode
void decode(const uint8_t **bufp, size_t *remainp, bool owner)
Definition: CellKey.h:337
encoder
Core::Encoder::Type encoder
Buffer Encoder.
Definition: HeaderBufferInfo.h:50
SWC::DB::Cells::MASK_HAVE_ENCODER
constexpr const uint8_t MASK_HAVE_ENCODER
Definition: Cell.h:86
SWC::Error::OK
@ OK
Definition: Error.h:45
CellValueSerialFields.h
SWC::Core::Encoder::decode
void decode(int &err, Type encoder, const uint8_t *src, size_t sz_enc, uint8_t *dst, size_t sz)
Definition: Encoder.cc:99
SWC::Core::Buffer::base
value_type * base
Definition: Buffer.h:131
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Cells::Cell::display
void display(std::ostream &out, Types::Column typ=Types::Column::PLAIN, uint8_t flags=0, bool meta=false) const
Definition: Cell.cc:111
SWC::DB::Cells::Cell::have_encoder
constexpr SWC_CAN_INLINE bool have_encoder() const noexcept
Definition: Cell.h:334
SWC::DB::Types::is_counter
bool SWC_CONST_FUNC is_counter(const Column typ) noexcept
Definition: Column.cc:26
SWC::Core::BufferDyn< StaticBuffer >
SWC::Core::Buffer
Definition: Buffer.h:18
SWC::DB::Cells::HAVE_ENCODER
constexpr const uint8_t HAVE_ENCODER
Definition: Cell.h:78
SWC::Core::Buffer::size
size_t size
Definition: Buffer.h:130
SWC::DB::Cells::HAVE_REVISION
constexpr const uint8_t HAVE_REVISION
Definition: Cell.h:81
SWC::DB::Cells::Cell::get_value
Types::Encoder get_value(StaticBuffer &v, bool owner) const
Definition: Cell.cc:77
SWC::DB::Cell::Key::to_string
SWC_CAN_INLINE std::string to_string() const
Definition: CellKey.h:232
SWC::DB::Cells::Cell::value
uint8_t * value
Definition: Cell.h:362
SWC_THROWF
#define SWC_THROWF(_code_, _fmt_,...)
Definition: Exception.h:136
SWC::DB::Cells::INSERT
@ INSERT
Definition: Cell.h:62
SWC::DB::Cells::Cell::control
uint8_t control
Definition: Cell.h:360
SWC::DB::Cells::Cell::_free
SWC_CAN_INLINE void _free() noexcept
Definition: Cell.h:156
SWC::DB::Cells::HAVE_TIMESTAMP
constexpr const uint8_t HAVE_TIMESTAMP
Definition: Cell.h:80
SWC::DB::Cells::TIMESTAMP_NULL
constexpr const int64_t TIMESTAMP_NULL
Definition: Cell.h:72
SWC::Core::Buffer::free
SWC_CAN_INLINE void free() noexcept
Definition: Buffer.h:85
SWC::Condition::NONE
@ NONE
Definition: Comparators.h:28
SWC::DB::Cell::Serial::Value::Fields::display
static void display(const uint8_t *ptr, size_t remain, std::ostream &out)
Definition: CellValueSerialFields.cc:75
SWC::DB::Cells::Cell::print
void print(std::ostream &out, Types::Column typ) const
Definition: Cell.cc:187
SWC::DB::Cells::Cell::vlen
uint32_t vlen
Definition: Cell.h:361
SWC::DB::Types::Encoder
Core::Encoder::Type Encoder
Definition: Encoder.h:15
SWC::Core::BufferDyn::fill
constexpr SWC_CAN_INLINE size_t fill() const noexcept
Definition: Buffer.h:192
SWC::DB::Cells::Cell::get_timestamp
constexpr SWC_CAN_INLINE int64_t get_timestamp() const noexcept
Definition: Cell.h:317
SWC::DB::Cells::OP_EQUAL
constexpr const uint8_t OP_EQUAL
Definition: Cell.h:83
SWC::DB::Cell::Serial::Value::skip_type_and_id
SWC_CAN_INLINE void skip_type_and_id(const uint8_t **bufp, size_t *remainp)
Definition: CellValueSerialField.h:61
SWC::DB::Cells::Cell::get_counter
constexpr SWC_CAN_INLINE int64_t get_counter() const
Definition: Cell.h:260
SWC::DB::TIMESTAMP
@ TIMESTAMP
Definition: Cell.h:42
SWC::Serialization::decode_vi64
constexpr SWC_CAN_INLINE uint64_t decode_vi64(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:302
SWC::Serialization::encode_vi32
constexpr SWC_CAN_INLINE void encode_vi32(uint8_t **bufp, uint32_t val)
Definition: Serialization.h:243
SWC::Core::Encoder::to_string
const char *SWC_CONST_FUNC to_string(Type typ) noexcept
Definition: Encoder.cc:31
SWC::Core::Encoder::encode
void encode(int &err, Type encoder, const uint8_t *src, size_t src_sz, size_t *sz_enc, DynamicBuffer &output, uint32_t reserve, bool no_plain_out=false, bool ok_more=false)
Definition: Encoder.cc:222
SWC::DB::DATETIME
@ DATETIME
Definition: Cell.h:43
SWC::DB::Cells::Cell::_value
SWC_CAN_INLINE uint8_t * _value(const uint8_t *v)
Definition: Cell.h:367
SWC::DB::Cell::Key::print
void print(std::ostream &out) const
Definition: CellKey.cc:182
SWC::DB::Cells::Cell::equal
bool SWC_PURE_FUNC equal(const Cell &other) const noexcept
Definition: Cell.cc:100
flags
uint8_t flags
Flags.
Definition: Header.h:55
SWC::DB::Types::Column::SERIAL
@ SERIAL
SWC::DB::Cells::flag_from
Flag SWC_PURE_FUNC flag_from(const uint8_t *rptr, uint32_t len) noexcept
Definition: Cell.cc:32
SWC::Serialization::decode_i8
constexpr SWC_CAN_INLINE uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:91
SWC::Time::fmt_ns
std::string fmt_ns(int64_t ns)
Definition: Time.cc:133
SWC::Condition::str_case_eq
bool str_case_eq(const char *s1, const char *s2, size_t count) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:257
SWC::DB::Cell::Key::display
void display(std::ostream &out, bool pretty=true, const char *sep=",") const
Definition: CellKey.cc:152
SWC::DB::Cells::Cell::flag
uint8_t flag
Definition: Cell.h:359
SWC::DB::Cells::Cell::get_revision
constexpr SWC_CAN_INLINE int64_t get_revision() const noexcept
Definition: Cell.h:322
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::Buffer::set
void set(value_type *data, size_t len, bool take_ownership) noexcept
Definition: Buffer.h:109