forked from redis/redis-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdt_stream.py
423 lines (374 loc) · 9.95 KB
/
dt_stream.py
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# EXAMPLE: stream_tutorial
# HIDE_START
"""
Code samples for Stream doc pages:
https://redis.io/docs/latest/develop/data-types/streams/
"""
import redis
r = redis.Redis(decode_responses=True)
# HIDE_END
# REMOVE_START
r.delete("race:france", "race:italy", "race:usa")
# REMOVE_END
# STEP_START xadd
res1 = r.xadd(
"race:france",
{"rider": "Castilla", "speed": 30.2, "position": 1, "location_id": 1},
)
print(res1) # >>> 1692629576966-0
res2 = r.xadd(
"race:france",
{"rider": "Norem", "speed": 28.8, "position": 3, "location_id": 1},
)
print(res2) # >>> 1692629594113-0
res3 = r.xadd(
"race:france",
{"rider": "Prickett", "speed": 29.7, "position": 2, "location_id": 1},
)
print(res3) # >>> 1692629613374-0
# STEP_END
# REMOVE_START
assert r.xlen("race:france") == 3
# REMOVE_END
# STEP_START xrange
res4 = r.xrange("race:france", "1691765278160-0", "+", 2)
print(
res4
) # >>> [
# ('1692629576966-0',
# {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}
# ),
# ('1692629594113-0',
# {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}
# )
# ]
# STEP_END
# STEP_START xread_block
res5 = r.xread(streams={"race:france": 0}, count=100, block=300)
print(
res5
)
# >>> [
# ['race:france',
# [('1692629576966-0',
# {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}
# ),
# ('1692629594113-0',
# {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}
# ),
# ('1692629613374-0',
# {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'}
# )]
# ]
# ]
# STEP_END
# STEP_START xadd_2
res6 = r.xadd(
"race:france",
{"rider": "Castilla", "speed": 29.9, "position": 1, "location_id": 2},
)
print(res6) # >>> 1692629676124-0
# STEP_END
# STEP_START xlen
res7 = r.xlen("race:france")
print(res7) # >>> 4
# STEP_END
# STEP_START xadd_id
res8 = r.xadd("race:usa", {"racer": "Castilla"}, id="0-1")
print(res8) # >>> 0-1
res9 = r.xadd("race:usa", {"racer": "Norem"}, id="0-2")
print(res9) # >>> 0-2
# STEP_END
# STEP_START xadd_bad_id
try:
res10 = r.xadd("race:usa", {"racer": "Prickett"}, id="0-1")
print(res10) # >>> 0-1
except redis.exceptions.ResponseError as e:
print(e) # >>> WRONGID
# STEP_END
# STEP_START xadd_7
# Not yet implemented
# STEP_END
# STEP_START xrange_all
res11 = r.xrange("race:france", "-", "+")
print(
res11
)
# >>> [
# ('1692629576966-0',
# {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}
# ),
# ('1692629594113-0',
# {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}
# ),
# ('1692629613374-0',
# {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'}
# ),
# ('1692629676124-0',
# {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'}
# )
# ]
# STEP_END
# STEP_START xrange_time
res12 = r.xrange("race:france", 1692629576965, 1692629576967)
print(
res12
)
# >>> [
# ('1692629576966-0',
# {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}
# )
# ]
# STEP_END
# STEP_START xrange_step_1
res13 = r.xrange("race:france", "-", "+", 2)
print(
res13
)
# >>> [
# ('1692629576966-0',
# {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}
# ),
# ('1692629594113-0',
# {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}
# )
# ]
# STEP_END
# STEP_START xrange_step_2
res14 = r.xrange("race:france", "(1692629594113-0", "+", 2)
print(
res14
)
# >>> [
# ('1692629613374-0',
# {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'}
# ),
# ('1692629676124-0',
# {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'}
# )
# ]
# STEP_END
# STEP_START xrange_empty
res15 = r.xrange("race:france", "(1692629676124-0", "+", 2)
print(res15) # >>> []
# STEP_END
# STEP_START xrevrange
res16 = r.xrevrange("race:france", "+", "-", 1)
print(
res16
)
# >>> [
# ('1692629676124-0',
# {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'}
# )
# ]
# STEP_END
# STEP_START xread
res17 = r.xread(streams={"race:france": 0}, count=2)
print(
res17
)
# >>> [
# ['race:france', [
# ('1692629576966-0',
# {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}
# ),
# ('1692629594113-0',
# {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}
# )
# ]
# ]
# ]
# STEP_END
# STEP_START xgroup_create
res18 = r.xgroup_create("race:france", "france_riders", "$")
print(res18) # >>> True
# STEP_END
# STEP_START xgroup_create_mkstream
res19 = r.xgroup_create("race:italy", "italy_riders", "$", mkstream=True)
print(res19) # >>> True
# STEP_END
# STEP_START xgroup_read
r.xadd("race:italy", {"rider": "Castilla"})
r.xadd("race:italy", {"rider": "Royce"})
r.xadd("race:italy", {"rider": "Sam-Bodden"})
r.xadd("race:italy", {"rider": "Prickett"})
r.xadd("race:italy", {"rider": "Norem"})
res20 = r.xreadgroup(
streams={"race:italy": ">"},
consumername="Alice",
groupname="italy_riders",
count=1,
)
print(res20) # >>> [['race:italy', [('1692629925771-0', {'rider': 'Castilla'})]]]
# STEP_END
# STEP_START xgroup_read_id
res21 = r.xreadgroup(
streams={"race:italy": 0},
consumername="Alice",
groupname="italy_riders",
count=1,
)
print(res21) # >>> [['race:italy', [('1692629925771-0', {'rider': 'Castilla'})]]]
# STEP_END
# STEP_START xack
res22 = r.xack("race:italy", "italy_riders", "1692629925771-0")
print(res22) # >>> 1
res23 = r.xreadgroup(
streams={"race:italy": 0},
consumername="Alice",
groupname="italy_riders",
count=1,
)
print(res23) # >>> [['race:italy', []]]
# STEP_END
# STEP_START xgroup_read_bob
res24 = r.xreadgroup(
streams={"race:italy": ">"},
consumername="Bob",
groupname="italy_riders",
count=2,
)
print(
res24
)
# >>> [
# ['race:italy', [
# ('1692629925789-0',
# {'rider': 'Royce'}
# ),
# ('1692629925790-0',
# {'rider': 'Sam-Bodden'}
# )
# ]
# ]
# ]
# STEP_END
# STEP_START xpending
res25 = r.xpending("race:italy", "italy_riders")
print(
res25
)
# >>> {
# 'pending': 2, 'min': '1692629925789-0', 'max': '1692629925790-0',
# 'consumers': [{'name': 'Bob', 'pending': 2}]
# }
# STEP_END
# STEP_START xpending_plus_minus
res26 = r.xpending_range("race:italy", "italy_riders", "-", "+", 10)
print(
res26
)
# >>> [
# {
# 'message_id': '1692629925789-0', 'consumer': 'Bob',
# 'time_since_delivered': 31084, 'times_delivered': 1
# },
# {
# 'message_id': '1692629925790-0', 'consumer': 'Bob',
# 'time_since_delivered': 31084, 'times_delivered': 1
# }
# ]
# STEP_END
# STEP_START xrange_pending
res27 = r.xrange("race:italy", "1692629925789-0", "1692629925789-0")
print(res27) # >>> [('1692629925789-0', {'rider': 'Royce'})]
# STEP_END
# STEP_START xclaim
res28 = r.xclaim("race:italy", "italy_riders", "Alice", 60000, ["1692629925789-0"])
print(res28) # >>> [('1692629925789-0', {'rider': 'Royce'})]
# STEP_END
# STEP_START xautoclaim
res29 = r.xautoclaim("race:italy", "italy_riders", "Alice", 1, "0-0", 1)
print(res29) # >>> ['1692629925790-0', [('1692629925789-0', {'rider': 'Royce'})]]
# STEP_END
# STEP_START xautoclaim_cursor
res30 = r.xautoclaim("race:italy", "italy_riders", "Alice", 1, "(1692629925789-0", 1)
print(res30) # >>> ['0-0', [('1692629925790-0', {'rider': 'Sam-Bodden'})]]
# STEP_END
# STEP_START xinfo
res31 = r.xinfo_stream("race:italy")
print(
res31
)
# >>> {
# 'length': 5, 'radix-tree-keys': 1, 'radix-tree-nodes': 2,
# 'last-generated-id': '1692629926436-0', 'groups': 1,
# 'first-entry': ('1692629925771-0', {'rider': 'Castilla'}),
# 'last-entry': ('1692629926436-0', {'rider': 'Norem'})
# }
# STEP_END
# STEP_START xinfo_groups
res32 = r.xinfo_groups("race:italy")
print(
res32
)
# >>> [
# {
# 'name': 'italy_riders', 'consumers': 2, 'pending': 2,
# 'last-delivered-id': '1692629925790-0'
# }
# ]
# STEP_END
# STEP_START xinfo_consumers
res33 = r.xinfo_consumers("race:italy", "italy_riders")
print(
res33
)
# >>> [
# {'name': 'Alice', 'pending': 2, 'idle': 199332},
# {'name': 'Bob', 'pending': 0, 'idle': 489170}
# ]
# STEP_END
# STEP_START maxlen
r.xadd("race:italy", {"rider": "Jones"}, maxlen=2)
r.xadd("race:italy", {"rider": "Wood"}, maxlen=2)
r.xadd("race:italy", {"rider": "Henshaw"}, maxlen=2)
res34 = r.xlen("race:italy")
print(res34) # >>> 8
res35 = r.xrange("race:italy", "-", "+")
print(
res35
)
# >>> [
# ('1692629925771-0', {'rider': 'Castilla'}),
# ('1692629925789-0', {'rider': 'Royce'}),
# ('1692629925790-0', {'rider': 'Sam-Bodden'}),
# ('1692629925791-0', {'rider': 'Prickett'}),
# ('1692629926436-0', {'rider': 'Norem'}),
# ('1692630612602-0', {'rider': 'Jones'}),
# ('1692630641947-0', {'rider': 'Wood'}),
# ('1692630648281-0', {'rider': 'Henshaw'})
# ]
r.xadd("race:italy", {"rider": "Smith"}, maxlen=2, approximate=False)
res36 = r.xrange("race:italy", "-", "+")
print(
res36
)
# >>> [
# ('1692630648281-0', {'rider': 'Henshaw'}),
# ('1692631018238-0', {'rider': 'Smith'})
# ]
# STEP_END
# STEP_START xtrim
res37 = r.xtrim("race:italy", maxlen=10, approximate=False)
print(res37) # >>> 0
# STEP_END
# STEP_START xtrim2
res38 = r.xtrim("race:italy", maxlen=10)
print(res38) # >>> 0
# STEP_END
# STEP_START xdel
res39 = r.xrange("race:italy", "-", "+")
print(
res39
)
# >>> [
# ('1692630648281-0', {'rider': 'Henshaw'}),
# ('1692631018238-0', {'rider': 'Smith'})
# ]
res40 = r.xdel("race:italy", "1692631018238-0")
print(res40) # >>> 1
res41 = r.xrange("race:italy", "-", "+")
print(res41) # >>> [('1692630648281-0', {'rider': 'Henshaw'})]
# STEP_END