Repository navigation
Expand file tree
/
Copy pathevent.cpp
More file actions
232 lines (215 loc) · 9.7 KB
/
Copy pathevent.cpp
File metadata and controls
232 lines (215 loc) · 9.7 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
#include <teaql/runtime/event.hpp>
#include <teaql/runtime/context.hpp>
#include <algorithm>
#include <array>
#include <bit>
#include <limits>
namespace teaql {
namespace {
std::optional<Value> lookup(Memory r, const Record* values, std::string_view field) {
if (!values) {
return {};
}
auto it = values->find(field);
if (it == values->end()) {
return {};
}
return Value(r, it->second);
}
void changes_for_fields(RawAuditEvent& event,
const Record* old_values,
const Record* new_values,
std::span<const String> fields) {
for (const auto& field : fields) {
event.changes.emplace_back(field,
lookup(event.memory_resource(), old_values, field),
lookup(event.memory_resource(), new_values, field));
}
}
void all_changes(RawAuditEvent& event, const Record& values, bool old) {
for (const auto& [field, value] : values) {
std::optional<Value> copy(Value(event.memory_resource(), value));
std::optional<Value> absent;
if (old) {
event.changes.emplace_back(field, copy, absent);
} else {
event.changes.emplace_back(field, absent, copy);
}
}
}
RawAuditEvent
schema(Memory r, RawAuditEventKind kind, std::string_view entity, std::string_view table) {
RawAuditEvent out(std::allocator_arg, Allocator(r), kind, entity);
out.values.emplace("table_name", Value(r, table));
return out;
}
}
RawAuditEvent RawAuditEvent::created(Memory r, std::string_view entity, const Record& values) {
RawAuditEvent out(std::allocator_arg, Allocator(r), RawAuditEventKind::Created, entity);
out.values = clone_record(r, values);
out.new_values.emplace(clone_record(r, values));
all_changes(out, values, false);
return out;
}
RawAuditEvent RawAuditEvent::updated(Memory r, std::string_view entity, const Record& values) {
RawAuditEvent out(std::allocator_arg, Allocator(r), RawAuditEventKind::Updated, entity);
out.values = clone_record(r, values);
out.new_values.emplace(clone_record(r, values));
for (const auto& [field, _] : values) {
out.updated_fields.emplace_back(field);
}
changes_for_fields(out, nullptr, &values, out.updated_fields);
return out;
}
RawAuditEvent RawAuditEvent::updated_with_old_values(Memory r,
std::string_view entity,
const Record& values,
const std::optional<Record>& old_values,
const Record& new_values,
std::span<const String> fields) {
RawAuditEvent out(std::allocator_arg, Allocator(r), RawAuditEventKind::Updated, entity);
out.values = clone_record(r, values);
out.old_values = detail::clone_in(r, old_values);
out.new_values.emplace(clone_record(r, new_values));
for (const auto& field : fields) {
out.updated_fields.emplace_back(field);
}
changes_for_fields(out, old_values ? &*old_values : nullptr, &new_values, fields);
return out;
}
RawAuditEvent RawAuditEvent::deleted(Memory r,
std::string_view entity,
const Value& id,
std::optional<std::int64_t> version) {
RawAuditEvent out(std::allocator_arg, Allocator(r), RawAuditEventKind::Deleted, entity);
out.values.emplace("id", Value(r, id));
if (version) {
out.values.emplace("version", Value(r, *version));
}
return out;
}
RawAuditEvent RawAuditEvent::deleted_with_old_values(Memory r,
std::string_view entity,
const Value& id,
std::optional<std::int64_t> version,
const std::optional<Record>& old_values) {
auto out = deleted(r, entity, id, version);
out.old_values = detail::clone_in(r, old_values);
if (old_values) {
all_changes(out, *old_values, true);
}
return out;
}
RawAuditEvent
RawAuditEvent::recovered(Memory r, std::string_view entity, const Value& id, std::int64_t version) {
auto out = deleted(r, entity, id, version);
out.kind = RawAuditEventKind::Recovered;
return out;
}
Result<RawAuditEvent, MutationVersionError>
RawAuditEvent::recovered_with_old_values(Memory r,
std::string_view entity,
const Value& id,
std::int64_t version,
const std::optional<Record>& old_values) {
auto recovered_version = mutation_version::recovered(r, version);
if (!recovered_version) {
return Result<RawAuditEvent, MutationVersionError>::err(r, recovered_version.error());
}
auto out = recovered(r, entity, id, version);
out.old_values = detail::clone_in(r, old_values);
out.new_values.emplace(old_values ? clone_record(r, *old_values) : Record(r));
out.new_values->insert_or_assign(String("id", r), Value(r, id));
out.new_values->insert_or_assign(String("version", r), Value(r, recovered_version.value()));
String field("version", r);
changes_for_fields(
out, old_values ? &*old_values : nullptr, &*out.new_values, std::span(&field, 1));
return Result<RawAuditEvent, MutationVersionError>::ok(r, std::move(out));
}
Result<RawAuditEvent, MutationVersionError>
RawAuditEvent::recovered_with_changes(Memory resource,
std::string_view entity,
const Value& id,
std::int64_t version,
const std::optional<Record>& old_values,
const Record& values) {
auto result = recovered_with_old_values(resource, entity, id, version, old_values);
if (!result || values.empty()) {
return result;
}
auto& event = result.value();
for (const auto& [field, value] : values) {
event.updated_fields.emplace_back(field);
event.values.insert_or_assign(String(field, resource), Value(resource, value));
event.new_values->insert_or_assign(String(field, resource), Value(resource, value));
}
event.updated_fields.emplace_back("version");
event.changes.clear();
changes_for_fields(event,
event.old_values ? &*event.old_values : nullptr,
&*event.new_values,
event.updated_fields);
return result;
}
RawAuditEvent RawAuditEvent::schema_created(Memory r,
std::string_view entity,
std::string_view table,
std::size_t count) {
auto out = schema(r, RawAuditEventKind::SchemaCreated, entity, table);
out.values.emplace("field_count", Value(r, std::bit_cast<std::int64_t>(std::uint64_t(count))));
all_changes(out, out.values, false);
return out;
}
RawAuditEvent RawAuditEvent::schema_verified(Memory r,
std::string_view entity,
std::string_view table,
std::size_t count) {
auto out = schema_created(r, entity, table, count);
out.kind = RawAuditEventKind::SchemaVerified;
return out;
}
RawAuditEvent RawAuditEvent::field_added(Memory r,
std::string_view entity,
std::string_view table,
std::string_view field) {
auto out = schema(r, RawAuditEventKind::FieldAdded, entity, table);
out.values.emplace("field_name", Value(r, field));
all_changes(out, out.values, false);
return out;
}
RawAuditEvent RawAuditEvent::data_seeded(Memory r,
std::string_view entity,
std::string_view table,
std::size_t inserted,
std::size_t updated) {
auto out = schema(r, RawAuditEventKind::DataSeeded, entity, table);
out.values.emplace("inserted", Value(r, std::bit_cast<std::int64_t>(std::uint64_t(inserted))));
out.values.emplace("updated", Value(r, std::bit_cast<std::int64_t>(std::uint64_t(updated))));
all_changes(out, out.values, false);
return out;
}
Result<std::monostate, RuntimeError> InMemoryRawAuditEventSink::on_event(
Memory r, const UserContext& context, const RawAuditEvent& event) const {
// Each argument can borrow a different scope. Register each distinct arena
// once; a synchronous callback cannot join its own stack during teardown.
const std::array domains{
context.memory_resource(), memory_resource(), event.memory_resource(), r};
std::array<Arena::Quiescence, 4> active;
for (std::size_t index = 0; index < domains.size(); ++index) {
if (std::find(domains.begin(), domains.begin() + index, domains[index]) !=
domains.begin() + index) {
continue;
}
active[index].attach(detail::resource_arena(domains[index]), nullptr, [](void*) noexcept {
std::terminate();
});
}
for (const auto& sink : sinks_) {
auto result = sink.get().on_event(r, context, event);
if (!result) {
return result;
}
}
return Result<std::monostate, RuntimeError>::ok(r, {});
}
}