mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
PhysicalPlanTable.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <functional>
6
7
8namespace m {
9
10// forward declarations
11struct ConcretePhysicalPlanTableEntry;
12
13
14/*======================================================================================================================
15 * ConcretePhysicalPlanTable
16 *====================================================================================================================*/
17
18namespace detail {
19
20template<bool Ref, bool C>
21requires (not Ref) or C // references to condition-entry pairs must be const, thus only a const iterator is allowed
24{
25 using super =
30 static constexpr bool IsReference = Ref;
31
32 private:
33 using iterable_entry_type = std::conditional_t<IsReference, std::reference_wrapper<const value_type>, value_type>;
34 using iterator_type = std::vector<iterable_entry_type>::iterator;
36#ifdef M_ENABLE_SANITY_FIELDS
37 iterator_type end_;
38#endif
39
40 public:
41 using difference_type = iterator_type::difference_type; // to satisfy std::input_iterator for std::find_if()
42
44 ConcretePhysicalPlanTableIterator(const std::vector<iterable_entry_type> &iterable, std::size_t idx)
45 : current_(const_cast<std::vector<iterable_entry_type>&>(iterable).begin() + idx)
46#ifdef M_ENABLE_SANITY_FILEDS
47 , end_(const_cast<std::vector<iterable_entry_type>&>(iterable).end())
48#endif
49 {
50 M_insist(idx <= iterable.size(), "invalid index");
51 }
52
53 bool operator==(const ConcretePhysicalPlanTableIterator &other) const { return this->current_ == other.current_; }
54 bool operator!=(const ConcretePhysicalPlanTableIterator &other) const { return not operator==(other); }
55
57#ifdef M_ENABLE_SANITY_FILEDS
58 M_insist(current_ < end_, "cannot increment end iterator");
59#endif
60 ++current_;
61 return *this;
62 }
63 ConcretePhysicalPlanTableIterator operator++(int) { auto cpy = *this; operator++(); return cpy; }
64
66#ifdef M_ENABLE_SANITY_FILEDS
67 M_insist(current_ < end_, "cannot dereference end iterator");
68#endif
69 return [&]() -> reference { // M_CONSTEXPR_COND cannot be used since it would drop reference and try to copy
70 if constexpr (IsReference)
71 return current_->get();
72 else
73 return *current_;
74 }();
75 }
77#ifdef M_ENABLE_SANITY_FILEDS
78 M_insist(current_ < end_, "cannot dereference end iterator");
79#endif
80 return M_CONSTEXPR_COND(IsReference, &current_->get(), &*current_);
81 }
82};
85
86}
87
89 : PhysicalPlanTableEntry<ConcretePhysicalPlanTableEntry, detail::PhysicalPlanTableEntryChildIterator>
90{
94
96 using std::swap;
97 swap(first.match_, second.match_);
98 swap(first.children_, second.children_);
99 swap(first.cost_, second.cost_);
100 }
101
102 private:
108 std::vector<std::reference_wrapper<const detail::condition_entry_t<entry_type>>> children_;
110
111 public:
112 template<typename It>
114 ConcretePhysicalPlanTableEntry(std::unique_ptr<MatchBase> &&match, const std::vector<It> &children, cost_type cost)
115 : match_(match.release()) // convert to unsharable shared pointer
116 , cost_(cost)
117 {
118 children_.reserve(children.size());
119 for (auto &it : children)
120 children_.emplace_back(*it);
121 }
122
126 { swap(*this, other); }
127
129 swap(*this, other);
130 return *this;
131 }
132
133 const MatchBase & match() const { return *match_; }
136
137 cost_type cost() const { return cost_; }
138
143};
144
147 ConcreteCondition2PPTEntryMap, detail::Condition2PPTEntryMapIterator, ConcretePhysicalPlanTableEntry
148 >
149{
152 >;
156
158 using std::swap;
159 swap(first.map_, second.map_);
160 }
161
162 private:
163 std::vector<detail::condition_entry_t<entry_type>> map_;
164
165 public:
169 { swap(*this, other); }
170
172 swap(*this, other);
173 return *this;
174 }
175
176 bool empty() const { return map_.empty(); }
177
178 void insert(ConditionSet &&condition, entry_type &&entry) {
179 map_.emplace_back(std::move(condition), std::move(entry));
180 }
181
182 iterator begin() { return iterator(map_, 0); }
183 iterator end() { return iterator(map_, map_.size()); }
184 const_iterator begin() const { return const_iterator(map_, 0); }
185 const_iterator end() const { return const_iterator(map_, map_.size()); }
186 const_iterator cbegin() const { return begin(); }
187 const_iterator cend() const { return end(); }
188};
189
190struct ConcretePhysicalPlanTable : PhysicalPlanTable<ConcretePhysicalPlanTable, ConcreteCondition2PPTEntryMap>
191{
195
197 using std::swap;
198 swap(first.table_, second.table_);
199 }
200
201 private:
202 std::vector<condition2entry_map_type> table_;
203
204 public:
207
208 ConcretePhysicalPlanTable & operator=(ConcretePhysicalPlanTable other) { swap(*this, other); return *this; }
209
210 void clear() { table_.clear(); }
211 size_type size() const { return table_.size(); }
212 void resize(size_type size) { table_.resize(size); }
213
215 M_insist(idx < size(), "invalid index");
216 return table_[idx];
217 }
219 return const_cast<ConcretePhysicalPlanTable*>(this)->operator[](idx);
220 }
221
222 condition2entry_map_type & back() { return table_.back(); }
223 const condition2entry_map_type & back() const { return const_cast<ConcretePhysicalPlanTable*>(this)->back(); }
224};
225
226}
Check whether.
Definition: concepts.hpp:39
#define M_CONSTEXPR_COND(COND, IF_TRUE, IF_FALSE)
Definition: macro.hpp:54
#define M_insist(...)
Definition: macro.hpp:129
ConcretePhysicalPlanTableIterator< false, C > Condition2PPTEntryMapIterator
‍mutable namespace
Definition: Backend.hpp:10
STL namespace.
std::vector< detail::condition_entry_t< entry_type > > map_
ConcreteCondition2PPTEntryMap(ConcreteCondition2PPTEntryMap &&other)
void insert(ConditionSet &&condition, entry_type &&entry)
friend void swap(ConcreteCondition2PPTEntryMap &first, ConcreteCondition2PPTEntryMap &second)
ConcreteCondition2PPTEntryMap & operator=(ConcreteCondition2PPTEntryMap other)
unsharable_shared_ptr< MatchBase > extract_match()
const_child_iterator end_children() const
unsharable_shared_ptr< MatchBase > match_
‍the found match; as unsharable shared pointer to share sub-matches between entries while being able ...
unsharable_shared_ptr< MatchBase > share_match() const
friend void swap(ConcretePhysicalPlanTableEntry &first, ConcretePhysicalPlanTableEntry &second)
ConcretePhysicalPlanTableEntry(ConcretePhysicalPlanTableEntry &&other)
const_child_iterator begin_children() const
super::const_child_iterator const_child_iterator
cost_type cost_
cumulative cost, i.e. cost of the physical operator itself plus costs of its children
ConcretePhysicalPlanTableEntry & operator=(ConcretePhysicalPlanTableEntry other)
ConcretePhysicalPlanTableEntry(std::unique_ptr< MatchBase > &&match, const std::vector< It > &children, cost_type cost)
std::vector< std::reference_wrapper< const detail::condition_entry_t< entry_type > > > children_
‍all children, i.e. condition and entry per child
const_child_iterator cbegin_children() const
const_child_iterator cend_children() const
condition2entry_map_type & back()
ConcretePhysicalPlanTable(ConcretePhysicalPlanTable &&other)
condition2entry_map_type & operator[](size_type idx)
const condition2entry_map_type & operator[](size_type idx) const
friend void swap(ConcretePhysicalPlanTable &first, ConcretePhysicalPlanTable &second)
super::condition2entry_map_type condition2entry_map_type
std::vector< condition2entry_map_type > table_
const condition2entry_map_type & back() const
ConcretePhysicalPlanTable & operator=(ConcretePhysicalPlanTable other)
Interface for a mapping between ConditionSets and physical plan table entries of type.
Interface for a single physical plan table entry.
Interface for an entire physical plan table containing a ConditionSet-entry-mapping.
ConcretePhysicalPlanTableIterator & operator++()
bool operator!=(const ConcretePhysicalPlanTableIterator &other) const
ConcretePhysicalPlanTableIterator(const std::vector< iterable_entry_type > &iterable, std::size_t idx)
std::vector< iterable_entry_type >::iterator iterator_type
iterator_type current_
the iterator to the current position in the iterable
iterator_type::difference_type difference_type
bool operator==(const ConcretePhysicalPlanTableIterator &other) const
ConcretePhysicalPlanTableIterator operator++(int)
std::conditional_t< IsReference, std::reference_wrapper< const value_type >, value_type > iterable_entry_type
Iterator interface to iterate over pairs of ConditionSet and.
std::conditional_t< IsConst, const value_type *, value_type * > pointer
std::conditional_t< IsConst, const value_type &, value_type & > reference
This class extends std::shared_ptr to allow for unsharing an exclusively held object and thereby conv...