forked from y-crdt/ypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy_doc.rs
332 lines (307 loc) · 11.4 KB
/
y_doc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use crate::y_array::YArray;
use crate::y_map::YMap;
use crate::y_text::YText;
use crate::y_transaction::YTransaction;
use crate::y_xml::YXmlElement;
use crate::y_xml::YXmlText;
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::types::PyTuple;
use yrs::updates::encoder::Encode;
use yrs::AfterTransactionEvent as YrsAfterTransactionEvent;
use yrs::Doc;
use yrs::OffsetKind;
use yrs::Options;
use yrs::SubscriptionId;
use yrs::Transaction;
/// A Ypy document type. Documents are most important units of collaborative resources management.
/// All shared collections live within a scope of their corresponding documents. All updates are
/// generated on per document basis (rather than individual shared type). All operations on shared
/// collections happen via [YTransaction], which lifetime is also bound to a document.
///
/// Document manages so called root types, which are top-level shared types definitions (as opposed
/// to recursively nested types).
///
/// A basic workflow sample:
///
/// ```python
/// from y_py import YDoc
///
/// doc = YDoc()
/// with doc.begin_transaction() as txn:
/// text = txn.get_text('name')
/// text.push(txn, 'hello world')
/// output = text.to_string(txn)
/// print(output)
/// ```
#[pyclass(unsendable, subclass)]
pub struct YDoc(pub Doc);
#[pymethods]
impl YDoc {
/// Creates a new Ypy document. If `client_id` parameter was passed it will be used as this
/// document globally unique identifier (it's up to caller to ensure that requirement).
/// Otherwise it will be assigned a randomly generated number.
#[new]
pub fn new(
client_id: Option<u64>,
offset_kind: Option<String>,
skip_gc: Option<bool>,
) -> PyResult<Self> {
let mut options = Options::default();
if let Some(client_id) = client_id {
options.client_id = client_id;
}
if let Some(raw_offset) = offset_kind {
let clean_offset = raw_offset.to_lowercase().replace("-", "");
let offset = match clean_offset.as_str() {
"utf8" => Ok(OffsetKind::Bytes),
"utf16" => Ok(OffsetKind::Utf16),
"utf32" => Ok(OffsetKind::Utf32),
_ => Err(pyo3::exceptions::PyValueError::new_err(format!(
"'{}' is not a valid offset kind (utf8, utf16, or utf32).",
clean_offset
))),
}?;
options.offset_kind = offset;
}
if let Some(skip_gc) = skip_gc {
options.skip_gc = skip_gc;
}
Ok(YDoc(Doc::with_options(options)))
}
/// Gets globally unique identifier of this `YDoc` instance.
#[getter]
pub fn client_id(&self) -> u64 {
self.0.client_id as u64
}
/// Returns a new transaction for this document. Ypy shared data types execute their
/// operations in a context of a given transaction. Each document can have only one active
/// transaction at the time - subsequent attempts will cause exception to be thrown.
///
/// Transactions started with `doc.begin_transaction` can be released by deleting the transaction object
/// method.
///
/// Example:
///
/// ```python
/// from y_py import YDoc
/// doc = YDoc()
/// text = doc.get_text('name')
/// with doc.begin_transaction() as txn:
/// text.insert(txn, 0, 'hello world')
/// ```
pub fn begin_transaction(&self) -> YTransaction {
YTransaction::new(self.0.transact())
}
pub fn transact(&mut self, callback: PyObject) -> PyResult<PyObject> {
let txn = self.begin_transaction();
Python::with_gil(|py| {
let args = PyTuple::new(py, std::iter::once(txn.into_py(py)));
callback.call(py, args, None)
})
}
/// Returns a `YMap` shared data type, that's accessible for subsequent accesses using given
/// `name`.
///
/// If there was no instance with this name before, it will be created and then returned.
///
/// If there was an instance with this name, but it was of different type, it will be projected
/// onto `YMap` instance.
pub fn get_map(&mut self, name: &str) -> YMap {
self.begin_transaction().get_map(name)
}
/// Returns a `YXmlElement` shared data type, that's accessible for subsequent accesses using
/// given `name`.
///
/// If there was no instance with this name before, it will be created and then returned.
///
/// If there was an instance with this name, but it was of different type, it will be projected
/// onto `YXmlElement` instance.
pub fn get_xml_element(&mut self, name: &str) -> YXmlElement {
YXmlElement(self.begin_transaction().get_xml_element(name))
}
/// Returns a `YXmlText` shared data type, that's accessible for subsequent accesses using given
/// `name`.
///
/// If there was no instance with this name before, it will be created and then returned.
///
/// If there was an instance with this name, but it was of different type, it will be projected
/// onto `YXmlText` instance.
pub fn get_xml_text(&mut self, name: &str) -> YXmlText {
YXmlText(self.begin_transaction().get_xml_text(name))
}
/// Returns a `YArray` shared data type, that's accessible for subsequent accesses using given
/// `name`.
///
/// If there was no instance with this name before, it will be created and then returned.
///
/// If there was an instance with this name, but it was of different type, it will be projected
/// onto `YArray` instance.
pub fn get_array(&mut self, name: &str) -> YArray {
self.begin_transaction().get_array(name)
}
/// Returns a `YText` shared data type, that's accessible for subsequent accesses using given
/// `name`.
///
/// If there was no instance with this name before, it will be created and then returned.
///
/// If there was an instance with this name, but it was of different type, it will be projected
/// onto `YText` instance.
pub fn get_text(&mut self, name: &str) -> YText {
self.begin_transaction().get_text(name)
}
/// Subscribes a callback to a `YDoc` lifecycle event.
pub fn observe_after_transaction(&mut self, callback: PyObject) -> SubscriptionId {
self.0
.observe_transaction_cleanup(move |txn, event| {
Python::with_gil(|py| {
let event = AfterTransactionEvent::new(event, txn);
if let Err(err) = callback.call1(py, (event,)) {
err.restore(py)
}
})
})
.into()
}
}
/// Encodes a state vector of a given Ypy document into its binary representation using lib0 v1
/// encoding. State vector is a compact representation of updates performed on a given document and
/// can be used by `encode_state_as_update` on remote peer to generate a delta update payload to
/// synchronize changes between peers.
///
/// Example:
///
/// ```python
/// from y_py import YDoc, encode_state_vector, encode_state_as_update, apply_update from y_py
///
/// # document on machine A
/// local_doc = YDoc()
/// local_sv = encode_state_vector(local_doc)
///
/// # document on machine B
/// remote_doc = YDoc()
/// remote_delta = encode_state_as_update(remote_doc, local_sv)
///
/// apply_update(local_doc, remote_delta)
/// ```
#[pyfunction]
pub fn encode_state_vector(doc: &mut YDoc) -> PyObject {
doc.begin_transaction().state_vector_v1()
}
/// Encodes all updates that have happened since a given version `vector` into a compact delta
/// representation using lib0 v1 encoding. If `vector` parameter has not been provided, generated
/// delta payload will contain all changes of a current Ypy document, working effectively as its
/// state snapshot.
///
/// Example:
///
/// ```python
/// from y_py import YDoc, encode_state_vector, encode_state_as_update, apply_update
///
/// # document on machine A
/// local_doc = YDoc()
/// local_sv = encode_state_vector(local_doc)
///
/// # document on machine B
/// remote_doc = YDoc()
/// remote_delta = encode_state_as_update(remote_doc, local_sv)
///
/// apply_update(local_doc, remote_delta)
/// ```
#[pyfunction]
pub fn encode_state_as_update(doc: &YDoc, vector: Option<Vec<u8>>) -> PyResult<PyObject> {
doc.begin_transaction().diff_v1(vector)
}
/// Applies delta update generated by the remote document replica to a current document. This
/// method assumes that a payload maintains lib0 v1 encoding format.
///
/// Example:
///
/// ```python
/// from y_py import YDoc, encode_state_vector, encode_state_as_update, apply_update
///
/// # document on machine A
/// local_doc = YDoc()
/// local_sv = encode_state_vector(local_doc)
///
/// # document on machine B
/// remote_doc = YDoc()
/// remote_delta = encode_state_as_update(remote_doc, local_sv)
///
/// apply_update(local_doc, remote_delta)
/// ```
#[pyfunction]
pub fn apply_update(doc: &mut YDoc, diff: Vec<u8>) -> PyResult<()> {
doc.begin_transaction().apply_v1(diff)?;
Ok(())
}
#[pyclass(unsendable)]
pub struct AfterTransactionEvent {
inner: *const YrsAfterTransactionEvent,
txn: *const Transaction,
before_state: Option<PyObject>,
after_state: Option<PyObject>,
delete_set: Option<PyObject>,
}
impl AfterTransactionEvent {
fn new(event: &YrsAfterTransactionEvent, txn: &Transaction) -> Self {
let inner = event as *const YrsAfterTransactionEvent;
let txn = txn as *const Transaction;
AfterTransactionEvent {
inner,
txn,
before_state: None,
after_state: None,
delete_set: None,
}
}
fn inner(&self) -> &YrsAfterTransactionEvent {
unsafe { self.inner.as_ref().unwrap() }
}
fn txn(&self) -> &Transaction {
unsafe { self.txn.as_ref().unwrap() }
}
}
#[pymethods]
impl AfterTransactionEvent {
/// Returns a current shared type instance, that current event changes refer to.
#[getter]
pub fn before_state(&mut self) -> PyObject {
if let Some(before_state) = self.before_state.as_ref() {
before_state.clone()
} else {
let before_state = self.inner().before_state.encode_v1();
let before_state: PyObject =
Python::with_gil(|py| PyBytes::new(py, &before_state).into());
self.before_state = Some(before_state.clone());
before_state
}
}
#[getter]
pub fn after_state(&mut self) -> PyObject {
if let Some(after_state) = self.after_state.as_ref() {
after_state.clone()
} else {
let after_state = self.inner().after_state.encode_v1();
let after_state: PyObject =
Python::with_gil(|py| PyBytes::new(py, &after_state).into());
self.after_state = Some(after_state.clone());
after_state
}
}
#[getter]
pub fn delete_set(&mut self) -> PyObject {
if let Some(delete_set) = self.delete_set.as_ref() {
delete_set.clone()
} else {
let delete_set = self.inner().delete_set.encode_v1();
let delete_set: PyObject = Python::with_gil(|py| PyBytes::new(py, &delete_set).into());
self.delete_set = Some(delete_set.clone());
delete_set
}
}
pub fn get_update(&self) -> PyObject {
let update = self.txn().encode_update_v1();
Python::with_gil(|py| PyBytes::new(py, &update).into())
}
}