loro_internal/delta/
map_delta.rs1use std::{hash::Hash, sync::Weak};
2
3use rustc_hash::FxHashMap;
4use loro_common::IdLp;
5use serde::{ser::SerializeStruct, Serialize};
6
7use crate::{
8 change::Lamport, handler::ValueOrHandler, id::PeerID, span::HasLamport, InternalString,
9 LoroDocInner, LoroValue,
10};
11
12#[derive(Default, Debug, Clone, Serialize)]
13pub struct MapDelta {
14 pub updated: FxHashMap<InternalString, Option<MapValue>>,
17}
18
19#[derive(Debug, Clone)]
20pub struct MapValue {
21 pub value: Option<LoroValue>,
22 pub lamp: Lamport,
23 pub peer: PeerID,
24}
25
26impl Ord for MapValue {
27 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
28 self.lamp
29 .cmp(&other.lamp)
30 .then_with(|| self.peer.cmp(&other.peer))
31 }
32}
33
34impl PartialOrd for MapValue {
35 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
36 Some(self.cmp(other))
37 }
38}
39
40impl PartialEq for MapValue {
41 fn eq(&self, other: &Self) -> bool {
42 self.lamp == other.lamp && self.peer == other.peer
43 }
44}
45
46impl Eq for MapValue {}
47
48impl MapValue {
49 pub fn idlp(&self) -> IdLp {
50 IdLp::new(self.peer, self.lamp)
51 }
52}
53
54#[derive(Default, Debug, Clone)]
55pub struct ResolvedMapDelta {
56 pub updated: FxHashMap<InternalString, ResolvedMapValue>,
57}
58
59#[derive(Debug, Clone)]
60pub struct ResolvedMapValue {
61 pub value: Option<ValueOrHandler>,
62 pub idlp: IdLp,
63}
64
65impl ResolvedMapValue {
66 pub(crate) fn from_map_value(v: MapValue, doc: &Weak<LoroDocInner>) -> Self {
67 let doc = &doc.upgrade().unwrap();
68 ResolvedMapValue {
69 idlp: IdLp::new(v.peer, v.lamp),
70 value: v.value.map(|v| ValueOrHandler::from_value(v, doc)),
71 }
72 }
73
74 pub fn new_unset() -> Self {
76 ResolvedMapValue {
77 idlp: IdLp::new(PeerID::default(), Lamport::MAX),
78 value: None,
79 }
80 }
81}
82
83impl MapDelta {
84 pub(crate) fn compose(mut self, x: MapDelta) -> MapDelta {
85 for (k, v) in x.updated.into_iter() {
86 if let Some(old) = self.updated.get_mut(&k) {
87 if &v > old {
88 *old = v;
89 }
90 } else {
91 self.updated.insert(k, v);
92 }
93 }
94 self
95 }
96
97 #[inline]
98 pub fn new() -> Self {
99 MapDelta {
100 updated: FxHashMap::default(),
101 }
102 }
103
104 #[inline]
105 pub fn with_entry(mut self, key: InternalString, map_value: MapValue) -> Self {
106 self.updated.insert(key, Some(map_value));
107 self
108 }
109}
110
111impl ResolvedMapDelta {
112 pub(crate) fn compose(&self, x: ResolvedMapDelta) -> ResolvedMapDelta {
113 let mut updated = self.updated.clone();
114 for (k, v) in x.updated.into_iter() {
115 if let Some(old) = updated.get_mut(&k) {
116 if v.idlp > old.idlp {
117 *old = v;
118 }
119 } else {
120 updated.insert(k, v);
121 }
122 }
123 ResolvedMapDelta { updated }
124 }
125
126 #[inline]
127 pub fn new() -> Self {
128 ResolvedMapDelta {
129 updated: FxHashMap::default(),
130 }
131 }
132
133 #[inline]
134 pub fn with_entry(mut self, key: InternalString, map_value: ResolvedMapValue) -> Self {
135 self.updated.insert(key, map_value);
136 self
137 }
138
139 pub(crate) fn transform(&mut self, b: &ResolvedMapDelta, left_prior: bool) {
140 for (k, _) in b.updated.iter() {
141 if !left_prior {
142 self.updated.remove(k);
143 }
144 }
145 }
146}
147
148impl Hash for MapValue {
149 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
150 self.peer.hash(state);
152 self.lamp.hash(state);
153 }
154}
155
156impl HasLamport for MapValue {
157 fn lamport(&self) -> Lamport {
158 self.lamp
159 }
160}
161
162impl Serialize for MapValue {
163 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
164 where
165 S: serde::Serializer,
166 {
167 let mut s = serializer.serialize_struct("MapValue", 2)?;
168 s.serialize_field("value", &self.value)?;
169 s.serialize_field("lamport", &self.lamp)?;
170 s.serialize_field("id", &self.idlp())?;
171 s.end()
172 }
173}