-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.js
46 lines (38 loc) · 1.35 KB
/
range.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
const { client, key } = require('./utils')
const date = new Date()
function main () {
let sum = 0
async function sumWithRangeQuery () {
const c = client()
/**
* https://redis.io/commands/xrange
* Returns the stream entries matching a given range of IDs.
*
* Arguments:
* key - The name of our stream.
* '-' - Special ID meaning the minimum ID possible.
* '+' - Special ID meaning the maximum ID possible.
*
* Explanation:
* '-' is effectively the same as declaring 0-0 while '+' is the same as
* declaring 18446744073709551615-18446744073709551615 just easier to
* type. Autogenerated messages follow the format of timestamp-index,
* therefore multiple messages can be written with the same timestamp if
* need be but ordering _in which they were written_ will be guaranteed
* because of indexing. The significance of 18446744073709551615 is that
* the timestamp and the index can each be at most a 64-bit integer.
**/
const msgs = await c.xrangeAsync(key, '-', '+')
if (!msgs) {
console.log(`${date.getTime()}: Stream "${key}" has no messages.`)
process.exit()
}
msgs.forEach(msg => {
sum += parseInt(msg[1][1])
})
console.log(`${date.getTime()}: Sum of stream "${key}" is ${sum}.`)
process.exit()
}
sumWithRangeQuery()
}
main()