git ssb

16+

Dominic / patchbay



Tree: 7041fd47bd18aed28bf562a96b236b93c482d4c2

Files: 7041fd47bd18aed28bf562a96b236b93c482d4c2 / modules_core / names.js

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

Built with git-ssb-web