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
use parking_lot::Mutex;
use crate::history::History;
use crate::{Mvcc,Transaction,TxnId};
use guest_cell::{GuestCell,Guest,id::{Id,UniqueId}};
use std::sync::Arc;
use std::hash::Hash;
use std::borrow::Borrow;
use std::ops::{Index,IndexMut};
use std::marker::PhantomData;

/// A transactional memory location.
///
/// Note that cloning an `MvccCell` will alias the cell instead of 
/// creating a new transactional slot.
pub struct MvccCell<T:?Sized>(pub(crate) Arc<MvccBox<T>>);

impl<T:?Sized> Clone for MvccCell<T> {
    /// Creates an alias that shares internal state with `self`
    fn clone(&self)->Self { MvccCell(Arc::clone(&self.0)) }
}

impl<T:?Sized> MvccCell<T> {
    /// Initialize a new transactional memory location
    pub fn new(mvcc: &Mvcc, init: Box<T>)->Self {
        MvccCell(MvccBox::new(mvcc, init))
    }

    pub(crate) fn erase<'a>(&self)->ErasedCell<'a> where T:'a {
        ErasedCell(Arc::clone(&self.0) as Arc<dyn 'a + Erased>)
    }
}

pub(crate) struct MvccBox<T:?Sized> {
    pub(crate) mvcc_id: Id,
    pub(crate) history: Mutex<History<Box<T>>>,
    pub(crate) pending: GuestCell<T>,

    // We use unsafe to break &Ts out of the Mutex,
    // and this is required to fixup the automatic
    // Send + Sync bounds
    phantom: PhantomData<std::sync::RwLock<T>>,
}


impl<T:?Sized> MvccBox<T> {
    pub fn new(mvcc: &Mvcc, init:Box<T>)->Arc<Self> {
        Arc::new(MvccBox {
            mvcc_id: mvcc.id(),
            history: Mutex::new(History::new(init)),
            pending: GuestCell::default(),
            phantom: PhantomData,
        })
    }

    fn pending_access(self: &Arc<Self>)->PendingAccessor<T> {
        PendingAccessor(Arc::clone(self))
    }
}

impl<'a, T:'a + ?Sized> Index<&'_ MvccCell<T>> for Transaction<'a> {
    type Output = T;
    fn index(&self, cell: &MvccCell<T>)->&T {
        let txn = self;
        txn.visible.lock().insert(cell.erase());
        txn.pending
            .get(cell.0.pending_access())
            .unwrap_or_else(|| txn.get_committed_raw(cell))
    }
}

impl<'a, T:'a + ?Sized> IndexMut<&'_ MvccCell<T>>
    for Transaction<'a>
    where Box<T>: Clone
{
    fn index_mut(&mut self, cell:&MvccCell<T>)->&mut T {
        let txn = self;
        txn.visible.get_mut().insert(cell.erase());
        txn.pending.get_or_init_mut(
            cell.0.pending_access(),
            || cell.0.history.lock().lookup(*txn.seq).clone()
        )
    }
}

impl<'a> Transaction<'a> {
    /// Store a new value inside the cell.
    ///
    /// Returns the old value iff it was previously written by this transaction
    pub fn replace<T:'a+?Sized>(&mut self, cell:&MvccCell<T>, val: Box<T>)->Option<Box<T>> {
        self.visible.get_mut().insert(cell.erase());
        self.pending.set(cell.0.pending_access(), val)
    }

    /// Reverts this cell to its value at the beginning of the transaction.
    ///
    /// Returns the pending value, if any.
    pub fn revert<T:'a+?Sized>(&mut self, cell:&MvccCell<T>)->Option<Box<T>> {
        self.visible.get_mut().insert(cell.erase());
        self.pending.take(cell.0.pending_access())
    }

    /// Get the value of this cell as of the start of the transaction
    pub fn get_committed<T:'a+?Sized>(&self, cell: &MvccCell<T>)->&T {
        self.visible.lock().insert(cell.erase());
        self.get_committed_raw(cell)
    }
        
    fn get_committed_raw<T:'a+?Sized>(&self, cell: &MvccCell<T>)->&T {
        let guard = cell.0.history.lock();
        let result: *const T = &***guard.lookup(*self.seq) as *const T;

        // Safety: Value is boxed and read-only; will remain valid
        //         until cleaned up by vacuum, which has its own
        //         aliasing protections
        unsafe { &* result }
    }
}

pub(crate) trait Erased {
    fn last_changed(&self)->TxnId;
    fn commit<'a>(self: Arc<Self>, guard: &mut UniqueId, seq:TxnId, txn: &mut Guest<'a>) where Self:'a;
}

impl<T:?Sized> Erased for MvccBox<T> {
    fn last_changed(&self)->TxnId {
        self.history.lock().current()
    }

    fn commit<'a>(self: Arc<Self>, guard: &mut UniqueId, seq: TxnId, txn: &mut Guest<'a>) where Self:'a {
        assert_eq!(**guard, self.mvcc_id);
        let Some(val) = txn.take(self.pending_access()) else { return };
        self.history.lock().push(seq, val);
    }
}

struct PendingAccessor<T:?Sized>(Arc<MvccBox<T>>);
impl<T:?Sized> Borrow<GuestCell<T>> for PendingAccessor<T> {
    fn borrow(&self)->&GuestCell<T> {
        &self.0.pending
    }
}

unsafe impl<T:?Sized> guest_cell::IndirectGuestCell for PendingAccessor<T> {
    type Inner = T;
}

pub(crate) struct ErasedCell<'a>(pub Arc<dyn 'a + Erased>);

impl Eq for ErasedCell<'_> {}
impl PartialEq for ErasedCell<'_> {
    fn eq(&self, rhs:&Self)->bool {
        Arc::as_ptr(&self.0) == Arc::as_ptr(&rhs.0)
    }
}

impl Hash for ErasedCell<'_> {
    fn hash<H: std::hash::Hasher>(&self, hasher:&mut H) {
        Arc::as_ptr(&self.0).hash(hasher)
    }
}