Files: 697e3e1780dfe42cdc94d4e26e2788489ddfb896 / modules_basic / names.js
3535 bytesRaw
1 | var pull = require('pull-stream') |
2 | var many = require('pull-many') |
3 | var mfr = require('map-filter-reduce') |
4 | |
5 | function all(stream, cb) { |
6 | pull(stream, pull.collect(cb)) |
7 | } |
8 | |
9 | var plugs = require('../plugs') |
10 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
11 | var sbot_query = plugs.first(exports.sbot_query = []) |
12 | |
13 | /* |
14 | filter(rel: ['mentions', prefix('@')]) | reduce(name: rel[1], value: count()) |
15 | */ |
16 | |
17 | var filter = { |
18 | $filter: { |
19 | rel: ["mentions", {$prefix: "@"}] |
20 | } |
21 | } |
22 | var map = { |
23 | $map: { |
24 | name: ['rel', 1], |
25 | id: 'dest', |
26 | ts: 'ts', |
27 | } |
28 | } |
29 | |
30 | var reduce = { |
31 | $reduce: { |
32 | name: 'name', |
33 | id: 'id', |
34 | rank: {$count: true}, |
35 | ts: {$max: 'ts'} |
36 | } |
37 | } |
38 | |
39 | var filter2 = { |
40 | $filter: { |
41 | value: { |
42 | content: { |
43 | type: "about", |
44 | name: {"$prefix": ""}, |
45 | about: {"$prefix": ""} |
46 | } |
47 | } |
48 | } |
49 | } |
50 | |
51 | var map2 = { |
52 | $map: { |
53 | name: ["value", "content", "name"], |
54 | id: ['value', 'content', 'about'], |
55 | ts: "timestamp" |
56 | } |
57 | } |
58 | |
59 | //union with this query... |
60 | |
61 | var names = NAMES = [] |
62 | function update(name) { |
63 | var n = names.find(function (e) { |
64 | return e.id == name.id && e.name == e.name |
65 | }) |
66 | if(!n) { |
67 | name.rank = 1 |
68 | //this should be inserted at the right place... |
69 | names.push(name) |
70 | } |
71 | else |
72 | n.rank = n.rank += (name.rank || 1) |
73 | } |
74 | |
75 | var ready = false, waiting = [] |
76 | |
77 | var merge = { |
78 | $reduce: { |
79 | name: 'name', |
80 | id: 'id', |
81 | rank: {$sum: 'rank'}, |
82 | ts: {$max: 'ts'} |
83 | } |
84 | } |
85 | |
86 | function add_sigil(stream) { |
87 | return pull(stream, pull.map(function (e) { |
88 | if (e && e.id && e.name && e.id[0] !== e.name[0]) |
89 | e.name = e.id[0] + e.name |
90 | return e |
91 | }) |
92 | ) |
93 | } |
94 | |
95 | var queryNamedGitRepos = [ |
96 | {$filter: { |
97 | value: { |
98 | content: { |
99 | type: "git-repo", |
100 | name: {"$prefix": ""} |
101 | } |
102 | } |
103 | }}, |
104 | {$map: { |
105 | name: ["value", "content", "name"], |
106 | id: ['key'], |
107 | ts: "timestamp" |
108 | }}, |
109 | reduce |
110 | ] |
111 | |
112 | |
113 | exports.connection_status = function (err) { |
114 | if(!err) { |
115 | pull( |
116 | many([ |
117 | sbot_links2({query: [filter, map, reduce]}), |
118 | add_sigil(sbot_query({query: [filter2, map2, reduce]})), |
119 | add_sigil(sbot_query({query: queryNamedGitRepos})) |
120 | ]), |
121 | //reducing also ensures order by the lookup properties |
122 | //in this case: [name, id] |
123 | mfr.reduce(merge), |
124 | pull.collect(function (err, ary) { |
125 | if(!err) { |
126 | NAMES = names = ary |
127 | ready = true |
128 | while(waiting.length) waiting.shift()() |
129 | } |
130 | }) |
131 | ) |
132 | |
133 | pull(many([ |
134 | sbot_links2({query: [filter, map], old: false}), |
135 | add_sigil(sbot_query({query: [filter2, map2], old: false})), |
136 | add_sigil(sbot_query({query: queryNamedGitRepos, old: false})) |
137 | ]), |
138 | pull.drain(update)) |
139 | } |
140 | } |
141 | |
142 | function async(fn) { |
143 | return function (value, cb) { |
144 | function go () { cb(null, fn(value)) } |
145 | if(ready) go() |
146 | else waiting.push(go) |
147 | } |
148 | } |
149 | |
150 | function rank(ary) { |
151 | //sort by most used, or most recently used |
152 | return ary.sort(function (a, b) { return b.rank - a.rank || b.ts - a.ts }) |
153 | } |
154 | |
155 | //we are just iterating over the entire array. |
156 | //if this becomes a problem, maintain two arrays |
157 | //one of each sort order, but do not duplicate the objects. |
158 | //that should mean the space required is just 2x object references, |
159 | //not 2x objects, and we can use binary search to find matches. |
160 | |
161 | exports.signifier = async(function (id) { |
162 | return rank(names.filter(function (e) { return e.id == id})) |
163 | }) |
164 | |
165 | exports.signified = async(function (name) { |
166 | var rx = new RegExp('^'+name) |
167 | return rank(names.filter(function (e) { return rx.test(e.name) })) |
168 | }) |
169 | |
170 |
Built with git-ssb-web