diff --git a/src/engine/__init__.py b/src/engine/__init__.py new file mode 100644 index 0000000..af07e6b --- /dev/null +++ b/src/engine/__init__.py @@ -0,0 +1,46 @@ +from engine.types.operation import Operation + + +class SyncEngine: + def __init__(self): + pass + + def sync( + self, cursor: int, operations: list[Operation] + ) -> tuple[int, list[Operation]]: + """Sync the local database with the server's database. + + Args: + cursor (int): The cursor of the last operation that was applied to the local database. + operations (list[Operation]): The list of operations that were performed on the local database since the last sync. + + Returns: + tuple[int, list[Operation]]: A tuple containing the new cursor and the list of operations that need to be applied to the local database. + """ + # For now, just return the same cursor and an empty list of operations. + new_operations = self.get_operations_after_cursor(cursor) + if len(operations) > 0: + self.apply_operations(operations) + return cursor + len(new_operations), new_operations + + def apply_operations(self, operations: list[Operation]) -> None: + """Apply the given operations to the local database. + + Args: + operations (list[Operation]): The list of operations to apply to the local database. + """ + # For now, just print the operations. + for operation in operations: + print("APPLYING OPERATION:", operation.model_dump(), flush=True) + + def get_operations_after_cursor(self, cursor: int) -> list[Operation]: + """Get the list of operations that were performed on the server's database after the given cursor. + + Args: + cursor (int): The cursor of the last operation that was applied to the local database. + + Returns: + list[Operation]: The list of operations that were performed on the server's database after the given cursor. + """ + # For now, just return an empty list of operations. + return [] diff --git a/src/server/types/operation.py b/src/engine/types/operation.py similarity index 100% rename from src/server/types/operation.py rename to src/engine/types/operation.py diff --git a/src/server/types/snapshot.py b/src/engine/types/snapshot.py similarity index 100% rename from src/server/types/snapshot.py rename to src/engine/types/snapshot.py diff --git a/src/server/main.py b/src/server/main.py index 528d332..cdfc43b 100644 --- a/src/server/main.py +++ b/src/server/main.py @@ -1,7 +1,7 @@ import asyncio import time -from .types.operation import Operation +from engine.types.operation import Operation from .types.sync_request import SyncRequest from .types.sync_response import SyncResponse diff --git a/src/server/types/sync_request.py b/src/server/types/sync_request.py index 3ce61fd..33c7746 100644 --- a/src/server/types/sync_request.py +++ b/src/server/types/sync_request.py @@ -1,6 +1,6 @@ from pydantic import BaseModel -from .operation import Operation +from engine.types.operation import Operation class SyncRequest(BaseModel): @@ -15,4 +15,4 @@ class SyncRequest(BaseModel): @classmethod def decode(cls, data: bytes): - return cls.model_validate_json(data) \ No newline at end of file + return cls.model_validate_json(data) diff --git a/src/server/types/sync_response.py b/src/server/types/sync_response.py index 35568c0..78e51cd 100644 --- a/src/server/types/sync_response.py +++ b/src/server/types/sync_response.py @@ -1,7 +1,7 @@ from pydantic import BaseModel -from .operation import Operation -from .snapshot import Snapshot +from engine.types.operation import Operation +from engine.types.snapshot import Snapshot class SyncResponse(BaseModel):