-
Notifications
You must be signed in to change notification settings - Fork 0
/
liblodex.py
225 lines (181 loc) · 6.92 KB
/
liblodex.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
#!/usr/bin/env python3
import cbor
class DirtyBlocks:
def __init__(self):
self.dirty_blocks = []
def put(self, value):
self.dirty_blocks.append(value)
return len(self.dirty_blocks) - 1
def get(self, offset):
return self.dirty_blocks[offset]
def __len__(self):
return len(self.dirty_blocks)
class IndexBlock:
def __init__(self, index_block=None):
self.index_block = index_block if index_block is not None else {}
def has(self, key_frag):
return key_frag in self.index_block
def put(self, key_frag, value, typ):
self.index_block[key_frag] = (value, typ)
def get(self, key_frag):
return self.index_block[key_frag]
def keys(self):
return sorted(self.index_block.keys())
class FileLog:
def __init__(self, filename):
self.filename = filename
try:
self.file = open(filename, "rb+")
except IOError:
self.file = open(filename, "wb+")
if len(self) == 0:
self.write_checkpoint(0)
self.write_checkpoint(self.put(IndexBlock().index_block))
def write_checkpoint(self, offset):
self.file.seek(0, 0)
self.file.write(offset.to_bytes(4, byteorder="big"))
self.file.write(offset.to_bytes(4, byteorder="big"))
self.file.flush()
def read_checkpoint(self):
self.file.seek(0, 0)
offset = int.from_bytes(self.file.read(4), byteorder="big")
offset2 = int.from_bytes(self.file.read(4), byteorder="big")
assert(offset == offset2)
return offset
def put(self, value):
offset = len(self)
value_bytes = cbor.dumps(value)
length = len(value_bytes)
self.file.write(length.to_bytes(2, byteorder="big"))
self.file.write(value_bytes)
return offset
def get(self, offset):
self.file.seek(offset, 0)
length = int.from_bytes(self.file.read(2), byteorder="big")
value_bytes = self.file.read(length)
return cbor.loads(value_bytes) if value_bytes else None
def __len__(self):
self.file.seek(0, 2)
return self.file.tell()
def close(self):
self.file.close()
self.file = None
def split_by_n(seq, n):
while seq:
yield seq[:n]
seq = seq[n:]
class LogIndex:
def __init__(self, log, value_log, key="_id"):
self.log = log
self.value_log = value_log
self.index_key = key
self.reset()
def reset(self):
self.root = IndexBlock(index_block=self.log.get(self.log.read_checkpoint()))
self.in_memory_blocks = DirtyBlocks()
def walk(self, callback):
def rec_do(block, depth):
for subkey in block.keys():
entry = block.get(subkey)
if entry[1] == 1:
rec_do(IndexBlock(index_block=self.log.get(entry[0])), depth + 1)
elif entry[1] == 2:
rec_do(self.in_memory_blocks.get(entry[0]), depth + 1)
elif entry[1] == 0 and entry[0] is not None:
record = self.value_log.get(entry[0])
if record["value"] is not None:
callback(record["_id"], record["value"])
return rec_do(self.root, 0)
def put(self, key, value):
block = self.root
self.in_memory_blocks.put(block)
for subkey in split_by_n(key, 2):
if block.has(subkey) is False:
block.put(subkey, value, 0)
return
entry = block.get(subkey)
if entry[1] == 0:
old_record = self.value_log.get(entry[0])
if old_record[self.index_key] == key:
block.put(subkey, value, 0)
return
else:
index = self.in_memory_blocks.put(IndexBlock())
block.put(subkey, index, 2)
self.put(old_record[self.index_key], entry[0])
self.put(key, value)
return
# Just traversal
elif entry[1] == 1:
child_block = IndexBlock(index_block=self.log.get(entry[0]))
child_block_index = self.in_memory_blocks.put(child_block)
block.put(subkey, child_block_index, 2)
block = child_block
continue
elif entry[1] == 2:
child_block = self.in_memory_blocks.get(entry[0])
block = child_block
continue
# NODE is also LEAF
block.put("", value, 0)
def get(self, key):
block = self.root
for subkey in split_by_n(key, 2):
if block.has(subkey):
entry = block.get(subkey)
entry_type = entry[1]
if entry_type == 0:
if entry[0] is not None:
record = self.value_log.get(entry[0])
if record[self.index_key] == key:
return entry[0]
raise KeyError("Key '{}' not found".format(key))
elif entry_type == 1:
block = IndexBlock(index_block=self.log.get(entry[0]))
elif entry_type == 2:
block = self.in_memory_blocks.get(entry[0])
else:
raise KeyError("Key '{}' not found".format(key))
# If NODE is also LEAF
if block.has(""):
entry = block.get("")
record = self.value_log.get(entry[0])
if record[self.index_key] == key and entry[0] is not None:
return entry[0]
raise KeyError("Key '{}' not found".format(key))
def commit(self):
if not len(self.in_memory_blocks):
return None
def commit_rec(block):
for subkey in block.keys():
entry = block.get(subkey)
if entry[1] == 2:
block_id = commit_rec(self.in_memory_blocks.get(entry[0]))
block.put(subkey, block_id, 1)
return self.log.put(block.index_block)
root_offset = commit_rec(self.root)
self.log.write_checkpoint(root_offset)
self.reset()
class Lodex:
def __init__(self, filename="database.ldx"):
self.filename = filename
self.log = FileLog(filename)
self.index_log = FileLog(filename+".idx")
self.index = LogIndex(self.index_log, self.log, key="_id")
def put(self, key, value):
offset = self.log.put({"_id": key, "value": value})
self.index.put(key, offset)
return key
def get(self, key):
offset = self.index.get(key)
return self.log.get(offset)
def delete(self, key):
self.index.put(key, None)
def walk(self, cb):
self.index.walk(cb)
def commit(self):
self.index.commit()
def __len__(self):
return len(self.log)
def close(self):
self.log.close()