Skip to content

Commit 012b515

Browse files
committed
Add code to build keydir
1 parent 50d6362 commit 012b515

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pycask
2-
A log-structured disk-based key-value store implemented in Python
2+
A log-structured disk-based key-value store implemented in Python.
33

44
This is an implementation of the BitCask database described in [this paper](https://riak.com/assets/bitcask-intro.pdf).
55
The implementation is ideally independent of machine and environment considerations.
@@ -22,3 +22,9 @@ datastore.close()
2222
[driver.py](https://github.com/abaksy/pycask/blob/main/driver.py) runs some benchmark tests for key access times for different database sizes (ranging from 10 entries to 1mn entries)
2323

2424
## How bitcask works
25+
The database consists of two major portions, the disk-based datastore and an in-memory data structure called the ```keydir```.
26+
27+
28+
## Changelog
29+
1. Made sure to avoid using local timestamps, use UTC timestamps instead for internalization (avoid various kinds of timing bugs)
30+
2. Renamed class ```BitCaskDiskStore``` to ```BitCaskKVPair``` to more accurately reflect what the class actually does

Diff for: bitcask.py

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ def __init__(self, datafile_name="database.db") -> None:
99
self.datafile = datafile_name
1010
self.file_handle = open(self.datafile, "a+b")
1111
self.keydir: Dict[str, BitCaskKeyDirEntry] = dict()
12+
self.build_keydir()
13+
14+
def build_keydir(self):
15+
filesize = os.path.getsize(self.datafile)
16+
if filesize == 0:
17+
return
18+
with open(self.datafile, 'rb') as f:
19+
while f.tell() < filesize:
20+
offset = f.tell()
21+
byte_data = f.read(16)
22+
timestamp, keysize, valuesize = struct.unpack("<QLL", byte_data)
23+
key = f.read(keysize).decode()
24+
value = f.read(valuesize).decode()
25+
self.keydir[key] = BitCaskKeyDirEntry(valuesize, offset)
26+
1227

1328
def get(self, key):
1429
"""

0 commit comments

Comments
 (0)