Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/treecrdt-core/benches/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::fs;
use std::path::PathBuf;
use std::time::Instant;

use treecrdt_core::{Lamport, LamportClock, MemoryStorage, NodeId, ReplicaId, TreeCrdt};
use treecrdt_core::{
Lamport, LamportClock, LocalPlacement, MemoryStorage, NodeId, ReplicaId, TreeCrdt,
};

const BENCH_CONFIG: &[(u64, u64)] = &[(100, 10), (1_000, 10), (10_000, 10)];

Expand Down Expand Up @@ -53,15 +55,15 @@ fn run_benchmark(replica: &ReplicaId, count: u64) -> f64 {
let mut tree = TreeCrdt::new(replica.clone(), storage, LamportClock::default()).unwrap();

let start = Instant::now();
let mut last: Option<NodeId> = None;
let mut last_placement = LocalPlacement::First;
for i in 0..count {
let node = hex_id(i + 1);
let _ = tree.local_insert_after(NodeId::ROOT, node, last).unwrap();
last = Some(node);
let _ = tree.local_insert(NodeId::ROOT, node, last_placement, None).unwrap();
last_placement = LocalPlacement::After(node);
}
for i in 0..count {
let node = hex_id(i + 1);
let _ = tree.local_move_after(node, NodeId::ROOT, None).unwrap();
let _ = tree.local_move(node, NodeId::ROOT, LocalPlacement::First).unwrap();
}
let _ = tree.operations_since(0 as Lamport).unwrap();
start.elapsed().as_secs_f64() * 1000.0
Expand Down
68 changes: 68 additions & 0 deletions packages/treecrdt-core/src/affected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::ids::NodeId;
use crate::ops::OperationKind;

pub(crate) fn affected_parents(
snapshot_parent: Option<NodeId>,
kind: &OperationKind,
) -> Vec<NodeId> {
let mut parents = Vec::new();
if let Some(p) = snapshot_parent {
parents.push(p);
}
match kind {
OperationKind::Insert { parent, .. } => parents.push(*parent),
OperationKind::Move { new_parent, .. } => parents.push(*new_parent),
OperationKind::Delete { .. }
| OperationKind::Tombstone { .. }
| OperationKind::Payload { .. } => {}
}
parents.sort();
parents.dedup();
parents
}

pub(crate) fn sorted_node_ids(nodes: impl IntoIterator<Item = NodeId>) -> Vec<NodeId> {
let mut ids: Vec<NodeId> = nodes.into_iter().collect();
ids.sort();
ids.dedup();
ids
}

pub(crate) fn parent_hints_from(parent: Option<NodeId>) -> Vec<NodeId> {
parent.into_iter().collect()
}

fn push_if_live(nodes: &mut Vec<NodeId>, id: NodeId) {
if id != NodeId::TRASH {
nodes.push(id);
}
}

fn push_snapshot_parent(nodes: &mut Vec<NodeId>, snapshot_parent: Option<NodeId>) {
if let Some(p) = snapshot_parent {
push_if_live(nodes, p);
}
}

pub(crate) fn direct_affected_nodes(
snapshot_parent: Option<NodeId>,
kind: &OperationKind,
) -> Vec<NodeId> {
let mut nodes = Vec::new();
push_if_live(&mut nodes, kind.node());
match kind {
OperationKind::Insert { parent, .. } => {
push_snapshot_parent(&mut nodes, snapshot_parent);
push_if_live(&mut nodes, *parent);
}
OperationKind::Move { new_parent, .. } => {
push_snapshot_parent(&mut nodes, snapshot_parent);
push_if_live(&mut nodes, *new_parent);
}
OperationKind::Delete { .. } | OperationKind::Tombstone { .. } => {
push_snapshot_parent(&mut nodes, snapshot_parent);
}
OperationKind::Payload { .. } => {}
}
sorted_node_ids(nodes)
}
8 changes: 5 additions & 3 deletions packages/treecrdt-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
//! This crate stays independent of concrete storage engines so it can be embedded in SQLite,
//! WASM, or any host that can satisfy the traits defined here.

pub(crate) mod affected;
pub mod error;
pub mod ids;
pub mod materialization;
pub mod ops;
pub mod order_key;
pub mod traits;
pub mod tree;
pub mod types;
mod validation;
pub mod version_vector;

pub use error::{Error, Result};
Expand All @@ -30,7 +33,6 @@ pub use traits::{
MemoryPayloadStore, MemoryStorage, NodeStore, NoopParentOpIndex, NoopStorage, ParentOpIndex,
PayloadStore, Storage, TruncatingParentOpIndex,
};
pub use tree::{
ApplyDelta, LocalFinalizePlan, LocalPlacement, NodeExport, NodeSnapshotExport, TreeCrdt,
};
pub use tree::TreeCrdt;
pub use types::{ApplyDelta, LocalFinalizePlan, LocalPlacement, NodeExport, NodeSnapshotExport};
pub use version_vector::VersionVector;
Loading
Loading