Experiments in updating faster.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3845 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-02-25 18:52:01 +00:00
parent ac0482d7f5
commit ffaaec5b37
2 changed files with 32 additions and 4 deletions

View File

@ -337,7 +337,6 @@ async function getVotes(db, id) {
f.votes = [].concat(votes, f.votes).slice(0, k_votes_max);
var j = JSON.stringify(f);
if (o != j) {
print('set', id + ':votes');
await db.set(id + ":votes", j);
}
}
@ -369,15 +368,44 @@ core.register('onConnectionsChanged', async function() {
await app.postMessage({connections: connections});
});
async function updateSequences(db) {
var k_batch_max = 100;
var changes = {};
var keys = Object.keys(g_sequence);
for (var i = 0; i < keys.length; i += k_batch_max) {
var ids_batch = keys.slice(i, Math.min(i + k_batch_max, keys.length));
await ssb.sqlStream(
"SELECT "+
" author, "+
" MAX(sequence) AS sequence "+
"FROM messages "+
"WHERE "+
" author IN (" + ids_batch.map(x => '?').join(", ") + ")",
ids_batch,
function(row) {
if (g_sequence[row.author] != row.sequence) {
g_sequence[row.author] = row.sequence;
changes[row.author] = row.sequence;
}
});
}
return changes;
}
async function refresh(selected) {
var timing = [];
timing.push({name: 'start', time: new Date()});
g_following_cache = {};
g_following_deep_cache = {};
g_sequence = {};
await app.postMessage({clear: true});
var whoami = await ssb.whoami();
var db = await database("ssb");
g_sequence = {};
try {
g_sequence = JSON.parse(await db.get('sequence'));
} catch (e) {
}
await updateSequences(db);
timing.push({name: 'init', time: new Date()});
var blocked = await blocking(db, whoami);
timing.push({name: 'blocked', time: new Date()});
@ -437,6 +465,7 @@ async function refresh(selected) {
timing.push({name: 'following', time: new Date()});
await app.postMessage({blocking: {id: whoami, users: [...(g_blocking_cache[whoami] || [])]}});
timing.push({name: 'send_blocking', time: new Date()});
await db.set('sequence', JSON.stringify(g_sequence));
var times = {};
var previous = null;
@ -444,7 +473,6 @@ async function refresh(selected) {
times[t.name] = (t.time - (previous || t).time) / 1000.0 + ' s';
previous = t;
}
await app.postMessage({ready: true, times: times});
}