zbox 0.3.0

Zbox is a zero-details, privacy-focused embeddable file system.
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use std::fmt::{self, Debug};
use std::sync::{Arc, RwLock, Weak};
use std::clone::Clone;
use std::default::Default;
use std::ops::Deref;

use error::{Error, Result};
use base::IntoRef;
use base::lru::{Lru, CountMeter, Pinnable};
use volume::{VolumeRef, Persistable};
use super::{Eid, Id, CloneNew, Txid, TxMgrRef};
use super::trans::{Action, Transable};

/// Cow switch
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
enum Switch {
    Left,
    Right,
}

impl Switch {
    #[inline]
    fn other(&self) -> Switch {
        match *self {
            Switch::Left => Switch::Right,
            Switch::Right => Switch::Left,
        }
    }

    #[inline]
    fn toggle(&mut self) {
        *self = self.other();
    }
}

impl Default for Switch {
    #[inline]
    fn default() -> Self {
        Switch::Left
    }
}

/// Cow slot
#[derive(Debug, Deserialize, Serialize)]
struct Slot<T: Id> {
    id: Eid,
    txid: Option<Txid>,

    #[serde(skip_serializing, skip_deserializing, default)]
    inner: Option<T>,
}

impl<T: Id> Slot<T> {
    fn new(inner: T) -> Self {
        Slot {
            id: inner.id().clone(),
            txid: None,
            inner: Some(inner),
        }
    }

    fn inner_ref(&self) -> &T {
        match self.inner {
            Some(ref inner) => inner,
            None => panic!("Cow slot not loaded"),
        }
    }

    fn inner_ref_mut(&mut self) -> &mut T {
        match self.inner {
            Some(ref mut inner) => inner,
            None => panic!("Cow slot not loaded"),
        }
    }
}

/// Copy-on-write wrapper
#[derive(Default, Deserialize, Serialize)]
pub struct Cow<T>
where
    T: Debug + Default + Send + Sync + CloneNew,
{
    id: Eid,
    switch: Switch,
    left: Option<Slot<T>>,
    right: Option<Slot<T>>,

    #[serde(skip_serializing, skip_deserializing, default)]
    txid: Option<Txid>,

    #[serde(skip_serializing, skip_deserializing, default)]
    self_ref: CowWeakRef<T>,

    #[serde(skip_serializing, skip_deserializing, default)]
    txmgr: TxMgrRef,
}

impl<'d, T> Cow<T>
where
    T: Debug
        + Default
        + Send
        + Sync
        + CloneNew
        + Persistable<'d>
        + 'static,
{
    fn new(inner: T, txmgr: &TxMgrRef) -> Self {
        Cow {
            id: Eid::new(),
            switch: Switch::default(),
            left: Some(Slot::new(inner)),
            right: None,
            txid: None,
            self_ref: Weak::default(),
            txmgr: txmgr.clone(),
        }
    }

    /// Add self to transaction
    fn add_to_trans(&mut self, action: Action) -> Result<()> {
        let curr_txid = Txid::current()?;

        if let Some(txid) = self.txid {
            if txid != curr_txid {
                return Err(Error::InUse);
            }
        }

        // add self to transaction
        {
            let mut txmgr = self.txmgr.write().unwrap();
            let self_ref = self.self_ref.upgrade().unwrap();
            txmgr.add_to_trans(&self.id, curr_txid, self_ref, action)?;
        }

        Ok(match self.txid {
            Some(txid) => assert_eq!(txid, curr_txid),
            None => {
                self.txid = Some(curr_txid);
                if action == Action::New {
                    self.slot_mut().txid = self.txid;
                }
            }
        })
    }

    /// Get mutable reference for inner object by cloning it
    pub fn make_mut(&mut self) -> Result<&mut T> {
        self.add_to_trans(Action::Update)?;

        if self.slot().txid == self.txid {
            let switch = self.switch;
            return Ok(self.index_mut_by(switch));
        }

        if !self.has_other() {
            let new_val = T::clone_new(self);
            let mut slot = Slot::new(new_val);
            slot.txid = self.txid;
            *self.other_mut() = Some(slot);
        }

        assert_eq!(self.other_slot().txid, self.txid);
        let other = self.switch.other();
        Ok(self.index_mut_by(other))
    }

    /// Get mutable reference of inner object
    /// without adding to transaction
    pub fn make_mut_naive(&mut self) -> Result<&mut T> {
        if self.txid.is_some() {
            return Err(Error::InTrans);
        }
        let curr_switch = self.switch;
        Ok(self.index_mut_by(curr_switch))
    }

    /// Mark cow as to be deleted
    pub fn make_del(&mut self) -> Result<()> {
        self.add_to_trans(Action::Delete)
    }

    // mutable index by switch
    fn index_mut_by(&mut self, switch: Switch) -> &mut T {
        match switch {
            Switch::Left => {
                match self.left {
                    Some(ref mut slot) => return slot.inner_ref_mut(),
                    None => {}
                }
            }
            Switch::Right => {
                match self.right {
                    Some(ref mut slot) => return slot.inner_ref_mut(),
                    None => {}
                }
            }
        }
        panic!("Cow slot is empty");
    }

    fn has_other(&self) -> bool {
        match self.switch {
            Switch::Left => self.right.is_some(),
            Switch::Right => self.left.is_some(),
        }
    }

    fn other_mut(&mut self) -> &mut Option<Slot<T>> {
        match self.switch {
            Switch::Left => &mut self.right,
            Switch::Right => &mut self.left,
        }
    }

    fn slot_by(&self, switch: Switch) -> &Slot<T> {
        match switch {
            Switch::Left => {
                match self.left {
                    Some(ref slot) => return slot,
                    None => {}
                }
            }
            Switch::Right => {
                match self.right {
                    Some(ref slot) => return slot,
                    None => {}
                }
            }
        }
        panic!("Cow slot is empty");
    }

    #[inline]
    fn slot(&self) -> &Slot<T> {
        self.slot_by(self.switch)
    }

    #[inline]
    fn other_slot(&self) -> &Slot<T> {
        self.slot_by(self.switch.other())
    }

    fn slot_mut_by(&mut self, switch: Switch) -> &mut Slot<T> {
        match switch {
            Switch::Left => {
                match self.left {
                    Some(ref mut slot) => return slot,
                    None => {}
                }
            }
            Switch::Right => {
                match self.right {
                    Some(ref mut slot) => return slot,
                    None => {}
                }
            }
        }
        panic!("Cow slot is empty");
    }

    fn slot_mut(&mut self) -> &mut Slot<T> {
        let switch = self.switch;
        self.slot_mut_by(switch)
    }

    pub fn load_cow(
        id: &Eid,
        txmgr: &TxMgrRef,
        vol: &VolumeRef,
    ) -> Result<CowRef<T>> {
        let txid = Txid::current_or_empty();

        // load cow
        let mut cow = <Cow<T> as Persistable>::load(id, txid, vol)?;
        assert!(!cow.has_other());
        {
            // load inner value
            let slot = cow.slot_mut();
            let inner = T::load(&slot.id, txid, vol)?;
            slot.inner = Some(inner);
        }

        let cow_ref = cow.into_ref();
        {
            let mut c = cow_ref.write().unwrap();
            c.self_ref = Arc::downgrade(&cow_ref);
            c.txmgr = txmgr.clone();
        }

        Ok(cow_ref)
    }

    pub fn save_cow(&self, txid: Txid, vol: &VolumeRef) -> Result<()> {
        // save inner value
        T::save(self, txid, vol)?;

        // save cow
        <Cow<T> as Persistable>::save(self, txid, vol)
    }
}

impl<'d, T> Deref for Cow<T>
where
    T: Debug
        + Default
        + Send
        + Sync
        + CloneNew
        + Persistable<'d>
        + 'static,
{
    type Target = T;

    fn deref(&self) -> &T {
        let curr_txid = Txid::current_or_empty();
        if self.slot().txid == Some(curr_txid) || !self.has_other() {
            self.slot().inner_ref()
        } else {
            self.other_slot().inner_ref()
        }
    }
}

impl<T> Debug for Cow<T>
where
    T: Debug + Default + Send + Sync + CloneNew,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Cow")
            .field("id", &self.id)
            .field("txid", &self.txid)
            .field("switch", &self.switch)
            .field("left", &self.left)
            .field("right", &self.right)
            .finish()
    }
}

impl<T> Id for Cow<T>
where
    T: Debug + Default + Send + Sync + CloneNew,
{
    #[inline]
    fn id(&self) -> &Eid {
        &self.id
    }

    #[inline]
    fn id_mut(&mut self) -> &mut Eid {
        &mut self.id
    }
}

impl<T> IntoRef for Cow<T>
where
    T: Debug + Default + Send + Sync + CloneNew,
{
}

impl<'d, T> Persistable<'d> for Cow<T>
where
    T: Debug
        + Default
        + Send
        + Sync
        + CloneNew
        + Persistable<'d>
        + 'static,
{
}

impl<'d, T> Transable for Cow<T>
where
    T: Debug
        + Default
        + Send
        + Sync
        + CloneNew
        + Persistable<'d>
        + 'static,
{
    fn commit(&mut self, action: Action, vol: &VolumeRef) -> Result<()> {
        let txid = self.txid.unwrap();

        match action {
            Action::New => self.save_cow(txid, vol),
            Action::Update => {
                // toggle switch and then save the new inner value
                self.switch.toggle();
                T::save(self, txid, vol)?;

                // remove old inner value
                T::remove(&self.other_slot().id, txid, vol)?;

                // save cow itself
                let other_bk = self.other_mut().take();
                let result = <Cow<T> as Persistable>::save(self, txid, vol);
                *self.other_mut() = other_bk;
                result
            }
            Action::Delete => {
                if self.has_other() {
                    T::remove(&self.other_slot().id, txid, vol)?;
                }
                T::remove(&self.slot().id, txid, vol)?;
                Cow::<T>::remove(&self.id, txid, vol)?;
                Ok(())
            }
        }
    }

    fn complete_commit(&mut self, action: Action) {
        match action {
            Action::New | Action::Delete => {}
            Action::Update => {
                self.other_mut().take();
            }
        }
        self.txid = None;
    }

    fn abort(&mut self, action: Action) {
        match action {
            Action::New => {}
            Action::Update => {
                if self.slot().txid == self.txid {
                    // toggle switch back to old inner value
                    self.switch.toggle();
                }
                self.other_mut().take();
            }
            Action::Delete => {
                if self.has_other() {
                    self.other_mut().take();
                }
            }
        }
        self.txid = None;
    }
}

/// Cow reference type
pub type CowRef<T> = Arc<RwLock<Cow<T>>>;

/// Cow weak reference type
pub type CowWeakRef<T> = Weak<RwLock<Cow<T>>>;

/// Wrap value into Cow reference
pub trait IntoCow<'de>
where
    Self: Debug
        + Default
        + Send
        + Sync
        + CloneNew
        + Persistable<'de>
        + 'static,
{
    fn into_cow(self, txmgr: &TxMgrRef) -> Result<CowRef<Self>> {
        let cow_ref = Cow::new(self, txmgr).into_ref();
        {
            let mut cow = cow_ref.write().unwrap();
            cow.self_ref = Arc::downgrade(&cow_ref);
            cow.add_to_trans(Action::New)?;
        }
        Ok(cow_ref)
    }
}

/// Cow cache pin checker
#[derive(Debug, Clone, Default)]
pub struct CowPinChecker {}

impl<T> Pinnable<CowRef<T>> for CowPinChecker
where
    T: Debug
        + Default
        + Send
        + Sync
        + CloneNew,
{
    fn is_pinned(&self, item: &CowRef<T>) -> bool {
        // if cannot read the inner cow entity, we assume it is pinned
        match item.try_read() {
            Ok(cow) => cow.txid.is_some(),
            Err(_) => true,
        }
    }
}

/// Cow LRU cache
#[derive(Debug, Clone, Default)]
pub struct CowCache<T: Debug + Default + Send + Sync + CloneNew> {
    lru: Arc<RwLock<Lru<Eid, CowRef<T>, CountMeter<CowRef<T>>, CowPinChecker>>>,
    txmgr: TxMgrRef,
}

impl<'d, T> CowCache<T>
where
    T: Debug
        + Default
        + Send
        + Sync
        + CloneNew
        + Persistable<'d>
        + 'static,
{
    pub fn new(capacity: usize, txmgr: &TxMgrRef) -> Self {
        CowCache {
            lru: Arc::new(RwLock::new(Lru::new(capacity))),
            txmgr: txmgr.clone(),
        }
    }

    pub fn get(&self, id: &Eid, vol: &VolumeRef) -> Result<CowRef<T>> {
        let mut lru = self.lru.write().unwrap();

        // get from cache first
        if let Some(val) = lru.get_refresh(id) {
            return Ok(val.clone());
        }

        // if not in cache, load it from volume
        // then insert into cache
        let cow_ref = Cow::<T>::load_cow(id, &self.txmgr, vol)?;
        lru.insert(id.clone(), cow_ref.clone());
        Ok(cow_ref)
    }

    pub fn remove(&self, id: &Eid) -> Option<CowRef<T>> {
        let mut lru = self.lru.write().unwrap();
        lru.remove(id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::{thread, time};
    use base::init_env;
    use base::crypto::{Cipher, Cost};
    use trans::{TxMgr, Txid, Eid, CloneNew};
    use volume::Volume;

    fn setup_vol() -> VolumeRef {
        init_env();
        let uri = "mem://test".to_string();
        let mut vol = Volume::new(&uri).unwrap();
        vol.init(Cost::default(), Cipher::Xchacha).unwrap();
        vol.into_ref()
    }

    #[derive(Debug, Default, Clone, Deserialize, Serialize)]
    struct Obj {
        id: Eid,
        val: u8,
    }

    impl Obj {
        fn new(val: u8) -> Self {
            Obj {
                id: Eid::new(),
                val,
            }
        }

        fn val(&self) -> u8 {
            self.val
        }
    }

    impl Id for Obj {
        fn id(&self) -> &Eid {
            &self.id
        }

        fn id_mut(&mut self) -> &mut Eid {
            &mut self.id
        }
    }

    impl CloneNew for Obj {}
    impl<'de> Persistable<'de> for Obj {}

    #[test]
    fn inner_obj_ref() {
        let vol = setup_vol();
        let txid = Txid::from(0);
        let txmgr = TxMgr::new(txid, &vol).into_ref();
        let val = 42;
        let obj = Obj::new(val);
        let obj2 = Obj::new(val);
        let children_cnt = 4;
        let cow_ref = Cow::new(obj, &txmgr).into_ref();
        {
            let mut c = cow_ref.write().unwrap();
            c.self_ref = Arc::downgrade(&cow_ref);
            c.slot_mut().txid = Some(txid);
        }
        let cow_ref2 = Cow::new(obj2, &txmgr).into_ref();

        let mut children = vec![];
        for i in 0..children_cnt {
            let txmgr = txmgr.clone();
            let cow_ref = cow_ref.clone();
            let cow_ref2 = cow_ref2.clone();
            children.push(thread::spawn(move || {
                if i == 0 {
                    // writer thread to update value
                    TxMgr::begin_trans(&txmgr).unwrap();
                    let mut cow = cow_ref.write().unwrap();
                    assert_eq!(cow.val(), val);
                    assert!(!cow.has_other());
                    {
                        let c = cow.make_mut().unwrap();
                        c.val += 1;
                    }
                    assert!(cow.has_other());
                    assert_eq!(cow.val(), val + 1);
                } else {
                    thread::sleep(time::Duration::from_millis(100));

                    // reader thread should still read old value
                    let cow = cow_ref.read().unwrap();
                    assert_eq!(cow.val(), val);

                    // read unchanged value
                    let cow2 = cow_ref2.read().unwrap();
                    assert_eq!(cow2.val(), val);
                }
            }));
        }
        for child in children {
            let _ = child.join();
        }

    }
}