-
Notifications
You must be signed in to change notification settings - Fork 1
/
commitlog.py
34 lines (29 loc) · 947 Bytes
/
commitlog.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
from os import path
from common import TOMBSTONE
from memtable import Memtable
from binio import kv_iter, KVWriter
class CommitLog:
def __init__(self, writer):
self.writer = writer
@classmethod
def resume(cls, log_path):
memtable = Memtable()
# recover the memtable if necessary
if path.exists(log_path):
for key, value in kv_iter(log_path):
if value == TOMBSTONE:
memtable.unset(key)
else:
memtable.set(key, value)
writer = KVWriter(log_path, append=True)
else:
writer = KVWriter(log_path)
return cls(writer), memtable
def record_set(self, k, v):
self.writer.write_entry(k, v)
self.writer.sync()
def record_unset(self, k):
self.writer.write_entry(k, TOMBSTONE)
self.writer.sync()
def purge(self):
self.writer.truncate()