From 934de535d980decf54460f5ccf99cc90558b51db Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 24 Oct 2022 21:57:11 +0800 Subject: [PATCH] Add pick_operation to sync with client --- api/__init__.py | 2 +- api/operation.py | 16 ++++++++++------ tjnetwork.py | 3 +++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 737d03e..b8abc42 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -9,7 +9,7 @@ from .operation import ChangeSet from .operation import get_current_operation from .operation import execute_undo, execute_redo from .operation import have_snapshot, take_snapshot, pick_snapshot -from .operation import sync_with_server +from .operation import pick_operation, sync_with_server from .command import execute_batch_commands diff --git a/api/operation.py b/api/operation.py index 79a3a3b..c1e1c89 100644 --- a/api/operation.py +++ b/api/operation.py @@ -129,12 +129,8 @@ def _get_parents(name: str, id: int) -> list[int]: return ids -def pick_snapshot(name: str, tag: str, discard: bool) -> ChangeSet: - if not have_snapshot(name, tag): - return ChangeSet() - - target = int(read(name, f"select id from snapshot_operation where tag = '{tag}'")['id']) - +def pick_operation(name: str, operation: int, discard: bool) -> ChangeSet: + target = operation curr = get_current_operation(name) curr_parents = _get_parents(name, curr) @@ -171,6 +167,14 @@ def pick_snapshot(name: str, tag: str, discard: bool) -> ChangeSet: return change.compress() +def pick_snapshot(name: str, tag: str, discard: bool) -> ChangeSet: + if not have_snapshot(name, tag): + return ChangeSet() + + target = int(read(name, f"select id from snapshot_operation where tag = '{tag}'")['id']) + return pick_operation(name, target, discard) + + def _get_change_set(name: str, operation: int, undo: bool) -> dict[str, Any]: row = read(name, f'select * from operation where id = {operation}') return eval(row['undo_cs']) if undo else eval(row['redo_cs']) diff --git a/tjnetwork.py b/tjnetwork.py index 58fbcf0..a8151fe 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -93,6 +93,9 @@ def take_snapshot(name: str, tag: str) -> int | None: def pick_snapshot(name: str, tag: str, discard: bool = False) -> ChangeSet: return api.pick_snapshot(name, tag, discard) +def pick_operation(name: str, operation: int, discard: bool = False) -> ChangeSet: + return api.pick_operation(name, operation, discard) + def sync_with_server(name: str, operation: int) -> ChangeSet: return api.sync_with_server(name, operation)