SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
CellStoreReaders.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 Ranger { namespace CellStore {
11 
12 
14 void Readers::load(int& err) {
15  if(empty()) {
17  return;
18  }
19 }
20 
21 void Readers::expand(DB::Cells::Interval& intval) const {
22  for(auto cs : *this)
23  intval.expand(cs->interval);
24 }
25 
27  for(auto cs : *this) {
28  intval.expand(cs->interval);
29  intval.align(cs->interval);
30  }
31 }
32 
33 size_t Readers::size_bytes(bool only_loaded) const {
34  size_t sz = 0;
35  for(auto cs : *this)
36  sz += cs->size_bytes(only_loaded);
37  return sz;
38 }
39 
41 uint32_t Readers::get_cell_revs() const {
42  return front()->cell_revs;
43 }
44 
45 int64_t Readers::get_ts_earliest() const {
46  int64_t ts = DB::Cells::TIMESTAMP_AUTO;
47  for(auto cs : *this)
48  if(cs->interval.ts_earliest.comp != Condition::NONE &&
49  (ts == DB::Cells::TIMESTAMP_AUTO || cs->interval.ts_earliest.value < ts))
50  ts = cs->interval.ts_earliest.value;
51  return ts;
52 }
53 
55 size_t Readers::blocks_count() const {
56  size_t sz = 0;
57  for(auto cs : *this)
58  sz += cs->blocks_count();
59  return sz;
60 }
61 
63 size_t Readers::release(size_t bytes) {
64  size_t released = 0;
65  for(auto cs : *this) {
66  released += cs->release(bytes - released);
67  if(released >= bytes)
68  break;
69  }
70  return released;
71 }
72 
73 bool Readers::processing() const noexcept {
74  for(auto cs : *this)
75  if(cs->processing())
76  return true;
77  return false;
78 }
79 
80 void Readers::remove(int &err) {
81  _close();
82  for(auto cs : *this)
83  cs->remove(err);
84  _free();
85  range = nullptr;
86 }
87 
89  _close();
90  _free();
91  range = nullptr;
92 }
93 
95  _close();
96  _free();
97 }
98 
101  for(auto cs : *this) {
102  if(loader->block->is_consist(cs->interval)) {
103  cs->load_cells(loader);
104  } else if(!cs->interval.key_end.empty() &&
105  !loader->block->is_in_end(cs->interval.key_end)) {
106  break;
107  }
108  }
109 }
110 
112 void Readers::get_blocks(int& err, Read::Blocks& to) const {
113  to.reserve(blocks_count());
114  for(auto cs : *this) {
115  cs->get_blocks(err, to);
116  if(err)
117  break;
118  }
119 }
120 
122 void Readers::get_prev_key_end(uint32_t idx, DB::Cell::Key& key) const {
123  key.copy((*(cbegin()+idx))->prev_key_end);
124 }
125 
128  return key.copy(back()->key_end);
129 }
130 
131 bool Readers::need_compaction(size_t cs_max, size_t cs_sz,
132  size_t blk_size) const {
133  if(cs_max > 1 && size() > cs_max) {
134  if(get_cell_revs() == 1)
135  return true;
136  for(auto it = cbegin() + 1; it < cend(); ++it) {
137  if(!(*it)->prev_key_end.equal((*it)->interval.key_begin))
138  return true;
139  }
140  }
141  size_t sz;
142  size_t sz_enc;
143  for(auto cs : *this) {
144  if(!(sz_enc = cs->size_bytes_enc(false)) || !(sz = cs->size_bytes(false)))
145  continue;
146  if(sz_enc > cs_sz || sz/cs->blocks_count() > blk_size)
147  return true;
148  }
149  return false;
150 }
151 
152 uint32_t Readers::encoded_length() const {
153  uint32_t sz = Serialization::encoded_length_vi32(size());
154  for(auto cs : *this) {
155  sz += Serialization::encoded_length_vi32(cs->csid);
156  }
157  return sz;
158 }
159 
160 void Readers::encode(uint8_t** ptr) const {
162  for(auto cs : *this) {
163  Serialization::encode_vi32(ptr, cs->csid);
164  }
165 }
166 
167 void Readers::decode(int &err, const uint8_t** ptr, size_t* remain) {
168  _close();
169  _free();
170  uint32_t len = Serialization::decode_vi32(ptr, remain);
171  Vec::reserve(len);
172  for(size_t i=0; i<len; ++i) {
173  push_back(
174  Read::make(err, Serialization::decode_vi32(ptr, remain), range));
175  //if(err == Error::FS_PATH_NOT_FOUND) ?without cs
176  }
177 }
178 
180 void Readers::load_from_path(int &err) {
181  const auto& fs_if = Env::FsInterface::interface();
182 
183  const std::string& cs_path = range->get_path(DB::RangeBase::CELLSTORES_DIR);
184  const std::string& bak_path = range->get_path(Range::CELLSTORES_BAK_DIR);
185 
186  if(fs_if->exists(err, bak_path)) {
187  const std::string& rcvd_path = range->get_path(Range::CELLSTORES_RCVD_DIR);
189  "Backup dir='%s' has remained - Recovering from uncomplete Compaction, "
190  "backing-up current to='%s'", bak_path.c_str(), rcvd_path.c_str());
191  if(fs_if->exists(err, cs_path))
192  fs_if->rename(err, cs_path, rcvd_path);
193  if(err)
194  return;
195  fs_if->rename(err, bak_path, cs_path);
196  }
197  if(err)
198  return;
199 
200  FS::DirentList dirs;
201  fs_if->readdir(err, cs_path, dirs);
202  if(err)
203  return;
204 
205  FS::IdEntries_t entries;
206  entries.reserve(dirs.size());
207  for(auto& entry : dirs) {
208  if(entry.name.find(".cs", entry.name.length()-3) != std::string::npos) {
209  auto idn = entry.name.substr(0, entry.name.length()-3);
210  entries.push_back(strtoull(idn.c_str(), nullptr, 0));
211  }
212  }
213 
214  _close();
215  _free();
216 
217  std::sort(entries.begin(), entries.end());
218  Vec::reserve(entries.size());
219  for(csid_t csid : entries) {
220  push_back(Read::make(err, csid, range));
221  }
222 }
223 
224 void Readers::replace(int &err, Writers& w_cellstores) {
225  const auto& fs_if = Env::FsInterface::interface();
226 
227  _close();
228 
229  const std::string& cs_path = range->get_path(DB::RangeBase::CELLSTORES_DIR);
230  const std::string& bak_path = range->get_path(Range::CELLSTORES_BAK_DIR);
231 
232  fs_if->rename(err, cs_path, bak_path);
233  if(err)
234  return;
235 
236  fs_if->rename(err, range->get_path(Range::CELLSTORES_TMP_DIR), cs_path);
237 
238  if(!err) {
239  Vec cellstores;
240  cellstores.reserve(w_cellstores.size());
241  for(auto cs : w_cellstores) {
242  cellstores.push_back(Read::make(err, cs->csid, range, true));
243  if(err)
244  break;
245  }
246  if(err) {
247  for(auto cs : cellstores)
248  delete cs;
249  } else {
250  _free();
251  Vec::operator=(std::move(cellstores));
252  }
253  }
254 
255  if(err) {
256  fs_if->rmdir(err, cs_path);
257  fs_if->rename(err, bak_path, cs_path);
258  return;
259  }
260 
261  fs_if->rmdir(err, bak_path);
262 
263  err = Error::OK;
264 
265 }
266 
267 void Readers::move_from(int &err, Readers::Vec& mv_css) {
268  const auto& fs = Env::FsInterface::interface();
269 
270  Vec moved;
271  moved.reserve(mv_css.size());
272  int tmperr;
273  for(auto cs : mv_css) {
274  cs->close(tmperr = Error::OK);
275  fs->rename(
276  err,
277  cs->filepath(),
278  range->get_path_cs(cs->csid)
279  );
280  if(err)
281  break;
282  moved.push_back(cs);
283  }
284 
285  Vec cellstores;
286  cellstores.reserve(moved.size());
287  if(!err) {
288  for(auto cs : moved) {
289  cellstores.push_back(Read::make(err, cs->csid, range, true));
290  if(err)
291  break;
292  }
293  }
294 
295  if(err) {
296  for(auto cs : cellstores)
297  delete cs;
298 
299  for(auto& cs : moved) {
300  fs->rename(
301  tmperr = Error::OK,
302  range->get_path_cs(cs->csid),
303  cs->filepath()
304  );
305  }
306  mv_css.clear();
307  } else {
308  Vec::operator=(std::move(cellstores));
309  }
310 }
311 
312 void Readers::print(std::ostream& out, bool minimal) const {
313  out << "CellStores(count=" << size()
314  << " cellstores=[";
315  for(auto cs : *this) {
316  cs->print(out, minimal);
317  out << ", ";
318  }
319  out << "] processing=" << processing()
320  << " used/actual=" << size_bytes(true) << '/' << size_bytes()
321  << ')';
322 }
323 
324 
326  for(auto cs : *this) {
327  delete cs;
328  }
329  Vec::clear();
331 }
332 
334  int err = Error::OK;
335  for(auto cs : *this)
336  cs->close(err);
337 }
338 
339 
340 }}}
SWC::Ranger::CellStore::Readers::encoded_length
uint32_t SWC_PURE_FUNC encoded_length() const
Definition: CellStoreReaders.cc:152
SWC::Core::Vector< Read::Ptr >::front
constexpr SWC_CAN_INLINE reference front() noexcept
Definition: Vector.h:243
SWC::Ranger::Range::CELLSTORES_RCVD_DIR
static constexpr const char CELLSTORES_RCVD_DIR[]
Definition: Range.h:38
SWC::Ranger::CellStore::Readers::expand
void expand(DB::Cells::Interval &intval) const
Definition: CellStoreReaders.cc:21
SWC_LOGF
#define SWC_LOGF(priority, fmt,...)
Definition: Logger.h:188
SWC::Core::Vector< Read::Ptr >::clear
SWC_CAN_INLINE void clear() noexcept(_NoExceptDestructor)
Definition: Vector.h:120
SWC::Core::Vector< Read::Ptr >::operator=
SWC_CAN_INLINE Vector & operator=(Vector &&other) noexcept
Definition: Vector.h:141
SWC::DB::Cells::Interval::align
SWC_CAN_INLINE bool align(const Interval &other)
Definition: Interval.h:152
SWC::Serialization::encoded_length_vi32
constexpr SWC_CAN_INLINE uint8_t encoded_length_vi32(uint32_t val) noexcept
Definition: Serialization.h:234
SWC::Env::FsInterface::interface
static SWC_CAN_INLINE FS::Interface::Ptr & interface() noexcept
Definition: Interface.h:150
SWC::Ranger::CellStore::Readers::get_ts_earliest
int64_t SWC_PURE_FUNC get_ts_earliest() const
Definition: CellStoreReaders.cc:45
SWC::csid_t
uint32_t csid_t
Definition: Identifiers.h:19
SWC::DB::Cells::TIMESTAMP_AUTO
constexpr const int64_t TIMESTAMP_AUTO
Definition: Cell.h:73
SWC::DB::Cells::Interval::expand
void expand(const Interval &other)
Definition: Interval.cc:38
SWC::Ranger::Block::is_in_end
bool is_in_end(const DB::Cell::Key &key) const
Definition: RangeBlock.cc:106
SWC::DB::Cell::Key
Definition: CellKey.h:24
SWC::Ranger::CellStore::Readers::expand_and_align
void expand_and_align(DB::Cells::Interval &intval) const
Definition: CellStoreReaders.cc:26
SWC::Error::SERIALIZATION_INPUT_OVERRUN
@ SERIALIZATION_INPUT_OVERRUN
Definition: Error.h:61
SWC::Ranger::CellStore::Readers::need_compaction
bool SWC_PURE_FUNC need_compaction(size_t cs_max, size_t cs_sz, size_t blk_size) const
Definition: CellStoreReaders.cc:131
SWC::Ranger::CellStore::Readers::remove
void remove(int &err)
Definition: CellStoreReaders.cc:80
SWC::Ranger::CellStore::Readers::size_bytes
size_t size_bytes(bool only_loaded=false) const
Definition: CellStoreReaders.cc:33
SWC::Error::OK
@ OK
Definition: Error.h:45
SWC::Core::Vector< Read::Ptr >::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: Vector.h:168
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Core::Vector< Read::Ptr >::back
constexpr SWC_CAN_INLINE reference back() noexcept
Definition: Vector.h:254
SWC::Core::Vector::end
constexpr SWC_CAN_INLINE iterator end() noexcept
Definition: Vector.h:227
SWC::Ranger::CellStore::Readers::load
void load(int &err)
Definition: CellStoreReaders.cc:14
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Ranger::CellStore::Readers::print
void print(std::ostream &out, bool minimal=true) const
Definition: CellStoreReaders.cc:312
SWC::Ranger::Range::CELLSTORES_BAK_DIR
static constexpr const char CELLSTORES_BAK_DIR[]
Definition: Range.h:37
SWC::Ranger::CellStore::Readers::release
size_t release(size_t bytes)
Definition: CellStoreReaders.cc:63
SWC::Ranger::CellStore::Read::make
static Ptr make(int &err, const csid_t csid, const RangePtr &range, bool chk_base=false)
Definition: CellStore.cc:13
SWC::Ranger::CellStore::Readers::load_from_path
void load_from_path(int &err)
Definition: CellStoreReaders.cc:180
CellStoreReaders.h
SWC::Ranger::CellStore::Readers::range
RangePtr range
Definition: CellStoreReaders.h:32
SWC::Ranger::CellStore::Readers::unload
void unload()
Definition: CellStoreReaders.cc:88
SWC::DB::Cell::Key::copy
void copy(const Key &other)
Definition: CellKey.h:314
SWC::Ranger::CellStore::Readers::move_from
void move_from(int &err, Readers::Vec &mv_css)
Definition: CellStoreReaders.cc:267
SWC::Ranger::Range::CELLSTORES_TMP_DIR
static constexpr const char CELLSTORES_TMP_DIR[]
Definition: Range.h:39
SWC::DB::RangeBase::CELLSTORES_DIR
static constexpr const char CELLSTORES_DIR[]
Definition: RangeBase.h:21
SWC::Ranger::BlockLoader
Definition: RangeBlockLoader.h:19
SWC::Ranger::CellStore::Readers::get_blocks
void get_blocks(int &err, Read::Blocks &to) const
Definition: CellStoreReaders.cc:112
SWC::Ranger::CellStore::Readers::decode
void decode(int &err, const uint8_t **ptr, size_t *remain)
Definition: CellStoreReaders.cc:167
SWC::Comm::Resolver::sort
void sort(const Networks &nets, const EndPoints &endpoints, EndPoints &sorted)
Definition: Resolver.cc:243
SWC::Ranger::BlockLoader::block
Block::Ptr block
Definition: RangeBlockLoader.h:22
SWC::Condition::NONE
@ NONE
Definition: Comparators.h:28
SWC::Ranger::CellStore::Readers::replace
void replace(int &err, Writers &w_cellstores)
Definition: CellStoreReaders.cc:224
SWC::Core::Vector< Read::Ptr >::shrink_to_fit
SWC_CAN_INLINE void shrink_to_fit(size_type sz=0)
Definition: Vector.h:276
SWC::Core::Vector
Definition: Vector.h:14
SWC::Ranger::CellStore::Readers::get_key_end
void get_key_end(DB::Cell::Key &key) const
Definition: CellStoreReaders.cc:127
SWC::Core::Vector< Read::Ptr >::reserve
SWC_CAN_INLINE void reserve()
Definition: Vector.h:294
SWC::Core::Vector< Read::Ptr >::cend
constexpr SWC_CAN_INLINE const_iterator cend() const noexcept
Definition: Vector.h:232
SWC::Ranger::Block::is_consist
bool is_consist(const DB::Cells::Interval &intval) const
Definition: RangeBlock.cc:96
SWC::Serialization::encode_vi32
constexpr SWC_CAN_INLINE void encode_vi32(uint8_t **bufp, uint32_t val)
Definition: Serialization.h:243
SWC::Ranger::CellStore::Readers::encode
void encode(uint8_t **ptr) const
Definition: CellStoreReaders.cc:160
SWC::Ranger::CellStore::Readers::processing
bool processing() const noexcept
Definition: CellStoreReaders.cc:73
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Ranger::CellStore::Readers::get_cell_revs
uint32_t get_cell_revs() const
Definition: CellStoreReaders.cc:41
SWC::Ranger::CellStore::Readers::load_cells
void load_cells(BlockLoader *loader)
Definition: CellStoreReaders.cc:100
SWC::DB::Cells::Interval
Definition: Interval.h:17
SWC::Core::Vector< Read::Ptr >::push_back
SWC_CAN_INLINE void push_back(ArgsT &&... args)
Definition: Vector.h:331
SWC::Core::Vector< Read::Ptr >::cbegin
constexpr SWC_CAN_INLINE const_iterator cbegin() const noexcept
Definition: Vector.h:216
SWC::Ranger::CellStore::Readers::blocks_count
size_t blocks_count() const
Definition: CellStoreReaders.cc:55
SWC::Ranger::CellStore::Readers::_free
void _free()
Definition: CellStoreReaders.cc:325
SWC::Core::Vector< Read::Ptr >::size
constexpr SWC_CAN_INLINE size_type size() const noexcept
Definition: Vector.h:189
SWC::Ranger::CellStore::Readers::clear
void clear()
Definition: CellStoreReaders.cc:94
SWC::Ranger::CellStore::Readers::get_prev_key_end
void get_prev_key_end(uint32_t idx, DB::Cell::Key &key) const
Definition: CellStoreReaders.cc:122
SWC::Core::Vector::reserve
SWC_CAN_INLINE void reserve(size_type cap)
Definition: Vector.h:288
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::Vector::begin
constexpr SWC_CAN_INLINE iterator begin() noexcept
Definition: Vector.h:211
SWC::Ranger::CellStore::Readers::_close
void _close()
Definition: CellStoreReaders.cc:333