Commit b5a110f05db20715d91f8c4487810adc05a33d09
break ties my most recent timestamp
Dominic Tarr committed on 8/1/2016, 5:54:30 AMParent: 3369e03e90e8b441a5c23b79c41b65724a98a39e
Files changed
modules/names.js | changed |
modules/names.js | ||
---|---|---|
@@ -30,9 +30,10 @@ | ||
30 | 30 | var reduce = { |
31 | 31 | $reduce: { |
32 | 32 | name: 'name', |
33 | 33 | id: 'id', |
34 | - rank: {$count: true} | |
34 | + rank: {$count: true}, | |
35 | + ts: {$max: 'ts'} | |
35 | 36 | } |
36 | 37 | } |
37 | 38 | |
38 | 39 | var filter2 = { |
@@ -96,8 +97,10 @@ | ||
96 | 97 | many([ |
97 | 98 | sbot_links2({query: [filter, map, reduce]}), |
98 | 99 | add_at(sbot_query({query: [filter2, map2, reduce]})) |
99 | 100 | ]), |
101 | + //reducing also ensures order by the lookup properties | |
102 | + //in this case: [name, id] | |
100 | 103 | mfr.reduce(merge), |
101 | 104 | pull.collect(function (err, ary) { |
102 | 105 | if(!err) { |
103 | 106 | names = ary |
@@ -123,11 +126,18 @@ | ||
123 | 126 | } |
124 | 127 | } |
125 | 128 | |
126 | 129 | function rank(ary) { |
127 | - return ary.sort(function (a, b) { return b.rank - a.rank }) | |
130 | + //sort by most used, or most recently used | |
131 | + return ary.sort(function (a, b) { return b.rank - a.rank || b.ts - a.ts }) | |
128 | 132 | } |
129 | 133 | |
134 | +//we are just iterating over the entire array. | |
135 | +//if this becomes a problem, maintain two arrays | |
136 | +//one of each sort order, but do not duplicate the objects. | |
137 | +//that should mean the space required is just 2x object references, | |
138 | +//not 2x objects, and we can use binary search to find matches. | |
139 | + | |
130 | 140 | exports.signifier = async(function (id) { |
131 | 141 | return rank(names.filter(function (e) { return e.id == id})) |
132 | 142 | }) |
133 | 143 | |
@@ -135,8 +145,4 @@ | ||
135 | 145 | var rx = new RegExp('^'+name) |
136 | 146 | return rank(names.filter(function (e) { return rx.test(e.name) })) |
137 | 147 | }) |
138 | 148 | |
139 | - | |
140 | - | |
141 | - | |
142 | - |
Built with git-ssb-web