From 06cd6b577dd7716f743ba112c0655ad7087cabfb Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Wed, 17 Jul 2024 12:32:03 +0800 Subject: [PATCH] refactor: rename "OpGroup" to "ContainerHistoryCache" --- crates/loro-internal/src/{group.rs => history_cache.rs} | 9 +++++++-- crates/loro-internal/src/lib.rs | 2 +- crates/loro-internal/src/oplog.rs | 6 +++--- 3 files changed, 11 insertions(+), 6 deletions(-) rename crates/loro-internal/src/{group.rs => history_cache.rs} (97%) diff --git a/crates/loro-internal/src/group.rs b/crates/loro-internal/src/history_cache.rs similarity index 97% rename from crates/loro-internal/src/group.rs rename to crates/loro-internal/src/history_cache.rs index 2578da4d..bbe85fd8 100644 --- a/crates/loro-internal/src/group.rs +++ b/crates/loro-internal/src/history_cache.rs @@ -19,13 +19,18 @@ use crate::{ VersionVector, }; +/// A cache for the history of a container. +/// +/// There are cases where a container needs a view of the history of the container. +/// For example, when switching to an older version, a map container may need to know the value of a key at a certain version. +/// This cache provides a faster way to do that than scanning the oplog. #[derive(Debug)] -pub(crate) struct OpGroups { +pub(crate) struct ContainerHistoryCache { arena: SharedArena, groups: FxHashMap, } -impl OpGroups { +impl ContainerHistoryCache { pub(crate) fn fork(&self, arena: SharedArena) -> Self { let mut groups = FxHashMap::with_capacity_and_hasher(self.groups.len(), Default::default()); for (container_idx, group) in self.groups.iter() { diff --git a/crates/loro-internal/src/lib.rs b/crates/loro-internal/src/lib.rs index 1a8ef965..90a42999 100644 --- a/crates/loro-internal/src/lib.rs +++ b/crates/loro-internal/src/lib.rs @@ -54,7 +54,7 @@ pub mod event; pub use error::{LoroError, LoroResult}; pub mod estimated_size; -pub(crate) mod group; +pub(crate) mod history_cache; pub(crate) mod macros; pub(crate) mod state; pub mod undo; diff --git a/crates/loro-internal/src/oplog.rs b/crates/loro-internal/src/oplog.rs index 2a7954d1..7ab3abd4 100644 --- a/crates/loro-internal/src/oplog.rs +++ b/crates/loro-internal/src/oplog.rs @@ -16,7 +16,7 @@ use crate::container::list::list_op; use crate::dag::{Dag, DagUtils}; use crate::encoding::ParsedHeaderAndBody; use crate::encoding::{decode_oplog, encode_oplog, EncodeMode}; -use crate::group::OpGroups; +use crate::history_cache::ContainerHistoryCache; use crate::id::{Counter, PeerID, ID}; use crate::op::{FutureInnerContent, ListSlice, RawOpContent, RemoteOp, RichOp}; use crate::span::{HasCounterSpan, HasIdSpan, HasLamportSpan}; @@ -45,7 +45,7 @@ pub struct OpLog { pub(crate) dag: AppDag, pub(crate) arena: SharedArena, change_store: ChangeStore, - pub(crate) op_groups: OpGroups, + pub(crate) op_groups: ContainerHistoryCache, /// **lamport starts from 0** pub(crate) next_lamport: Lamport, pub(crate) latest_timestamp: Timestamp, @@ -187,7 +187,7 @@ impl OpLog { Self { change_store: ChangeStore::new_mem(&arena, cfg.merge_interval.clone()), dag: AppDag::default(), - op_groups: OpGroups::new(arena.clone()), + op_groups: ContainerHistoryCache::new(arena.clone()), arena, next_lamport: 0, latest_timestamp: Timestamp::default(),