-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
292 lines (248 loc) · 8.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict'
require('make-promises-safe')
const assert = require('assert')
const deepEqual = require('fast-deep-equal')
const lodash = require('lodash')
async function setPlugin (upring, opts) {
if (upring.kv === undefined) {
throw new Error('Missig `upring-kv` plugin')
}
upring.set = new SetUtils(upring, opts)
}
function SetUtils (upring, opts) {
this.opts = opts
this.kv = upring.kv
}
SetUtils.prototype.sadd = async function sadd (key, value) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key) || []
if (Array.isArray(value)) {
set.push.apply(set, value)
} else {
set.push(value)
}
await this.kv.put(key, set)
return Array.isArray(value) ? value.length : 1
}
SetUtils.prototype.zadd = async function zadd (key, score, value) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key) || []
if (set.length > 0) {
assert(set._scored, 'the set is not scored')
}
set.push({ value, score })
const ordered = lodash.sortBy(set, 'score')
ordered._scored = true
await this.kv.put(key, ordered)
return 1
}
SetUtils.prototype.srem = async function srem (key, value) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key)
if (!set) return 0
if (Array.isArray(value)) {
for (var i = 0, len = value.length; i < len; i++) {
const isObj = typeof value[i] === 'object'
lodash.remove(set, ele => {
if (isObj) return deepEqual(ele, value[i])
return ele === value[i]
})
}
} else {
const isObj = typeof value === 'object'
lodash.remove(set, ele => {
if (isObj) return deepEqual(ele, value)
return ele === value
})
}
await this.kv.put(key, set)
return Array.isArray(value) ? (set.length - value.length) : (set.length - 1)
}
SetUtils.prototype.zrem = async function zrem (key, value) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key)
if (!set) return 0
assert(set._scored, 'the set should be scored')
if (Array.isArray(value)) {
for (var i = 0, len = value.length; i < len; i++) {
const isObj = typeof value[i] === 'object'
lodash.remove(set, ele => {
if (isObj) return deepEqual(ele.value, value[i])
return ele.value === value[i]
})
}
} else {
const isObj = typeof value === 'object'
lodash.remove(set, ele => {
if (isObj) return deepEqual(ele.value, value)
return ele.value === value
})
}
await this.kv.put(key, set)
return Array.isArray(value) ? (set.length - value.length) : (set.length - 1)
}
SetUtils.prototype.del = async function del (keys) {
if (!Array.isArray(keys)) keys = [keys]
for (var i = 0, len = keys.length; i < len; i++) {
assert(typeof keys[i] === 'string', 'key should be a string')
await this.kv.put(keys[i], undefined)
}
return keys.length
}
SetUtils.prototype.sinter = async function sinter (keys) {
assert(Array.isArray(keys), 'keys should be an array of strings')
const sets = []
for (var i = 0, len = keys.length; i < len; i++) {
const set = await this.kv.get(keys[i])
if (set === undefined) continue
sets.push(
set._scored === true
? set.map(e => e.value)
: set
)
}
return lodash.intersection.apply(null, sets)
}
SetUtils.prototype.sunion = async function sunion (keys) {
assert(Array.isArray(keys), 'keys should be an array of strings')
const sets = []
for (var i = 0, len = keys.length; i < len; i++) {
const set = await this.kv.get(keys[i])
if (set === undefined) continue
sets.push(
set._scored === true
? set.map(e => e.value)
: set
)
}
return lodash.union.apply(null, sets)
}
SetUtils.prototype.scard = async function scard (key) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key)
if (!set) return undefined
return set.length
}
SetUtils.prototype.zcard = async function zcard (key) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key)
if (!set) return undefined
assert(set._scored, 'the set should be scored')
return set.length
}
SetUtils.prototype.smembers = async function smembers (key) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key)
if (!set) return undefined
if (set._scored) {
return set.map(e => e.value)
}
return set
}
SetUtils.prototype.sismembers = async function sismembers (key, ele) {
assert(typeof key === 'string', 'key should be a string')
const set = await this.kv.get(key)
if (!set) return 0
if (set._scored) {
return set.map(e => e.value).indexOf(ele) > -1 ? 1 : 0
}
return set.indexOf(ele) > -1 ? 1 : 0
}
SetUtils.prototype.sunionstore = async function sunionstore (key, keys) {
assert(typeof key === 'string', 'key should be a string')
assert(Array.isArray(keys), 'keys should be an array of strings')
const sets = []
for (var i = 0, len = keys.length; i < len; i++) {
const set = await this.kv.get(keys[i])
if (set === undefined) continue
sets.push(
set._scored === true
? set.map(e => e.value)
: set
)
}
const unionSet = lodash.union.apply(null, sets)
await this.kv.put(key, unionSet)
return unionSet.length
}
SetUtils.prototype.sdiff = async function sdiff (keys) {
assert(Array.isArray(keys), 'keys should be an array of strings')
const sets = []
for (var i = 0, len = keys.length; i < len; i++) {
const set = await this.kv.get(keys[i])
if (set === undefined) continue
sets.push(
set._scored === true
? set.map(e => e.value)
: set
)
}
return lodash.difference.apply(null, sets)
}
SetUtils.prototype.zrange = async function zrange (key, start, stop) {
assert(typeof key === 'string', 'the key should be a string')
assert(typeof start === 'number', 'start should be a number')
assert(typeof stop === 'number', 'stop should be a number')
const set = await this.kv.get(key)
if (!set) return undefined
assert(set._scored, 'the set should be scored')
return set.map(e => e.value).slice(start, stop)
}
SetUtils.prototype.zrevrange = async function zrevrange (key, start, stop) {
assert(typeof key === 'string', 'the key should be a string')
assert(typeof start === 'number', 'start should be a number')
assert(typeof stop === 'number', 'stop should be a number')
const set = await this.kv.get(key)
if (!set) return undefined
assert(set._scored, 'the set should be scored')
set.reverse()
return set.map(e => e.value).slice(start, stop)
}
SetUtils.prototype.zscore = async function zscore (key, ele) {
assert(typeof key === 'string', 'the key should be a string')
const set = await this.kv.get(key)
if (!set) return undefined
assert(set._scored, 'the set should be scored')
for (var i = 0, len = set.length; i < len; i++) {
var scoredElement = set[i]
if (typeof scoredElement.value === 'object') {
if (deepEqual(scoredElement.value, ele)) {
return scoredElement.score
}
} else {
if (scoredElement.value === ele) {
return scoredElement.score
}
}
}
return null
}
SetUtils.prototype.zincrby = async function zincrby (key, score, ele) {
assert(typeof key === 'string', 'the key should be a string')
assert(typeof score === 'number', 'the score should be a number')
const set = await this.kv.get(key)
if (set === undefined) {
return this.zadd(key, score, ele)
}
assert(set._scored, 'the set should be scored')
var scoredElement = {}
for (var i = 0, len = set.length; i < len; i++) {
scoredElement = set[i]
if (typeof scoredElement.value === 'object') {
if (deepEqual(scoredElement.value, ele)) {
scoredElement.score += score
break
}
} else {
if (scoredElement.value === ele) {
scoredElement.score += score
break
}
}
}
const ordered = lodash.sortBy(set, 'score')
ordered._scored = true
await this.kv.put(key, ordered)
return scoredElement.score
}
module.exports = setPlugin