-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNode.py
61 lines (51 loc) · 1.9 KB
/
Node.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
import threading
import time
class Node(object):
"""Implements a node.
Node will run in its own thread and emulate a separate machine.
Eventually :)
Part of 'Imperative Shell' (except the do_pop() method), does destructive
operations on node's storage and updates the information related to
eviction strategies, like frequencies counts, use order...
"""
def __init__(self, name, nodeid, size=128):
self.name = name
self.id = nodeid
# self.thread = threading.Thread(target=self.run)
self.size = size
self.hashmap = {}
def read(self, accessor, key):
to_evict = accessor.get_evictions(self, key)
value = self.hashmap.get(key, None)
res = None
if value:
res = value[0]
for key in to_evict:
del self.hashmap[key]
return res
def write(self, accessor, key, value):
timestamp = time.time()
self.hashmap[key] = (value, timestamp)
def do_pop(self, start, end):
"""Part of 'Functional Core', does not update state, just returns."""
if end < start:
left_updates, left_storage = self.do_pop(-2**63, end)
right_updates, right_storage = self.do_pop(start, 2**63)
new_storage = {x: left_storage[x] for x in left_storage
if x in right_storage}
return left_updates + right_updates, new_storage
updates = []
new_storage = {}
for k, v in self.hashmap.items():
if start < hash(k) < end:
updates.append((k, v))
else:
new_storage[k] = v
return updates, new_storage
def pop(self, start, end):
updates, self.hashmap = self.do_pop(start, end)
return updates
def push(self, entries):
self.hashmap.update(entries)
def run(self):
pass