Repository navigation
Expand file tree
/
Copy pathquery_stores.cpp
More file actions
291 lines (259 loc) · 9.9 KB
/
Copy pathquery_stores.cpp
File metadata and controls
291 lines (259 loc) · 9.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <teaql/runtime/cache.hpp>
#include <teaql/runtime/pagination.hpp>
#include <charconv>
namespace teaql {
std::optional<Vector<CompactRow>>
InMemoryAggregationCache::get(Memory r, std::string_view key, std::uint64_t age) const {
auto it = entries_.find(key);
if (it == entries_.end()) {
return {};
}
auto elapsed = std::chrono::steady_clock::now() - it->second.stored_at;
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed);
if (age && elapsed > decltype(elapsed)::zero() &&
(std::uint64_t(millis.count()) > age ||
(std::uint64_t(millis.count()) == age && elapsed > millis))) {
return {};
}
return Vector<CompactRow>(it->second.rows, r);
}
void InMemoryAggregationCache::put(std::string_view key, std::span<const CompactRow> rows) {
auto r = memory_resource();
auto it = entries_.find(key);
if (it != entries_.end()) {
Entry entry(std::allocator_arg, Allocator(r), rows);
it->second.rows.swap(entry.rows);
it->second.stored_at = entry.stored_at;
} else {
entries_.emplace(
std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(rows));
}
}
void InMemoryAggregationCache::invalidate_namespace(std::string_view name) {
std::erase_if(entries_, [&](const auto& entry) {
return entry.first.starts_with(name) &&
std::string_view(entry.first).substr(name.size()).starts_with("::");
});
}
namespace {
String cursor_key(Memory r, std::string_view query, std::uint64_t offset) {
String key(query, r);
key += ':';
char buffer[32];
auto end = std::to_chars(buffer, buffer + sizeof(buffer), offset).ptr;
key.append(buffer, end);
return key;
}
template <class Map>
void evict_earliest(Map& map) {
auto oldest = std::min_element(map.begin(), map.end(), [](const auto& a, const auto& b) {
return a.second.expires_at < b.second.expires_at;
});
if (oldest != map.end()) {
map.erase(oldest);
}
}
std::size_t saturating_add(std::size_t a, std::size_t b) {
return b > std::numeric_limits<std::size_t>::max() - a ? std::numeric_limits<std::size_t>::max()
: a + b;
}
std::size_t id_bytes(std::size_t count) {
return count > std::numeric_limits<std::size_t>::max() / sizeof(std::uint64_t)
? std::numeric_limits<std::size_t>::max()
: count * sizeof(std::uint64_t);
}
}
std::optional<ContinuousPageCursor>
InMemoryContinuousPageCursorStore::get_now(Memory r, std::string_view query, std::uint64_t offset) {
auto key = cursor_key(r, query, offset);
auto it = cursors_.find(key);
if (it == cursors_.end()) {
return {};
}
if (it->second.expires_at <= SystemTime::now()) {
cursors_.erase(it);
return {};
}
return detail::clone_in(r, it->second);
}
void InMemoryContinuousPageCursorStore::put_now(const ContinuousPageCursor& cursor) {
auto r = memory_resource();
auto key = cursor_key(r, cursor.query_key, cursor.next_offset);
auto copy = detail::clone_in(r, cursor);
if (cursors_.size() >= max_entries_) {
evict_earliest(cursors_);
}
cursors_.insert_or_assign(std::move(key), std::move(copy));
}
void InMemoryContinuousPageCursorStore::invalidate_now(std::string_view key) {
std::erase_if(cursors_, [&](const auto& entry) {
return entry.first.starts_with(key) &&
std::string_view(entry.first).substr(key.size()).starts_with(':');
});
}
std::optional<RetainedIdSet> InMemoryIdSetStore::get_now(Memory r, std::string_view key) {
auto it = sets_.find(key);
if (it == sets_.end()) {
return {};
}
if (it->second.expires_at <= SystemTime::now()) {
sets_.erase(it);
return {};
}
return detail::clone_in(r, it->second);
}
std::size_t InMemoryIdSetStore::retained_bytes() const {
std::size_t total = 0;
for (const auto& [key, value] : sets_) {
(void)key;
total += id_bytes(value.ids.size());
}
return total;
}
PaginationWriteResult InMemoryIdSetStore::put_now(Memory result, const RetainedIdSet& input) {
auto incoming = id_bytes(input.ids.size());
if (incoming > max_bytes_) {
return PaginationWriteResult::err(
result, String("ID set exceeds the process-local store memory ceiling", result));
}
std::erase_if(sets_, [](const auto& entry) {
return entry.second.expires_at <= SystemTime::now();
});
while (sets_.size() >= max_entries_ ||
saturating_add(retained_bytes(), incoming) > max_bytes_) {
if (sets_.empty()) {
break;
}
evict_earliest(sets_);
}
sets_.insert_or_assign(String(input.query_key, memory_resource()),
detail::clone_in(memory_resource(), input));
return PaginationWriteResult::ok(result, {});
}
void InMemoryIdSetStore::invalidate_now(std::string_view key) {
auto it = sets_.find(key);
if (it != sets_.end()) {
sets_.erase(it);
}
}
namespace detail {
// These immediate in-memory operations are still lazy futures. Their state
// snapshots input in the result Context and borrows a store that outlives it.
struct CursorStoreOperation {
using allocator_type = Allocator;
ResourceBorrow<InMemoryContinuousPageCursorStore> store;
String key;
std::uint64_t offset = 0;
std::optional<ContinuousPageCursor> cursor;
CursorStoreOperation(std::allocator_arg_t,
Allocator a,
InMemoryContinuousPageCursorStore& store,
std::string_view key,
std::uint64_t offset,
const ContinuousPageCursor* cursor)
: store(std::allocator_arg, a, store, resource_arena(store.memory_resource())),
key(key, a.resource()), offset(offset) {
if (cursor) {
this->cursor.emplace(std::allocator_arg, a, *cursor);
}
}
auto read() {
return PaginationResult<std::optional<ContinuousPageCursor>>::ok(
key.get_allocator().resource(),
store.get().get_now(key.get_allocator().resource(), key, offset));
}
auto write() {
if (cursor) {
store.get().put_now(*cursor);
} else {
store.get().invalidate_now(key);
}
return PaginationWriteResult::ok(key.get_allocator().resource(), {});
}
};
struct IdSetStoreOperation {
using allocator_type = Allocator;
ResourceBorrow<InMemoryIdSetStore> store;
String key;
std::optional<RetainedIdSet> ids;
IdSetStoreOperation(std::allocator_arg_t,
Allocator a,
InMemoryIdSetStore& store,
std::string_view key,
const RetainedIdSet* ids)
: store(std::allocator_arg, a, store, resource_arena(store.memory_resource())),
key(key, a.resource()) {
if (ids) {
this->ids.emplace(std::allocator_arg, a, *ids);
}
}
auto read() {
return PaginationResult<std::optional<RetainedIdSet>>::ok(
key.get_allocator().resource(),
store.get().get_now(key.get_allocator().resource(), key));
}
auto write() {
if (ids) {
return store.get().put_now(key.get_allocator().resource(), *ids);
}
store.get().invalidate_now(key);
return PaginationWriteResult::ok(key.get_allocator().resource(), {});
}
};
template <class Operation, bool Read>
struct StoreOperation : Operation {
using Operation::Operation;
using R = decltype([](Operation& op) {
if constexpr (Read) {
return op.read();
} else {
return op.write();
}
}(std::declval<Operation&>()));
std::optional<R> poll(WakeSignal&) {
if constexpr (Read) {
return this->read();
} else {
return this->write();
}
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {}
};
}
AsyncOperation<PaginationResult<std::optional<ContinuousPageCursor>>>&
InMemoryContinuousPageCursorStore::get(Memory r, std::string_view key, std::uint64_t offset) {
return make_operation<PaginationResult<std::optional<ContinuousPageCursor>>,
detail::StoreOperation<detail::CursorStoreOperation, true>>(
r, *this, key, offset, nullptr);
}
AsyncOperation<PaginationWriteResult>&
InMemoryContinuousPageCursorStore::put(Memory r, const ContinuousPageCursor& cursor) {
return make_operation<PaginationWriteResult,
detail::StoreOperation<detail::CursorStoreOperation, false>>(
r, *this, std::string_view{}, 0, &cursor);
}
AsyncOperation<PaginationWriteResult>&
InMemoryContinuousPageCursorStore::invalidate(Memory r, std::string_view key) {
return make_operation<PaginationWriteResult,
detail::StoreOperation<detail::CursorStoreOperation, false>>(
r, *this, key, 0, nullptr);
}
AsyncOperation<PaginationResult<std::optional<RetainedIdSet>>>&
InMemoryIdSetStore::get(Memory r, std::string_view key) {
return make_operation<PaginationResult<std::optional<RetainedIdSet>>,
detail::StoreOperation<detail::IdSetStoreOperation, true>>(
r, *this, key, nullptr);
}
AsyncOperation<PaginationWriteResult>& InMemoryIdSetStore::put(Memory r, const RetainedIdSet& ids) {
return make_operation<PaginationWriteResult,
detail::StoreOperation<detail::IdSetStoreOperation, false>>(
r, *this, std::string_view{}, &ids);
}
AsyncOperation<PaginationWriteResult>& InMemoryIdSetStore::invalidate(Memory r,
std::string_view key) {
return make_operation<PaginationWriteResult,
detail::StoreOperation<detail::IdSetStoreOperation, false>>(
r, *this, key, nullptr);
}
}