From bb81fc87b91bee91818e843a3d15f5047e20108f Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 8 Jan 2022 21:12:36 +0000 Subject: [PATCH] Fast path for viewing a single user's feed, and reduce churn in collecting votes. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3741 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/cory/index.json | 2 +- apps/cory/index/app.js | 43 ++++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/apps/cory/index.json b/apps/cory/index.json index 5491dfda..43312926 100644 --- a/apps/cory/index.json +++ b/apps/cory/index.json @@ -1 +1 @@ -{"type":"tildefriends-app","files":{"app.js":"&SJrK87ZgHkxZQDB1nm/Xej/bzajzWpiY9a384zqZVzE=.sha256","index.html":"&O8+Vr4YHP8NIORZzM3Ee9tyLXr2xCETT2Eva0rO5ebA=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&DdJwZYEo7AqFyutYMvEjykoVXxdHVog0UXye6Sbo0TU=.sha256","tf-message.js":"&ox9wj2+P3BhqokfHPI3ZMpSYNpNhl91K5R6Z4GbWxCA=.sha256","tf.js":"&rUCI1QKOoyWcFegV72DfG/P7HykmkX4DcvWV87hCeBM=.sha256","commonmark.min.js":"&5x6ek3tFrKTZX6hXNNyFsjmhvrjmWpUkwuuaiyVV1Us=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256"}} \ No newline at end of file +{"type":"tildefriends-app","files":{"app.js":"&F1q+r6zHWzbJE9t2rjrhupV2xCIFyJNabXkbGWjHTQQ=.sha256","index.html":"&O8+Vr4YHP8NIORZzM3Ee9tyLXr2xCETT2Eva0rO5ebA=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&DdJwZYEo7AqFyutYMvEjykoVXxdHVog0UXye6Sbo0TU=.sha256","tf-message.js":"&ox9wj2+P3BhqokfHPI3ZMpSYNpNhl91K5R6Z4GbWxCA=.sha256","tf.js":"&rUCI1QKOoyWcFegV72DfG/P7HykmkX4DcvWV87hCeBM=.sha256","commonmark.min.js":"&5x6ek3tFrKTZX6hXNNyFsjmhvrjmWpUkwuuaiyVV1Us=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256"}} \ No newline at end of file diff --git a/apps/cory/index/app.js b/apps/cory/index/app.js index 5f6b1597..3f6b0053 100644 --- a/apps/cory/index/app.js +++ b/apps/cory/index/app.js @@ -134,7 +134,32 @@ function fnv32a(value) return result >>> 0; } +async function getRecentPostsSingleId(db, id, limit) { + var recent = []; + await ssb.sqlStream( + "SELECT "+ + " rowid, "+ + " id, "+ + " timestamp "+ + "FROM messages "+ + "WHERE "+ + " author = ? AND "+ + " json_extract(content, '$.type') = 'post' "+ + "ORDER BY sequence DESC LIMIT ?", + [id, limit], + function(row) { + if (row.id) { + recent.push({id: row.id, timestamp: row.timestamp}); + } + }); + recent.sort((x, y) => y.timestamp - x.timestamp); + return recent.map(x => x.id); +} + async function getRecentPostIds(db, id, ids, limit) { + if (ids.length == 1) { + return await getRecentPostsSingleId(db, ids[0], limit); + } const k_version = 11; const k_batch_max = 16; var o = await db.get(id + ':recent_posts'); @@ -192,15 +217,14 @@ async function getRecentPostIds(db, id, ids, limit) { async function getVotes(db, id) { var o = await db.get(id + ":votes"); - const k_version = 5; + const k_version = 7; var votes = []; var f = o ? JSON.parse(o) : o; if (!f || f.version != k_version) { - f = {votes: [], rowid: 0, version: k_version}; + f = {votes: [], sequence: 0, version: k_version}; } await ssb.sqlStream( "SELECT "+ - " rowid, "+ " author, "+ " id, "+ " sequence, "+ @@ -208,20 +232,19 @@ async function getVotes(db, id) { " content "+ "FROM messages "+ "WHERE "+ - " rowid > ? AND "+ " author = ? AND "+ + " sequence > ? AND "+ " json_extract(content, '$.type') = 'vote' "+ - "UNION SELECT MAX(rowid) as rowid, NULL, NULL AS id, NULL, NULL, NULL FROM messages "+ - "ORDER BY rowid DESC LIMIT ?", - [f.rowid, id, k_votes_max], + "UNION SELECT NULL, NULL, MAX(sequence), NULL, NULL FROM messages WHERE author = ? "+ + "ORDER BY sequence DESC LIMIT ?", + [f.sequence, id, id, k_votes_max], function(row) { if (row.id) { votes.push(row); - } else { - f.rowid = row.rowid; } + f.sequence = Math.max(f.sequence, row.sequence); }); - f.votes = [].concat(votes.reverse(), f.votes).slice(0, k_votes_max); + 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);