Skip to content

Commit

Permalink
Add serialization function args, replace asserts with ValueErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosgavalas committed Sep 16, 2023
1 parent b4d9181 commit e6c3f1f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 27 deletions.
10 changes: 2 additions & 8 deletions kevo/engines/appendlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
return self.set(key, value)

def get(self, key: bytes):
assert type(key) is bytes
assert 0 < len(key) <= self.max_key_len

def _get(self, key):
if key not in self.hash_index:
return KVStore.EMPTY

Expand All @@ -108,10 +105,7 @@ def get(self, key: bytes):
assert k == key
return v

def set(self, key: bytes, value: bytes = KVStore.EMPTY):
assert type(key) is bytes and type(value) is bytes
assert 0 < len(key) <= self.max_key_len and len(value) <= self.max_value_len

def _set(self, key, value=KVStore.EMPTY):
if not value and key in self.hash_index:
del self.hash_index[key]
return
Expand Down
10 changes: 2 additions & 8 deletions kevo/engines/hybridlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
return self.set(key, value)

def get(self, key: bytes):
assert type(key) is bytes
assert 0 < len(key) <= self.max_key_len

def _get(self, key):
if key not in self.hash_index:
return KVStore.EMPTY

Expand All @@ -145,10 +142,7 @@ def get(self, key: bytes):
_, v = self._read_kv_pair(log_file)
return v

def set(self, key: bytes, value: bytes = KVStore.EMPTY):
assert type(key) is bytes and type(value) is bytes
assert 0 < len(key) <= self.max_key_len and len(value) <= self.max_value_len

def _set(self, key, value=KVStore.EMPTY):
if self.memory.is_full():
self.flush(self.ro_offset)

Expand Down
27 changes: 25 additions & 2 deletions kevo/engines/kvstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,33 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
raise NotImplementedError('')

def get(self, key: bytes):
def get(self, key, serializer=None, serializer_args=None):
if serializer is not None:
key = serializer(key, **serializer_args)

if type(key) is not bytes:
raise ValueError('expecting bytes for keys')
if len(key) == 0 or len(key) > self.max_key_len:
raise ValueError('expecting 0 < len(key) <= max_key_length')

return self._get(key=key)

def _get(self, key: bytes):
raise NotImplementedError('')

def set(self, key: bytes, value: bytes):
def set(self, key, value, serializer=None, serializer_args=None):
if serializer is not None:
key = serializer(key, **serializer_args)
value = serializer(value, **serializer_args)

if type(key) is not bytes or type(value) is not bytes:
raise ValueError('expecting bytes for keys and values.')
if len(key) == 0 or len(key) > self.max_key_len or len(value) > self.max_value_len:
raise ValueError('expecting 0 < len(key) <= max_key_length and len(value) <= max_value_len')

self._set(key=key, value=value)

def _set(self, key: bytes, value: bytes):
raise NotImplementedError('')

def __sizeof__(self):
Expand Down
10 changes: 2 additions & 8 deletions kevo/engines/lsmtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
return self.set(key, value)

def get(self, key: bytes):
assert type(key) is bytes
assert 0 < len(key) <= self.max_key_len

def _get(self, key: bytes):
if key in self.memtable:
return self.memtable[key]

Expand All @@ -137,10 +134,7 @@ def get(self, key: bytes):

return KVStore.EMPTY

def set(self, key: bytes, value: bytes = KVStore.EMPTY):
assert type(key) is bytes and type(value) is bytes
assert 0 < len(key) <= self.max_key_len and len(value) <= self.max_value_len

def _set(self, key, value=KVStore.EMPTY):
if key not in self.memtable:
self.memtable_bytes_count += len(key) + len(value)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
]
setuptools.setup(
name="kevo",
version="0.0.1",
version="0.1.0",
author="Nikos Gavalas",
description="Key-value store with 3 backend engines",
classifiers=classifiers,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_appendlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def test_basic(self):
l.set(b'a', b'a1')
l.set(b'e', b'55')
l.set(b'to be deleted', b'')
l.set(256, 257, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 2})
l.set(1, 2, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 1})

self.assertEqual(l.get(b'a'), b'a1')
self.assertEqual(l.get(b'asdf'), b'\x00\x01\x00\x00')
Expand All @@ -37,6 +39,8 @@ def test_basic(self):
self.assertEqual(l.get(b'd'), b'3\x002\x00')
self.assertEqual(l.get(b'e'), b'55')
self.assertEqual(l.get(b'to be deleted'), b'')
self.assertEqual(l.get(256, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 2}), b'\x01\x01')
self.assertEqual(l.get(b'\x01'), b'\x02')

l.close()

Expand Down
4 changes: 4 additions & 0 deletions tests/test_hybridlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ def test_basic(self):
l.set(b'b', b'\x00\x00\x02\x00')
l.set(b'd', b'3\x002\x00')
l.set(b'e', b'55')
l.set(256, 257, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 2})
l.set(1, 2, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 1})

self.assertEqual(l.get(b'asdf'), b'\x00\x01\x00\x00')
self.assertEqual(l.get(b'b'), b'\x00\x00\x02\x00')
self.assertEqual(l.get(b'c'), b'')
self.assertEqual(l.get(b'd'), b'3\x002\x00')
self.assertEqual(l.get(b'e'), b'55')
self.assertEqual(l.get(256, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 2}), b'\x01\x01')
self.assertEqual(l.get(b'\x01'), b'\x02')

l.close()

Expand Down
6 changes: 6 additions & 0 deletions tests/test_lsmtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ def test_basic(self):
l.set(b'asdf', b'12345')
l.set(b'cc', b'cici345')
l.set(b'b', b'3')
l.set(256, 257, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 2})
l.set(1, 2, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 1})

self.assertEqual(l.get(b'b'), b'3')
self.assertEqual(l.get(b'asdf'), b'12345')
self.assertEqual(l.get(b'cc'), b'cici345')
self.assertEqual(l.get(b'\x01\x00'), b'\x01\x01')
self.assertEqual(l.get(b'\x01'), b'\x02')
self.assertEqual(l.get(256, serializer=lambda i, length: i.to_bytes(length=length), serializer_args={'length': 2}), b'\x01\x01')
self.assertEqual(l.get(b'\x01'), b'\x02')

l.close()

Expand Down

0 comments on commit e6c3f1f

Please sign in to comment.