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
333
334
335
336
337
338
339
340
use {
crate::{
core::{
self,
cache::{CacheRead, CacheWrite},
misc::range_ext::{OwnedRangeBounds, RangeBoundsExt},
primitive::prollytree::refimpl,
workspace::Workspace,
Commit,
},
error::Type as TypeError,
path::{SegmentResolve, SegmentUpdate},
Addr, Error, Key, Value,
},
std::fmt,
};
pub struct Map<'f>(Box<dyn InnerMap<'f> + 'f>);
impl<'f> Map<'f> {
pub fn new<C, W>(inner: core::Map<'f, C, W>) -> Self
where
C: CacheRead + CacheWrite,
W: Workspace,
{
Self(Box::new(inner))
}
pub fn batch(&self) -> BatchMap<'f> {
self.0.inner_batch()
}
/// Insert a value into the map to later be committed.
///
/// This value is written to the store immediately, as a staged value.
/// For a large multi-key insertion, see [`Self::batch`].
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() {
/// use fixity::{Fixity, Path, Addr};
/// let f = Fixity::memory();
/// let mut m = f.map(Path::new());
/// let _: Addr = m.insert("foo", "bar").await.unwrap();
/// assert_eq!(m.get("foo").await.unwrap(), Some("bar".into()));
/// # }
/// ```
pub async fn insert<K, V>(&mut self, key: K, value: V) -> Result<Addr, Error>
where
K: Into<Key>,
V: Into<Value>,
{
self.0.inner_insert(key.into(), value.into()).await
}
/// Get a value at the current [`Path`].
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() {
/// use fixity::{Fixity, Path, Addr};
/// let f = Fixity::memory();
/// let mut m = f.map(Path::new());
/// let _: Addr = m.insert("foo", "bar").await.unwrap();
/// assert_eq!(m.get("foo").await.unwrap(), Some("bar".into()));
/// # }
/// ```
pub async fn get<K>(&self, key: K) -> Result<Option<Value>, Error>
where
K: Into<Key>,
{
self.0.inner_get(key.into()).await
}
pub async fn iter<R>(
&self,
range: R,
) -> Result<Box<dyn Iterator<Item = Result<(Key, Value), Error>>>, Error>
where
R: RangeBoundsExt<Key>,
{
self.0.inner_iter(range.into_bounds()).await
}
pub async fn commit(&self) -> Result<Addr, Error> {
self.0.inner_commit().await
}
}
#[async_trait::async_trait]
trait InnerMap<'f> {
fn inner_batch(&self) -> BatchMap<'f>;
async fn inner_insert(&self, key: Key, value: Value) -> Result<Addr, Error>;
async fn inner_get(&self, key: Key) -> Result<Option<Value>, Error>;
async fn inner_iter(
&self,
range: OwnedRangeBounds<Key>,
) -> Result<Box<dyn Iterator<Item = Result<(Key, Value), Error>>>, Error>;
// async fn inner_remove(&self, key: Key) -> Result<Addr, Error>;
async fn inner_commit(&self) -> Result<Addr, Error>;
}
#[async_trait::async_trait]
impl<'f, C, W> InnerMap<'f> for core::Map<'f, C, W>
where
C: CacheRead + CacheWrite,
W: Workspace,
{
fn inner_batch(&self) -> BatchMap<'f> {
let b = self.batch();
BatchMap::new(b)
}
async fn inner_insert(&self, key: Key, value: Value) -> Result<Addr, Error> {
self.insert(key, value).await
}
async fn inner_get(&self, key: Key) -> Result<Option<Value>, Error> {
self.get(key).await
}
async fn inner_iter(
&self,
range: OwnedRangeBounds<Key>,
) -> Result<Box<dyn Iterator<Item = Result<(Key, Value), Error>>>, Error> {
self.iter(range).await
}
// async fn inner_remove(&self, key: Key) -> Result<Addr, Error> {
// self.remove(key).await
// }
async fn inner_commit(&self) -> Result<Addr, Error> {
self.commit().await
}
}
pub struct BatchMap<'f>(Box<dyn InnerBatchMap + 'f>);
impl<'f> BatchMap<'f> {
pub fn new<C, W>(inner_map: core::map::BatchMap<'f, C, W>) -> Self
where
C: CacheRead + CacheWrite,
W: Workspace,
{
Self(Box::new(inner_map))
}
/// Drop the internal change cache that has not yet been staged or committed to storage.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() {
/// # use fixity::{Fixity,Map,path::Path};
/// let f = Fixity::memory();
/// let mut m = f.map(Path::new()).batch();
/// m.insert("foo", "bar");
/// m.clear();
/// assert!(m.get("foo").await.unwrap().is_none());
/// # }
/// ```
pub fn clear(&mut self) {
self.0.inner_clear()
}
/// Insert a value into the map to later be staged or committed.
///
/// This value is not written to the store until [`Self::stage`] or [`Self::commit`]
/// is called, but it can be retrived from the internal cache.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() {
/// # use fixity::{Fixity,Map,path::Path};
/// let f = Fixity::memory();
/// let mut m_1 = f.map(Path::new()).batch();
/// let m_2 = f.map(Path::new()).batch();
/// m_1.insert("foo", "bar");
/// // get pulls from in-memory cache, awaiting stage/commit.
/// assert_eq!(m_1.get("foo").await.unwrap(), Some("bar".into()));
/// // not yet written to storage.
/// assert_eq!(m_2.get("foo").await.unwrap(), None);
/// # }
/// ```
pub fn insert<K, V>(&mut self, key: K, value: V)
where
K: Into<Key>,
V: Into<Value>,
{
self.0.inner_insert(key.into(), value.into())
}
pub async fn get<K>(&self, key: K) -> Result<Option<Value>, Error>
where
K: Into<Key>,
{
self.0.inner_get(key.into()).await
}
/// Write any changes to storage, staging them for a later commit.
///
/// # Examples
///
/// ```rust
/// # #[tokio::main]
/// # async fn main() {
/// # use fixity::{Fixity,Map,path::Path};
/// let f = Fixity::memory();
/// let mut m_1 = f.map(Path::new()).batch();
/// let m_2 = f.map(Path::new()).batch();
/// m_1.insert("foo", "bar");
/// // not yet written to storage.
/// assert_eq!(m_2.get("foo").await.unwrap(), None);
/// let _staged_addr = m_1.stage().await.unwrap();
/// // now in storage.
/// assert_eq!(m_2.get("foo").await.unwrap(), Some("bar".into()));
/// # }
pub async fn stage(&mut self) -> Result<Addr, Error> {
self.0.inner_stage().await
}
/// Write any [staged](Self::stage) changes at the current [`Path`] into the workspace.
///
/// # Examples
///
/// ```rust
/// # #[tokio::main]
/// # async fn main() {
/// # use fixity::{Fixity,Map,path::Path};
/// let f = fixity::fixity::Fixity::memory();
/// let mut m_1 = f.map(Path::new()).batch();
/// let m_2 = f.map(Path::new()).batch();
/// m_1.insert("foo", "bar");
/// // not yet written to storage.
/// assert_eq!(m_2.get("foo").await.unwrap(), None);
/// m_1.stage().await.unwrap();
/// m_1.commit().await.unwrap();
/// // now in storage.
/// assert_eq!(m_2.get("foo").await.unwrap(), Some("bar".into()));
/// # }
pub async fn commit(&mut self) -> Result<Addr, Error> {
self.0.inner_commit().await
}
}
#[async_trait::async_trait]
trait InnerBatchMap {
fn inner_clear(&mut self);
fn inner_insert(&mut self, key: Key, value: Value);
async fn inner_get(&self, key: Key) -> Result<Option<Value>, Error>;
async fn inner_stage(&mut self) -> Result<Addr, Error>;
async fn inner_commit(&mut self) -> Result<Addr, Error>;
}
#[async_trait::async_trait]
impl<'f, C, W> InnerBatchMap for core::map::BatchMap<'f, C, W>
where
C: CacheRead + CacheWrite,
W: Workspace,
{
fn inner_clear(&mut self) {
self.clear();
}
fn inner_insert(&mut self, key: Key, value: Value) {
self.insert(key, value);
}
async fn inner_get(&self, key: Key) -> Result<Option<Value>, Error> {
self.get(key).await
}
async fn inner_stage(&mut self) -> Result<Addr, Error> {
self.stage().await
}
async fn inner_commit(&mut self) -> Result<Addr, Error> {
self.commit().await
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PathSegment {
pub key: Key,
}
impl PathSegment {
pub fn new<T: Into<Key>>(t: T) -> Self {
Self { key: t.into() }
}
}
impl fmt::Debug for PathSegment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Map(")?;
self.key.fmt(f)?;
f.write_str(")")
}
}
impl fmt::Display for PathSegment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Map(")?;
self.key.fmt(f)?;
f.write_str(")")
}
}
#[async_trait::async_trait]
impl<C> SegmentResolve<C> for PathSegment
where
C: CacheRead,
{
async fn resolve(&self, storage: &C, self_addr: Addr) -> Result<Option<Addr>, Error> {
let reader = refimpl::Read::new(storage, self_addr);
let value = match reader.get(&self.key).await? {
Some(v) => v,
None => return Ok(None),
};
let addr = match value {
Value::Addr(addr) => addr,
_ => {
return Err(Error::Type(TypeError::UnexpectedValueVariant {
at_segment: Some(self.key.to_string()),
// addr moved, not sure it's worth prematurely cloning for the failure state.
at_addr: None,
}));
},
};
Ok(Some(addr))
}
}
#[async_trait::async_trait]
impl<C> SegmentUpdate<C> for PathSegment
where
C: CacheRead + CacheWrite,
{
async fn update(
&self,
storage: &C,
self_addr: Option<Addr>,
child_addr: Addr,
) -> Result<Addr, Error> {
if let Some(self_addr) = self_addr {
let kvs = vec![(
self.key.clone(),
refimpl::Change::Insert(Value::Addr(child_addr)),
)];
refimpl::Update::new(storage, self_addr).with_vec(kvs).await
} else {
let kvs = vec![(self.key.clone(), Value::Addr(child_addr))];
refimpl::Create::new(storage).with_vec(kvs).await
}
}
}
impl<T> From<T> for PathSegment
where
T: Into<Key>,
{
fn from(t: T) -> Self {
Self { key: t.into() }
}
}