refactor: better interface

This commit is contained in:
Zixuan Chen 2023-08-05 12:44:31 +08:00
parent a661b776c4
commit ccee201641
3 changed files with 53 additions and 17 deletions

View file

@ -50,6 +50,22 @@ impl<O> Change<O> {
timestamp,
}
}
pub fn ops(&self) -> &RleVec<[O; 1]> {
&self.ops
}
pub fn lamport(&self) -> Lamport {
self.lamport
}
pub fn timestamp(&self) -> Timestamp {
self.timestamp
}
pub fn id(&self) -> ID {
self.id
}
}
impl<O: Mergable + HasLength + HasIndex + Debug> HasIndex for Change<O> {
@ -119,18 +135,6 @@ impl DagNode for Change {
}
impl Change {
pub fn lamport(&self) -> Lamport {
self.lamport
}
pub fn timestamp(&self) -> Timestamp {
self.timestamp
}
pub fn id(&self) -> ID {
self.id
}
pub fn can_merge_right(&self, other: &Self) -> bool {
other.id.peer == self.id.peer
&& other.id.counter == self.id.counter + self.content_len() as Counter

View file

@ -21,19 +21,37 @@ pub struct TextHandler {
state: Weak<Mutex<DocState>>,
}
impl std::fmt::Debug for TextHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("TextHandler")
}
}
#[derive(Clone)]
pub struct MapHandler {
container_idx: ContainerIdx,
state: Weak<Mutex<DocState>>,
}
impl std::fmt::Debug for MapHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("MapHandler")
}
}
#[derive(Clone)]
pub struct ListHandler {
container_idx: ContainerIdx,
state: Weak<Mutex<DocState>>,
}
#[derive(Clone, EnumAsInner)]
impl std::fmt::Debug for ListHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("ListHandler")
}
}
#[derive(Clone, EnumAsInner, Debug)]
pub enum Handler {
Text(TextHandler),
Map(MapHandler),
@ -322,6 +340,11 @@ impl ListHandler {
)
}
pub fn push(&self, txn: &mut Transaction, v: LoroValue) -> LoroResult<()> {
let pos = self.len();
self.insert(txn, pos, v)
}
pub fn insert_container(
&self,
txn: &mut Transaction,
@ -435,9 +458,9 @@ impl ListHandler {
})
}
pub fn for_each<I>(&self, f: I)
pub fn for_each<I>(&self, mut f: I)
where
I: Fn(&LoroValue),
I: FnMut(&LoroValue),
{
self.state
.upgrade()
@ -468,6 +491,11 @@ impl MapHandler {
return Ok(());
}
if self.get(key).map(|x| x == value).unwrap_or(false) {
// skip if the value is already set
return Ok(());
}
txn.apply_local_op(
self.container_idx,
crate::op::RawOpContent::Map(crate::container::map::MapSet {
@ -514,9 +542,9 @@ impl MapHandler {
)
}
pub fn for_each<I>(&self, f: I)
pub fn for_each<I>(&self, mut f: I)
where
I: Fn(&str, &MapValue),
I: FnMut(&str, &MapValue),
{
self.state
.upgrade()

View file

@ -228,6 +228,10 @@ impl OpLog {
ID::new(peer, cnt)
}
pub fn get_peer_changes(&self, peer: PeerID) -> Option<&RleVec<[Change; 0]>> {
self.changes.get(&peer)
}
pub(crate) fn vv(&self) -> &VersionVector {
&self.dag.vv
}