git ssb

7+

dinoworm 🐛 / patchcore



Commit d4680d520c5801ecd428d4e65f37c47ae0c5bd9e

modify rollup to pass thru roots if already available, lru cache on root lookup

Matt McKegg committed on 6/22/2017, 2:31:23 AM
Parent: f3e40defadc9708c81d36dc78c0311e673480e3d

Files changed

feed/pull/rollup.jschanged
package.jsonchanged
feed/pull/rollup.jsView
@@ -4,8 +4,9 @@
44
55 var pull = require('pull-stream')
66 var nest = require('depnest')
77 var extend = require('xtend')
8 +var HLRU = require('hashlru')
89
910 exports.needs = nest({
1011 'sbot.pull.backlinks': 'first',
1112 'sbot.async.get': 'first',
@@ -15,42 +16,86 @@
1516
1617 exports.gives = nest('feed.pull.rollup', true)
1718
1819 exports.create = function (api) {
20 + // cache mostly just to avoid reading the same roots over and over again
21 + // not really big enough for multiple refresh cycles
22 + var cache = HLRU(100)
23 +
1924 return nest('feed.pull.rollup', function (rootFilter) {
25 + var seen = new Set()
2026 return pull(
21- pull.map(msg => api.message.sync.root(msg) || msg.key),
22- pull.unique(),
23- Lookup(),
27 + pull.map(msg => {
28 + if (msg.value) {
29 + var root = api.message.sync.root(msg)
30 + if (!root) {
31 + // already a root, pass thru!
32 + return msg
33 + } else {
34 + return root
35 + }
36 + }
37 + }),
38 +
39 + // UNIQUE
40 + pull.filter(idOrMsg => {
41 + if (idOrMsg) {
42 + if (idOrMsg.key) idOrMsg = idOrMsg.key
43 + if (typeof idOrMsg === 'string') {
44 + var key = idOrMsg
45 + if (!seen.has(key)) {
46 + seen.add(key)
47 + return true
48 + }
49 + }
50 + }
51 + }),
52 +
53 + // LOOKUP (if needed)
54 + pull.asyncMap((keyOrMsg, cb) => {
55 + if (keyOrMsg.value) {
56 + cb(null, keyOrMsg)
57 + } else {
58 + var key = keyOrMsg
59 + if (cache.has(key)) {
60 + cb(null, cache.get(key))
61 + } else {
62 + api.sbot.async.get(key, (_, value) => {
63 + var msg = {key, value}
64 + if (msg.value) {
65 + cache.set(key, msg)
66 + }
67 + cb(null, msg)
68 + })
69 + }
70 + }
71 + }),
72 +
73 + // UNBOX (if needed)
74 + pull.map(msg => {
75 + if (msg.value && typeof msg.value.content === 'string') {
76 + var unboxed = api.message.sync.unbox(msg)
77 + if (unboxed) return unboxed
78 + }
79 + return msg
80 + }),
81 +
82 + // FILTER
2483 pull.filter(msg => msg && msg.value && !api.message.sync.root(msg)),
2584 pull.filter(rootFilter || (() => true)),
26- AddReplies()
85 +
86 + // ADD REPLIES
87 + pull.asyncMap((rootMessage, cb) => {
88 + pull(
89 + api.sbot.pull.backlinks({
90 + query: [{$filter: { dest: rootMessage.key }}]
91 + }),
92 + pull.filter(msg => (api.message.sync.root(msg) || rootMessage.key) === rootMessage.key),
93 + pull.collect((err, replies) => {
94 + if (err) return cb(err)
95 + cb(null, extend(rootMessage, { replies }))
96 + })
97 + )
98 + })
2799 )
28100 })
29-
30- // scoped
31- function Lookup () {
32- return pull.asyncMap((key, cb) => {
33- api.sbot.async.get(key, (_, value) => {
34- if (value && typeof value.content === 'string') {
35- value = api.message.sync.unbox(value)
36- }
37- cb(null, {key, value})
38- })
39- })
40- }
41-
42- function AddReplies () {
43- return pull.asyncMap((rootMessage, cb) => {
44- pull(
45- api.sbot.pull.backlinks({
46- query: [{$filter: { dest: rootMessage.key }}]
47- }),
48- pull.filter(msg => (api.message.sync.root(msg) || rootMessage.key) === rootMessage.key),
49- pull.collect((err, replies) => {
50- if (err) return cb(err)
51- cb(null, extend(rootMessage, { replies }))
52- })
53- )
54- })
55- }
56101 }
package.jsonView
@@ -36,8 +36,9 @@
3636 "color-hash": "^1.0.3",
3737 "depnest": "^1.0.2",
3838 "emoji-named-characters": "^1.0.2",
3939 "es2040": "^1.2.4",
40 + "hashlru": "^2.2.0",
4041 "html-escape": "^2.0.0",
4142 "human-time": "0.0.1",
4243 "mutant": "^3.21.0",
4344 "mutant-pull-reduce": "^1.1.0",

Built with git-ssb-web