SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
CellKey.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_CellKey_h
8 #define swcdb_db_cells_CellKey_h
9 
10 
11 #include "swcdb/core/Compat.h"
12 #include "swcdb/core/Comparators.h"
15 
16 
17 namespace SWC { namespace DB {
18 
19 
21 namespace Cell {
22 
23 
24 class Key final {
25  public:
26 
27  typedef std::shared_ptr<Key> Ptr;
28 
29  constexpr SWC_CAN_INLINE
30  explicit Key() noexcept : own(), count(0), size(0), data(nullptr) { }
31 
32  explicit Key(const uint8_t** bufp, size_t* remainp, bool owner);
33 
34  explicit Key(const Key& other);
35 
36  explicit Key(const Key& other, bool own);
37 
38  constexpr SWC_CAN_INLINE
39  Key(Key&& other) noexcept
40  : own(other.own), count(other.count), size(other.size),
41  data(other.data) {
42  other.data = nullptr;
43  other.size = 0;
44  other.count = 0;
45  }
46 
47  Key& operator=(const Key&) = delete;
48 
50  Key& operator=(Key&& other) noexcept {
51  move(other);
52  return *this;
53  }
54 
55  void move(Key& other) noexcept;
56 
57  void copy(const Key& other);
58 
59  void copy(uint24_t after_idx, const Key& other);
60 
62  ~Key() noexcept {
63  _free();
64  }
65 
67  void _free() noexcept {
68  if(own)
69  delete [] data;
70  }
71 
73  void free() noexcept {
74  _free();
75  data = nullptr;
76  size = 0;
77  count = 0;
78  }
79 
80  constexpr SWC_CAN_INLINE
81  bool sane() const noexcept {
82  return (count && size && data) || (!count && !size && !data);
83  }
84 
86  void add(const std::string_view& fraction) {
87  add(fraction.data(), fraction.length());
88  }
89 
91  void add(const std::string& fraction) {
92  add(fraction.c_str(), fraction.length());
93  }
94 
96  void add(const char* fraction) {
97  add(fraction, strlen(fraction));
98  }
99 
101  void add(const char* fraction, uint32_t len) {
102  add(reinterpret_cast<const uint8_t*>(fraction), len);
103  }
104 
105  void add(const uint8_t* fraction, uint24_t len);
106 
107  template<typename T>
109  void add(const T cbegin, const T cend) {
110  if(cbegin == cend)
111  return;
112 
113  const uint8_t* old = data;
114  uint32_t old_size = size;
115 
116  for(auto it=cbegin; it != cend; ++it)
117  size += Serialization::encoded_length_vi24(it->size()) + it->size();
118  //SWC_EXPECT(size > old_size, ERANGE);
119 
120  uint8_t* ptr = data = new uint8_t[size];
121  if(old) {
122  memcpy(ptr, old, old_size);
123  ptr += old_size;
124  if(own)
125  delete [] old;
126  }
127  for(auto it=cbegin; it != cend; ++it) {
128  Serialization::encode_vi24(&ptr, it->size());
129  memcpy(ptr, reinterpret_cast<const uint8_t*>(it->c_str()), it->size());
130  ptr += it->size();
131  }
132  count += cend - cbegin;
133  own = true;
134  }
135 
136  template<typename T>
138  void add(const T& key) {
139  add(key.cbegin(), key.cend());
140  }
141 
142  template<typename T>
144  void read(const T& key) {
145  free();
146  add(key);
147  }
148 
150  void insert(uint32_t idx, const std::string& fraction) {
151  insert(idx, fraction.c_str(), fraction.length());
152  }
153 
155  void insert(uint32_t idx, const char* fraction) {
156  insert(idx, fraction, strlen(fraction));
157  }
158 
160  void insert(uint32_t idx, const char* fraction, uint32_t len) {
161  insert(idx, reinterpret_cast<const uint8_t*>(fraction), len);
162  }
163 
164  void insert(uint32_t idx, const uint8_t* fraction, uint24_t len);
165 
166  void remove(uint32_t idx, bool recursive=false);
167 
169  std::string get_string(uint32_t idx) const {
170  const char* fraction;
171  uint32_t length;
172  get(idx, &fraction, &length);
173  return std::string(fraction, length);
174  }
175 
176  void get(uint32_t idx, const char** fraction, uint32_t* length) const;
177 
178  constexpr SWC_CAN_INLINE
179  bool equal(const Key& other) const noexcept {
180  return count == other.count &&
181  ((!data && !other.data) ||
182  Condition::eq(data, size, other.data, other.size));
183  }
184 
185  constexpr SWC_CAN_INLINE
186  bool empty() const noexcept {
187  return !count;
188  }
189 
190  constexpr
191  uint32_t encoded_length() const noexcept;
192 
193  void encode(uint8_t** bufp) const;
194 
195  void decode(const uint8_t** bufp, size_t* remainp, bool owner);
196 
197  template<typename T>
199  void convert_to(T& key) const {
200  uint24_t len;
201  const uint8_t* ptr = data;
202  key.clear();
203  key.resize(count);
204  for(auto it = key.begin(); it != key.cend(); ++it, ptr+=len) {
205  len = Serialization::decode_vi24(&ptr);
206  it->assign(
207  reinterpret_cast<const typename T::value_type::value_type*>(ptr),
208  len
209  );
210  }
211  }
212 
213  template<typename T>
215  bool equal(const T& key) const {
216  if(key.size() != count)
217  return false;
218 
219  uint24_t len;
220  const uint8_t* ptr = data;
221  for(auto it = key.cbegin(); it != key.cend(); ++it, ptr+=len) {
222  len = Serialization::decode_vi24(&ptr);
223  if(!Condition::eq(
224  ptr, len,
225  reinterpret_cast<const uint8_t*>(it->c_str()), it->length()))
226  return false;
227  }
228  return true;
229  }
230 
232  std::string to_string() const {
233  std::string s;
234  {
235  std::stringstream ss;
236  print(ss);
237  s = ss.str();
238  }
239  return s;
240  }
241 
242  void display(std::ostream& out, bool pretty=true,
243  const char* sep = ",") const;
244 
245  void display_details(std::ostream& out, bool pretty=true) const;
246 
247  void print(std::ostream& out) const;
248 
249  friend std::ostream& operator<<(std::ostream& out, const Key& key) {
250  key.print(out);
251  return out;
252  }
253 
254  bool own;
256  uint32_t size;
257  uint8_t* data;
258 
259  private:
260 
262  uint8_t* _data(const uint8_t* ptr) {
263  return size
264  ? static_cast<uint8_t*>(memcpy(new uint8_t[size], ptr, size))
265  : nullptr;
266  }
267 
268 };
269 
270 
272 Key::Key(const uint8_t** bufp, size_t* remainp, bool owner)
273  : own(owner),
274  count(Serialization::decode_vi24(bufp, remainp)),
275  size(), data() {
276  if(count) {
277  uint24_t n=count;
278  const uint8_t* ptr_start = *bufp;
279  do *bufp += Serialization::decode_vi24(bufp);
280  while(--n);
281  *remainp -= (size = *bufp - ptr_start);
282  data = own ? _data(ptr_start) : const_cast<uint8_t*>(ptr_start);
283  } else {
284  size = 0;
285  data = nullptr;
286  }
287 }
288 
290 Key::Key(const Key& other)
291  : own(other.size), count(other.count), size(other.size),
292  data(_data(other.data)) {
293 }
294 
296 Key::Key(const Key& other, bool a_own)
297  : own(a_own), count(other.count), size(other.size),
298  data(own ? _data(other.data): other.data) {
299 }
300 
302 void Key::move(Key& other) noexcept {
303  _free();
304  own = other.own;
305  size = other.size;
306  count = other.count;
307  data = other.data;
308  other.data = nullptr;
309  other.size = 0;
310  other.count = 0;
311 }
312 
314 void Key::copy(const Key& other) {
315  _free();
316  own = true;
317  size = other.size;
318  count = other.count;
319  data = _data(other.data);
320 }
321 
322 constexpr SWC_CAN_INLINE
323 uint32_t Key::encoded_length() const noexcept {
325 }
326 
328 void Key::encode(uint8_t** bufp) const {
330  if(size) {
331  memcpy(*bufp, data, size);
332  *bufp += size;
333  }
334 }
335 
337 void Key::decode(const uint8_t** bufp, size_t* remainp, bool owner) {
338  _free();
339  if((count = Serialization::decode_vi24(bufp, remainp))) {
340  uint24_t n=count;
341  const uint8_t* ptr_start = *bufp;
342  do *bufp += Serialization::decode_vi24(bufp);
343  while(--n);
344  *remainp -= (size = *bufp - ptr_start);
345  data = (own = owner) ? _data(ptr_start) : const_cast<uint8_t*>(ptr_start);
346  } else {
347  own = owner;
348  data = nullptr;
349  size = 0;
350  }
351 }
352 
353 
354 
355 }}}
356 
357 #ifdef SWC_IMPL_SOURCE
358 #include "swcdb/db/Cells/CellKey.cc"
359 #endif
360 
361 #endif // swcdb_db_Cells_CellKey_h
SWC::DB::Cell::Key::add
SWC_CAN_INLINE void add(const T &key)
Definition: CellKey.h:138
SWC::DB::Cell::Key::get_string
SWC_CAN_INLINE std::string get_string(uint32_t idx) const
Definition: CellKey.h:169
SWC::Serialization::encode_vi24
constexpr SWC_CAN_INLINE void encode_vi24(uint8_t **bufp, uint24_t val)
Definition: Serialization.h:207
SWC::DB::Cell::Key::get
void get(uint32_t idx, const char **fraction, uint32_t *length) const
Definition: CellKey.cc:131
SWC::Serialization::encoded_length_vi24
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi24(uint24_t val) noexcept
Definition: Serialization.h:199
SWC::DB::Cell::Key::sane
constexpr SWC_CAN_INLINE bool sane() const noexcept
Definition: CellKey.h:81
CellKeyVec.h
data
T data
Definition: BitFieldInt.h:1
SWC::DB::Cell::Key::Key
constexpr SWC_CAN_INLINE Key(Key &&other) noexcept
Definition: CellKey.h:39
SWC::DB::Cell::Key::insert
SWC_CAN_INLINE void insert(uint32_t idx, const std::string &fraction)
Definition: CellKey.h:150
SWC::DB::Cell::Key::equal
constexpr SWC_CAN_INLINE bool equal(const Key &other) const noexcept
Definition: CellKey.h:179
SWC::DB::Cell::Key::~Key
SWC_CAN_INLINE ~Key() noexcept
Definition: CellKey.h:62
SWC::DB::Cell::Key::operator=
Key & operator=(const Key &)=delete
SWC::DB::Cell::Key
Definition: CellKey.h:24
SWC::Condition::eq
SWC_CAN_INLINE bool eq(const uint8_t *p1, uint32_t p1_len, const uint8_t *p2, uint32_t p2_len) noexcept
Definition: Comparators.h:195
SWC::DB::Cell::Key::add
SWC_CAN_INLINE void add(const char *fraction)
Definition: CellKey.h:96
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::Cell::Key::display_details
void display_details(std::ostream &out, bool pretty=true) const
Definition: CellKey.cc:147
SWC::DB::Cell::Key::add
SWC_CAN_INLINE void add(const T cbegin, const T cend)
Definition: CellKey.h:109
SWC::DB::Cell::Key::data
uint8_t * data
Definition: CellKey.h:257
SWC::DB::Cell::Key::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: CellKey.h:186
SWC::DB::Cell::Key::convert_to
SWC_CAN_INLINE void convert_to(T &key) const
Definition: CellKey.h:199
SWC::DB::Cell::Key::insert
SWC_CAN_INLINE void insert(uint32_t idx, const char *fraction, uint32_t len)
Definition: CellKey.h:160
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::DB::Cell::Key::insert
SWC_CAN_INLINE void insert(uint32_t idx, const char *fraction)
Definition: CellKey.h:155
SWC::DB::Cell::Key::own
bool own
Definition: CellKey.h:254
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Cell::Key::_free
SWC_CAN_INLINE void _free() noexcept
Definition: CellKey.h:67
SWC::DB::Cell::Key::Key
constexpr SWC_CAN_INLINE Key() noexcept
Definition: CellKey.h:30
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::Cell::Key::size
uint32_t size
Definition: CellKey.h:256
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
Compat.h
SWC::DB::Cell::Key::to_string
SWC_CAN_INLINE std::string to_string() const
Definition: CellKey.h:232
SWC::DB::Cell::Key::copy
void copy(const Key &other)
Definition: CellKey.h:314
CellKey.cc
Serialization.h
SWC::Comm::Protocol::FsBroker::Handler::length
void length(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Length.h:17
Comparators.h
SWC::DB::Cell::Key::encoded_length
constexpr uint32_t encoded_length() const noexcept
Definition: CellKey.h:323
SWC::DB::Cell::Key::free
SWC_CAN_INLINE void free() noexcept
Definition: CellKey.h:73
SWC::DB::Cell::Key::add
SWC_CAN_INLINE void add(const std::string &fraction)
Definition: CellKey.h:91
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::Config::T
const uint64_t T
Definition: Property.h:27
SWC::DB::Cell::Key::read
SWC_CAN_INLINE void read(const T &key)
Definition: CellKey.h:144
SWC::DB::Cell::Key::_data
SWC_CAN_INLINE uint8_t * _data(const uint8_t *ptr)
Definition: CellKey.h:262
SWC::DB::Cell::Key::operator=
SWC_CAN_INLINE Key & operator=(Key &&other) noexcept
Definition: CellKey.h:50
SWC::DB::Cell::Key::print
void print(std::ostream &out) const
Definition: CellKey.cc:182
SWC::DB::Cell::Key::remove
void remove(uint32_t idx, bool recursive=false)
Definition: CellKey.cc:94
SWC::DB::Cell::Key::equal
SWC_CAN_INLINE bool equal(const T &key) const
Definition: CellKey.h:215
SWC::DB::Cell::Key::Ptr
std::shared_ptr< Key > Ptr
Definition: CellKey.h:27
SWC::DB::Cell::Key::move
void move(Key &other) noexcept
Definition: CellKey.h:302
SWC::DB::Cell::Key::add
SWC_CAN_INLINE void add(const char *fraction, uint32_t len)
Definition: CellKey.h:101
SWC::DB::Cell::Key::display
void display(std::ostream &out, bool pretty=true, const char *sep=",") const
Definition: CellKey.cc:152
SWC::DB::Cell::Key::operator<<
friend std::ostream & operator<<(std::ostream &out, const Key &key)
Definition: CellKey.h:249
SWC::DB::Cell::Key::encode
void encode(uint8_t **bufp) const
Definition: CellKey.h:328