mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Schema.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstring>
5#include <functional>
6#include <iosfwd>
7#include <iterator>
8#include <list>
9#include <memory>
12#include <mutable/mutable-config.hpp>
16#include <mutable/util/ADT.hpp>
19#include <mutable/util/fn.hpp>
21#include <type_traits>
22#include <unordered_map>
23#include <utility>
24#include <vector>
25
26
27namespace m {
28
29namespace storage {
30
31// forward declarations
33
34}
35
38struct M_EXPORT Schema
39{
42 {
43 private:
45
46 public:
49
50 Identifier(ThreadSafePooledString name) : name(std::move(name)) { }
52 : prefix(std::move(prefix)), name(std::move(name))
53 {
54 if (this->prefix.has_value() and strlen(*this->prefix) == 0)
55 throw invalid_argument("prefix must not be the empty string");
56 }
57 explicit Identifier(const ast::Expr&);
58
59 static Identifier GetConstant();
60 bool is_constant() const { return operator==(GetConstant()); }
61
62 bool operator==(const Identifier &other) const {
63 return this->prefix == other.prefix and this->name == other.name;
64 }
65 bool operator!=(const Identifier &other) const { return not operator==(other); }
66
68 friend std::ostream & operator<<(std::ostream &out, const Identifier &id) {
69 if (id.prefix.has_value())
70 out << id.prefix << '.';
71 return out << id.name;
72 }
74 };
75
77 {
78 enum constraints_t : uint64_t
79 {
80 NOT_NULLABLE = 0b1U,
81 UNIQUE = 0b10U,
82 REFERENCES_UNIQUE = 0b100U,
83 IS_HIDDEN = 0b1000U,
84 };
85
87 const Type *type;
89
90 private:
91 entry_type();
92
93 public:
94 entry_type(Identifier id, const Type *type, constraints_t constraints = constraints_t{0})
95 : id(std::move(id))
96 , type(M_notnull(type))
97 , constraints(constraints)
98 { }
99
101
102 bool nullable() const { return not (NOT_NULLABLE & constraints); }
103 bool unique() const { return bool(UNIQUE & constraints); }
104 bool references_unique() const { return bool(REFERENCES_UNIQUE & constraints); }
105 };
106
107 private:
108 std::vector<entry_type> entries_;
109
110 public:
111 using iterator = decltype(entries_)::iterator;
112 using const_iterator = decltype(entries_)::const_iterator;
113
114 const std::vector<entry_type> & entries() const { return entries_; }
115
116 iterator begin() { return entries_.begin(); }
117 iterator end() { return entries_.end(); }
118 const_iterator begin() const { return entries_.cbegin(); }
119 const_iterator end() const { return entries_.cend(); }
120 const_iterator cbegin() const { return entries_.cbegin(); }
121 const_iterator cend() const { return entries_.cend(); }
122
124 std::size_t num_entries() const { return entries_.size(); }
125
126 bool empty() const { return entries_.empty(); }
127
130 auto pred = [&id](const entry_type &e) -> bool { return e.id == id; }; // match qualified
131 auto it = std::find_if(begin(), end(), pred);
132 if (it != end() and std::find_if(std::next(it), end(), pred) != end())
133 throw invalid_argument("duplicate identifier, lookup ambiguous");
134 return it;
135 }
137 const_iterator find(const Identifier &id) const { return const_cast<Schema*>(this)->find(id); }
138
140 bool has(const Identifier &id) const { return find(id) != end(); }
141
143 entry_type & at(std::size_t idx) {
144 if (idx >= entries_.size())
145 throw out_of_range("index out of bounds");
146 return entries_[idx];
147 }
149 const entry_type & at(std::size_t idx) const { return const_cast<Schema*>(this)->at(idx); }
151 entry_type & operator[](std::size_t idx) {
152 M_insist(idx < entries_.size(), "index out of bounds");
153 return entries_[idx];
154 }
156 const entry_type & operator[](std::size_t idx) const { return const_cast<Schema*>(this)->operator[](idx); }
157
160 std::pair<std::size_t, entry_type&> at(const Identifier &id) {
161 auto pos = find(id);
162 if (pos == end())
163 throw out_of_range("identifier not found");
164 return { std::distance(begin(), pos), *pos };
165 }
168 std::pair<std::size_t, const entry_type&> at(const Identifier &id) const { return const_cast<Schema*>(this)->at(id); }
170 std::pair<std::size_t, entry_type&> operator[](const Identifier &id) {
171 auto pos = find(id);
172 M_insist(pos != end(), "identifier not found");
173 return { std::distance(begin(), pos), *pos };
174 }
176 std::pair<std::size_t, const entry_type&> operator[](const Identifier &id) const {
177 return const_cast<Schema*>(this)->operator[](id);
178 }
179
181 void add(entry_type e) { entries_.emplace_back(std::move(e)); }
183 void add(Identifier id, const Type *type) { entries_.emplace_back(std::move(id), type); }
185 void add(Identifier id, const Type *type, entry_type::constraints_t constraints) {
186 entries_.emplace_back(std::move(id), type, constraints);
187 }
188
191 Schema res;
192 for (auto &e : *this) {
193 if (not res.has(e.id))
194 res.add(e.id, e.type, e.constraints);
195 }
196 return res;
197 }
198
201 Schema res;
202 for (auto &e : *this) {
203 if (not e.id.is_constant())
204 res.add(e.id, e.type, e.constraints);
205 }
206 return res;
207 }
208
211 Schema & operator+=(const Schema &other) {
212 for (auto &e : other)
213 entries_.emplace_back(e);
214 return *this;
215 }
216
220 Schema & operator|=(const Schema &other) {
221 for (auto &e : other) {
222 if (not has(e.id))
223 entries_.emplace_back(e);
224 }
225 return *this;
226 }
227
229 bool operator==(const Schema &other) const {
230 return std::all_of(this->begin(), this->end(), [&](const entry_type &p) { return other.has(p.id); }) and
231 std::all_of(other.begin(), other.end(), [&](const entry_type &p) { return this->has(p.id); });
232 }
233 bool operator!=(const Schema &other) const { return not operator==(other); }
234
236 friend std::ostream & operator<<(std::ostream &out, const Schema &schema) {
237 out << "{[";
238 for (auto it = schema.begin(), end = schema.end(); it != end; ++it) {
239 if (it != schema.begin()) out << ',';
240 out << ' ' << it->id << " :" << *it->type;
241 }
242 return out << " ]}";
243 }
245
246 void dump(std::ostream &out) const;
247 void dump() const;
248};
249
250inline Schema operator+(const Schema &left, const Schema &right)
251{
252 Schema S(left);
253 S += right;
254 return S;
255}
256
258inline Schema operator&(const Schema &left, const Schema &right)
259{
260 Schema res;
261 for (auto &e : left) {
262 auto it = right.find(e.id);
263 if (it != right.end()) {
264 if (e.type != it->type)
265 throw invalid_argument("type mismatch");
266 res.add(e.id, e.type, e.constraints | it->constraints); // merge constraints from both
267 }
268 }
269 return res;
270}
271
272inline Schema operator|(const Schema &left, const Schema &right)
273{
274 Schema res(left);
275 res |= right;
276 return res;
277}
278
279
280/*======================================================================================================================
281 * Attribute, Table, Function, Database
282 *====================================================================================================================*/
283
284struct ConcreteTable;
285struct TableDecorator;
286
288struct M_EXPORT Attribute
289{
290 friend struct ConcreteTable;
291
292 std::size_t id;
293 const Table &table;
296 bool not_nullable = false;
298 bool unique = false;
299 bool is_hidden = false;
300 const Attribute *reference = nullptr;
301
302 private:
303 explicit Attribute(std::size_t id, const Table &table, const PrimitiveType *type, ThreadSafePooledString name)
304 : id(id)
305 , table(table)
306 , type(M_notnull(type))
307 , name(std::move(name))
308 {
309 if (not type->is_vectorial())
310 throw invalid_argument("attributes must be of vectorial type");
311 }
312
313 public:
314 Attribute(const Attribute&) = delete;
315 Attribute(Attribute&&) = default;
316
319 bool is_unique() const;
320
322 bool operator==(const Attribute &other) const { return &this->table == &other.table and this->id == other.id; }
323 bool operator!=(const Attribute &other) const { return not operator==(other); }
324
326 friend std::ostream & operator<<(std::ostream &out, const Attribute &attr) {
327 return out << '`' << attr.name << "` " << *attr.type;
328 }
330
331 void dump(std::ostream &out) const;
332 void dump() const;
333};
334
336template<typename T>
337bool type_check(const Attribute &attr)
338{
339 auto ty = attr.type;
340
341 /* Boolean */
342 if constexpr (std::is_same_v<T, bool>) {
343 if (is<const Boolean>(ty))
344 return true;
345 }
346
347 /* CharacterSequence */
348 if constexpr (std::is_same_v<T, std::string>) {
349 if (auto s = cast<const CharacterSequence>(ty)) {
350 if (not s->is_varying)
351 return true;
352 }
353 }
354 if constexpr (std::is_same_v<T, const char*>) {
355 if (auto s = cast<const CharacterSequence>(ty)) {
356 if (not s->is_varying)
357 return true;
358 }
359 }
360
361 /* Numeric */
362 if constexpr (std::is_arithmetic_v<T>) {
363 if (auto n = cast<const Numeric>(ty)) {
364 switch (n->kind) {
365 case Numeric::N_Int:
366 if (std::is_integral_v<T> and sizeof(T) * 8 == ty->size())
367 return true;
368 break;
369
370 case Numeric::N_Float:
371 if (std::is_floating_point_v<T> and sizeof(T) * 8 == ty->size())
372 return true;
373 break;
374
375 case Numeric::N_Decimal:
376 if (std::is_integral_v<T> and ceil_to_pow_2(ty->size()) == 8 * sizeof(T))
377 return true;
378 break;
379 }
380 }
381 }
382
383 return false;
384}
385
387struct M_EXPORT Table
388{
389 protected:
390 using table_type = std::vector<Attribute>;
391
392 template<bool V, bool H>
394 {
395 static constexpr bool Show_Visible = V;
396 static constexpr bool Show_Hidden = H;
397
398
400 using it_type = table_type::const_iterator;
401 using difference_type = std::ptrdiff_t;
402 using pointer = const value_type*;
403 using reference = const value_type&;
404
405 private:
406 it_type it_, start_, end_;
407
408 public:
409 the_iterator() = default;
410 the_iterator(it_type start, it_type end) : it_(start), start_(start), end_(end) {
411 if constexpr (Show_Visible and not Show_Hidden)
412 while (it_ != end_ and it_->is_hidden) {
413 ++it_;
414 }
415 else if constexpr (not Show_Visible and Show_Hidden)
416 while (it_ != end_ and not it_->is_hidden) {
417 ++it_;
418 }
419 else if constexpr (not Show_Visible and not Show_Hidden)
420 it_ = end_;
421 }
422 the_iterator(it_type it, it_type start, it_type end) : it_(it), start_(start), end_(end) { }
423
425 ++it_;
426 if constexpr (Show_Visible and not Show_Hidden)
427 while (it_ != end_ and it_->is_hidden) {
428 ++it_;
429 }
430 else if constexpr (not Show_Visible and Show_Hidden)
431 while (it_ != end_ and not it_->is_hidden) {
432 ++it_;
433 }
434 return *this;
435 }
436
437 the_iterator operator++(int) { the_iterator clone = *this; operator++(); return clone; }
438
440 --it_;
441 if constexpr (Show_Visible and not Show_Hidden)
442 while (it_ != start_ and it_->is_hidden) {
443 --it_;
444 }
445 else if constexpr (not Show_Visible and Show_Hidden)
446 while (it_ != start_ and not it_->is_hidden) {
447 --it_;
448 }
449 return *this;
450 }
451
452 the_iterator operator--(int) { the_iterator clone = *this; operator--(); return clone; }
453
454 reference operator*() const { return *it_; }
455 pointer operator->() const { return it_.operator->(); }
456
457 bool operator==(const the_iterator &other) const {
458 return it_ == other.it_ and start_ == other.start_ and end_ == other.end_;
459 }
460 bool operator!=(const the_iterator &other) const { return not operator==(other); }
461
462 the_iterator & operator+=(int offset) {
463 if constexpr (Show_Visible and Show_Hidden) {
464 it_ += offset;
465 return *this;
466 }
467
468 for (size_t i = 0 ; i < offset; ++i) {
469 ++it_;
470 if constexpr (Show_Visible and not Show_Hidden)
471 while (it_ != end_ and it_->is_hidden) {
472 ++it_;
473 }
474 else if constexpr (not Show_Visible and Show_Hidden)
475 while (it_ != end_ and not it_->is_hidden) {
476 ++it_;
477 }
478 }
479 return *this;
480 }
481 the_iterator & operator-=(int offset) {
482 if constexpr (Show_Visible and Show_Hidden) {
483 it_ -= offset;
484 return *this;
485 }
486
487 for (size_t i = 0 ; i < offset; ++i) {
488 --it_;
489 if constexpr (Show_Visible and not Show_Hidden)
490 while (it_ != start_ and it_->is_hidden) {
491 --it_;
492 }
493 else if constexpr (not Show_Visible and Show_Hidden)
494 while (it_ != start_ and not it_->is_hidden) {
495 --it_;
496 }
497 }
498 return *this;
499 }
500
502 if constexpr (Show_Visible and Show_Hidden) return this->it_ - other.it_;
503 if (this->it_ - other.it_ == 0) return 0;
504
505 auto smaller_it = (this->it_ < other.it_) ? this->it_ : other.it_;
506 auto larger_it = (this->it_ < other.it_) ? other.it_ : this->it_;
507 difference_type ignored = 0;
508 for (auto i = smaller_it; i != larger_it; ++i) {
509 if constexpr (Show_Visible and not Show_Hidden) {
510 if (i != end_ and i->is_hidden) ignored++;
511 } else if constexpr (not Show_Visible and Show_Hidden) {
512 if (i != end_ and not i->is_hidden) ignored++;
513 }
514 }
515 difference_type distance = this->it_ - other.it_;
516 distance += (distance > 0) ? -ignored : ignored;
517 return distance;
518 }
519 };
520
521 public:
525
526 virtual ~Table() = default;
527
529 virtual std::size_t num_attrs() const = 0;
530 virtual std::size_t num_hidden_attrs() const = 0;
531 virtual std::size_t num_all_attrs() const = 0;
532
533 virtual iterator begin() const = 0;
534 virtual iterator end() const = 0;
535 virtual iterator cbegin() const = 0;
536 virtual iterator cend() const = 0;
537
538 virtual hidden_iterator begin_hidden() const = 0;
539 virtual hidden_iterator end_hidden() const = 0;
540 virtual hidden_iterator cbegin_hidden() const = 0;
541 virtual hidden_iterator cend_hidden() const = 0;
542
543 virtual all_iterator begin_all() const = 0;
544 virtual all_iterator end_all() const = 0;
545 virtual all_iterator cbegin_all() const = 0;
546 virtual all_iterator cend_all() const = 0;
547
550 virtual Attribute & at(std::size_t id) = 0;
551 virtual const Attribute & at(std::size_t id) const = 0;
553 virtual Attribute & operator[](std::size_t id) = 0;
554 virtual const Attribute & operator[](std::size_t id) const = 0;
555
558 virtual Attribute & at(const ThreadSafePooledString &name) = 0;
559 virtual const Attribute & at(const ThreadSafePooledString &name) const = 0;
561 virtual Attribute & operator[](const ThreadSafePooledString &name) = 0;
562 virtual const Attribute & operator[](const ThreadSafePooledString &name) const = 0;
563
565 virtual bool has_attribute(const ThreadSafePooledString &name) const = 0;
566
568 virtual bool operator== (const Table &other) = 0;
569 virtual bool operator== (const Table &other) const = 0;
570
572 virtual const ThreadSafePooledString & name() const = 0;
573
575 virtual Store & store() const = 0;
577 virtual void store(std::unique_ptr<Store> new_store) = 0;
578
580 virtual const storage::DataLayout & layout() const = 0;
582 virtual void layout(storage::DataLayout &&new_layout) = 0;
584 virtual void layout(const storage::DataLayoutFactory &factory) = 0;
585
587 virtual std::vector<std::reference_wrapper<const Attribute>> primary_key() const = 0;
588
591 virtual void add_primary_key(const ThreadSafePooledString &name) = 0;
592
595 virtual void push_back(ThreadSafePooledString name, const PrimitiveType *type) = 0;
596
598 virtual Schema schema(const ThreadSafePooledOptionalString &alias = {}) const = 0;
599
602 virtual size_t convert_id(size_t id) = 0;
603
604 virtual void dump(std::ostream &out) const = 0;
605 virtual void dump() const = 0;
606};
607
609struct M_EXPORT ConcreteTable : Table
610{
611 private:
614 std::unordered_map<ThreadSafePooledString, table_type::size_type> name_to_attr_;
615 std::unique_ptr<Store> store_;
618
619 public:
620 ConcreteTable(ThreadSafePooledString name) : name_(std::move(name)) { }
621 virtual ~ConcreteTable() = default;
622
624 std::size_t num_attrs() const override { return end() - begin(); }
625
627 std::size_t num_hidden_attrs() const override { return end_hidden() - begin_hidden(); }
628
630 std::size_t num_all_attrs() const override { return attrs_.size(); }
631
632 virtual iterator begin() const override { return iterator(attrs_.begin(), attrs_.end()); }
633 virtual iterator end() const override { return iterator(attrs_.end(), attrs_.begin(), attrs_.end()); }
634 virtual iterator cbegin() const override { return iterator(attrs_.begin(), attrs_.end()); }
635 virtual iterator cend() const override { return iterator(attrs_.end(), attrs_.begin(), attrs_.end()); }
636
637 virtual hidden_iterator begin_hidden() const override { return hidden_iterator(attrs_.begin(), attrs_.end()); }
638 virtual hidden_iterator end_hidden() const override { return hidden_iterator(attrs_.end(), attrs_.begin(), attrs_.end()); }
639 virtual hidden_iterator cbegin_hidden() const override { return hidden_iterator(attrs_.begin(), attrs_.end()); }
640 virtual hidden_iterator cend_hidden() const override { return hidden_iterator(attrs_.end(), attrs_.begin(), attrs_.end()); }
641
642 virtual all_iterator begin_all() const override { return all_iterator(attrs_.begin(), attrs_.end()); }
643 virtual all_iterator end_all() const override { return all_iterator(attrs_.end(), attrs_.begin(), attrs_.end()); }
644 virtual all_iterator cbegin_all() const override { return all_iterator(attrs_.begin(), attrs_.end()); }
645 virtual all_iterator cend_all() const override { return all_iterator(attrs_.end(), attrs_.begin(), attrs_.end()); }
646
649 Attribute & at(std::size_t id) override {
650 if (id >= attrs_.size())
651 throw std::out_of_range("id out of bounds");
652 auto &attr = attrs_[id];
653 M_insist(attr.id == id, "attribute ID mismatch");
654 return attr;
655 }
656 const Attribute & at(std::size_t id) const override { return const_cast<ConcreteTable*>(this)->at(id); }
658 Attribute & operator[](std::size_t id) override {
659 M_insist(id < attrs_.size());
660 auto &attr = attrs_[id];
661 M_insist(attr.id == id, "attribute ID mismatch");
662 return attr;
663 }
664 const Attribute & operator[](std::size_t id) const override { return const_cast<ConcreteTable*>(this)->operator[](id); }
665
668 Attribute & at(const ThreadSafePooledString &name) override {
669 if (auto it = name_to_attr_.find(name); it != name_to_attr_.end()) {
670 M_insist(it->second < attrs_.size());
671 return operator[](it->second);
672 }
673 throw std::out_of_range("name does not exists");
674 }
675 const Attribute & at(const ThreadSafePooledString &name) const override { return const_cast<ConcreteTable*>(this)->at(name); }
677 Attribute & operator[](const ThreadSafePooledString &name) override { return operator[](name_to_attr_.find(name)->second); }
678 const Attribute & operator[](const ThreadSafePooledString &name) const override { return const_cast<ConcreteTable*>(this)->operator[](name); }
679
681 bool has_attribute(const ThreadSafePooledString &name) const override { return name_to_attr_.contains(name); }
682
684 bool operator== (const Table &other) override {
685 if (is<const ConcreteTable>(other))
686 return this == &other; // check for referential equality.
687 if (is<const TableDecorator>(other))
688 return other.operator==(*this);
689 M_unreachable("unknown table type");
690 }
691 bool operator== (const Table &other) const override { return const_cast<ConcreteTable*>(this)->operator==(other); }
692
694 const ThreadSafePooledString & name() const override { return name_; }
695
697 Store & store() const override { return *store_; }
699 void store(std::unique_ptr<Store> new_store) override { using std::swap; swap(store_, new_store); }
700
702 const storage::DataLayout & layout() const override { M_insist(bool(layout_)); return layout_; }
704 void layout(storage::DataLayout &&new_layout) override { layout_ = std::move(new_layout); }
706 virtual void layout(const storage::DataLayoutFactory &factory) override;
707
709 std::vector<std::reference_wrapper<const Attribute>> primary_key() const override {
710 std::vector<std::reference_wrapper<const Attribute>> res;
711 for (auto id : primary_key_)
712 res.emplace_back(operator[](id));
713 return res;
714 }
717 void add_primary_key(const ThreadSafePooledString &name) override {
718 auto &attr = at(name);
719 primary_key_(attr.id) = true;
720 }
721
724 void push_back(ThreadSafePooledString name, const PrimitiveType *type) override {
725 auto res = name_to_attr_.emplace(name, attrs_.size());
726 if (not res.second)
727 throw std::invalid_argument("attribute name already in use");
728 attrs_.emplace_back(Attribute(attrs_.size(), *this, type, std::move(name)));
729 }
730
732 Schema schema(const ThreadSafePooledOptionalString &alias = {}) const override;
733
736 size_t convert_id(size_t id) override {
737 for (size_t i = 0; i <= id; ++i)
738 if (attrs_[i].is_hidden) ++id;
739 return id;
740 }
741
742 virtual void dump(std::ostream &out) const override;
743 virtual void dump() const override;
744};
745
748{
749 protected:
750 std::unique_ptr<Table> table_;
751
752 public:
753 TableDecorator(std::unique_ptr<Table> table) : table_(std::move(table)) { }
754 virtual ~TableDecorator() = default;
755
756 virtual size_t num_attrs() const override { return table_->num_attrs(); }
757 virtual size_t num_hidden_attrs() const override { return table_->num_hidden_attrs(); }
758 virtual size_t num_all_attrs() const override { return table_->num_all_attrs(); }
759
760 virtual iterator begin() const override { return table_->begin(); }
761 virtual iterator end() const override { return table_->end(); }
762 virtual iterator cbegin() const override { return table_->cbegin(); }
763 virtual iterator cend() const override { return table_->cend(); }
764
765 virtual hidden_iterator begin_hidden() const override { return table_->begin_hidden(); }
766 virtual hidden_iterator end_hidden() const override { return table_->end_hidden(); }
767 virtual hidden_iterator cbegin_hidden() const override { return table_->cbegin_hidden(); }
768 virtual hidden_iterator cend_hidden() const override { return table_->cend_hidden(); }
769
770 virtual all_iterator begin_all() const override { return table_->begin_all(); }
771 virtual all_iterator end_all() const override { return table_->end_all(); }
772 virtual all_iterator cbegin_all() const override { return table_->cbegin_all(); }
773 virtual all_iterator cend_all() const override { return table_->cend_all(); }
774
775 virtual Attribute & at(std::size_t id) override { return table_->at(id); }
776 virtual const Attribute & at(std::size_t id) const override { return table_->at(id); }
777
778 virtual Attribute & operator[](std::size_t id) override { return table_->operator[](id); }
779 virtual const Attribute & operator[](std::size_t id) const override { return table_->operator[](id); }
780
781 virtual Attribute & at(const ThreadSafePooledString &name) override { return table_->at(name); }
782 virtual const Attribute & at(const ThreadSafePooledString &name) const override { return table_->at(name); }
783
784 virtual Attribute & operator[](const ThreadSafePooledString &name) override { return table_->at(name); }
785 virtual const Attribute & operator[](const ThreadSafePooledString &name) const override { return table_->at(name); }
786
787 virtual bool has_attribute(const ThreadSafePooledString &name) const override { return table_->has_attribute(name); }
788
789 virtual bool operator== (const Table &other) override { return other == *table_; }
790 virtual bool operator== (const Table &other) const override { return const_cast<TableDecorator*>(this)->operator==(other); }
791
792 virtual const ThreadSafePooledString & name() const override { return table_->name(); }
793
794 virtual Store & store() const override { return table_->store(); }
795 virtual void store(std::unique_ptr<Store> new_store) override { table_->store(std::move(new_store)); }
796
797 virtual const storage::DataLayout & layout() const override { return table_->layout(); }
798 virtual void layout(storage::DataLayout &&new_layout) override { table_->layout(std::move(new_layout)); }
799 virtual void layout(const storage::DataLayoutFactory &factory) override { table_->layout(factory); }
800
801 virtual std::vector<std::reference_wrapper<const Attribute>> primary_key() const override { return table_->primary_key(); }
802 virtual void add_primary_key(const ThreadSafePooledString &name) override { table_->add_primary_key(name); }
803
804 virtual void push_back(ThreadSafePooledString name, const PrimitiveType * type) override { table_->push_back(std::move(name), type); }
805
806 virtual Schema schema(const ThreadSafePooledOptionalString &alias) const override { return table_->schema(alias); }
807
808 virtual size_t convert_id(size_t id) override { return table_->convert_id(id); }
809
810 virtual void dump(std::ostream & out) const override { table_->dump(out); }
811 virtual void dump() const override { table_->dump(); }
812};
813
816{
817 public:
818 MultiVersioningTable(std::unique_ptr<Table> table);
819
821
822 void dump(std::ostream &out) const override;
823 void dump() const override;
824};
825
827struct M_EXPORT Function
828{
829#define kind_t(X) \
830 X(FN_Scalar) \
831 X(FN_Aggregate)
832
833 enum fnid_t {
834#define M_FUNCTION(NAME, KIND) FN_ ## NAME,
835#include <mutable/tables/Functions.tbl>
836#undef M_FUNCTION
837 FN_UDF, // for all user-defined functions
838 };
839
843
844 Function(ThreadSafePooledString name, fnid_t fnid, kind_t kind) : name(std::move(name)), fnid(fnid), kind(kind) { }
845
847 bool is_UDF() const { return fnid == FN_UDF; }
848
850 bool is_scalar() const { return kind == FN_Scalar; }
852 bool is_aggregate() const { return kind == FN_Aggregate; }
853
854 void dump(std::ostream &out) const;
855 void dump() const;
856
857 private:
858 static constexpr const char *FNID_TO_STR_[] = {
859#define M_FUNCTION(NAME, KIND) "FN_" #NAME,
860#include <mutable/tables/Functions.tbl>
861#undef M_FUNCTION
862 "FN_UDF",
863 };
864 static constexpr const char *KIND_TO_STR_[] = { M_ENUM_TO_STR(kind_t) };
865#undef kind_t
866};
867
869struct M_EXPORT Database
870{
871 friend struct Catalog;
872
874 {
876 const Table &table;
878 std::unique_ptr<idx::IndexBase> index;
879 bool is_valid;
880
881 index_entry_type(ThreadSafePooledString name, const Table &table, const Attribute &attribute,
882 std::unique_ptr<idx::IndexBase> index)
883 : name(std::move(name))
884 , table(table)
885 , attribute(attribute)
886 , index(std::move(index))
887 , is_valid(true)
888 { }
889 };
890
891 public:
893 private:
894 std::unordered_map<ThreadSafePooledString, std::unique_ptr<Table>> tables_;
895 std::unordered_map<ThreadSafePooledString, Function*> functions_;
896 std::unique_ptr<CardinalityEstimator> cardinality_estimator_;
897 std::list<index_entry_type> indexes_;
898
899 private:
901
902 public:
903 ~Database();
904
906 std::size_t size() const { return tables_.size(); }
907 auto begin_tables() const { return tables_.cbegin(); }
908 auto end_tables() const { return tables_.cend(); }
909
910 /*===== Tables ===================================================================================================*/
913 Table & get_table(const ThreadSafePooledString &name) const { return *tables_.at(name); }
916 Table & add_table(ThreadSafePooledString name);
918 Table & add(std::unique_ptr<Table> table) {
919 auto it = tables_.find(table->name());
920 if (it != tables_.end()) throw std::invalid_argument("table with that name already exists");
921 it = tables_.emplace_hint(it, table->name(), std::move(table));
922 return *it->second;
923 }
925 bool has_table(const ThreadSafePooledString &name) const { return tables_.contains(name); }
928 auto it = tables_.find(name);
929 if (it == tables_.end())
930 throw std::invalid_argument("Table of that name does not exist.");
931 drop_indexes(name);
932 tables_.erase(it);
933 };
934
935 /*===== Functions ================================================================================================*/
939 const Function * get_function(const ThreadSafePooledString &name) const;
940
941 /*===== Statistics ===============================================================================================*/
946 std::unique_ptr<CardinalityEstimator> cardinality_estimator(std::unique_ptr<CardinalityEstimator> CE) {
947 auto old = std::move(cardinality_estimator_); cardinality_estimator_ = std::move(CE); return old;
948 }
949 const CardinalityEstimator & cardinality_estimator() const { return *cardinality_estimator_; }
950
951 /*===== Indexes ==================================================================================================*/
956 void add_index(std::unique_ptr<idx::IndexBase> index, const ThreadSafePooledString &table_name,
957 const ThreadSafePooledString &attribute_name, ThreadSafePooledString index_name)
958 {
959 if (has_index(index_name))
960 throw invalid_argument("Index with that name already exists.");
961 auto &table = get_table(table_name);
962 auto &attribute = table.at(attribute_name);
963 indexes_.emplace_back(std::move(index_name), table, attribute, std::move(index));
964 }
967 void drop_index(const ThreadSafePooledString &index_name) {
968 for (auto it = indexes_.cbegin(); it != indexes_.cend(); ++it) {
969 if (it->name == index_name) {
970 indexes_.erase(it);
971 return;
972 }
973 }
974 throw invalid_argument("Index of that name does not exist.");
975 }
978 void drop_indexes(const ThreadSafePooledString &table_name) {
979 if (not has_table(table_name))
980 throw invalid_argument("Table with that name does not exist.");
981 for (auto it = indexes_.cbegin(); it != indexes_.end();) {
982 if (it->table.name() == table_name)
983 it = indexes_.erase(it);
984 else
985 ++it;
986 }
987 }
989 bool has_index(const ThreadSafePooledString &index_name) const {
990 for (auto it = indexes_.cbegin(); it != indexes_.cend(); ++it)
991 if (it->name == index_name) return true;
992 return false;
993 }
997 bool has_index(const ThreadSafePooledString &table_name, const ThreadSafePooledString &attribute_name,
998 idx::IndexMethod method) const {
999 auto it = tables_.find(table_name);
1000 if (it == tables_.end())
1001 throw m::invalid_argument("Table with that name does not exist.");
1002 auto &table = it->second;
1003 if (not table->has_attribute(attribute_name))
1004 throw m::invalid_argument("Attribute with that name does not exist.");
1005 for (auto &entry : indexes_) {
1006 if (entry.is_valid and entry.table.name() == table_name and entry.attribute.name == attribute_name and
1007 entry.index->method() == method)
1008 {
1009 return true;
1010 }
1011 }
1012 return false;
1013 }
1016 const idx::IndexBase & get_index(const ThreadSafePooledString &index_name) const {
1017 for (auto &entry : indexes_) {
1018 if (entry.name == index_name)
1019 return *entry.index;
1020 }
1021 throw m::invalid_argument("Index of that name does not exist.");
1022 }
1028 const ThreadSafePooledString &attribute_name, idx::IndexMethod method) const {
1029 auto it = tables_.find(table_name);
1030 if (it == tables_.end())
1031 throw m::invalid_argument("Table with that name does not exist.");
1032 auto &table = it->second;
1033 if (not table->has_attribute(attribute_name))
1034 throw m::invalid_argument("Attribute with that name does not exist.");
1035 for (auto &entry : indexes_) {
1036 if (entry.is_valid and entry.table.name() == table_name and entry.attribute.name == attribute_name and
1037 entry.index->method() == method)
1038 {
1039 return *entry.index;
1040 }
1041 }
1042 throw m::invalid_argument("Index of that method on that attribute of that table does not exist.");
1043 }
1047 if (not has_table(table_name))
1048 throw m::invalid_argument("Table with that name does not exist.");
1049 for (auto &entry : indexes_) {
1050 if (entry.table.name() == table_name)
1051 entry.is_valid = false;
1052 }
1053 }
1054};
1055
1056}
1057
1058namespace std {
1059
1061template<>
1062struct hash<m::Schema::Identifier>
1063{
1064 uint64_t operator()(const m::Schema::Identifier &id) const {
1065 using std::hash;
1066 uint64_t h = hash<m::ThreadSafePooledString>{}(id.name);
1067 if (id.prefix.has_value())
1068 h *= hash<m::ThreadSafePooledOptionalString>{}(id.prefix);
1069 return h;
1070 }
1071};
1072
1074template<>
1075struct hash<m::Attribute>
1076{
1077 uint64_t operator()(const m::Attribute &attr) const {
1078 using std::hash;
1079 auto h = hash<m::ThreadSafePooledString>{}(attr.table.name());
1080 return h * (attr.id + 1);
1081 }
1082};
1083
1084}
#define id(X)
#define kind_t(X)
Definition: Schema.hpp:829
and(sizeof(T)==4) U64x1 reinterpret_to_U64(m
Definition: WasmAlgo.cpp:266
Check whether.
Definition: concepts.hpp:39
#define M_unreachable(MSG)
Definition: macro.hpp:146
#define M_notnull(ARG)
Definition: macro.hpp:182
#define M_ENUM_TO_STR(LIST)
Definition: macro.hpp:75
#define M_insist(...)
Definition: macro.hpp:129
IndexMethod
An enum class that lists all supported index methods.
Definition: Index.hpp:23
‍mutable namespace
Definition: Backend.hpp:10
Schema operator&(const Schema &left, const Schema &right)
Computes the set intersection of two Schemas.
Definition: Schema.hpp:258
bool type_check(const Attribute &attr)
Checks that the type of the attr matches the template type T.
Definition: Schema.hpp:337
Schema operator+(const Schema &left, const Schema &right)
Definition: Schema.hpp:250
void swap(PlanTableBase< Actual > &first, PlanTableBase< Actual > &second)
Definition: PlanTable.hpp:394
T(x)
ThreadSafeStringPool::proxy_type ThreadSafePooledString
Definition: Pool.hpp:464
Schema operator|(const Schema &left, const Schema &right)
Definition: Schema.hpp:272
and
Definition: enum_ops.hpp:12
and arithmetic< U > and same_signedness< T, U > U
Definition: concepts.hpp:90
ThreadSafeStringPool::proxy_optional_type ThreadSafePooledOptionalString
Definition: Pool.hpp:465
STL namespace.
An attribute of a table.
Definition: Schema.hpp:289
const PrimitiveType * type
the type of the attribute
Definition: Schema.hpp:294
M_LCOV_EXCL_START friend std::ostream & operator<<(std::ostream &out, const Attribute &attr)
Definition: Schema.hpp:326
Attribute(Attribute &&)=default
const Table & table
the table the attribute belongs to
Definition: Schema.hpp:293
bool operator==(const Attribute &other) const
Compares to attributes.
Definition: Schema.hpp:322
std::size_t id
the internal identifier of the attribute, unique within its table
Definition: Schema.hpp:292
Attribute(std::size_t id, const Table &table, const PrimitiveType *type, ThreadSafePooledString name)
Definition: Schema.hpp:303
bool operator!=(const Attribute &other) const
Definition: Schema.hpp:323
Attribute(const Attribute &)=delete
ThreadSafePooledString name
the name of the attribute
Definition: Schema.hpp:295
The catalog contains all Databases and keeps track of all meta information of the database system.
Definition: Catalog.hpp:215
Basic implementation of Table.
Definition: Schema.hpp:610
void add_primary_key(const ThreadSafePooledString &name) override
Adds an attribute with the given name to the primary key of this table.
Definition: Schema.hpp:717
void push_back(ThreadSafePooledString name, const PrimitiveType *type) override
Adds a new attribute with the given name and type to the table.
Definition: Schema.hpp:724
virtual hidden_iterator end_hidden() const override
Definition: Schema.hpp:638
virtual all_iterator begin_all() const override
Definition: Schema.hpp:642
std::size_t num_hidden_attrs() const override
Returns the number of hidden attributes in this table.
Definition: Schema.hpp:627
virtual all_iterator cend_all() const override
Definition: Schema.hpp:645
const Attribute & operator[](std::size_t id) const override
Definition: Schema.hpp:664
virtual iterator end() const override
Definition: Schema.hpp:633
const storage::DataLayout & layout() const override
Returns a reference to the physical data layout.
Definition: Schema.hpp:702
virtual hidden_iterator begin_hidden() const override
Definition: Schema.hpp:637
SmallBitset primary_key_
the primary key of this table, maintained as a SmallBitset over attribute id's
Definition: Schema.hpp:617
const ThreadSafePooledString & name() const override
Returns the name of the Table.
Definition: Schema.hpp:694
storage::DataLayout layout_
the physical data layout for this table
Definition: Schema.hpp:616
Attribute & operator[](const ThreadSafePooledString &name) override
Returns the attribute with the given name.
Definition: Schema.hpp:677
const Attribute & at(std::size_t id) const override
Definition: Schema.hpp:656
virtual all_iterator end_all() const override
Definition: Schema.hpp:643
std::size_t num_attrs() const override
Returns the number of non-hidden attributes in this table.
Definition: Schema.hpp:624
std::size_t num_all_attrs() const override
Returns the number of attributes in this table.
Definition: Schema.hpp:630
Store & store() const override
Returns a reference to the backing store.
Definition: Schema.hpp:697
size_t convert_id(size_t id) override
Converts the id an non-hidden attribute would have in a table without any hidden attributes and retur...
Definition: Schema.hpp:736
table_type attrs_
the attributes of this table, maintained as a sorted set
Definition: Schema.hpp:613
bool has_attribute(const ThreadSafePooledString &name) const override
Returns true iff the table has an attribute name.
Definition: Schema.hpp:681
Attribute & operator[](std::size_t id) override
Returns the attribute with the given id.
Definition: Schema.hpp:658
const Attribute & operator[](const ThreadSafePooledString &name) const override
Definition: Schema.hpp:678
ThreadSafePooledString name_
the name of the table
Definition: Schema.hpp:612
virtual hidden_iterator cend_hidden() const override
Definition: Schema.hpp:640
std::unique_ptr< Store > store_
the store backing this table; may be nullptr
Definition: Schema.hpp:615
virtual all_iterator cbegin_all() const override
Definition: Schema.hpp:644
std::vector< std::reference_wrapper< const Attribute > > primary_key() const override
Returns all attributes forming the primary key.
Definition: Schema.hpp:709
virtual ~ConcreteTable()=default
Attribute & at(const ThreadSafePooledString &name) override
Returns the attribute with the given name.
Definition: Schema.hpp:668
virtual iterator cend() const override
Definition: Schema.hpp:635
const Attribute & at(const ThreadSafePooledString &name) const override
Definition: Schema.hpp:675
ConcreteTable(ThreadSafePooledString name)
Definition: Schema.hpp:620
virtual iterator cbegin() const override
Definition: Schema.hpp:634
std::unordered_map< ThreadSafePooledString, table_type::size_type > name_to_attr_
maps attribute names to attributes
Definition: Schema.hpp:614
virtual hidden_iterator cbegin_hidden() const override
Definition: Schema.hpp:639
Attribute & at(std::size_t id) override
Returns the attribute with the given id.
Definition: Schema.hpp:649
void store(std::unique_ptr< Store > new_store) override
Sets the backing store for this table.
Definition: Schema.hpp:699
void layout(storage::DataLayout &&new_layout) override
Sets the physical data layout for this table.
Definition: Schema.hpp:704
virtual iterator begin() const override
Definition: Schema.hpp:632
Definition: Schema.hpp:874
index_entry_type(ThreadSafePooledString name, const Table &table, const Attribute &attribute, std::unique_ptr< idx::IndexBase > index)
Definition: Schema.hpp:881
bool is_valid
indicates if the index should be used to answer queries
Definition: Schema.hpp:879
const Table & table
the table of the index
Definition: Schema.hpp:876
std::unique_ptr< idx::IndexBase > index
the actual index
Definition: Schema.hpp:878
ThreadSafePooledString name
the name of the index
Definition: Schema.hpp:875
const Attribute & attribute
the indexed attribute
Definition: Schema.hpp:877
A Database is a set of Tables, Functions, and Statistics.
Definition: Schema.hpp:870
std::unordered_map< ThreadSafePooledString, Function * > functions_
functions defined in this database
Definition: Schema.hpp:895
void drop_table(const ThreadSafePooledString &name)
Drops the Table with the given name.
Definition: Schema.hpp:927
std::unordered_map< ThreadSafePooledString, std::unique_ptr< Table > > tables_
the tables of this database
Definition: Schema.hpp:894
Table & get_table(const ThreadSafePooledString &name) const
Returns a reference to the Table with the given name.
Definition: Schema.hpp:913
void invalidate_indexes(const ThreadSafePooledString &table_name)
Invalidates all indexes on attributes of Table table_name s.t.
Definition: Schema.hpp:1046
void drop_indexes(const ThreadSafePooledString &table_name)
Drops all indexes from the table with the given table_name.
Definition: Schema.hpp:978
bool has_index(const ThreadSafePooledString &index_name) const
Returns true iff there is an index with the given index_name.
Definition: Schema.hpp:989
std::size_t size() const
Returns the number of tables in this Database.
Definition: Schema.hpp:906
auto begin_tables() const
Definition: Schema.hpp:907
void add_index(std::unique_ptr< idx::IndexBase > index, const ThreadSafePooledString &table_name, const ThreadSafePooledString &attribute_name, ThreadSafePooledString index_name)
Adds an index with index_name on attribute_name from table_name.
Definition: Schema.hpp:956
void drop_index(const ThreadSafePooledString &index_name)
Drops the index with the given index_name.
Definition: Schema.hpp:967
const idx::IndexBase & get_index(const ThreadSafePooledString &table_name, const ThreadSafePooledString &attribute_name, idx::IndexMethod method) const
Returns a valid index using method on attribute_name of table_name iff one exists.
Definition: Schema.hpp:1027
std::unique_ptr< CardinalityEstimator > cardinality_estimator_
the CardinalityEstimator of this Database
Definition: Schema.hpp:896
auto end_tables() const
Definition: Schema.hpp:908
const idx::IndexBase & get_index(const ThreadSafePooledString &index_name) const
Returns the index with the given index_name.
Definition: Schema.hpp:1016
std::list< index_entry_type > indexes_
the indexes of this database
Definition: Schema.hpp:897
std::unique_ptr< CardinalityEstimator > cardinality_estimator(std::unique_ptr< CardinalityEstimator > CE)
Sets the CardinalityEstimator of this Database.
Definition: Schema.hpp:946
const CardinalityEstimator & cardinality_estimator() const
Definition: Schema.hpp:949
Table & add(std::unique_ptr< Table > table)
Adds a new Table to this Database.
Definition: Schema.hpp:918
bool has_table(const ThreadSafePooledString &name) const
Returns true iff a Table with the given name exists.
Definition: Schema.hpp:925
ThreadSafePooledString name
the name of the database
Definition: Schema.hpp:892
bool has_index(const ThreadSafePooledString &table_name, const ThreadSafePooledString &attribute_name, idx::IndexMethod method) const
Returns true iff there is a valid index using method on attribute_name of table_name.
Definition: Schema.hpp:997
Defines a function.
Definition: Schema.hpp:828
void dump() const
bool is_UDF() const
Returns true iff this is a user-defined function.
Definition: Schema.hpp:847
M_DECLARE_ENUM(kind_t) kind
the function kind: Scalar, Aggregate, etc.
Function(ThreadSafePooledString name, fnid_t fnid, kind_t kind)
Definition: Schema.hpp:844
bool is_scalar() const
Returns true iff this function is scalar, i.e. if it is evaluated per tuple.
Definition: Schema.hpp:850
fnid_t fnid
the function id
Definition: Schema.hpp:841
ThreadSafePooledString name
the name of the function
Definition: Schema.hpp:840
bool is_aggregate() const
Returns true iff this function is an aggregation, i.e. if it is evaluated on all tuples.
Definition: Schema.hpp:852
A multi-versioning table is a Table with additional invisible timestamp attributes.
Definition: Schema.hpp:816
PrimitiveTypes represent Types of values.
Definition: Type.hpp:159
bool is_vectorial() const
Returns true iff this PrimitiveType is vectorial, i.e. if it is for a sequence of values.
Definition: Type.hpp:170
An Identifier is composed of a name and an optional prefix.
Definition: Schema.hpp:42
static Identifier CONST_ID_
Definition: Schema.hpp:44
bool is_constant() const
Definition: Schema.hpp:60
bool operator!=(const Identifier &other) const
Definition: Schema.hpp:65
Identifier(ThreadSafePooledOptionalString prefix, ThreadSafePooledString name)
Definition: Schema.hpp:51
Identifier(ThreadSafePooledString name)
Definition: Schema.hpp:50
ThreadSafePooledOptionalString prefix
optional prefix of this Identifier, may not have a value
Definition: Schema.hpp:47
M_LCOV_EXCL_START friend std::ostream & operator<<(std::ostream &out, const Identifier &id)
Definition: Schema.hpp:68
ThreadSafePooledString name
the name of this Identifier
Definition: Schema.hpp:48
bool operator==(const Identifier &other) const
Definition: Schema.hpp:62
const Type * type
Definition: Schema.hpp:87
static entry_type CreateArtificial()
Definition: Schema.hpp:100
bool unique() const
Definition: Schema.hpp:103
entry_type(Identifier id, const Type *type, constraints_t constraints=constraints_t{0})
Definition: Schema.hpp:94
constraints_t constraints
Definition: Schema.hpp:88
bool references_unique() const
Definition: Schema.hpp:104
bool nullable() const
Definition: Schema.hpp:102
A Schema represents a sequence of identifiers, optionally with a prefix, and their associated types.
Definition: Schema.hpp:39
std::size_t num_entries() const
Returns the number of entries in this Schema.
Definition: Schema.hpp:124
iterator end()
Definition: Schema.hpp:117
iterator begin()
Definition: Schema.hpp:116
const_iterator end() const
Definition: Schema.hpp:119
const_iterator find(const Identifier &id) const
Returns an iterator to the entry with the given Identifier id, or end() if no such entry exists.
Definition: Schema.hpp:137
const entry_type & operator[](std::size_t idx) const
Returns the entry at index idx.
Definition: Schema.hpp:156
decltype(entries_)::const_iterator const_iterator
Definition: Schema.hpp:112
const_iterator cend() const
Definition: Schema.hpp:121
entry_type & at(std::size_t idx)
Returns the entry at index idx with in-bounds checking.
Definition: Schema.hpp:143
const entry_type & at(std::size_t idx) const
Returns the entry at index idx with in-bounds checking.
Definition: Schema.hpp:149
bool operator!=(const Schema &other) const
Definition: Schema.hpp:233
entry_type & operator[](std::size_t idx)
Returns the entry at index idx.
Definition: Schema.hpp:151
std::vector< entry_type > entries_
Definition: Schema.hpp:108
bool empty() const
Definition: Schema.hpp:126
std::pair< std::size_t, entry_type & > operator[](const Identifier &id)
Returns a std::pair of the index and a reference to the entry with Identifier id.
Definition: Schema.hpp:170
std::pair< std::size_t, const entry_type & > operator[](const Identifier &id) const
Returns a std::pair of the index and a reference to the entry with Identifier id.
Definition: Schema.hpp:176
const std::vector< entry_type > & entries() const
Definition: Schema.hpp:114
const_iterator cbegin() const
Definition: Schema.hpp:120
void add(Identifier id, const Type *type)
Adds a new entry id of type type to this Schema.
Definition: Schema.hpp:183
Schema deduplicate() const
Returns a deduplicated version of this Schema, i.e.
Definition: Schema.hpp:190
iterator find(const Identifier &id)
Returns an iterator to the entry with the given Identifier id, or end() if no such entry exists.
Definition: Schema.hpp:129
void add(Identifier id, const Type *type, entry_type::constraints_t constraints)
Adds a new entry id of type type with constraints constraints to this Schema.
Definition: Schema.hpp:185
Schema & operator|=(const Schema &other)
Adds all entries of other to this Schema using set semantics.
Definition: Schema.hpp:220
M_LCOV_EXCL_START friend std::ostream & operator<<(std::ostream &out, const Schema &schema)
Definition: Schema.hpp:236
Schema & operator+=(const Schema &other)
Adds all entries of other to this Schema, potentially introducing duplicates.
Definition: Schema.hpp:211
std::pair< std::size_t, const entry_type & > at(const Identifier &id) const
Returns a std::pair of the index and a reference to the entry with Identifier id with in-bounds check...
Definition: Schema.hpp:168
void add(entry_type e)
Adds the entry e to this Schema.
Definition: Schema.hpp:181
decltype(entries_)::iterator iterator
Definition: Schema.hpp:111
Schema drop_constants() const
Returns a copy of this Schema where all constant entries are removed.
Definition: Schema.hpp:200
bool operator==(const Schema &other) const
Checks whether two Schemas have identical Identifiers by checking for mutual set-inclusion.
Definition: Schema.hpp:229
std::pair< std::size_t, entry_type & > at(const Identifier &id)
Returns a std::pair of the index and a reference to the entry with Identifier id with in-bounds check...
Definition: Schema.hpp:160
bool has(const Identifier &id) const
Returns true iff this Schema contains an entry with Identifier id.
Definition: Schema.hpp:140
const_iterator begin() const
Definition: Schema.hpp:118
Implements a small and efficient set over integers in the range of 0 to 63 (including).
Definition: ADT.hpp:26
Defines a generic store interface.
Definition: Store.hpp:22
Abstract Decorator class that concrete TableDecorator inherit from.
Definition: Schema.hpp:748
virtual iterator end() const override
Definition: Schema.hpp:761
virtual std::vector< std::reference_wrapper< const Attribute > > primary_key() const override
Returns all attributes forming the primary key.
Definition: Schema.hpp:801
virtual hidden_iterator end_hidden() const override
Definition: Schema.hpp:766
virtual const storage::DataLayout & layout() const override
Returns a reference to the physical data layout.
Definition: Schema.hpp:797
virtual void store(std::unique_ptr< Store > new_store) override
Sets the backing store for this table.
Definition: Schema.hpp:795
virtual iterator cend() const override
Definition: Schema.hpp:763
virtual all_iterator begin_all() const override
Definition: Schema.hpp:770
virtual hidden_iterator begin_hidden() const override
Definition: Schema.hpp:765
virtual size_t num_all_attrs() const override
Definition: Schema.hpp:758
virtual hidden_iterator cend_hidden() const override
Definition: Schema.hpp:768
virtual all_iterator end_all() const override
Definition: Schema.hpp:771
virtual void dump(std::ostream &out) const override
Definition: Schema.hpp:810
virtual void add_primary_key(const ThreadSafePooledString &name) override
Adds an attribute with the given name to the primary key of this table.
Definition: Schema.hpp:802
virtual iterator begin() const override
Definition: Schema.hpp:760
virtual const Attribute & operator[](std::size_t id) const override
Definition: Schema.hpp:779
virtual Attribute & at(std::size_t id) override
Returns the attribute with the given id.
Definition: Schema.hpp:775
virtual Store & store() const override
Returns a reference to the backing store.
Definition: Schema.hpp:794
virtual const Attribute & at(const ThreadSafePooledString &name) const override
Definition: Schema.hpp:782
virtual size_t num_attrs() const override
Returns the number of attributes in this table.
Definition: Schema.hpp:756
virtual all_iterator cend_all() const override
Definition: Schema.hpp:773
std::unique_ptr< Table > table_
Definition: Schema.hpp:750
virtual const Attribute & at(std::size_t id) const override
Definition: Schema.hpp:776
virtual Attribute & operator[](std::size_t id) override
Returns the attribute with the given id.
Definition: Schema.hpp:778
virtual hidden_iterator cbegin_hidden() const override
Definition: Schema.hpp:767
virtual bool operator==(const Table &other) override
Returns true iff the Table other is the same as this, false otherwise.
Definition: Schema.hpp:789
virtual void layout(const storage::DataLayoutFactory &factory) override
Sets the physical data layout for this table by calling factory.make().
Definition: Schema.hpp:799
virtual size_t convert_id(size_t id) override
Converts the id an non-hidden attribute would have in a table without any hidden attributes and retur...
Definition: Schema.hpp:808
virtual iterator cbegin() const override
Definition: Schema.hpp:762
virtual void push_back(ThreadSafePooledString name, const PrimitiveType *type) override
Adds a new attribute with the given name and type to the table.
Definition: Schema.hpp:804
virtual all_iterator cbegin_all() const override
Definition: Schema.hpp:772
TableDecorator(std::unique_ptr< Table > table)
Definition: Schema.hpp:753
virtual ~TableDecorator()=default
virtual bool has_attribute(const ThreadSafePooledString &name) const override
Returns true iff the table has an attribute name.
Definition: Schema.hpp:787
virtual Attribute & at(const ThreadSafePooledString &name) override
Returns the attribute with the given name.
Definition: Schema.hpp:781
virtual const ThreadSafePooledString & name() const override
Returns the name of the Table.
Definition: Schema.hpp:792
virtual void layout(storage::DataLayout &&new_layout) override
Sets the physical data layout for this table.
Definition: Schema.hpp:798
virtual Attribute & operator[](const ThreadSafePooledString &name) override
Returns the attribute with the given name.
Definition: Schema.hpp:784
virtual const Attribute & operator[](const ThreadSafePooledString &name) const override
Definition: Schema.hpp:785
virtual void dump() const override
Definition: Schema.hpp:811
virtual size_t num_hidden_attrs() const override
Definition: Schema.hpp:757
virtual Schema schema(const ThreadSafePooledOptionalString &alias) const override
Returns a Schema for this Table given the alias alias.
Definition: Schema.hpp:806
difference_type operator-(the_iterator other) const
Definition: Schema.hpp:501
the_iterator(it_type start, it_type end)
Definition: Schema.hpp:410
the_iterator operator--(int)
Definition: Schema.hpp:452
the_iterator & operator+=(int offset)
Definition: Schema.hpp:462
std::ptrdiff_t difference_type
Definition: Schema.hpp:401
the_iterator & operator--()
Definition: Schema.hpp:439
bool operator==(const the_iterator &other) const
Definition: Schema.hpp:457
the_iterator & operator-=(int offset)
Definition: Schema.hpp:481
pointer operator->() const
Definition: Schema.hpp:455
reference operator*() const
Definition: Schema.hpp:454
the_iterator operator++(int)
Definition: Schema.hpp:437
table_type::const_iterator it_type
Definition: Schema.hpp:400
bool operator!=(const the_iterator &other) const
Definition: Schema.hpp:460
the_iterator(it_type it, it_type start, it_type end)
Definition: Schema.hpp:422
the_iterator & operator++()
Definition: Schema.hpp:424
A table is a sorted set of attributes.
Definition: Schema.hpp:388
virtual const storage::DataLayout & layout() const =0
Returns a reference to the physical data layout.
virtual all_iterator begin_all() const =0
virtual void layout(storage::DataLayout &&new_layout)=0
Sets the physical data layout for this table.
virtual iterator cend() const =0
virtual const ThreadSafePooledString & name() const =0
Returns the name of the Table.
virtual hidden_iterator cbegin_hidden() const =0
virtual const Attribute & operator[](std::size_t id) const =0
virtual hidden_iterator begin_hidden() const =0
virtual std::size_t num_hidden_attrs() const =0
virtual void store(std::unique_ptr< Store > new_store)=0
Sets the backing store for this table.
virtual iterator cbegin() const =0
virtual bool has_attribute(const ThreadSafePooledString &name) const =0
Returns true iff the table has an attribute name.
virtual const Attribute & at(const ThreadSafePooledString &name) const =0
virtual all_iterator cbegin_all() const =0
virtual hidden_iterator end_hidden() const =0
virtual const Attribute & at(std::size_t id) const =0
virtual void dump() const =0
virtual ~Table()=default
virtual all_iterator end_all() const =0
virtual const Attribute & operator[](const ThreadSafePooledString &name) const =0
virtual void add_primary_key(const ThreadSafePooledString &name)=0
Adds an attribute with the given name to the primary key of this table.
virtual Attribute & operator[](const ThreadSafePooledString &name)=0
Returns the attribute with the given name.
virtual iterator end() const =0
virtual Store & store() const =0
Returns a reference to the backing store.
virtual hidden_iterator cend_hidden() const =0
virtual void dump(std::ostream &out) const =0
virtual Attribute & at(std::size_t id)=0
Returns the attribute with the given id.
virtual std::vector< std::reference_wrapper< const Attribute > > primary_key() const =0
Returns all attributes forming the primary key.
virtual std::size_t num_attrs() const =0
Returns the number of attributes in this table.
virtual std::size_t num_all_attrs() const =0
virtual Attribute & operator[](std::size_t id)=0
Returns the attribute with the given id.
virtual Schema schema(const ThreadSafePooledOptionalString &alias={}) const =0
Returns a Schema for this Table given the alias alias.
virtual size_t convert_id(size_t id)=0
Converts the id an non-hidden attribute would have in a table without any hidden attributes and retur...
std::vector< Attribute > table_type
Definition: Schema.hpp:390
virtual void push_back(ThreadSafePooledString name, const PrimitiveType *type)=0
Adds a new attribute with the given name and type to the table.
virtual Attribute & at(const ThreadSafePooledString &name)=0
Returns the attribute with the given name.
virtual iterator begin() const =0
virtual all_iterator cend_all() const =0
virtual void layout(const storage::DataLayoutFactory &factory)=0
Sets the physical data layout for this table by calling factory.make().
This class represents types in the SQL type system.
Definition: Type.hpp:46
An expression.
Definition: AST.hpp:39
The base class for indexes.
Definition: Index.hpp:27
Signals that an argument to a function of method was invalid.
Definition: exception.hpp:37
Signals that an index-based or key-based access was out of range.
Definition: exception.hpp:43
This is an interface for factories that compute particular DataLayouts for a given sequence of Types,...
Models how data is laid out in a linear address space.
Definition: DataLayout.hpp:29
uint64_t operator()(const m::Attribute &attr) const
Definition: Schema.hpp:1077
uint64_t operator()(const m::Schema::Identifier &id) const
Definition: Schema.hpp:1064