-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiteration.h.tt
More file actions
217 lines (166 loc) · 8.55 KB
/
iteration.h.tt
File metadata and controls
217 lines (166 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Iteration methods
inline void generic_foreachRaw(lmdb::txn &txn, lmdb::dbi &dbi, const std::function<bool(std::string_view, std::string_view)> &cb, bool reverse = false, std::optional<std::string_view> startingPoint = std::nullopt, bool skipDups = false) {
auto cursor = lmdb::cursor::open(txn, dbi);
std::string_view k, v;
if (startingPoint) {
k = *startingPoint;
if (reverse) {
if (!cursor.get(k, v, MDB_SET_RANGE)) {
if (!cursor.get(k, v, MDB_LAST)) return;
} else {
if (k != *startingPoint) cursor.get(k, v, MDB_PREV);
}
} else {
if (!cursor.get(k, v, MDB_SET_RANGE)) return;
}
if (!cursor.get(k, v, MDB_SET_RANGE)) return;
} else {
if (!cursor.get(k, v, reverse ? MDB_LAST : MDB_FIRST)) return;
}
MDB_cursor_op traversal;
if (reverse) {
traversal = skipDups ? MDB_PREV_NODUP : MDB_PREV;
} else {
traversal = skipDups ? MDB_NEXT_NODUP : MDB_NEXT;
}
do {
if (!cb(k, v)) return;
} while (cursor.get(k, v, traversal));
}
inline bool generic_foreachFull(lmdb::txn &txn, lmdb::dbi &dbi, std::string_view startingPoint, std::string_view startingPointDup, const std::function<bool(std::string_view, std::string_view)> &cb, bool reverse = false) {
auto cursor = lmdb::cursor::open(txn, dbi);
std::string_view k = startingPoint, v = startingPointDup;
if (reverse) {
if (cursor.get(k, v, MDB_GET_BOTH_RANGE)) {
if (v != startingPointDup) {
if (!cursor.get(k, v, MDB_PREV)) return true;
}
} else {
if (cursor.get(k, v, MDB_SET)) {
if (!cursor.get(k, v, MDB_LAST_DUP)) return true;
} else {
if (cursor.get(k, v, MDB_SET_RANGE)) {
if (!cursor.get(k, v, MDB_PREV)) return true;
} else {
if (!cursor.get(k, v, MDB_LAST)) return true;
}
}
}
} else {
if (!cursor.get(k, v, MDB_GET_BOTH_RANGE)) {
if (cursor.get(k, v, MDB_SET)) {
if (!cursor.get(k, v, MDB_NEXT_NODUP)) return true;
} else {
if (!cursor.get(k, v, MDB_SET_RANGE)) return true;
if (!cursor.get(k, v, MDB_FIRST_DUP)) return true;
}
}
}
MDB_cursor_op traversal = reverse ? MDB_PREV : MDB_NEXT;
do {
if (!cb(k, v)) return false;
} while (cursor.get(k, v, traversal));
return true;
}
inline void generic_foreach(lmdb::txn &txn, lmdb::dbi &dbi, const std::function<bool(uint64_t, std::string_view)> &cb, bool reverse = false, uint64_t startingPoint = 0) {
generic_foreachRaw(txn, dbi, [&cb](std::string_view k, std::string_view v){ return cb(lmdb::from_sv<uint64_t>(k), v); }, reverse, startingPoint ? std::optional<std::string_view>(lmdb::to_sv<uint64_t>(startingPoint)) : std::nullopt);
}
inline void generic_foreachByIndex(lmdb::txn &txn, lmdb::dbi &tableDbi, lmdb::dbi &indexDbi, std::optional<std::string_view> startKey, std::optional<std::string_view> indexKey, const std::function<bool(uint64_t, std::string_view, std::string_view)> &cb, bool reverse = false, uint64_t *count = nullptr) {
if (count) *count = 0;
auto cursorT = lmdb::cursor::open(txn, tableDbi);
auto cursorI = lmdb::cursor::open(txn, indexDbi);
std::string_view k, v;
if (startKey) {
k = *startKey;
if (!cursorI.get(k, v, MDB_SET_RANGE)) return;
if (reverse && k != *startKey) cursorI.get(k, v, MDB_PREV);
} else if (indexKey) {
k = *indexKey;
if (!cursorI.get(k, v, MDB_SET)) return;
cursorI.get(k, v, reverse ? MDB_LAST_DUP : MDB_FIRST_DUP);
} else {
if (!cursorI.get(k, v, reverse ? MDB_LAST : MDB_FIRST)) return;
}
if (count) {
if (indexKey) {
*count = cursorI.count();
} else {
*count = indexDbi.size(txn);
}
}
do {
uint64_t primaryKey = lmdb::from_sv<uint64_t>(v);
std::string_view recordKey = lmdb::to_sv<uint64_t>(primaryKey), record;
if (cursorT.get(recordKey, record, MDB_SET)) {
if (!cb(primaryKey, record, k)) return;
} else {
//throw hoytech::error("broken index");
}
} while (cursorI.get(k, v, reverse ? (indexKey ? MDB_PREV_DUP : MDB_PREV) : (indexKey ? MDB_NEXT_DUP : MDB_NEXT)));
}
inline void generic_foreachDup(lmdb::txn &txn, lmdb::dbi &tableDbi, lmdb::dbi &indexDbi, std::string_view k, const std::function<bool(uint64_t, std::string_view)> &cb, bool reverse = false, uint64_t startingPoint = 0, uint64_t *count = nullptr, bool allow_dangling = false) {
std::string_view v;
if (count) *count = 0;
auto cursorT = lmdb::cursor::open(txn, tableDbi);
auto cursorI = lmdb::cursor::open(txn, indexDbi);
if (startingPoint) {
v = lmdb::to_sv<uint64_t>(startingPoint);
if (reverse) {
if (!cursorI.get(k, v, MDB_GET_BOTH_RANGE)) {
if (!cursorI.get(k, v, MDB_SET)) return;
if (!cursorI.get(k, v, MDB_LAST_DUP)) return;
} else {
uint64_t curr = lmdb::from_sv<uint64_t>(v);
if (curr > startingPoint) {
if (!cursorI.get(k, v, MDB_PREV_DUP)) return;
}
}
} else {
if (!cursorI.get(k, v, MDB_GET_BOTH_RANGE)) return;
}
} else {
if (!cursorI.get(k, v, MDB_SET)) return;
if (reverse) cursorI.get(k, v, MDB_LAST_DUP);
else cursorI.get(k, v, MDB_FIRST_DUP);
}
if (count) *count = cursorI.count();
do {
uint64_t primaryKey = lmdb::from_sv<uint64_t>(v);
std::string_view recordKey = lmdb::to_sv<uint64_t>(primaryKey);
std::string_view record;
if (cursorT.get(recordKey, record, MDB_SET)) {
if (!cb(primaryKey, record)) return;
} else {
if (!allow_dangling) throw hoytech::error("broken index");
}
} while (cursorI.get(k, v, reverse ? MDB_PREV_DUP : MDB_NEXT_DUP));
}
[% FOREACH table IN tables.keys %]
inline void foreach_[% table %](lmdb::txn &txn, const std::function<bool(View_[% table %]&)> &cb, bool reverse = false, uint64_t startingPoint = 0) {
generic_foreach(txn, dbi_[% table %], [&cb](uint64_t primaryKey, std::string_view v){
View_[% table %] view(primaryKey, v);
return cb(view);
}, reverse, startingPoint);
}
[% FOREACH indexName IN tables.$table.indices.keys.sort() %]
[% indexDef = tables.$table.indices.$indexName %]
inline void foreach_[% table %]__[% indexName %](lmdb::txn &txn, const std::function<bool(View_[% table %]&, std::string_view)> &cb, bool reverse = false, std::optional<[% utils.index_name_to_type(indexDef) %]> startKey = std::nullopt, uint64_t *count = nullptr) {
generic_foreachByIndex(txn, dbi_[% table %], dbi_[% table %]__[% indexName %], startKey ? std::optional<std::string_view>([% utils.index_conversion(indexDef) %](*startKey)) : std::nullopt, std::nullopt, [&](uint64_t primaryKeyId, std::string_view v, std::string_view indexKey){
View_[% table %] view(primaryKeyId, v);
return cb(view, indexKey);
}, reverse, count);
}
inline void foreach_[% table %]__[% indexName %](lmdb::txn &txn, const std::function<bool(View_[% table %]&)> &cb, bool reverse = false, std::optional<[% utils.index_name_to_type(indexDef) %]> startKey = std::nullopt, uint64_t *count = nullptr) {
foreach_[% table %]__[% indexName %](txn, [&](auto &view, std::string_view){ return cb(view); }, reverse, startKey, count);
}
inline void foreachDup_[% table %]__[% indexName %](lmdb::txn &txn, [% utils.index_name_to_type(indexDef) %] k, const std::function<bool(View_[% table %]&)> &cb, bool reverse = false, std::optional<uint64_t> startingPoint = std::nullopt, uint64_t *count = nullptr) {
generic_foreachDup(txn, dbi_[% table %], dbi_[% table %]__[% indexName %], [% utils.index_conversion(indexDef) %](k), [&cb](uint64_t primaryKeyId, std::string_view v){
View_[% table %] view(primaryKeyId, v);
return cb(view);
}, reverse, startingPoint ? *startingPoint : 0, count);
}
inline void foreachKey_[% table %]__[% indexName %](lmdb::txn &txn, const std::function<bool([% utils.index_name_to_type(indexDef) %] k)> &cb, bool reverse = false, std::optional<std::string_view> startKey = std::nullopt) {
generic_foreachRaw(txn, dbi_[% table %]__[% indexName %], [&cb](std::string_view k, std::string_view){ return cb([% utils.index_conversion_rev(indexDef) %](k)); }, reverse, startKey, true);
}
[% END %]
[% END %]