1mod str_arena;
2use self::str_arena::StrArena;
3use crate::sync::{Mutex, MutexGuard};
4use crate::{
5 change::Lamport,
6 container::{
7 idx::ContainerIdx,
8 list::list_op::{InnerListOp, ListOp},
9 map::MapSet,
10 ContainerID,
11 },
12 id::Counter,
13 op::{InnerContent, ListSlice, Op, RawOp, RawOpContent, SliceRange},
14 LoroValue,
15};
16use append_only_bytes::BytesSlice;
17use fxhash::FxHashMap;
18use loro_common::PeerID;
19use std::fmt;
20use std::{
21 num::NonZeroU16,
22 ops::{Range, RangeBounds},
23 sync::Arc,
24};
25
26pub(crate) struct LoadAllFlag;
27type ParentResolver = dyn Fn(ContainerID) -> Option<ContainerID> + Send + Sync + 'static;
28
29#[derive(Default)]
30struct InnerSharedArena {
31 container_idx_to_id: Mutex<Vec<ContainerID>>,
34 depth: Mutex<Vec<Option<NonZeroU16>>>,
36 container_id_to_idx: Mutex<FxHashMap<ContainerID, ContainerIdx>>,
37 parents: Mutex<FxHashMap<ContainerIdx, Option<ContainerIdx>>>,
39 values: Mutex<Vec<LoroValue>>,
40 root_c_idx: Mutex<Vec<ContainerIdx>>,
41 str: Arc<Mutex<StrArena>>,
42 parent_resolver: Mutex<Option<Arc<ParentResolver>>>,
45}
46
47impl fmt::Debug for InnerSharedArena {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 f.debug_struct("InnerSharedArena")
50 .field("container_idx_to_id", &"<Mutex<_>>")
51 .field("depth", &"<Mutex<_>>")
52 .field("container_id_to_idx", &"<Mutex<_>>")
53 .field("parents", &"<Mutex<_>>")
54 .field("values", &"<Mutex<_>>")
55 .field("root_c_idx", &"<Mutex<_>>")
56 .field("str", &"<Arc<Mutex<_>>>")
57 .field("parent_resolver", &"<Mutex<Option<...>>>")
58 .finish()
59 }
60}
61
62#[derive(Debug, Clone)]
65pub struct SharedArena {
66 inner: Arc<InnerSharedArena>,
67}
68
69#[derive(Debug)]
70pub struct StrAllocResult {
71 pub start: usize,
73 pub end: usize,
75}
76
77pub(crate) struct ArenaGuards<'a> {
78 container_id_to_idx: MutexGuard<'a, FxHashMap<ContainerID, ContainerIdx>>,
79 container_idx_to_id: MutexGuard<'a, Vec<ContainerID>>,
80 depth: MutexGuard<'a, Vec<Option<NonZeroU16>>>,
81 parents: MutexGuard<'a, FxHashMap<ContainerIdx, Option<ContainerIdx>>>,
82 root_c_idx: MutexGuard<'a, Vec<ContainerIdx>>,
83 parent_resolver: MutexGuard<'a, Option<Arc<ParentResolver>>>,
84}
85
86impl ArenaGuards<'_> {
87 pub fn register_container(&mut self, id: &ContainerID) -> ContainerIdx {
88 if let Some(&idx) = self.container_id_to_idx.get(id) {
89 return idx;
90 }
91
92 let idx = self.container_idx_to_id.len();
93 self.container_idx_to_id.push(id.clone());
94 let idx = ContainerIdx::from_index_and_type(idx as u32, id.container_type());
95 self.container_id_to_idx.insert(id.clone(), idx);
96 if id.is_root() {
97 self.root_c_idx.push(idx);
98 self.parents.insert(idx, None);
99 self.depth.push(NonZeroU16::new(1));
100 } else {
101 self.depth.push(None);
102 }
103 idx
104 }
105
106 pub fn set_parent(&mut self, child: ContainerIdx, parent: Option<ContainerIdx>) {
107 self.parents.insert(child, parent);
108
109 match parent {
110 Some(p) => {
111 if let Some(d) = get_depth(
112 p,
113 &mut self.depth,
114 &self.parents,
115 &self.parent_resolver,
116 &self.container_idx_to_id,
117 &self.container_id_to_idx,
118 ) {
119 self.depth[child.to_index() as usize] = NonZeroU16::new(d.get() + 1);
120 } else {
121 self.depth[child.to_index() as usize] = None;
122 }
123 }
124 None => {
125 self.depth[child.to_index() as usize] = NonZeroU16::new(1);
126 }
127 }
128 }
129}
130
131impl SharedArena {
132 #[allow(clippy::new_without_default)]
133 pub fn new() -> Self {
134 Self {
135 inner: Arc::new(InnerSharedArena::default()),
136 }
137 }
138
139 pub fn fork(&self) -> Self {
140 Self {
141 inner: Arc::new(InnerSharedArena {
142 container_idx_to_id: Mutex::new(
143 self.inner.container_idx_to_id.lock().unwrap().clone(),
144 ),
145 depth: Mutex::new(self.inner.depth.lock().unwrap().clone()),
146 container_id_to_idx: Mutex::new(
147 self.inner.container_id_to_idx.lock().unwrap().clone(),
148 ),
149 parents: Mutex::new(self.inner.parents.lock().unwrap().clone()),
150 values: Mutex::new(self.inner.values.lock().unwrap().clone()),
151 root_c_idx: Mutex::new(self.inner.root_c_idx.lock().unwrap().clone()),
152 str: self.inner.str.clone(),
153 parent_resolver: Mutex::new(self.inner.parent_resolver.lock().unwrap().clone()),
154 }),
155 }
156 }
157
158 pub(crate) fn with_guards(&self, f: impl FnOnce(&mut ArenaGuards)) {
159 let mut guards = self.get_arena_guards();
160 f(&mut guards);
161 }
162
163 fn get_arena_guards(&self) -> ArenaGuards<'_> {
164 ArenaGuards {
165 container_id_to_idx: self.inner.container_id_to_idx.lock().unwrap(),
166 container_idx_to_id: self.inner.container_idx_to_id.lock().unwrap(),
167 depth: self.inner.depth.lock().unwrap(),
168 parents: self.inner.parents.lock().unwrap(),
169 root_c_idx: self.inner.root_c_idx.lock().unwrap(),
170 parent_resolver: self.inner.parent_resolver.lock().unwrap(),
171 }
172 }
173
174 pub fn register_container(&self, id: &ContainerID) -> ContainerIdx {
175 let mut container_id_to_idx = self.inner.container_id_to_idx.lock().unwrap();
176 if let Some(&idx) = container_id_to_idx.get(id) {
177 return idx;
178 }
179
180 let mut container_idx_to_id = self.inner.container_idx_to_id.lock().unwrap();
181 let idx = container_idx_to_id.len();
182 container_idx_to_id.push(id.clone());
183 let idx = ContainerIdx::from_index_and_type(idx as u32, id.container_type());
184 container_id_to_idx.insert(id.clone(), idx);
185 if id.is_root() {
186 self.inner.root_c_idx.lock().unwrap().push(idx);
187 self.inner.parents.lock().unwrap().insert(idx, None);
188 self.inner.depth.lock().unwrap().push(NonZeroU16::new(1));
189 } else {
190 self.inner.depth.lock().unwrap().push(None);
191 }
192 idx
193 }
194
195 pub fn get_container_id(&self, idx: ContainerIdx) -> Option<ContainerID> {
196 let lock = self.inner.container_idx_to_id.lock().unwrap();
197 lock.get(idx.to_index() as usize).cloned()
198 }
199
200 pub fn id_to_idx(&self, id: &ContainerID) -> Option<ContainerIdx> {
210 self.inner
211 .container_id_to_idx
212 .lock()
213 .unwrap()
214 .get(id)
215 .copied()
216 }
217
218 #[inline]
219 pub fn idx_to_id(&self, id: ContainerIdx) -> Option<ContainerID> {
220 let lock = self.inner.container_idx_to_id.lock().unwrap();
221 lock.get(id.to_index() as usize).cloned()
222 }
223
224 #[inline]
225 pub fn with_idx_to_id<R>(&self, f: impl FnOnce(&Vec<ContainerID>) -> R) -> R {
226 let lock = self.inner.container_idx_to_id.lock().unwrap();
227 f(&lock)
228 }
229
230 pub fn alloc_str(&self, str: &str) -> StrAllocResult {
231 let mut text_lock = self.inner.str.lock().unwrap();
232 _alloc_str(&mut text_lock, str)
233 }
234
235 pub fn alloc_str_with_slice(&self, str: &str) -> (BytesSlice, StrAllocResult) {
237 let mut text_lock = self.inner.str.lock().unwrap();
238 _alloc_str_with_slice(&mut text_lock, str)
239 }
240
241 pub fn alloc_str_fast(&self, bytes: &[u8]) {
243 let mut text_lock = self.inner.str.lock().unwrap();
244 text_lock.alloc(std::str::from_utf8(bytes).unwrap());
245 }
246
247 #[inline]
248 pub fn utf16_len(&self) -> usize {
249 self.inner.str.lock().unwrap().len_utf16()
250 }
251
252 #[inline]
253 pub fn alloc_value(&self, value: LoroValue) -> usize {
254 let mut values_lock = self.inner.values.lock().unwrap();
255 _alloc_value(&mut values_lock, value)
256 }
257
258 #[inline]
259 pub fn alloc_values(&self, values: impl Iterator<Item = LoroValue>) -> std::ops::Range<usize> {
260 let mut values_lock = self.inner.values.lock().unwrap();
261 _alloc_values(&mut values_lock, values)
262 }
263
264 #[inline]
265 pub fn set_parent(&self, child: ContainerIdx, parent: Option<ContainerIdx>) {
266 let parents = &mut self.inner.parents.lock().unwrap();
267 parents.insert(child, parent);
268 let mut depth = self.inner.depth.lock().unwrap();
269
270 match parent {
271 Some(p) => {
272 if let Some(d) = get_depth(
273 p,
274 &mut depth,
275 parents,
276 &self.inner.parent_resolver.lock().unwrap(),
277 &self.inner.container_idx_to_id.lock().unwrap(),
278 &self.inner.container_id_to_idx.lock().unwrap(),
279 ) {
280 depth[child.to_index() as usize] = NonZeroU16::new(d.get() + 1);
281 } else {
282 depth[child.to_index() as usize] = None;
283 }
284 }
285 None => {
286 depth[child.to_index() as usize] = NonZeroU16::new(1);
287 }
288 }
289 }
290
291 pub fn log_hierarchy(&self) {
292 if cfg!(debug_assertions) {
293 for (c, p) in self.inner.parents.lock().unwrap().iter() {
294 tracing::info!(
295 "container {:?} {:?} {:?}",
296 c,
297 self.get_container_id(*c),
298 p.map(|x| self.get_container_id(x))
299 );
300 }
301 }
302 }
303
304 pub fn log_all_containers(&self) {
305 self.inner
306 .container_id_to_idx
307 .lock()
308 .unwrap()
309 .iter()
310 .for_each(|(id, idx)| {
311 tracing::info!("container {:?} {:?}", id, idx);
312 });
313 self.inner
314 .container_idx_to_id
315 .lock()
316 .unwrap()
317 .iter()
318 .enumerate()
319 .for_each(|(i, id)| {
320 tracing::info!("container {} {:?}", i, id);
321 });
322 }
323
324 pub fn get_parent(&self, child: ContainerIdx) -> Option<ContainerIdx> {
325 if self.get_container_id(child).unwrap().is_root() {
326 return None;
329 }
330
331 if let Some(p) = self.inner.parents.lock().unwrap().get(&child).copied() {
333 return p;
334 }
335
336 let resolver = self.inner.parent_resolver.lock().unwrap().clone();
338 if let Some(resolver) = resolver {
339 let child_id = self.get_container_id(child).unwrap();
340 if let Some(parent_id) = resolver(child_id.clone()) {
341 let parent_idx = self.register_container(&parent_id);
342 self.set_parent(child, Some(parent_idx));
343 return Some(parent_idx);
344 }
345 }
346
347 panic!("InternalError: Parent is not registered")
348 }
349
350 pub fn with_ancestors(&self, container: ContainerIdx, mut f: impl FnMut(ContainerIdx, bool)) {
354 let mut container = Some(container);
355 let mut is_first = true;
356 while let Some(c) = container {
357 f(c, is_first);
358 is_first = false;
359 container = self.get_parent(c)
360 }
361 }
362
363 #[inline]
364 pub fn slice_by_unicode(&self, range: impl RangeBounds<usize>) -> BytesSlice {
365 self.inner.str.lock().unwrap().slice_by_unicode(range)
366 }
367
368 #[inline]
369 pub fn slice_by_utf8(&self, range: impl RangeBounds<usize>) -> BytesSlice {
370 self.inner.str.lock().unwrap().slice_bytes(range)
371 }
372
373 #[inline]
374 pub fn slice_str_by_unicode_range(&self, range: Range<usize>) -> String {
375 let mut s = self.inner.str.lock().unwrap();
376 let s: &mut StrArena = &mut s;
377 let mut ans = String::with_capacity(range.len());
378 ans.push_str(s.slice_str_by_unicode(range));
379 ans
380 }
381
382 #[inline]
383 pub fn with_text_slice(&self, range: Range<usize>, mut f: impl FnMut(&str)) {
384 f(self.inner.str.lock().unwrap().slice_str_by_unicode(range))
385 }
386
387 #[inline]
388 pub fn get_value(&self, idx: usize) -> Option<LoroValue> {
389 self.inner.values.lock().unwrap().get(idx).cloned()
390 }
391
392 #[inline]
393 pub fn get_values(&self, range: Range<usize>) -> Vec<LoroValue> {
394 (self.inner.values.lock().unwrap()[range]).to_vec()
395 }
396
397 pub fn convert_single_op(
398 &self,
399 container: &ContainerID,
400 peer: PeerID,
401 counter: Counter,
402 lamport: Lamport,
403 content: RawOpContent,
404 ) -> Op {
405 let container = self.register_container(container);
406 self.inner_convert_op(content, peer, counter, lamport, container)
407 }
408
409 pub fn can_import_snapshot(&self) -> bool {
410 self.inner.str.lock().unwrap().is_empty() && self.inner.values.lock().unwrap().is_empty()
411 }
412
413 fn inner_convert_op(
414 &self,
415 content: RawOpContent<'_>,
416 _peer: PeerID,
417 counter: i32,
418 _lamport: Lamport,
419 container: ContainerIdx,
420 ) -> Op {
421 match content {
422 crate::op::RawOpContent::Map(MapSet { key, value }) => Op {
423 counter,
424 container,
425 content: crate::op::InnerContent::Map(MapSet { key, value }),
426 },
427 crate::op::RawOpContent::List(list) => match list {
428 ListOp::Insert { slice, pos } => match slice {
429 ListSlice::RawData(values) => {
430 let range = self.alloc_values(values.iter().cloned());
431 Op {
432 counter,
433 container,
434 content: crate::op::InnerContent::List(InnerListOp::Insert {
435 slice: SliceRange::from(range.start as u32..range.end as u32),
436 pos,
437 }),
438 }
439 }
440 ListSlice::RawStr { str, unicode_len } => {
441 let (slice, info) = self.alloc_str_with_slice(&str);
442 Op {
443 counter,
444 container,
445 content: crate::op::InnerContent::List(InnerListOp::InsertText {
446 slice,
447 unicode_start: info.start as u32,
448 unicode_len: unicode_len as u32,
449 pos: pos as u32,
450 }),
451 }
452 }
453 },
454 ListOp::Delete(span) => Op {
455 counter,
456 container,
457 content: crate::op::InnerContent::List(InnerListOp::Delete(span)),
458 },
459 ListOp::StyleStart {
460 start,
461 end,
462 info,
463 key,
464 value,
465 } => Op {
466 counter,
467 container,
468 content: InnerContent::List(InnerListOp::StyleStart {
469 start,
470 end,
471 key,
472 info,
473 value,
474 }),
475 },
476 ListOp::StyleEnd => Op {
477 counter,
478 container,
479 content: InnerContent::List(InnerListOp::StyleEnd),
480 },
481 ListOp::Move {
482 from,
483 to,
484 elem_id: from_id,
485 } => Op {
486 counter,
487 container,
488 content: InnerContent::List(InnerListOp::Move {
489 from,
490 to,
491 elem_id: from_id,
492 }),
493 },
494 ListOp::Set { elem_id, value } => Op {
495 counter,
496 container,
497 content: InnerContent::List(InnerListOp::Set { elem_id, value }),
498 },
499 },
500 crate::op::RawOpContent::Tree(tree) => Op {
501 counter,
502 container,
503 content: crate::op::InnerContent::Tree(tree.clone()),
504 },
505 #[cfg(feature = "counter")]
506 crate::op::RawOpContent::Counter(c) => Op {
507 counter,
508 container,
509 content: crate::op::InnerContent::Future(crate::op::FutureInnerContent::Counter(c)),
510 },
511 crate::op::RawOpContent::Unknown { prop, value } => Op {
512 counter,
513 container,
514 content: crate::op::InnerContent::Future(crate::op::FutureInnerContent::Unknown {
515 prop,
516 value: Box::new(value),
517 }),
518 },
519 }
520 }
521
522 #[inline]
523 pub fn convert_raw_op(&self, op: &RawOp) -> Op {
524 self.inner_convert_op(
525 op.content.clone(),
526 op.id.peer,
527 op.id.counter,
528 op.lamport,
529 op.container,
530 )
531 }
532
533 #[inline]
534 pub fn export_containers(&self) -> Vec<ContainerID> {
535 self.inner.container_idx_to_id.lock().unwrap().clone()
536 }
537
538 pub fn export_parents(&self) -> Vec<Option<ContainerIdx>> {
539 let parents = self.inner.parents.lock().unwrap();
540 let containers = self.inner.container_idx_to_id.lock().unwrap();
541 containers
542 .iter()
543 .enumerate()
544 .map(|(x, id)| {
545 let idx = ContainerIdx::from_index_and_type(x as u32, id.container_type());
546 let parent_idx = parents.get(&idx)?;
547 *parent_idx
548 })
549 .collect()
550 }
551
552 #[inline]
557 pub(crate) fn root_containers(&self, _f: LoadAllFlag) -> Vec<ContainerIdx> {
558 self.inner.root_c_idx.lock().unwrap().clone()
559 }
560
561 pub(crate) fn get_depth(&self, container: ContainerIdx) -> Option<NonZeroU16> {
563 get_depth(
564 container,
565 &mut self.inner.depth.lock().unwrap(),
566 &self.inner.parents.lock().unwrap(),
567 &self.inner.parent_resolver.lock().unwrap(),
568 &self.inner.container_idx_to_id.lock().unwrap(),
569 &self.inner.container_id_to_idx.lock().unwrap(),
570 )
571 }
572
573 pub(crate) fn iter_value_slice(
574 &self,
575 range: Range<usize>,
576 ) -> impl Iterator<Item = LoroValue> + '_ {
577 let values = self.inner.values.lock().unwrap();
578 range
579 .into_iter()
580 .map(move |i| values.get(i).unwrap().clone())
581 }
582
583 pub(crate) fn get_root_container_idx_by_key(
584 &self,
585 root_index: &loro_common::InternalString,
586 ) -> Option<ContainerIdx> {
587 let inner = self.inner.container_id_to_idx.lock().unwrap();
588 for t in loro_common::ContainerType::ALL_TYPES.iter() {
589 let cid = ContainerID::Root {
590 name: root_index.clone(),
591 container_type: *t,
592 };
593 if let Some(idx) = inner.get(&cid) {
594 return Some(*idx);
595 }
596 }
597 None
598 }
599
600 #[allow(unused)]
601 pub(crate) fn log_all_values(&self) {
602 let values = self.inner.values.lock().unwrap();
603 for (i, v) in values.iter().enumerate() {
604 loro_common::debug!("value {} {:?}", i, v);
605 }
606 }
607}
608
609fn _alloc_str_with_slice(
610 text_lock: &mut MutexGuard<'_, StrArena>,
611 str: &str,
612) -> (BytesSlice, StrAllocResult) {
613 let start = text_lock.len_bytes();
614 let ans = _alloc_str(text_lock, str);
615 (text_lock.slice_bytes(start..), ans)
616}
617
618fn _alloc_values(
619 values_lock: &mut MutexGuard<'_, Vec<LoroValue>>,
620 values: impl Iterator<Item = LoroValue>,
621) -> Range<usize> {
622 values_lock.reserve(values.size_hint().0);
623 let start = values_lock.len();
624 for value in values {
625 values_lock.push(value);
626 }
627
628 start..values_lock.len()
629}
630
631fn _alloc_value(values_lock: &mut MutexGuard<'_, Vec<LoroValue>>, value: LoroValue) -> usize {
632 values_lock.push(value);
633 values_lock.len() - 1
634}
635
636fn _alloc_str(text_lock: &mut MutexGuard<'_, StrArena>, str: &str) -> StrAllocResult {
637 let start = text_lock.len_unicode();
638 text_lock.alloc(str);
639 StrAllocResult {
640 start,
641 end: text_lock.len_unicode(),
642 }
643}
644
645fn _slice_str(range: Range<usize>, s: &mut StrArena) -> String {
646 let mut ans = String::with_capacity(range.len());
647 ans.push_str(s.slice_str_by_unicode(range));
648 ans
649}
650
651fn get_depth(
652 target: ContainerIdx,
653 depth: &mut Vec<Option<NonZeroU16>>,
654 parents: &FxHashMap<ContainerIdx, Option<ContainerIdx>>,
655 parent_resolver: &Option<Arc<ParentResolver>>,
656 idx_to_id: &Vec<ContainerID>,
657 id_to_idx: &FxHashMap<ContainerID, ContainerIdx>,
658) -> Option<NonZeroU16> {
659 let mut d = depth[target.to_index() as usize];
660 if d.is_some() {
661 return d;
662 }
663
664 let parent: Option<ContainerIdx> = if let Some(p) = parents.get(&target) {
665 *p
666 } else {
667 let id = idx_to_id.get(target.to_index() as usize).unwrap();
668 if id.is_root() {
669 None
670 } else if let Some(parent_resolver) = parent_resolver.as_ref() {
671 let parent_id = parent_resolver(id.clone())?;
672 let parent_idx = id_to_idx.get(&parent_id).unwrap();
673 Some(*parent_idx)
674 } else {
675 return None;
676 }
677 };
678
679 match parent {
680 Some(p) => {
681 d = NonZeroU16::new(
682 get_depth(p, depth, parents, parent_resolver, idx_to_id, id_to_idx)?.get() + 1,
683 );
684 depth[target.to_index() as usize] = d;
685 }
686 None => {
687 depth[target.to_index() as usize] = NonZeroU16::new(1);
688 d = NonZeroU16::new(1);
689 }
690 }
691
692 d
693}
694
695impl SharedArena {
696 pub fn set_parent_resolver<F>(&self, resolver: Option<F>)
702 where
703 F: Fn(ContainerID) -> Option<ContainerID> + Send + Sync + 'static,
704 {
705 let mut slot = self.inner.parent_resolver.lock().unwrap();
706 *slot = resolver.map(|f| Arc::new(f) as Arc<ParentResolver>);
707 }
708}