Skip to content

Commit

Permalink
refactor: keyrec as dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
eiri committed Mar 7, 2024
1 parent 39cc6ea commit 4d94b64
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/py_bitcask/bitcask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
from collections import namedtuple
import uuid
from dataclasses import dataclass
from functools import reduce
from struct import pack
from typing import Any, Callable, List, Optional, Union
Expand All @@ -19,8 +20,25 @@ def __call__(cls, *args, **kwargs):
return cls._instances[cls]


@dataclass
class KeyRec:
"""
Represents a record for a key in the keydir index.
Attributes:
- file_id (int): The identifier of the storage file containing the value.
- value_sz (int): The size of the value in bytes.
- value_pos (int): The position of the value in the storage file.
- tstamp (uuid.UUID): The timestamp associated with the key record as uuid7.
"""

file_id: int
value_sz: int
value_pos: int
tstamp: Union[uuid.UUID, str, int, bytes]


class Bitcask(metaclass=Singleton):
KeyRec = namedtuple("KeyRec", "file_id value_sz value_pos tstamp")
DEFAULT_THRESHOLD = 1024

def __init__(self, threshold: Optional[int] = DEFAULT_THRESHOLD) -> None:
Expand Down Expand Up @@ -80,7 +98,7 @@ def _get(self, keyrec: KeyRec) -> bytes:
Retrieves the value associated with the given key record.
Parameters:
- keyrec (KeyRec): The key record containing information about the value.
- keyrec (KeyRec): The keydir record containing information about the value.
Returns:
bytes: The value associated with the key record.
Expand Down Expand Up @@ -134,7 +152,7 @@ def _put(self, key: bytes, value: bytes) -> bool:
active.write(key)
active.write(value)
self.__cur += len(crc) + len(head) + key_sz + value_sz
self.__keydir[key] = self.KeyRec(
self.__keydir[key] = KeyRec(
self.__active,
value_sz,
self.__cur - value_sz,
Expand Down

0 comments on commit 4d94b64

Please sign in to comment.