18
16
self.hash = hashlib.sha256(data).hexdigest()
19
17
# TODO: Decide on using sha256 or sha1 (or even sha512)^
21
19
class StorageDB(libs.events.Handler):
22
20
'''An database type object to store and sort blocks.'''
23
21
def __init__(self):
28
26
def add_usk(self, block):
29
27
self.usks[block.hash] = block
31
29
def add_uuk(self, block):
32
30
self.uuks[block.hash] = block
34
32
def search(self, query):
35
33
if query.type == 'UUK':
36
34
if query.id in self.uuks:
38
36
elif query.type == 'USK':
39
37
if query.id in self.usks:
40
38
return self.usks[query.id]
41
'''Load the database from a storage file.'''
42
44
def save(self, path):
43
'''Save the database from a storage file.'''
45
for block in self.uuks.values():
46
out_json[block.hash] = block.data
48
db_file = open(path, 'w') # TODO: possibly make a backup?
49
db_file.write(json.dumps(out_json, indent = 2))
52
libs.events.broadcast('logthis', 'Saved the data store.')
55
'''Load the database to a storage file.'''
56
db_file = open(path, 'r')
57
db_json = json.loads(db_file.read())
61
self.uuks[block] = db_json[block]
45
'''Save the database to a storage file.'''
64
def got_message(self, packet):
65
message = packet.message
66
self.add_uuk(Block(message))
49
def got_message(self, data):
50
self.add_uuk(Block(data))
68
52
def got_request(self, query):
74
55
class Query(object):
75
56
def __init__(self, blocktype, id):
76
57
self.type = blocktype
81
62
'''Create the storage database.'''
82
63
database = StorageDB()
83
database.path = libs.config['vomundir'] + 'blocks.json'
84
64
libs.events.register_handler(database)