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
use crate::exid::ExId;
use crate::Parents;
use crate::Prop;
use crate::Value;
/// An observer of operations applied to the document.
pub trait OpObserver: Default + Clone {
/// A new value has been inserted into the given object.
///
/// - `parents`: A parents iterator that can be used to collect path information
/// - `objid`: the object that has been inserted into.
/// - `index`: the index the new value has been inserted at.
/// - `tagged_value`: the value that has been inserted and the id of the operation that did the
/// insert.
fn insert(
&mut self,
parents: Parents<'_>,
objid: ExId,
index: usize,
tagged_value: (Value<'_>, ExId),
);
/// A new value has been put into the given object.
///
/// - `parents`: A parents iterator that can be used to collect path information
/// - `objid`: the object that has been put into.
/// - `prop`: the prop that the value as been put at.
/// - `tagged_value`: the value that has been put into the object and the id of the operation
/// that did the put.
/// - `conflict`: whether this put conflicts with other operations.
fn put(
&mut self,
parents: Parents<'_>,
objid: ExId,
prop: Prop,
tagged_value: (Value<'_>, ExId),
conflict: bool,
);
/// A counter has been incremented.
///
/// - `parents`: A parents iterator that can be used to collect path information
/// - `objid`: the object that contains the counter.
/// - `prop`: they prop that the chounter is at.
/// - `tagged_value`: the amount the counter has been incremented by, and the the id of the
/// increment operation.
fn increment(
&mut self,
parents: Parents<'_>,
objid: ExId,
prop: Prop,
tagged_value: (i64, ExId),
);
/// A value has beeen deleted.
///
/// - `parents`: A parents iterator that can be used to collect path information
/// - `objid`: the object that has been deleted in.
/// - `prop`: the prop of the value that has been deleted.
fn delete(&mut self, parents: Parents<'_>, objid: ExId, prop: Prop);
/// Branch of a new op_observer later to be merged
///
/// Called by AutoCommit when creating a new transaction. Observer branch
/// will be merged on `commit()` or thrown away on `rollback()`
///
fn branch(&self) -> Self {
Self::default()
}
/// Merge observed information from a transaction.
///
/// Called by AutoCommit on `commit()`
///
/// - `other`: Another Op Observer of the same type
fn merge(&mut self, other: &Self);
}
impl OpObserver for () {
fn insert(
&mut self,
_parents: Parents<'_>,
_objid: ExId,
_index: usize,
_tagged_value: (Value<'_>, ExId),
) {
}
fn put(
&mut self,
_parents: Parents<'_>,
_objid: ExId,
_prop: Prop,
_tagged_value: (Value<'_>, ExId),
_conflict: bool,
) {
}
fn increment(
&mut self,
_parents: Parents<'_>,
_objid: ExId,
_prop: Prop,
_tagged_value: (i64, ExId),
) {
}
fn delete(&mut self, _parents: Parents<'_>, _objid: ExId, _prop: Prop) {}
fn merge(&mut self, _other: &Self) {}
}
/// Capture operations into a [`Vec`] and store them as patches.
#[derive(Default, Debug, Clone)]
pub struct VecOpObserver {
patches: Vec<Patch>,
}
impl VecOpObserver {
/// Take the current list of patches, leaving the internal list empty and ready for new
/// patches.
pub fn take_patches(&mut self) -> Vec<Patch> {
std::mem::take(&mut self.patches)
}
}
impl OpObserver for VecOpObserver {
fn insert(
&mut self,
mut parents: Parents<'_>,
obj: ExId,
index: usize,
(value, id): (Value<'_>, ExId),
) {
let path = parents.path();
self.patches.push(Patch::Insert {
obj,
path,
index,
value: (value.into_owned(), id),
});
}
fn put(
&mut self,
mut parents: Parents<'_>,
obj: ExId,
prop: Prop,
(value, id): (Value<'_>, ExId),
conflict: bool,
) {
let path = parents.path();
self.patches.push(Patch::Put {
obj,
path,
prop,
value: (value.into_owned(), id),
conflict,
});
}
fn increment(
&mut self,
mut parents: Parents<'_>,
obj: ExId,
prop: Prop,
tagged_value: (i64, ExId),
) {
let path = parents.path();
self.patches.push(Patch::Increment {
obj,
path,
prop,
value: tagged_value,
});
}
fn delete(&mut self, mut parents: Parents<'_>, obj: ExId, prop: Prop) {
let path = parents.path();
self.patches.push(Patch::Delete { obj, path, prop })
}
fn merge(&mut self, other: &Self) {
self.patches.extend_from_slice(other.patches.as_slice())
}
}
/// A notification to the application that something has changed in a document.
#[derive(Debug, Clone, PartialEq)]
pub enum Patch {
/// Associating a new value with a prop in a map, or an existing list element
Put {
/// path to the object
path: Vec<(ExId, Prop)>,
/// The object that was put into.
obj: ExId,
/// The prop that the new value was put at.
prop: Prop,
/// The value that was put, and the id of the operation that put it there.
value: (Value<'static>, ExId),
/// Whether this put conflicts with another.
conflict: bool,
},
/// Inserting a new element into a list/text
Insert {
/// path to the object
path: Vec<(ExId, Prop)>,
/// The object that was inserted into.
obj: ExId,
/// The index that the new value was inserted at.
index: usize,
/// The value that was inserted, and the id of the operation that inserted it there.
value: (Value<'static>, ExId),
},
/// Incrementing a counter.
Increment {
/// path to the object
path: Vec<(ExId, Prop)>,
/// The object that was incremented in.
obj: ExId,
/// The prop that was incremented.
prop: Prop,
/// The amount that the counter was incremented by, and the id of the operation that
/// did the increment.
value: (i64, ExId),
},
/// Deleting an element from a list/text
Delete {
/// path to the object
path: Vec<(ExId, Prop)>,
/// The object that was deleted from.
obj: ExId,
/// The prop that was deleted.
prop: Prop,
},
}