-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontainer3.py
More file actions
282 lines (238 loc) · 9.76 KB
/
container3.py
File metadata and controls
282 lines (238 loc) · 9.76 KB
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
"""
BSD 3-Clause License
Copyright (c) 2017, Jesus Llorente Santos, Aalto University, Finland
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
##########################################################################
## c o n t a i n e r 3 ##
##########################################################################
# This module defines a generic Container class and ContainerNode unit
import logging
LOGLEVELCONTAINER = logging.INFO
LOGLEVELNODE = logging.INFO
class Container(object):
def __init__(self, name='Container', loglevel=LOGLEVELCONTAINER, datatype='set'):
"""
Initialize the Container.
Added datatype parameter to select from list or set.
Use set for higher performance
Use list for preserving insertion order
"""
self._version = 0.7
self._logger = logging.getLogger(name)
self._logger.setLevel(loglevel)
self._name = name
self._dict = {} # Indexes lookup keys to nodes
self._dict_id2keys = {} # Indexes node ids to registered keys
if datatype == 'list':
self._nodes = [] # Stores a list of indexed nodes
self._gen_datatype = lambda: []
self._add_datatype = lambda x,data: x.append(data)
self._remove_datatype = lambda x,data: x.remove(data)
elif datatype == 'set':
self._nodes = set() # Stores a set of indexed nodes
self._gen_datatype = lambda: set()
self._add_datatype = lambda x,data: x.add(data)
self._remove_datatype = lambda x,data: x.remove(data)
else:
raise Exception('Datatype "{}" not supported!'.format(datatype))
def _add_lookupkeys(self, node, keys):
for key, isunique in keys:
# The key is not unique - create a storage of items for key
if not isunique:
if key not in self._dict:
self._dict[key] = self._gen_datatype()
self._add_datatype(self._dict[key], node)
# Check the unique key is not already in use
elif key in self._dict:
raise KeyError('Failed to add: key {} already exists for node {}'.format(key, self._dict[key]))
# Add the unique key to the dictionary
else:
self._dict[key] = node
def _remove_lookupkeys(self, node, keys):
for key, isunique in keys:
# The key is not unique - remove from stored items for key
if not isunique:
self._logger.debug('Removed shared key {} of node {}'.format(key, node))
self._remove_datatype(self._dict[key], node)
# The storage has no more items, remove it
if len(self._dict[key]) == 0:
del self._dict[key]
# Check the unique key is already in use
elif key not in self._dict:
raise KeyError('Failed to remove: key {} does not exists for node {}'.format(key, node))
# Add the unique key to the dictionary
else:
self._logger.debug('Removed unique key {} of node {}'.format(key, node))
del self._dict[key]
def add(self, node):
"""
Add a ContainerNode to the Container.
@param node: The node
"""
if node in self._nodes:
raise Exception('Failed to add: node already exists {}'.format(node))
# Register node lookup keys
keys = node.lookupkeys()
self._add_lookupkeys(node, keys)
# Map node id to lookup keys
self._dict_id2keys[id(node)] = keys
# Add node to the storage
self._add_datatype(self._nodes, node)
self._logger.debug('Added node {}'.format(node))
def get(self, key, update=False):
"""
Obtain a node with the given key.
@param key: The values of the node.
@param update: If activated, update the node.
@return: The node node or KeyError if not found
"""
node = self._dict[key]
if update and isinstance(node, ContainerNode):
node.update()
return node
def getall(self):
"""
Returns a shallow copy of the internal storage
"""
return list(self._nodes)
def has(self, key, check_expire=True):
"""
Check if there is a node with the given key
@param key: The values of the node.
@param check_expire: If activated, check for expired node.
@return: True if there is a node.
"""
try:
node = self._dict[key]
if not isinstance(node, ContainerNode):
return True
if check_expire and node.hasexpired():
self.remove(node)
return False
return True
except KeyError:
return False
def lookup(self, key, update=True, check_expire=True):
"""
Look up a node with the given key.
@param key: The values of the node.
@param update: If activated, update the node.
@param check_expire: If activated, check for expired node.
@return: The node node.
"""
try:
node = self._dict[key]
if not isinstance(node, ContainerNode):
return node
if check_expire and node.hasexpired():
self.remove(node)
if update:
node.update()
return node
except KeyError:
return None
def remove(self, node, callback=True):
"""
Remove an node from the Storage node.
@param node: The node
"""
# Remove node lookup keys
keys = node.lookupkeys()
self._remove_lookupkeys(node, keys)
# Remove map node id to lookup keys
del self._dict_id2keys[id(node)]
# Remove node from the storage
self._remove_datatype(self._nodes, node)
self._logger.debug('Removed node {}'.format(node))
# Evaluate callback to ContainerNode item
if callback:
self._logger.debug('Delete callback for node {}'.format(node))
node.delete()
def removeall(self, callback=True):
# Iterate all nodes in the storage and remove them
for node in self.getall():
self.remove(node, callback)
# Sanity clear
self._dict_id2keys.clear()
self._dict.clear()
self._nodes.clear()
def updatekeys(self, node):
# Get lookup keys
old_keys = self._dict_id2keys[id(node)]
new_keys = node.lookupkeys()
# Remove previous keys
self._remove_lookupkeys(node, old_keys)
# Remove map node id to lookup keys
del self._dict_id2keys[id(node)]
# Register node lookup keys
self._add_lookupkeys(node, new_keys)
# Map node id to lookup keys
self._dict_id2keys[id(node)] = new_keys
self._logger.debug('Updated keys for node {}'.format(node))
def __len__(self):
""" Returns the length of the internal list node """
return len(self._nodes)
def __repr__(self):
return '{} ({} items)'.format(self._name, len(self))
def dump(self):
return '\n'.join(['#{} {}'.format(i+1, node.dump()) for i, node in enumerate(self._nodes)])
class ContainerNode(object):
def __init__(self, name='ContainerNode', loglevel=LOGLEVELNODE):
""" Initialize the ContainerNode """
self._logger = logging.getLogger(name)
self._logger.setLevel(loglevel)
self._name = name
def lookupkeys(self):
""" Return the lookup keys of the node """
key = self._name
isunique = True
return ((key, isunique),)
def hasexpired(self):
""" Return True if the TTL of the node has expired """
return False
def update(self):
"""
Perform additional actions when the node is being updated.
"""
pass
def delete(self):
"""
Perform additional actions when the node is being deleted.
"""
pass
def dump(self):
"""
Return a string representation of the node.
"""
return ''
def __repr__(self):
return self._name
if __name__ == "__main__":
ct = Container()
cn1 = ContainerNode('cn1')
ct.add(cn1)
cn2 = ContainerNode('cn2')
ct.add(cn2)
ct.removeall()
print(ct)