SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
SpecsIntervalUpdate.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_SpecsIntervalUpdate_h
8 #define swcdb_db_cells_SpecsIntervalUpdate_h
9 
10 
12 
13 
14 namespace SWC { namespace DB { namespace Specs {
15 
16 
17 class IntervalUpdate final {
18  private:
19 
21  static uint8_t* alloc_value(uint32_t len) {
22  return len ? new uint8_t[len] : nullptr;
23  }
24 
26  static uint8_t* copy_value(const uint8_t* v, uint32_t len) {
27  return len
28  ? static_cast<uint8_t*>(memcpy(new uint8_t[len], v, len))
29  : nullptr;
30  }
31 
32  public:
33 
34  typedef std::unique_ptr<IntervalUpdate> Ptr;
35 
36  template<typename... ArgsT>
38  static Ptr make(ArgsT&&... args) {
39  return Ptr(new IntervalUpdate(std::forward<ArgsT>(args)...));
40  }
41 
43  int64_t a_timestamp=DB::Cells::TIMESTAMP_NULL) noexcept
44  : operation(),
45  encoder(a_encoder), timestamp(a_timestamp),
46  value(nullptr), vlen(0) {
47  }
48 
49  IntervalUpdate(Types::Encoder a_encoder, uint32_t a_vlen,
50  int64_t a_timestamp=DB::Cells::TIMESTAMP_NULL)
51  : operation(),
52  encoder(a_encoder), timestamp(a_timestamp),
53  value(alloc_value(a_vlen)), vlen(a_vlen) {
54  }
55 
57  uint8_t* a_value, uint32_t a_vlen,
58  int64_t a_timestamp, const UpdateOP& op,
59  bool cp)
60  : operation(op),
61  encoder(a_encoder), timestamp(a_timestamp),
62  value(cp ? copy_value(a_value, a_vlen) : a_value),
63  vlen(a_vlen) {
64  }
65 
67  const std::string& v,
68  int64_t a_timestamp, const UpdateOP& op)
69  : operation(op),
70  encoder(a_encoder), timestamp(a_timestamp),
72  reinterpret_cast<uint8_t*>(const_cast<char*>(v.c_str())),
73  v.size()
74  )),
75  vlen(v.size()) {
76  }
77 
79  IntervalUpdate(const uint8_t** bufp, size_t* remainp)
80  : operation(bufp, remainp),
81  encoder(Types::Encoder(Serialization::decode_i8(bufp, remainp))),
82  timestamp(Serialization::decode_vi64(bufp, remainp)),
83  value(), vlen() {
84  size_t len;
85  const uint8_t* ptr = Serialization::decode_bytes(bufp, remainp, &len);
86  vlen = len;
87  value = copy_value(ptr, vlen);
88  }
89 
91  : operation(other.operation),
92  encoder(other.encoder),
93  timestamp(other.timestamp),
94  value(copy_value(other.value, other.vlen)),
95  vlen(other.vlen) {
96  }
97 
98  IntervalUpdate(IntervalUpdate&& other) noexcept
99  : operation(other.operation),
100  encoder(other.encoder),
101  timestamp(other.timestamp),
102  value(other.value), vlen(other.vlen) {
103  other.value = nullptr;
104  other.vlen = 0;
105  }
106 
108  : operation(), encoder(),
109  timestamp(cell.get_timestamp()),
110  value(), vlen() {
111  StaticBuffer _v;
112  encoder = cell.get_value(_v, true);
113  value = _v.base;
114  vlen = _v.size;
115  _v.own = false;
116  }
117 
119  : operation(), encoder(),
120  timestamp(cell.get_timestamp()),
121  value(), vlen() {
122  StaticBuffer _v;
123  encoder = cell.get_value(_v, false);
124  value = _v.base;
125  vlen = _v.size;
126  if(_v.own) {
127  cell.free();
128  _v.own = false;
129  } else {
130  cell.value = nullptr;
131  cell.vlen = 0;
132  }
133  }
134 
135  ~IntervalUpdate() noexcept {
136  _free();
137  }
138 
140  void _free() noexcept {
141  delete [] value;
142  }
143 
145  void free() noexcept {
146  if(value) { // always owns value
147  _free();
148  value = nullptr;
149  vlen = 0;
150  }
151  }
152 
154  _free();
155  operation = other.operation;
156  encoder = other.encoder;
157  timestamp = other.timestamp;
158  value = copy_value(other.value, other.vlen);
159  vlen = other.vlen;
160  return *this;
161  }
162 
164  _free();
165  operation = other.operation;
166  encoder = other.encoder;
167  timestamp = other.timestamp;
168  value = other.value;
169  vlen = other.vlen;
170  other.value = nullptr;
171  other.vlen = 0;
172  other.encoder = Types::Encoder::DEFAULT;
173  return *this;
174  }
175 
177  _free();
178  operation = UpdateOP();
179  timestamp = cell.get_timestamp();
180  StaticBuffer _v;
181  encoder = cell.get_value(_v, true);
182  value = _v.base;
183  vlen = _v.size;
184  _v.own = false;
185  return *this;
186  }
187 
189  _free();
190  operation = UpdateOP();
191  timestamp = cell.get_timestamp();
192  StaticBuffer _v;
193  encoder = cell.get_value(_v, false);
194  value = _v.base;
195  vlen = _v.size;
196  if(_v.own) {
197  cell.free();
198  _v.own = false;
199  } else {
200  cell.value = nullptr;
201  cell.vlen = 0;
202  }
203  return *this;
204  }
205 
207  bool equal(const IntervalUpdate& other) const noexcept {
208  return encoder == other.encoder &&
209  timestamp == other.timestamp &&
210  vlen == other.vlen &&
211  operation.equal(other.operation) &&
212  Condition::mem_eq(value, other.value, vlen);
213  }
214 
215  void set(Types::Encoder a_encoder, uint8_t* a_value, uint32_t a_vlen,
216  int64_t a_timestamp=DB::Cells::TIMESTAMP_NULL, bool cp=false) {
217  _free();
218  operation = UpdateOP();
219  encoder = a_encoder;
220  timestamp = a_timestamp;
221  value = cp ? copy_value(a_value, a_vlen) : a_value;
222  vlen = a_vlen;
223  }
224 
226  void set(const UpdateOP& op) noexcept {
227  operation = op;
228  }
229 
231  size_t encoded_length() const noexcept {
232  return operation.encoded_length() +
233  1 +
236  }
237 
239  void encode(uint8_t** bufp) const {
240  operation.encode(bufp);
241  Serialization::encode_i8(bufp, uint8_t(encoder));
244  }
245 
246  void print(std::ostream& out) const {
247  out << "Value(";
248  if(vlen)
249  display(out, true);
250  out << ')';
251  }
252 
253  void display(std::ostream& out, bool pretty) const {
254  out << "ts=" << timestamp;
255  operation.print(out << " op=(");
256  out << ") len=" << vlen;
257  if(vlen) {
258  out << ' ' << '"';
259  char hex[5];
260  hex[4] = '\0';
261  const uint8_t* end = value + vlen;
262  for(const uint8_t* ptr = value; ptr < end; ++ptr) {
263  if(*ptr == '"')
264  out << '\\';
265  if(!pretty || (31 < *ptr && *ptr < 127)) {
266  out << *ptr;
267  } else {
268  sprintf(hex, "0x%X", *ptr);
269  out << hex;
270  }
271  }
272  out << '"';
273  }
274  out << " encoder=" << Core::Encoder::to_string(encoder);
275  }
276 
279  int64_t timestamp;
280  uint8_t* value;
281  uint32_t vlen;
282 };
283 
284 
285 }}}
286 
287 #endif // swcdb_db_cells_SpecsIntervalUpdate_h
SWC::DB::Specs::IntervalUpdate::copy_value
static SWC_CAN_INLINE uint8_t * copy_value(const uint8_t *v, uint32_t len)
Definition: SpecsIntervalUpdate.h:26
SWC::DB::Specs::UpdateOP::equal
SWC_CAN_INLINE bool equal(const UpdateOP &other) const noexcept
Definition: SpecsUpdateOp.h:90
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::Specs::IntervalUpdate::IntervalUpdate
SWC_CAN_INLINE IntervalUpdate(const uint8_t **bufp, size_t *remainp)
Definition: SpecsIntervalUpdate.h:79
SWC::Serialization::encoded_length_bytes
constexpr SWC_CAN_INLINE size_t encoded_length_bytes(size_t len) noexcept
Definition: Serialization.h:537
SWC::DB::Specs::IntervalUpdate::alloc_value
static SWC_CAN_INLINE uint8_t * alloc_value(uint32_t len)
Definition: SpecsIntervalUpdate.h:21
SWC::Core::Encoder::Type
Type
Definition: Encoder.h:28
SWC::Serialization::decode_bytes
constexpr SWC_CAN_INLINE const uint8_t * decode_bytes(const uint8_t **bufp, size_t *remainp, size_t *lenp)
Definition: Serialization.h:548
SWC::DB::Specs::IntervalUpdate::IntervalUpdate
IntervalUpdate(Types::Encoder a_encoder, uint8_t *a_value, uint32_t a_vlen, int64_t a_timestamp, const UpdateOP &op, bool cp)
Definition: SpecsIntervalUpdate.h:56
SWC::DB::Specs::IntervalUpdate::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsIntervalUpdate.h:239
SWC::DB::Cells::Cell
Definition: Cell.h:92
SWC::DB::Specs::IntervalUpdate::IntervalUpdate
IntervalUpdate(const IntervalUpdate &other)
Definition: SpecsIntervalUpdate.h:90
SWC::Serialization::encode_i8
constexpr SWC_CAN_INLINE void encode_i8(uint8_t **bufp, uint8_t val) noexcept
Definition: Serialization.h:85
SWC::Core::Encoder::Type::DEFAULT
@ DEFAULT
SWC::DB::Specs::UpdateOP::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: SpecsUpdateOp.h:125
SWC::DB::Specs::IntervalUpdate::IntervalUpdate
IntervalUpdate(DB::Cells::Cell &&cell)
Definition: SpecsIntervalUpdate.h:118
SWC::Serialization::encode_bytes
SWC_CAN_INLINE void encode_bytes(uint8_t **bufp, const void *data, size_t len)
Definition: Serialization.h:542
SWC::DB::Specs::IntervalUpdate::equal
SWC_CAN_INLINE bool equal(const IntervalUpdate &other) const noexcept
Definition: SpecsIntervalUpdate.h:207
SWC::DB::Specs::IntervalUpdate::operator=
IntervalUpdate & operator=(const DB::Cells::Cell &cell)
Definition: SpecsIntervalUpdate.h:176
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::DB::Specs::IntervalUpdate
Definition: SpecsIntervalUpdate.h:17
SWC::DB::Specs::IntervalUpdate::~IntervalUpdate
~IntervalUpdate() noexcept
Definition: SpecsIntervalUpdate.h:135
SWC::DB::Specs::IntervalUpdate::value
uint8_t * value
Definition: SpecsIntervalUpdate.h:280
SWC::Core::Buffer::base
value_type * base
Definition: Buffer.h:131
SWC::DB::Specs::IntervalUpdate::operator=
IntervalUpdate & operator=(IntervalUpdate &&other) noexcept
Definition: SpecsIntervalUpdate.h:163
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Specs::IntervalUpdate::operation
UpdateOP operation
Definition: SpecsIntervalUpdate.h:277
SWC::Core::Buffer
Definition: Buffer.h:18
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
SWC::DB::Specs::IntervalUpdate::print
void print(std::ostream &out) const
Definition: SpecsIntervalUpdate.h:246
SWC::Core::Buffer::size
size_t size
Definition: Buffer.h:130
SpecsUpdateOp.h
SWC::DB::Cells::Cell::get_value
Types::Encoder get_value(StaticBuffer &v, bool owner) const
Definition: Cell.cc:77
SWC::DB::Specs::IntervalUpdate::IntervalUpdate
IntervalUpdate(IntervalUpdate &&other) noexcept
Definition: SpecsIntervalUpdate.h:98
SWC::DB::Specs::IntervalUpdate::operator=
IntervalUpdate & operator=(const IntervalUpdate &other)
Definition: SpecsIntervalUpdate.h:153
SWC::Serialization::encoded_length_vi64
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi64(uint64_t val) noexcept
Definition: Serialization.h:272
SWC::DB::Cells::TIMESTAMP_NULL
constexpr const int64_t TIMESTAMP_NULL
Definition: Cell.h:72
SWC::DB::Specs::IntervalUpdate::IntervalUpdate
IntervalUpdate(Types::Encoder a_encoder, int64_t a_timestamp=DB::Cells::TIMESTAMP_NULL) noexcept
Definition: SpecsIntervalUpdate.h:42
SWC::DB::Specs::IntervalUpdate::IntervalUpdate
IntervalUpdate(const DB::Cells::Cell &cell)
Definition: SpecsIntervalUpdate.h:107
SWC::DB::Types::Encoder
Core::Encoder::Type Encoder
Definition: Encoder.h:15
SWC::DB::Cells::Cell::get_timestamp
constexpr SWC_CAN_INLINE int64_t get_timestamp() const noexcept
Definition: Cell.h:317
SWC::DB::Specs::IntervalUpdate::free
SWC_CAN_INLINE void free() noexcept
Definition: SpecsIntervalUpdate.h:145
SWC::DB::Specs::IntervalUpdate::timestamp
int64_t timestamp
Definition: SpecsIntervalUpdate.h:279
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::UpdateOP::encoded_length
SWC_CAN_INLINE size_t encoded_length() const noexcept
Definition: SpecsUpdateOp.h:120
SWC::Core::Encoder::to_string
const char *SWC_CONST_FUNC to_string(Type typ) noexcept
Definition: Encoder.cc:31
SWC::DB::Specs::IntervalUpdate::encoder
Types::Encoder encoder
Definition: SpecsIntervalUpdate.h:278
SWC::DB::Specs::IntervalUpdate::encoded_length
SWC_CAN_INLINE size_t encoded_length() const noexcept
Definition: SpecsIntervalUpdate.h:231
SWC::DB::Specs::IntervalUpdate::IntervalUpdate
IntervalUpdate(Types::Encoder a_encoder, const std::string &v, int64_t a_timestamp, const UpdateOP &op)
Definition: SpecsIntervalUpdate.h:66
SWC::Serialization::encode_vi64
constexpr SWC_CAN_INLINE void encode_vi64(uint8_t **bufp, uint64_t val)
Definition: Serialization.h:286
SWC::DB::Specs::IntervalUpdate::_free
SWC_CAN_INLINE void _free() noexcept
Definition: SpecsIntervalUpdate.h:140
SWC::DB::Specs::UpdateOP::print
SWC_CAN_INLINE void print(std::ostream &out) const
Definition: SpecsUpdateOp.h:132
SWC::DB::Specs::IntervalUpdate::display
void display(std::ostream &out, bool pretty) const
Definition: SpecsIntervalUpdate.h:253
SWC::DB::Specs::IntervalUpdate::set
void set(Types::Encoder a_encoder, uint8_t *a_value, uint32_t a_vlen, int64_t a_timestamp=DB::Cells::TIMESTAMP_NULL, bool cp=false)
Definition: SpecsIntervalUpdate.h:215
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::IntervalUpdate::IntervalUpdate
IntervalUpdate(Types::Encoder a_encoder, uint32_t a_vlen, int64_t a_timestamp=DB::Cells::TIMESTAMP_NULL)
Definition: SpecsIntervalUpdate.h:49
SWC::DB::Specs::IntervalUpdate::vlen
uint32_t vlen
Definition: SpecsIntervalUpdate.h:281
SWC::DB::Specs::UpdateOP
Definition: SpecsUpdateOp.h:18
SWC::DB::Specs::IntervalUpdate::make
static SWC_CAN_INLINE Ptr make(ArgsT &&... args)
Definition: SpecsIntervalUpdate.h:38
SWC::Core::Buffer::own
bool own
Definition: Buffer.h:129
SWC::DB::Specs::IntervalUpdate::set
SWC_CAN_INLINE void set(const UpdateOP &op) noexcept
Definition: SpecsIntervalUpdate.h:226
SWC::DB::Specs::IntervalUpdate::Ptr
std::unique_ptr< IntervalUpdate > Ptr
Definition: SpecsIntervalUpdate.h:34
SWC::DB::Specs::IntervalUpdate::operator=
IntervalUpdate & operator=(DB::Cells::Cell &&cell) noexcept
Definition: SpecsIntervalUpdate.h:188