Skip to content

Commit fa50709

Browse files
Merge pull request #236 from CybercentreCanada/dispatcher-task-recovery
Dispatcher task reclaiming
2 parents 6598d23 + 2552ac5 commit fa50709

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

assemblyline/remote/datatypes/hash.py

+24
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,37 @@
2525
"""
2626

2727

28+
class HashIterator:
29+
def __init__(self, hash_object):
30+
self.hash_object = hash_object
31+
self.cursor = 0
32+
self.buffer = []
33+
self._load_next()
34+
35+
def __next__(self):
36+
while True:
37+
if self.buffer:
38+
return self.buffer.pop(0)
39+
if self.cursor == 0:
40+
raise StopIteration()
41+
self._load_next()
42+
43+
def _load_next(self):
44+
self.cursor, data = retry_call(self.hash_object.c.hscan, self.hash_object.name, self.cursor)
45+
for key, value in data.items():
46+
self.buffer.append((key.decode('utf-8'), json.loads(value)))
47+
48+
2849
class Hash(object):
2950
def __init__(self, name, host=None, port=None):
3051
self.c = get_client(host, port, False)
3152
self.name = name
3253
self._pop = self.c.register_script(h_pop_script)
3354
self._limited_add = self.c.register_script(_limited_add)
3455

56+
def __iter__(self):
57+
return HashIterator(self)
58+
3559
def __enter__(self):
3660
return self
3761

test/test_remote_datatypes.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import itertools
22
import time
33

44
from threading import Thread
@@ -36,6 +36,17 @@ def test_hash(redis_connection):
3636
assert h.increment("a") == 2
3737
assert h.increment("a", 10) == 12
3838
assert h.increment("a", -22) == -10
39+
h.delete()
40+
41+
# Load a bunch of items and test iteration
42+
data_before = [''.join(_x) for _x in itertools.product('abcde', repeat=5)]
43+
data_before = {_x: _x + _x for _x in data_before}
44+
h.multi_set(data_before)
45+
46+
data_after = {}
47+
for key, value in h:
48+
data_after[key] = value
49+
assert data_before == data_after
3950

4051

4152
# noinspection PyShadowingNames

0 commit comments

Comments
 (0)