sync optimisations, DBQuery no longer loads eventPayload

- It is now up to the caller to do so
- QueryScheduler now can optionally not bother to ensure that the events are fresh
This commit is contained in:
Doug Hoyte
2023-07-29 01:14:38 -04:00
parent 94a60c3ad2
commit 206b14a473
8 changed files with 59 additions and 35 deletions

View File

@ -1,7 +1,6 @@
#include <Negentropy.h>
#include "RelayServer.h"
#include "DBQuery.h"
#include "QueryScheduler.h"
@ -9,6 +8,7 @@ struct NegentropyViews {
struct UserView {
Negentropy ne;
std::string initialMsg;
std::vector<uint64_t> levIds;
uint64_t startTime = hoytech::curr_time_us();
};
@ -63,17 +63,18 @@ void RelayServer::runNegentropy(ThreadPool<MsgNegentropy>::Thread &thr) {
QueryScheduler queries;
NegentropyViews views;
queries.ensureExists = false;
queries.onEventBatch = [&](lmdb::txn &txn, const auto &sub, const std::vector<uint64_t> &levIds){
auto *view = views.findView(sub.connId, sub.subId);
if (!view) return;
for (auto levId : levIds) {
auto ev = lookupEventByLevId(txn, levId);
view->ne.addItem(ev.flat_nested()->created_at(), sv(ev.flat_nested()->id()).substr(0, view->ne.idSize));
view->levIds.push_back(levId);
}
};
queries.onComplete = [&](Subscription &sub){
queries.onComplete = [&](lmdb::txn &txn, Subscription &sub){
auto *view = views.findView(sub.connId, sub.subId);
if (!view) return;
@ -94,6 +95,20 @@ void RelayServer::runNegentropy(ThreadPool<MsgNegentropy>::Thread &thr) {
return;
}
std::sort(view->levIds.begin(), view->levIds.end());
for (auto levId : view->levIds) {
try {
auto ev = lookupEventByLevId(txn, levId);
view->ne.addItem(ev.flat_nested()->created_at(), sv(ev.flat_nested()->id()).substr(0, view->ne.idSize));
} catch (std::exception &) {
// levId was deleted when query was paused
}
}
view->levIds.clear();
view->levIds.shrink_to_fit();
view->ne.seal();
auto resp = view->ne.reconcile(view->initialMsg);