-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodify.h.tt
More file actions
260 lines (203 loc) · 9.35 KB
/
modify.h.tt
File metadata and controls
260 lines (203 loc) · 9.35 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Insert methods
[% FOREACH table IN tables.keys %]
[% BLOCK indexManagementPrefix %]
(void)v;
[% FOREACH index IN tables.$table.indices.keys.sort() %]
[% indexDef = tables.$table.indices.$index %]
[% integer_index = indexDef.integer %]
[% multi_index = indexDef.multi %]
[% IF multi_index %]
std::vector<[% integer_index ? 'uint64_t' : 'std::string' %]> [% indexDef.accessor %];
[% IF indexDef.from_field %]
for (auto &f : v.[% indexDef.accessor %]()) {
[% IF integer_index %]
[% indexDef.accessor %].push_back(f);
[% ELSE %]
[% indexDef.accessor %].push_back(std::string(f));
[% END %]
}
[% END %]
[% ELSE %]
std::optional<[% integer_index ? 'uint64_t' : 'std::string' %]> [% indexDef.accessor -%];
[% IF indexDef.from_field %]
{
auto temp = v.[% indexDef.accessor %]();
[% IF integer_index %]
[% IF !indexDef.includeZero %]if (temp)[% END %] [% indexDef.accessor %] = temp;
[% ELSE %]
[% IF indexDef.includeZero %]#error Non-integer indices cannot have includeZero[% END %]
if (temp.size()) [% indexDef.accessor %] = std::string(temp);
[% END %]
}
[% END %]
[% END %]
[% END %]
[% tables.$table.indexPrelude %]
[% END %]
void _insertIndices_[% table %](lmdb::txn &txn, View_[% table %] &v) {
[% PROCESS indexManagementPrefix %]
[% FOREACH index IN tables.$table.indices.keys.sort() %]
[% indexDef = tables.$table.indices.$index %]
[% integer_index = indexDef.integer %]
[% multi_index = indexDef.multi %]
{
[% IF multi_index %]
for (auto &i : [% indexDef.accessor %]) dbi_[% table %]__[% index %].put(txn, [% IF integer_index %]lmdb::to_sv<uint64_t>[% END %](i), lmdb::to_sv<uint64_t>(v.primaryKeyId));
[% ELSE %]
if ([% indexDef.accessor %]) {
auto &indexAccessor_ = *[% indexDef.accessor %];
[% IF indexDef.exists('unique') %]
std::string_view junk;
if (dbi_[% table %]__[% index %].get(txn, [% IF integer_index %]lmdb::to_sv<uint64_t>[% END %](indexAccessor_), junk)) {
throw hoytech::error("unique constraint violated: ", "[% table %].[% index %]");
}
[% END %]
dbi_[% table %]__[% index %].put(txn, [% IF integer_index %]lmdb::to_sv<uint64_t>[% END %](indexAccessor_), lmdb::to_sv<uint64_t>(v.primaryKeyId));
}
[% END %]
}
[% END %]
}
void _deleteIndices_[% table %](lmdb::txn &txn, View_[% table %] &v) {
[% PROCESS indexManagementPrefix %]
[% FOREACH index IN tables.$table.indices.keys.sort() %]
[% indexDef = tables.$table.indices.$index %]
[% integer_index = indexDef.integer %]
[% multi_index = indexDef.multi %]
{
[% IF multi_index %]
for (auto &i : [% indexDef.accessor %]) dbi_[% table %]__[% index %].del(txn, [% IF integer_index %]lmdb::to_sv<uint64_t>[% END %](i), lmdb::to_sv<uint64_t>(v.primaryKeyId));
[% ELSE %]
if ([% indexDef.accessor %]) {
auto &indexAccessor_ = *[% indexDef.accessor %];
dbi_[% table %]__[% index %].del(txn, [% IF integer_index %]lmdb::to_sv<uint64_t>[% END %](indexAccessor_), lmdb::to_sv<uint64_t>(v.primaryKeyId));
}
[% END %]
}
[% END %]
}
uint64_t insert_[% table %](lmdb::txn &txn,
[% FOREACH field IN tables.$table.fields %]
[% utils.type_to_cpp(field.type) %] param__[% field.name %],
[% END %]
[% IF tables.$table.opaque %]
[% IF tables.$table.primaryKey %] uint64_t param__[% tables.$table.primaryKey %], [% END %]
std::string_view buf,
[% END %]
std::optional<View_[% table %]> prev = std::nullopt
) {
[% IF !tables.$table.opaque %]
flatbuffers::FlatBufferBuilder builder;
{
[% FOREACH field IN tables.$table.fields %]
[% IF field.type == "string" %]
auto builder_ptr__[% field.name %] = builder.CreateString(param__[% field.name %]);
[% ELSIF field.type == "ubytes" %]
auto builder_ptr__[% field.name %] = builder.CreateVector((uint8_t*)(param__[% field.name %].data()), param__[% field.name %].size());
[% ELSIF field.type == "string[]" || field.type == "ubytes[]" %]
auto builder_ptr__[% field.name %] = builder.CreateVector([&]{
std::vector<flatbuffers::Offset<flatbuffers::String>> vec;
for (auto &p : param__[% field.name %]) vec.push_back(builder.CreateString(p));
return vec;
}());
[% ELSIF field.type == "uint64[]" %]
auto builder_ptr__[% field.name %] = builder.CreateVector((uint64_t*)(param__[% field.name %].data()), param__[% field.name %].size());
[% END %]
[% END %]
[% db %]::[% table %]Builder mb(builder);
[% FOREACH field IN tables.$table.fields %]
[% IF field.type == "string" || field.type == "ubytes" || field.type == "string[]" || field.type == "ubytes[]" || field.type == "uint64[]" %]
mb.add_[% field.name %](builder_ptr__[% field.name %]);
[% ELSE %]
mb.add_[% field.name %](param__[% field.name %]);
[% END %]
[% END %]
builder.Finish(mb.Finish());
}
auto buf = std::string_view(reinterpret_cast<char*>(builder.GetBufferPointer()), builder.GetSize());
[% END %]
uint64_t primaryKeyId;
if (prev) {
primaryKeyId = prev->primaryKeyId;
// If all parameters are the same, update is a no-op
// NOTE: special return value of 0 indicates no update was done
if (buf == prev->buf) return 0;
_deleteIndices_[% table %](txn, *prev);
dbi_[% table %].put(txn, lmdb::to_sv<uint64_t>(primaryKeyId), buf);
} else {
[% IF tables.$table.primaryKey %]
primaryKeyId = param__[% tables.$table.primaryKey %];
[% ELSE %]
primaryKeyId = get_next_integer_key(txn, dbi_[% table %]);
[% END %]
bool inserted = dbi_[% table %].put(txn, lmdb::to_sv<uint64_t>(primaryKeyId), buf, MDB_NOOVERWRITE [% IF !tables.$table.primaryKey %]| MDB_APPEND[% END %]);
if (!inserted) throw hoytech::error("duplicate insert into [% table %]");
}
{
View_[% table %] v(primaryKeyId, buf);
_insertIndices_[% table %](txn, v);
}
return primaryKeyId;
}
[% IF !tables.$table.opaque %]
struct Updates_[% table %] {
[% FOREACH field IN tables.$table.fields %]
std::optional<[% utils.type_to_cpp(field.type) %]> [% field.name %];
[% END %]
};
uint64_t update_[% table %](lmdb::txn &txn, View_[% table %] prev, Updates_[% table %] upd) {
return insert_[% table %](txn, [% FOREACH field IN tables.$table.fields %]upd.[% field.name %] ? *upd.[% field.name %] : prev.[% field.name %](), [% END %]prev);
}
[% END %]
void delete_[% table %](lmdb::txn &txn, uint64_t primaryKeyId, bool allowNop = true) {
std::string_view buf;
bool found = dbi_[% table %].get(txn, lmdb::to_sv<uint64_t>(primaryKeyId), buf);
if (!found) {
if (allowNop) return;
throw hoytech::error("unable to find [% table %] record to delete");
}
// assume it's already verified since it's from the DB
//bool ok = flatbuffers::Verifier(reinterpret_cast<const uint8_t*>(buf.data()), buf.size()).VerifyBuffer<[% db %]::[% table %]>(nullptr);
//if (!ok) throw hoytech::error("verification failed for insert of [% table %]");
{
View_[% table %] v(primaryKeyId, buf);
_deleteIndices_[% table %](txn, v);
}
dbi_[% table %].del(txn, lmdb::to_sv<uint64_t>(primaryKeyId));
}
void reIndex_[% table %](lmdb::txn &txn, uint64_t primaryKeyId) {
std::string_view buf;
bool found = dbi_[% table %].get(txn, lmdb::to_sv<uint64_t>(primaryKeyId), buf);
if (!found) {
throw hoytech::error("unable to find [% table %] record to reindex");
}
{
View_[% table %] v(primaryKeyId, buf);
_deleteIndices_[% table %](txn, v);
_insertIndices_[% table %](txn, v);
}
}
struct Indices_[% table %] {
[% FOREACH index IN tables.$table.indices.keys.sort() %]
[% indexDef = tables.$table.indices.$index %]
[% integer_index = indexDef.integer %]
[% multi_index = indexDef.multi %]
[% IF multi_index %]
std::vector<[% integer_index ? 'uint64_t' : 'std::string' %]> [% indexDef.accessor %];
[% ELSE %]
std::optional<[% integer_index ? 'uint64_t' : 'std::string' %]> [% indexDef.accessor -%];
[% END %]
[% END %]
};
struct Indices_[% table %] getIndices_[% table %](View_[% table %] &v) {
[% PROCESS indexManagementPrefix %]
struct Indices_[% table %] _output;
[% FOREACH index IN tables.$table.indices.keys.sort() %]
[% indexDef = tables.$table.indices.$index %]
[% integer_index = indexDef.integer %]
[% multi_index = indexDef.multi %]
_output.[% index %] = std::move([% index %]);
[% END %]
return _output;
}
[% END %]