Blocking and some random attempts to make things faster.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3843 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-02-21 02:28:53 +00:00
parent 4bb095e81f
commit f4b46cc3a0
7 changed files with 125 additions and 41 deletions

View File

@@ -6,9 +6,9 @@ const k_votes_max = 20;
var g_ready = false;
var g_selected = null;
var g_blocking_cache = {};
var g_following_cache = {};
var g_following_deep_cache = {};
var g_stats = {};
var g_sequence = {};
async function following(db, id) {
@@ -41,9 +41,11 @@ async function following(db, id) {
} else {
f.users.delete(row.contact);
}
f.sequence = row.sequence;
g_sequence[id] = row.sequence;
if (row.sequence) {
f.sequence = row.sequence;
}
});
g_sequence[id] = f.sequence;
var as_set = f.users;
f.users = Array.from(f.users).sort();
var j = JSON.stringify(f);
@@ -55,30 +57,81 @@ async function following(db, id) {
return f.users;
}
async function followingDeep(db, seed_ids, depth) {
async function followingDeep(db, seed_ids, depth, blocked) {
if (depth <= 0) {
return seed_ids;
}
var key = JSON.stringify([seed_ids, depth]);
var key = JSON.stringify([seed_ids, depth, blocked]);
if (g_following_deep_cache[key]) {
return g_following_deep_cache[key];
}
var f = await Promise.all(seed_ids.map(x => following(db, x).then(x => [...x])));
var ids = [].concat(...f);
var x = await followingDeep(db, [...new Set(ids)].sort(), depth - 1);
if (blocked) {
ids = ids.filter(x => !blocked.has(x));
}
var x = await followingDeep(db, [...new Set(ids)].sort(), depth - 1, blocked);
x = [...new Set([].concat(...x, ...seed_ids))].sort();
g_following_deep_cache[key] = x;
return x;
}
async function blocking(db, id) {
if (g_blocking_cache[id]) {
return g_blocking_cache[id];
}
var o = await db.get(id + ":blocking");
const k_version = 5;
var f = o ? JSON.parse(o) : o;
if (!f || f.version != k_version) {
f = {users: [], sequence: 0, version: k_version};
}
f.users = new Set(f.users);
if (!g_sequence[id] || g_sequence[id] > f.sequence) {
await ssb.sqlStream(
"SELECT "+
" sequence, "+
" json_extract(content, '$.contact') AS contact, "+
" json_extract(content, '$.blocking') AS blocking "+
"FROM messages "+
"WHERE "+
" author = ?1 AND "+
" sequence > ?2 AND "+
" json_extract(content, '$.type') = 'contact' "+
"UNION SELECT MAX(sequence) AS sequence, NULL, NULL FROM messages WHERE author = ?1 "+
"ORDER BY sequence",
[id, f.sequence],
function(row) {
if (row.blocking) {
f.users.add(row.contact);
} else {
f.users.delete(row.contact);
}
if (row.sequence) {
f.sequence = row.sequence;
}
});
g_sequence[id] = f.sequence;
}
var as_set = f.users;
f.users = Array.from(f.users).sort();
var j = JSON.stringify(f);
if (o != j) {
await db.set(id + ":blocking", j);
}
f.users = as_set;
g_blocking_cache[id] = f.users;
return f.users;
}
async function getAbout(db, id) {
var o = await db.get(id + ":about");
const k_version = 4;
const k_version = 5;
var f = o ? JSON.parse(o) : o;
if (!f || f.version != k_version) {
f = {about: {}, sequence: 0, version: k_version};
}
if (g_sequence[id] > f.sequence) {
if (g_sequence[id] === undefined || g_sequence[id] > f.sequence) {
await ssb.sqlStream(
"SELECT "+
" sequence, "+
@@ -93,8 +146,6 @@ async function getAbout(db, id) {
"ORDER BY sequence",
[id, f.sequence],
function(row) {
g_stats.rows_read = (g_stats.rows_read || 0) + 1;
f.sequence = row.sequence;
if (row.content) {
var about = {};
try {
@@ -105,12 +156,15 @@ async function getAbout(db, id) {
delete about.type;
f.about = Object.assign(f.about, about);
}
if (row.sequence) {
f.sequence = Math.max(f.sequence, row.sequence);
}
});
}
var j = JSON.stringify(f);
if (o != j) {
g_stats.rows_written = (g_stats.rows_written || 0) + 1;
await db.set(id + ":about", j);
g_sequence[id] = f.sequence;
var j = JSON.stringify(f);
if (o != j) {
await db.set(id + ":about", j);
}
}
return f.about;
}
@@ -152,7 +206,7 @@ async function getRecentPostIds(db, id, ids, limit) {
return await getRecentPostsSingleId(db, ids[0], limit);
}
const k_version = 11;
const k_batch_max = 16;
const k_batch_max = 32;
var o = await db.get(id + ':recent_posts');
var recent = [];
var f = o ? JSON.parse(o) : o;
@@ -251,12 +305,12 @@ async function getRelatedPostIds(db, message, ids, limit) {
async function getVotes(db, id) {
var o = await db.get(id + ":votes");
const k_version = 7;
var votes = [];
var f = o ? JSON.parse(o) : o;
if (!f || f.version != k_version) {
f = {votes: [], sequence: 0, version: k_version};
}
if (!g_sequence[id] || g_sequence[id] > f.sequence) {
if (g_sequence[id] === undefined || g_sequence[id] > f.sequence) {
var votes = [];
await ssb.sqlStream(
"SELECT "+
" author, "+
@@ -269,20 +323,24 @@ async function getVotes(db, id) {
" author = ? AND "+
" sequence > ? AND "+
" json_extract(content, '$.type') = 'vote' "+
"UNION SELECT NULL, NULL, MAX(sequence), NULL, NULL FROM messages WHERE author = ? "+
"UNION SELECT NULL, NULL, MAX(sequence) AS sequence, NULL, NULL FROM messages WHERE author = ? "+
"ORDER BY sequence DESC LIMIT ?",
[id, f.sequence, id, k_votes_max],
function(row) {
if (row.id) {
votes.push(row);
}
f.sequence = Math.max(f.sequence, row.sequence);
if (row.sequence) {
f.sequence = Math.max(f.sequence, row.sequence);
}
});
}
f.votes = [].concat(votes, f.votes).slice(0, k_votes_max);
var j = JSON.stringify(f);
if (o != j) {
await db.set(id + ":votes", j);
g_sequence[id] = f.sequence;
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);
}
}
return f.votes;
}
@@ -322,7 +380,9 @@ async function refresh(selected) {
var whoami = await ssb.whoami();
var db = await database("ssb");
timing.push({name: 'init', time: new Date()});
var all_followed = await followingDeep(db, [whoami], 2);
var blocked = await blocking(db, whoami);
timing.push({name: 'blocked', time: new Date()});
var all_followed = await followingDeep(db, [whoami], 2, blocked);
timing.push({name: 'all_followed', time: new Date()});
if (selected) {
g_selected = selected;
@@ -376,13 +436,13 @@ async function refresh(selected) {
)
);
timing.push({name: 'following', time: new Date()});
print(JSON.stringify(g_stats));
await app.postMessage({blocking: {id: whoami, users: [...(g_blocking_cache[whoami] || [])]}});
timing.push({name: 'send_blocking', time: new Date()});
var times = {};
var previous = null;
for (let t of timing) {
times[t.name] = t.time - (previous || t).time;
times[t.name] = (t.time - (previous || t).time) / 1000.0 + ' s';
previous = t;
}