-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinmemory_store.py
50 lines (43 loc) · 1.53 KB
/
inmemory_store.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
from datetime import datetime, timezone
import time
import struct
class BitCaskKeyDirEntry:
"""
A single KeyDir entry in the bitcask database
contains the time of entry, offset in the file and the size of the value in bytes.
The keydir data structure is composed of entries of the form "key":BitCaskKeyDirEntry
"""
def __init__(self, valuesize, offset: int) -> None:
ms = datetime.now(tz=timezone.utc)
self.timestamp = int(time.mktime(ms.timetuple()) * 1000)
self.valuesize = valuesize
self.offset = offset
class BitCaskKVPair:
"""
Individual key-value entries of the bitcask datastore with methods
that encode/decode the information into a binary string
"""
def __init__(self, key, value) -> None:
ms = datetime.now(tz=timezone.utc)
self.timestamp = int(time.mktime(ms.timetuple()) * 1000)
self.key = str(key)
self.value = str(value)
self.keysize = len(key)
self.valuesize = len(value)
def encode(self) -> bytes:
"""
Encode the key-value data into a single binary string
"""
return struct.pack(
f"<QLL{self.keysize}s{self.valuesize}s",
self.timestamp,
self.keysize,
self.valuesize,
self.key.encode(),
self.value.encode(),
)
def decode(self, bytedata):
"""
Decode the byte string into a Python object
"""
return struct.unpack_from(f"<QLL{self.keysize}s{self.valuesize}s", bytedata)