Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jopemachine opened this issue Apr 29, 2024 · 0 comments
Labels

Comments

@jopemachine
Copy link
Member

jopemachine commented Apr 29, 2024

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)
@jopemachine jopemachine changed the title Establish actual relationships of the Python binding LogEntry, StateMachine Establish actual relationships of the Python binding's LogEntry, StateMachine types Apr 29, 2024
@jopemachine jopemachine changed the title Establish actual relationships of the Python binding's LogEntry, StateMachine types Establish actual relationship of the Python binding's LogEntry, StateMachine types Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant