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
/// Types for converting an OpTree op into a `ChangeOp` or a `DocOp`
use std::borrow::Cow;

use crate::{
    convert,
    op_set::OpSetData,
    storage::AsChangeOp,
    types::{ActorId, Key, MarkData, Op, OpId, OpType, ScalarValue},
};

/// Wrap an op in an implementation of `AsChangeOp` which represents actor IDs using a reference to
/// the actor ID stored in the opset data.
///
/// Note that the methods of `AsChangeOp` will panic if the actor is missing from the OpSetData
pub(crate) fn op_as_actor_id(op: Op<'_>) -> OpWithMetadata<'_> {
    OpWithMetadata { op }
}

pub(crate) struct OpWithMetadata<'a> {
    op: Op<'a>,
}

impl<'a> OpWithMetadata<'a> {
    fn wrap(&self, opid: OpId) -> OpIdWithMetadata<'a> {
        OpIdWithMetadata {
            opid,
            osd: self.op.osd(),
        }
    }
}

pub(crate) struct OpIdWithMetadata<'a> {
    opid: OpId,
    osd: &'a OpSetData,
}

impl<'a> convert::OpId<&'a ActorId> for OpIdWithMetadata<'a> {
    fn counter(&self) -> u64 {
        self.opid.counter()
    }

    fn actor(&self) -> &'a ActorId {
        self.osd.actors.get(self.opid.actor())
    }
}

pub(crate) struct PredWithMetadata<'a> {
    op: Op<'a>,
    offset: usize,
    osd: &'a OpSetData,
}

impl<'a> ExactSizeIterator for PredWithMetadata<'a> {
    fn len(&self) -> usize {
        self.op.pred().len()
    }
}

impl<'a> Iterator for PredWithMetadata<'a> {
    type Item = OpIdWithMetadata<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(opid) = self.op.pred().nth(self.offset).map(|o| *o.id()) {
            self.offset += 1;
            Some(OpIdWithMetadata {
                opid,
                osd: self.osd,
            })
        } else {
            None
        }
    }
}

impl<'a> AsChangeOp<'a> for OpWithMetadata<'a> {
    type ActorId = &'a ActorId;
    type OpId = OpIdWithMetadata<'a>;
    type PredIter = PredWithMetadata<'a>;

    fn action(&self) -> u64 {
        self.op.action().action_index()
    }

    fn insert(&self) -> bool {
        self.op.insert()
    }

    fn val(&self) -> Cow<'a, ScalarValue> {
        match &self.op.action() {
            OpType::Make(..) | OpType::Delete | OpType::MarkEnd(..) => {
                Cow::Owned(ScalarValue::Null)
            }
            OpType::Increment(i) => Cow::Owned(ScalarValue::Int(*i)),
            OpType::Put(s) => Cow::Borrowed(s),
            OpType::MarkBegin(_, MarkData { value, .. }) => Cow::Borrowed(value),
        }
    }

    fn obj(&self) -> convert::ObjId<Self::OpId> {
        if self.op.obj().is_root() {
            convert::ObjId::Root
        } else {
            convert::ObjId::Op(OpIdWithMetadata {
                opid: *self.op.obj().opid(),
                osd: self.op.osd(),
            })
        }
    }

    fn pred(&self) -> Self::PredIter {
        PredWithMetadata {
            op: self.op,
            offset: 0,
            osd: self.op.osd(),
        }
    }

    fn key(&self) -> convert::Key<'a, Self::OpId> {
        match &self.op.key() {
            Key::Map(idx) => convert::Key::Prop(Cow::Owned(self.op.osd().props.get(*idx).into())),
            Key::Seq(e) if e.is_head() => convert::Key::Elem(convert::ElemId::Head),
            Key::Seq(e) => convert::Key::Elem(convert::ElemId::Op(self.wrap(e.0))),
        }
    }

    fn expand(&self) -> bool {
        matches!(
            self.op.action(),
            OpType::MarkBegin(true, _) | OpType::MarkEnd(true)
        )
    }

    fn mark_name(&self) -> Option<Cow<'a, smol_str::SmolStr>> {
        if let OpType::MarkBegin(_, MarkData { name, .. }) = &self.op.action() {
            Some(Cow::Owned(name.clone()))
        } else {
            None
        }
    }
}