-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdt-bitfield.js
76 lines (69 loc) · 1.35 KB
/
dt-bitfield.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
// EXAMPLE: bitfield_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.flushDb();
// REMOVE_END
// STEP_START bf
let res1 = await client.bitField("bike:1:stats", [{
operation: 'SET',
encoding: 'u32',
offset: '#0',
value: 1000
}]);
console.log(res1); // >>> [0]
let res2 = await client.bitField('bike:1:stats', [
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#0',
increment: -50
},
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#1',
increment: 1
}
]);
console.log(res2); // >>> [950, 1]
let res3 = await client.bitField('bike:1:stats', [
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#0',
increment: 500
},
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#1',
increment: 1
}
]);
console.log(res3); // >>> [1450, 2]
let res4 = await client.bitField('bike:1:stats', [
{
operation: 'GET',
encoding: 'u32',
offset: '#0'
},
{
operation: 'GET',
encoding: 'u32',
offset: '#1'
}
]);
console.log(res4); // >>> [1450, 2]
// STEP_END
// REMOVE_START
assert.deepEqual(res1, [0])
assert.deepEqual(res2, [950, 1])
assert.deepEqual(res3, [1450, 2])
assert.deepEqual(res4, [1450, 2])
await client.quit();
// REMOVE_END