git ssb

16+

Dominic / patchbay



Tree: 10a6e10b5c3d8204fc1eca07af3083614c9d9db5

Files: 10a6e10b5c3d8204fc1eca07af3083614c9d9db5 / modules_core / names.js

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

Built with git-ssb-web