SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Schemas.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 
7 #include "swcdb/core/Exception.h"
9 
10 
11 namespace SWC { namespace DB {
12 
13 
14 uint64_t Schemas::size() {
16  return Map::size();
17 }
18 
19 void Schemas::add(int& err, const Schema::Ptr& schema) {
21  _add(err, schema);
22 }
23 
24 void Schemas::_add(int& err, const Schema::Ptr& schema) {
25  if(!emplace(schema->cid, schema).second) {
28  schema->print(SWC_LOG_OSTREAM << "Unable to add column ");
29  Error::print(SWC_LOG_OSTREAM << ", remove first ", err);
30  );
31  }
32 }
33 
36  _remove(cid);
37 }
38 
40  auto it = find(cid);
41  if(it != cend())
42  erase(it);
43 }
44 
45 void Schemas::replace(const Schema::Ptr& schema) {
47  _replace(schema);
48 }
49 
50 void Schemas::_replace(const Schema::Ptr& schema) {
51  auto res = emplace(schema->cid, schema);
52  if(!res.second)
53  res.first->second = schema;
54 }
55 
57  Core::MutexSptd::scope lock(m_mutex);
58  return _get(cid);
59 }
60 
61 Schema::Ptr Schemas::_get(cid_t cid) const noexcept {
62  auto it = find(cid);
63  return it == cend() ? nullptr : it->second;
64 }
65 
66 Schema::Ptr Schemas::get(const std::string& name) noexcept {
67  Core::MutexSptd::scope lock(m_mutex);
68  return _get(name);
69 }
70 
71 Schema::Ptr Schemas::_get(const std::string& name) const noexcept {
72  for(const auto& it : *this ) { // ? cross-map for names
73  if(Condition::str_eq(name, it.second->col_name))
74  return it.second;
75  }
76  return nullptr;
77 }
78 
79 void Schemas::all(SchemasVec& entries) {
81  entries.reserve(entries.size() + Map::size());
82  for(const auto& it : *this)
83  entries.push_back(it.second);
84 }
85 
86 
87 // local-declaration
88 namespace {
89 bool is_matching(const Schema::Tags& s_tags,
90  const Schemas::TagsPattern& tags,
91  Core::Vector<bool>& found);
92 }
93 
95  SchemasVec& entries,
96  bool no_sys) {
97  Core::Vector<bool> found;
98  if(patterns.tags.comp == Condition::SBS ||
99  patterns.tags.comp == Condition::SPS)
100  found.resize(patterns.tags.size());
101  bool match;
102 
104  for(const auto& it : *this) {
105  if(no_sys && it.second->cid <= DB::Types::SystemColumn::SYS_CID_END)
106  continue;
107  const Schema& schema = *it.second.get();
108  match = false;
109  for(auto& pattern : patterns.names) {
111  pattern.comp,
112  reinterpret_cast<const uint8_t*>(pattern.c_str()),
113  pattern.size(),
114  reinterpret_cast<const uint8_t*>(schema.col_name.c_str()),
115  schema.col_name.size()
116  );
117  if(match)
118  break;
119  }
120  if(match || is_matching(schema.tags, patterns.tags, found))
121  entries.push_back(it.second);
122  }
123 }
124 
125 // local-definition
126 namespace {
128 bool is_matching(const Schema::Tags& s_tags, const Schemas::TagsPattern& tags,
129  Core::Vector<bool>& found) {
130  switch(tags.comp) {
131  case Condition::NE: {
132  auto it = tags.cbegin();
133  auto itt = s_tags.cbegin();
134  for(; it != tags.cend() && itt != s_tags.cend(); ++it, ++itt) {
136  it->comp,
137  reinterpret_cast<const uint8_t*>(it->c_str()), it->size(),
138  reinterpret_cast<const uint8_t*>(itt->c_str()), itt->size())) {
139  return true;
140  }
141  }
142  return itt != s_tags.cend() || it != tags.cend();
143  }
144 
145  case Condition::GT:
146  case Condition::LT:
147  case Condition::GE:
148  case Condition::LE:
149  case Condition::EQ: {
150  auto it = tags.cbegin();
151  auto itt = s_tags.cbegin();
152  for(; it != tags.cend() && itt != s_tags.cend(); ++it, ++itt) {
154  it->comp,
155  reinterpret_cast<const uint8_t*>(it->c_str()), it->size(),
156  reinterpret_cast<const uint8_t*>(itt->c_str()), itt->size()))
157  break;
158  }
159  return itt != s_tags.cend()
160  ? it == tags.cend() &&
161  (tags.comp == Condition::GT || tags.comp == Condition::GE)
162  : (it == tags.cend()
163  ? (tags.comp == Condition::EQ ||
164  tags.comp == Condition::LE || tags.comp == Condition::GE)
165  : (tags.comp == Condition::LT || tags.comp == Condition::LE));
166  }
167 
168  case Condition::SBS: {
169  uint32_t sz = tags.size();
170  if(!sz)
171  return true;
172  if(sz > s_tags.size())
173  return false;
174  for(auto& f : found)
175  f = false;
176  uint32_t count = sz;
177  for(auto itt = s_tags.cbegin(); itt != s_tags.cend(); ++itt) {
178  for(uint32_t i = 0; i < sz; ++i) {
179  if(!found[i] &&
181  tags[i].comp,
182  reinterpret_cast<const uint8_t*>(tags[i].c_str()),
183  tags[i].size(),
184  reinterpret_cast<const uint8_t*>(itt->c_str()),
185  itt->size())) {
186  if(!--count)
187  return true;
188  found[i] = true;
189  break;
190  }
191  }
192  }
193  return false;
194  }
195 
196  case Condition::SPS: {
197  uint32_t sz = s_tags.size();
198  if(!sz)
199  return true;
200  if(sz > tags.size())
201  return false;
202  for(auto& f : found)
203  f = false;
204  uint32_t count = sz;
205  for(auto it = tags.cbegin(); it != tags.cend(); ++it) {
206  for(uint32_t i = 0; i < sz; ++i) {
207  if(!found[i] &&
209  it->comp,
210  reinterpret_cast<const uint8_t*>(it->c_str()),
211  it->size(),
212  reinterpret_cast<const uint8_t*>(s_tags[i].c_str()),
213  s_tags[i].size())) {
214  if(!--count)
215  return true;
216  found[i] = true;
217  break;
218  }
219  }
220  }
221  return false;
222  }
223 
224  case Condition::POSBS: {
225  auto it = tags.cbegin();
226  for(auto itt = s_tags.cbegin();
227  itt != s_tags.cend() && it != tags.cend(); ++itt) {
229  it->comp,
230  reinterpret_cast<const uint8_t*>(it->c_str()), it->size(),
231  reinterpret_cast<const uint8_t*>(itt->c_str()), itt->size()))
232  ++it;
233  }
234  return it == tags.cend();
235  }
236 
237  case Condition::FOSBS: {
238  auto it = tags.cbegin();
239  auto itt = s_tags.cbegin();
240  for(bool start = false;
241  itt != s_tags.cend() && it != tags.cend(); ++itt) {
243  it->comp,
244  reinterpret_cast<const uint8_t*>(it->c_str()), it->size(),
245  reinterpret_cast<const uint8_t*>(itt->c_str()), itt->size())) {
246  start = true;
247  ++it;
248  } else if(start) {
249  return false;
250  }
251  }
252  return it == tags.cend();
253  }
254 
255  case Condition::POSPS: {
256  auto itt = s_tags.cbegin();
257  for(auto ord = tags.cbegin();
258  itt != s_tags.cend() && ord != tags.cend(); ++itt) {
259  for(auto it = ord; ;) {
261  it->comp,
262  reinterpret_cast<const uint8_t*>(it->c_str()), it->size(),
263  reinterpret_cast<const uint8_t*>(itt->c_str()), itt->size())) {
264  ++ord;
265  break;
266  }
267  if(++it == tags.cend())
268  return false;
269  }
270  }
271  return itt == s_tags.cend();
272  }
273 
274  case Condition::FOSPS: {
275  auto itt = s_tags.cbegin();
276  bool start = false;
277  for(auto ord = tags.cbegin();
278  itt != s_tags.cend() && ord != tags.cend(); ++itt) {
279  for(auto it = ord; ;) {
281  it->comp,
282  reinterpret_cast<const uint8_t*>(it->c_str()), it->size(),
283  reinterpret_cast<const uint8_t*>(itt->c_str()), itt->size())) {
284  start = true;
285  ord = ++it;
286  break;
287  } else if(start) {
288  return false;
289  }
290  if(++it == tags.cend())
291  return false;
292  }
293  }
294  return itt == s_tags.cend();
295  }
296 
297  default:
298  break;
299  }
300  return false;
301 }
302 }
303 
304 
307  clear();
308 }
309 
310 }}
SWC::Condition::LE
@ LE
Definition: Comparators.h:33
SWC::DB::Schemas::add
void add(int &err, const Schema::Ptr &schema)
Definition: Schemas.cc:19
SWC::DB::Schemas::TagsPattern::comp
Condition::Comp comp
Definition: Schemas.h:87
SWC::Condition::POSPS
@ POSPS
Definition: Comparators.h:48
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC::Condition::FOSPS
@ FOSPS
Definition: Comparators.h:50
SWC::Condition::is_matching_extended
SWC_CAN_INLINE bool is_matching_extended(uint8_t comp, const uint8_t *p1, uint32_t p1_len, const uint8_t *p2, uint32_t p2_len)
Definition: Comparators.h:459
SWC::Core::Vector::resize
SWC_CAN_INLINE void resize(size_type sz, ArgsT &&... args)
Definition: Vector.h:308
SWC::DB::Schema::Ptr
std::shared_ptr< Schema > Ptr
Definition: Schema.h:185
SWC::Condition::GE
@ GE
Definition: Comparators.h:31
SWC_LOG_OUT
#define SWC_LOG_OUT(pr, _code_)
Definition: Logger.h:178
SWC::Condition::GT
@ GT
Definition: Comparators.h:30
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::DB::Types::SystemColumn::SYS_CID_END
const cid_t SYS_CID_END
Definition: SystemColumn.h:27
SWC::Condition::LT
@ LT
Definition: Comparators.h:34
SWC::DB::Schemas::_add
void _add(int &err, const Schema::Ptr &schema)
Definition: Schemas.cc:24
SWC::DB::Schemas::SelectorPatterns
Definition: Schemas.h:100
SWC::DB::Schemas::m_mutex
Core::MutexSptd m_mutex
Definition: Schemas.h:151
SWC::DB::Schemas::SelectorPatterns::names
NamePatterns names
Definition: Schemas.h:101
SWC::DB::Schema
Definition: Schema.h:182
SWC::Condition::str_eq
bool str_eq(const char *s1, const char *s2) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:237
SWC::DB::Schemas::size
uint64_t size()
Definition: Schemas.cc:14
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::DB::Schemas::get
Schema::Ptr get(cid_t cid) noexcept
Definition: Schemas.cc:56
Exception.h
SWC::DB::Schemas::all
void all(SchemasVec &entries)
Definition: Schemas.cc:79
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Schemas::remove
void remove(cid_t cid)
Definition: Schemas.cc:34
SWC::DB::Schemas::replace
void replace(const Schema::Ptr &schema)
Definition: Schemas.cc:45
SWC::DB::Schemas::TagsPattern
Definition: Schemas.h:86
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
SWC::Condition::EQ
@ EQ
Definition: Comparators.h:32
SWC::Condition::NE
@ NE
Definition: Comparators.h:35
SWC::DB::Schemas::_remove
void _remove(cid_t cid)
Definition: Schemas.cc:39
SWC::DB::Schema::tags
Tags tags
Definition: Schema.h:235
SWC::Condition::is_matching
SWC_CAN_INLINE bool is_matching(bool volumetric, uint8_t comp, const uint8_t *p1, uint32_t p1_len, const uint8_t *p2, uint32_t p2_len)
Definition: Comparators.h:439
SWC::DB::Schemas::reset
void reset()
Definition: Schemas.cc:305
SWC::cid_t
uint64_t cid_t
Definition: Identifiers.h:16
SWC::Condition::SPS
@ SPS
Definition: Comparators.h:46
SWC::DB::Schemas::_replace
void _replace(const Schema::Ptr &schema)
Definition: Schemas.cc:50
SWC::Condition::POSBS
@ POSBS
Definition: Comparators.h:47
SWC::Core::Vector< Schema::Ptr >
SWC::DB::Schemas::SelectorPatterns::tags
TagsPattern tags
Definition: Schemas.h:102
SWC::DB::Schema::col_name
std::string col_name
Definition: Schema.h:234
SWC::Error::COLUMN_SCHEMA_NAME_EXISTS
@ COLUMN_SCHEMA_NAME_EXISTS
Definition: Error.h:104
Schemas.h
SWC::Core::Vector::cend
constexpr SWC_CAN_INLINE const_iterator cend() const noexcept
Definition: Vector.h:232
SWC::DB::Schemas::_get
Schema::Ptr _get(cid_t cid) const noexcept
Definition: Schemas.cc:61
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Condition::SBS
@ SBS
Definition: Comparators.h:45
SWC::Condition::FOSBS
@ FOSBS
Definition: Comparators.h:49
SWC::DB::Schemas::matching
void matching(const SelectorPatterns &patterns, SchemasVec &entries, bool no_sys=true)
Definition: Schemas.cc:94
SWC::Core::Vector::push_back
SWC_CAN_INLINE void push_back(ArgsT &&... args)
Definition: Vector.h:331
SWC::Error::print
void print(std::ostream &out, int err)
Definition: Error.cc:191
SWC::Core::Vector::cbegin
constexpr SWC_CAN_INLINE const_iterator cbegin() const noexcept
Definition: Vector.h:216
SWC::Core::Vector::size
constexpr SWC_CAN_INLINE size_type size() const noexcept
Definition: Vector.h:189
SWC::Core::Vector::reserve
SWC_CAN_INLINE void reserve(size_type cap)
Definition: Vector.h:288