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
use litl::{impl_debug_as_litl, impl_single_tagged_data_serde, SingleTaggedData};
use thiserror::Error;
use ti64::MsSinceEpoch;
use crate::ops::{ObjID, Op, OpID, OpKind, OpWithTarget};
use rand::{rngs::OsRng, RngCore};
use crate::mem_use::{MemUsage, MemUser};
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SrcID([u8; 16]);
impl SingleTaggedData for SrcID {
const TAG: &'static str = "jmblSrc";
fn as_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
std::borrow::Cow::Borrowed(&self.0)
}
fn from_bytes(data: &[u8]) -> Result<Self, litl::TaggedDataError>
where
Self: Sized,
{
let bytes: [u8; 16] = data
.try_into()
.map_err(Into::<SrcIDError>::into)
.map_err(litl::TaggedDataError::data_error)?;
Ok(SrcID(bytes))
}
}
#[derive(Debug, Error)]
pub enum SrcIDError {
#[error("Invalid length for LogID")]
InvalidLength(#[from] std::array::TryFromSliceError),
}
impl_single_tagged_data_serde!(SrcID);
impl_debug_as_litl!(SrcID);
impl SrcID {
pub fn new_random() -> SrcID {
let mut id = [0u8; 16];
OsRng {}.fill_bytes(&mut id);
SrcID(id)
}
pub fn as_bytes(&self) -> &[u8; 16] {
&self.0
}
}
impl MemUser for SrcID {
fn mem_use(&self) -> MemUsage {
MemUsage::Simple(std::mem::size_of::<SrcID>())
}
}
pub struct OpOutput {
pub src_id: SrcID,
sender: Box<dyn OpSender>,
next_sequence_idx: u32,
}
impl OpOutput {
pub fn next_sequence_idx(&self) -> u32 {
self.next_sequence_idx
}
}
pub trait OpSender {
fn send(&mut self, value: OpWithTarget) -> Result<(), String>;
fn test_get_past_sent_ops(&mut self) -> Option<Vec<OpWithTarget>>;
}
#[derive(Default)]
struct DummySender {
sent_ops: Vec<OpWithTarget>,
}
impl OpSender for DummySender {
fn send(&mut self, value: OpWithTarget) -> Result<(), String> {
self.sent_ops.push(value);
Ok(())
}
fn test_get_past_sent_ops(&mut self) -> Option<Vec<OpWithTarget>> {
Some(std::mem::take(&mut self.sent_ops))
}
}
impl OpOutput {
pub fn new_dummy() -> OpOutput {
OpOutput {
src_id: SrcID::new_random(),
next_sequence_idx: 0,
sender: Box::new(DummySender::default()),
}
}
pub fn new(sender: Box<dyn OpSender>) -> OpOutput {
OpOutput {
src_id: SrcID::new_random(),
next_sequence_idx: 0,
sender,
}
}
pub fn write_op_at_time(
&mut self,
target_obj_id: ObjID,
time: MsSinceEpoch,
prev: OpID,
kind: OpKind,
val: Option<litl::Val>,
) -> Op {
let op = Op {
id: OpID {
src_id: self.src_id,
sequence_idx: self.next_sequence_idx,
},
time,
prev,
kind,
val,
};
self.next_sequence_idx += 1;
self.sender
.send(OpWithTarget {
target_obj_id,
op: op.clone(),
})
.unwrap();
op
}
pub fn write_op_now(
&mut self,
target_obj_id: ObjID,
prev: OpID,
kind: OpKind,
val: Option<litl::Val>,
) -> Op {
let op = Op::new(
OpID {
src_id: self.src_id,
sequence_idx: self.next_sequence_idx,
},
prev,
kind,
val,
);
self.next_sequence_idx += 1;
self.sender
.send(OpWithTarget {
target_obj_id,
op: op.clone(),
})
.unwrap();
op
}
pub fn write_create_op(&mut self, kind: OpKind, val: Option<litl::Val>) -> (Op, ObjID) {
let op = Op::new_initial(
OpID {
src_id: self.src_id,
sequence_idx: self.next_sequence_idx,
},
kind,
val,
);
let obj_id = ObjID::from_first_op_id(op.id);
self.next_sequence_idx += 1;
self.sender
.send(OpWithTarget {
target_obj_id: obj_id,
op: op.clone(),
})
.unwrap();
(op, obj_id)
}
pub fn test_get_past_sent_ops(&mut self) -> Option<Vec<OpWithTarget>> {
self.sender.test_get_past_sent_ops()
}
}