SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Result.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_Result_h
8 #define swcdb_db_Cells_Result_h
9 
10 #include "swcdb/db/Cells/Cell.h"
12 
13 namespace SWC { namespace DB { namespace Cells {
14 
15 
16 class Result final : private std::vector<Cell*> {
17  public:
18 
19  using Vec = std::vector<Cell*>;
20  using Vec::empty;
21  using Vec::capacity;
22  using Vec::reserve;
23  using Vec::size;
24  using Vec::back;
25  using Vec::front;
26  using Vec::begin;
27  using Vec::end;
28  using Vec::cbegin;
29  using Vec::cend;
30  using Vec::operator[];
31 
32  size_t bytes;
33  uint64_t ttl;
34 
36  explicit Result(const uint64_t ttl_ns=0) noexcept
37  : bytes(0), ttl(ttl_ns) {
38  }
39 
41  explicit Result(Result&& other) noexcept
42  : Vec(std::move(other)),
43  bytes(other.bytes), ttl(other.ttl) {
44  other.bytes = 0;
45  }
46 
47  Result(const Result& other) = delete;
48 
49  Result& operator=(const Result& other) = delete;
50 
51  ~Result() noexcept {
52  for(auto cell : *this)
53  delete cell;
54  }
55 
56  void free() noexcept {
57  for(auto cell : *this)
58  delete cell;
59  clear();
60  bytes = 0;
61  }
62 
63  constexpr SWC_CAN_INLINE
64  size_t size_bytes() const noexcept {
65  return bytes;
66  }
67 
68  void take(Result& other);
69 
70  void add(const Cell& cell, bool no_value=false);
71 
72  size_t add(const uint8_t* ptr, size_t remain);
73 
74 
75  Cell* takeout_begin(size_t idx);
76 
77  Cell* takeout_end(size_t idx);
78 
79 
80  void write(DynamicBuffer& cells) const;
81 
82  void write_and_free(DynamicBuffer& cells, uint32_t& cell_count,
83  Interval& intval, uint32_t threshold,
84  uint32_t max_cells);
85 
86 
87  void print(std::ostream& out, Types::Column col_type=Types::Column::PLAIN,
88  bool with_cells=false) const;
89 
90 };
91 
92 
93 
95 void Result::take(Result& other) {
96  if(empty()) {
97  Vec::operator=(std::move(other));
98  bytes = other.bytes;
99  } else {
100  reserve(size() + other.size());
101  insert(cend(), other.cbegin(), other.cend());
102  other.clear();
103  bytes += other.bytes;
104  }
105  other.bytes = 0;
106 }
107 
109 void Result::add(const Cell& cell, bool no_value) {
110  Cell* adding = new Cell(cell, no_value);
111  push_back(adding);
112  bytes += adding->encoded_length();
113 }
114 
116 size_t Result::add(const uint8_t* ptr, size_t remain) {
117  size_t count = 0;
118  bytes += remain;
119  while(remain) {
120  push_back(new Cell(&ptr, &remain, true));
121  ++count;
122  }
123  return count;
124 }
125 
126 
129  auto it = cbegin() + idx;
130  Cell* cell = *it;
131  erase(it);
132  bytes -= cell->encoded_length();
133  return cell;
134 }
135 
137 Cell* Result::takeout_end(size_t idx) {
138  auto it = cend() - idx;
139  Cell* cell = *it;
140  erase(it);
141  bytes -= cell->encoded_length();
142  return cell;
143 }
144 
145 
146 
147 }}}
148 
149 #ifdef SWC_IMPL_SOURCE
150 #include "swcdb/db/Cells/Result.cc"
151 #endif
152 
153 #endif // swcdb_db_Cells_Result_h
Cell.h
SWC::DB::Cells::Result::bytes
size_t bytes
Definition: Result.h:32
SWC::DB::Types::Column
Column
Definition: Column.h:18
SWC::DB::Cells::Result::Result
SWC_CAN_INLINE Result(Result &&other) noexcept
Definition: Result.h:41
SWC::DB::Cells::Result::takeout_end
Cell * takeout_end(size_t idx)
Definition: Result.h:137
SWC::DB::Cells::Cell
Definition: Cell.h:92
SWC::DB::Cells::Result::ttl
uint64_t ttl
Definition: Result.h:33
SWC::DB::Cells::Result
Definition: Result.h:16
Result.cc
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Cells::Result::Result
Result(const Result &other)=delete
SWC::Core::BufferDyn< StaticBuffer >
SWC::DB::Cells::Result::take
void take(Result &other)
Definition: Result.h:95
SWC::DB::Cells::Result::~Result
~Result() noexcept
Definition: Result.h:51
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
SWC::DB::Cells::Result::takeout_begin
Cell * takeout_begin(size_t idx)
Definition: Result.h:128
SWC::DB::Cells::Result::free
void free() noexcept
Definition: Result.h:56
SWC::DB::Cells::Result::operator=
Result & operator=(const Result &other)=delete
SWC::DB::Cells::Result::Vec
std::vector< Cell * > Vec
Definition: Result.h:19
SWC::DB::Cells::Result::write_and_free
void write_and_free(DynamicBuffer &cells, uint32_t &cell_count, Interval &intval, uint32_t threshold, uint32_t max_cells)
Definition: Result.cc:22
SWC::DB::Cells::Cell::encoded_length
constexpr SWC_CAN_INLINE size_t encoded_length(bool no_value=false) const noexcept
Definition: Cell.h:285
SWC::DB::Cells::Result::write
void write(DynamicBuffer &cells) const
Definition: Result.cc:14
SWC::DB::Cells::Result::print
void print(std::ostream &out, Types::Column col_type=Types::Column::PLAIN, bool with_cells=false) const
Definition: Result.cc:64
SWC::DB::Cells::Result::Result
SWC_CAN_INLINE Result(const uint64_t ttl_ns=0) noexcept
Definition: Result.h:36
SWC::DB::Cells::Result::size_bytes
constexpr SWC_CAN_INLINE size_t size_bytes() const noexcept
Definition: Result.h:64
SWC::DB::Cells::Interval
Definition: Interval.h:17
Interval.h
SWC::DB::Cells::Result::add
void add(const Cell &cell, bool no_value=false)
Definition: Result.h:109