Skip to content

Establish actual relationship of the Python binding's LogEntry, StateMachine types #99

Open
@jopemachine

Description

Ideally, the SetCommand and LogEntry in the sample code should be represented as shown below.

class SetCommand(AbstractLogEntry):
    """
    Represent simple key-value command.
    Use pickle to serialize the data.
    """

    def __init__(self, key: str, value: str) -> None:
        self.key = key
        self.value = value

    @override
    def encode(self) -> bytes:
        return pickle.dumps(self.__dict__)

    @override
    @classmethod
    def decode(cls, packed: bytes) -> "SetCommand":
        unpacked = pickle.loads(packed)
        return cls(unpacked["key"], unpacked["value"])


class HashStore(AbstractStateMachine):
    """
    A simple key-value store that stores data in memory.
    Use pickle to serialize the data.
    """

    def __init__(self):
        self._store = dict()
        self._loop = asyncio.get_running_loop()

    def get(self, key: str) -> Optional[str]:
        return self._store.get(key)

    def as_dict(self) -> dict:
        return self._store

    @override
    async def apply(self, msg: bytes) -> bytes:
        message = SetCommand.decode(msg)
        self._store[message.key] = message.value
        return msg

    @override
    async def snapshot(self) -> bytes:
        return pickle.dumps(self._store)

    @override
    async def restore(self, snapshot: bytes) -> None:
        self._store = pickle.loads(snapshot)

However, for now, these two types are only temporarily defined in the .pyi file and are not actually included in the whl, resulting in an import error.

from raftify import AbstractLogEntry, AbstractStateMachine
ImportError: cannot import name 'AbstractLogEntry' from 'raftify' (/opt/homebrew/lib/python3.12/site-packages/raftify/__init__.py)

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions