-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Intro Alternative Update API as pycrdt.Update * Fix testcase * Add verify merge result string * Change API --------- Co-authored-by: David Brochart <[email protected]>
- Loading branch information
1 parent
b135c24
commit cf03fd3
Showing
7 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from ._pycrdt import get_state as _get_state | ||
from ._pycrdt import get_update as _get_update | ||
from ._pycrdt import merge_updates as _merge_updates | ||
|
||
|
||
def get_state(update: bytes) -> bytes: | ||
"""Returns a state from an update.""" | ||
return _get_state(update) | ||
|
||
|
||
def get_update(update: bytes, state: bytes) -> bytes: | ||
"""Returns an update consisting of all changes from a given update which have not | ||
been seen in the given state. | ||
""" | ||
return _get_update(update, state) | ||
|
||
|
||
def merge_updates(*updates: bytes) -> bytes: | ||
"""Returns an update consisting of a combination of all given updates.""" | ||
return _merge_updates(updates) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::exceptions::PyValueError; | ||
use pyo3::types::{PyBytes, PyTuple}; | ||
use yrs::{diff_updates_v1, encode_state_vector_from_update_v1, merge_updates_v1}; | ||
|
||
#[pyfunction] | ||
pub fn merge_updates(updates: &Bound<'_, PyTuple>) -> PyResult<PyObject> { | ||
let updates: Vec<Vec<u8>> = updates.extract().unwrap(); | ||
let Ok(update) = merge_updates_v1(&updates) else { | ||
return Err(PyValueError::new_err("Cannot merge updates")); | ||
}; | ||
let bytes: PyObject = Python::with_gil(|py| PyBytes::new_bound(py, &update).into()); | ||
Ok(bytes) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn get_state(update: &Bound<'_, PyBytes>) -> PyResult<PyObject> { | ||
let update: &[u8] = update.extract()?; | ||
let Ok(u) = encode_state_vector_from_update_v1(&update) else { | ||
return Err(PyValueError::new_err( | ||
"Cannot encode state vector from update", | ||
)); | ||
}; | ||
let bytes: PyObject = Python::with_gil(|py| PyBytes::new_bound(py, &u).into()); | ||
Ok(bytes) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn get_update(update: &Bound<'_, PyBytes>, state: &Bound<'_, PyBytes>) -> PyResult<PyObject> { | ||
let update: &[u8] = update.extract()?; | ||
let state: &[u8] = state.extract()?; | ||
let Ok(u) = diff_updates_v1(&update, &state) else { | ||
return Err(PyValueError::new_err("Cannot diff updates")); | ||
}; | ||
let bytes: PyObject = Python::with_gil(|py| PyBytes::new_bound(py, &u).into()); | ||
Ok(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from pycrdt import Doc, Map, get_state, get_update, merge_updates | ||
|
||
|
||
def test_update(): | ||
data0 = Map({"key0": "val0"}) | ||
doc0 = Doc() | ||
doc0["data"] = data0 | ||
|
||
data1 = Map({"key1": "val1"}) | ||
doc1 = Doc() | ||
doc1["data"] = data1 | ||
|
||
update0 = doc0.get_update() | ||
update1 = doc1.get_update() | ||
|
||
del doc0 | ||
del doc1 | ||
state0 = get_state(update0) | ||
state1 = get_state(update1) | ||
|
||
update01 = get_update(update0, state1) | ||
update10 = get_update(update1, state0) | ||
|
||
# sync clients | ||
update0 = merge_updates(update0, update10) | ||
update1 = merge_updates(update1, update01) | ||
assert update0 == update1 | ||
|
||
doc0 = Doc() | ||
data0 = doc0.get("data", type=Map) | ||
doc0.apply_update(update0) | ||
doc1 = Doc() | ||
data1 = doc1.get("data", type=Map) | ||
doc1.apply_update(update1) | ||
|
||
assert data0.to_py() == data1.to_py() == {"key0": "val0", "key1": "val1"} |