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.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 
8 
9 
10 namespace SWC { namespace DB { namespace Cell {
11 
12 
13 
14 void Key::copy(uint24_t after_idx, const Key& other) {
15  _free();
16  own = true;
17  if(other.data && ++after_idx < other.count) {
18  count = other.count - after_idx;
19  const uint8_t* ptr = other.data;
20  for(; after_idx ; --after_idx)
21  ptr += Serialization::decode_vi24(&ptr);
22  size = other.size - (ptr - other.data);
23  data = _data(ptr);
24  } else {
25  size = 0;
26  count = 0;
27  data = nullptr;
28  }
29 }
30 
31 void Key::add(const uint8_t* fraction, uint24_t len) {
32  const uint8_t* old = data;
33  uint32_t old_size = size;
34 
36  size += uint32_t(len);
37  //SWC_EXPECT(size > old_size, ERANGE);
38 
39  uint8_t* ptr = data = new uint8_t[size];
40  if(old) {
41  memcpy(ptr, old, old_size);
42  ptr += old_size;
43  if(own)
44  delete [] old;
45  }
46  Serialization::encode_vi24(&ptr, len);
47  memcpy(ptr, fraction, len);
48  ++count;
49  own = true;
50 }
51 
52 void Key::insert(uint32_t idx, const uint8_t* fraction, uint24_t len) {
53  if(!data || idx >= count) {
54  add(fraction, len);
55  return;
56  }
57 
58  uint32_t prev_size = size;
60  size += uint32_t(len);
61  //SWC_EXPECT(size > prev_size, ERANGE);
62 
63  uint8_t* data_tmp = new uint8_t[size];
64  const uint8_t* ptr_tmp = data;
65 
66  uint8_t* fraction_ptr;
67 
68  uint32_t offset = 0;
69  for(uint32_t pos = 0;; ++pos) {
70  if(idx == pos) {
71  if(offset)
72  memcpy(data_tmp, data, offset);
73  fraction_ptr = data_tmp + offset;
74  Serialization::encode_vi24(&fraction_ptr, len);
75  memcpy(fraction_ptr, fraction, len);
76  fraction_ptr += len;
77  break;
78  }
79  ptr_tmp += Serialization::decode_vi24(&ptr_tmp);
80  offset += ptr_tmp - data;
81  }
82 
83  if(prev_size-offset)
84  memcpy(fraction_ptr, ptr_tmp, prev_size-offset);
85 
86  if(own)
87  delete [] data;
88  else
89  own = true;
90  data = data_tmp;
91  ++count;
92 }
93 
94 void Key::remove(uint32_t idx, bool recursive) {
95  if(!data || idx >= count)
96  return;
97 
98  const uint8_t* ptr_tmp = data;
99  if(!own) {
100  own = true;
101  ptr_tmp = (data = _data(data));
102  }
103 
104  uint8_t* begin;
105  for(uint24_t offset = 0; offset < count; ++offset) {
106  begin = const_cast<uint8_t*>(ptr_tmp);
107  ptr_tmp += Serialization::decode_vi24(&ptr_tmp);
108  if(offset < idx)
109  continue;
110 
111  if(recursive) {
112  count = offset;
113  size = begin-data;
114  } else if(--count) {
115  memmove(begin, ptr_tmp, size-(ptr_tmp-data));
116  size -= ptr_tmp-begin;
117  }
118 
119  ptr_tmp = data;
120  if(count) {
121  data = _data(ptr_tmp);
122  } else {
123  data = nullptr;
124  size = 0;
125  }
126  delete [] ptr_tmp;
127  break;
128  }
129 }
130 
131 void Key::get(uint32_t idx, const char** fraction, uint32_t* length) const {
132  if(data && idx < count) {
133  const uint8_t* ptr = data;
134  for(uint32_t len; ; --idx, ptr += len) {
135  len = Serialization::decode_vi24(&ptr);
136  if(!idx) {
137  *fraction = reinterpret_cast<const char*>(ptr);
138  *length = len;
139  return;
140  }
141  }
142  }
143  *fraction = nullptr;
144  *length = 0;
145 }
146 
147 void Key::display_details(std::ostream& out, bool pretty) const {
148  out << "size=" << size << " count=" << count << " fractions=";
149  display(out, pretty);
150 }
151 
152 void Key::display(std::ostream& out, bool pretty, const char* sep) const {
153  out << '[';
154  if(!count) {
155  out << ']';
156  return;
157  }
158  uint24_t len;
159  const uint8_t* ptr = data;
160  char hex[5];
161  hex[4] = 0;
162  for(uint24_t n=0; n<count; ) {
163  out << '"';
164  for(len = Serialization::decode_vi24(&ptr); len; --len, ++ptr) {
165  if(*ptr == '"')
166  out << '\\';
167  if(!pretty || (31 < *ptr && *ptr < 127)) {
168  out << *ptr;
169  } else {
170  sprintf(hex, "0x%X", *ptr);
171  out << hex;
172  }
173  }
174  out << '"';
175  if(++n < count)
176  out << sep;
177  }
178  out << ']';
179 
180 }
181 
182 void Key::print(std::ostream& out) const {
183  out << "Key(";
184  if(size)
185  display_details(out, true);
186  out << ')';
187 }
188 
189 
190 
191 }}}
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::insert
SWC_CAN_INLINE void insert(uint32_t idx, const std::string &fraction)
Definition: CellKey.h:150
SWC::DB::Cell::Key
Definition: CellKey.h:24
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::data
uint8_t * data
Definition: CellKey.h:257
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::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
SWC::DB::Cell::Key::copy
void copy(const Key &other)
Definition: CellKey.h:314
SWC::Comm::Protocol::FsBroker::Handler::length
void length(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Length.h:17
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::Cell::Key::_data
SWC_CAN_INLINE uint8_t * _data(const uint8_t *ptr)
Definition: CellKey.h:262
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::display
void display(std::ostream &out, bool pretty=true, const char *sep=",") const
Definition: CellKey.cc:152
CellKey.h