Skip to content

Commit

Permalink
Feat: diff calc bring back & tree new event and value (#149)
Browse files Browse the repository at this point in the history
* feat: new tree state

* fix: emit meta event

* fix: semantic tree event

* fix: diff calc bring_back

* chore: clear comments

* fix: merge

* fix: tree snapshot

* fix: filter empty bring back

* feat: tree add external diff

* fix: imported changes were not mergeable (#147)

* fix: imported changes were not mergeable
now the small encoding size is supported in example

* fix: stupid err in richtext checkout

* fix: rle oplog encode err
- support pending changes
- start counters were wrong

* fix: utf16 query err (#151)

* fix: tree movable node lamport

* fix: merge

* perf: bring back

* doc: add deep value meta doc

* refactor: bring back only when record diff

---------

Co-authored-by: Zixuan Chen <[email protected]>
  • Loading branch information
Leeeon233 and zxch3n authored Nov 5, 2023
1 parent 8bd953e commit acafc76
Show file tree
Hide file tree
Showing 25 changed files with 1,874 additions and 781 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/<executable file>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"tinyvec",
"txns",
"unbold",
"unexist",
"unmark",
"yspan"
],
Expand Down
12 changes: 5 additions & 7 deletions crates/loro-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{fmt::Display, sync::Arc};
use arbitrary::Arbitrary;
use enum_as_inner::EnumAsInner;

use fxhash::FxHashMap;
use serde::{Deserialize, Serialize};
mod error;
mod id;
Expand Down Expand Up @@ -95,12 +94,7 @@ impl ContainerType {
ContainerType::Map => LoroValue::Map(Arc::new(Default::default())),
ContainerType::List => LoroValue::List(Arc::new(Default::default())),
ContainerType::Text => LoroValue::String(Arc::new(Default::default())),
ContainerType::Tree => {
let mut map: FxHashMap<String, LoroValue> = FxHashMap::default();
map.insert("roots".to_string(), LoroValue::List(vec![].into()));
map.insert("deleted".to_string(), LoroValue::List(vec![].into()));
map.into()
}
ContainerType::Tree => LoroValue::List(Arc::new(Default::default())),
}
}

Expand Down Expand Up @@ -334,6 +328,10 @@ impl TreeID {
counter: self.counter,
}
}

pub fn associated_meta_container(&self) -> ContainerID {
ContainerID::new_normal(self.id(), ContainerType::Map)
}
}

impl Display for TreeID {
Expand Down
36 changes: 34 additions & 2 deletions crates/loro-internal/examples/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,33 @@ use std::time::Instant;
use loro_internal::LoroDoc;
use rand::{rngs::StdRng, Rng};

fn main() {
let s = Instant::now();
fn checkout() {
let depth = 300;
let mut loro = LoroDoc::default();
let tree = loro.get_tree("tree");
let mut ids = vec![];
let mut versions = vec![];
let id1 = loro.with_txn(|txn| tree.create(txn)).unwrap();
ids.push(id1);
versions.push(loro.oplog_frontiers());
for _ in 1..depth {
let id = loro
.with_txn(|txn| tree.create_and_mov(txn, *ids.last().unwrap()))
.unwrap();
ids.push(id);
versions.push(loro.oplog_frontiers());
}
let mut rng: StdRng = rand::SeedableRng::seed_from_u64(0);

for _ in 0..1000 {
let i = rng.gen::<usize>() % depth;
let f = &versions[i];
loro.checkout(f).unwrap();
}
}

#[allow(unused)]
fn mov() {
let loro = LoroDoc::default();
let tree = loro.get_tree("tree");
let mut ids = vec![];
Expand All @@ -22,5 +47,12 @@ fn main() {
tree.mov(&mut txn, ids[i], ids[j]).unwrap_or_default();
}
drop(txn);
}
fn main() {
let s = Instant::now();
for _ in 0..30 {
checkout();
}

println!("{} ms", s.elapsed().as_millis());
}
3 changes: 1 addition & 2 deletions crates/loro-internal/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{

use append_only_bytes::BytesSlice;
use fxhash::FxHashMap;
use loro_common::ContainerType;
use loro_common::PeerID;

use crate::{
Expand Down Expand Up @@ -165,7 +164,7 @@ impl<'a> OpConverter<'a> {
crate::op::RawOpContent::Tree(tree) => {
// we need create every meta container associated with target TreeID
let id = tree.target;
let meta_container_id = ContainerID::new_normal(id.id(), ContainerType::Map);
let meta_container_id = id.associated_meta_container();

if self.container_id_to_idx.get(&meta_container_id).is_none() {
let container_idx_to_id = &mut self.container_idx_to_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ impl CrdtRope {
while let Some((index, elem)) = iter.next() {
// The elements will not be changed by this method.
// This index is current index of the elem (calculated by `status` field rather than `diff_status` field)
debug_log::debug_dbg!(&index, &elem);
match elem.diff() {
DiffStatus::NotChanged => {}
DiffStatus::Created => {
Expand Down
5 changes: 4 additions & 1 deletion crates/loro-internal/src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ pub use map_delta::{MapDelta, MapValue};
mod text;
pub use text::{StyleMeta, StyleMetaItem};
mod tree;
pub use tree::{TreeDelta, TreeDiff, TreeDiffItem};
pub(crate) use tree::TreeValue;
pub use tree::{
TreeDelta, TreeDeltaItem, TreeDiff, TreeDiffItem, TreeExternalDiff, TreeInternalDiff,
};
2 changes: 1 addition & 1 deletion crates/loro-internal/src/delta/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ impl<Value: DeltaValue, M: Meta> Delta<Value, M> {
delta.chop()
}

fn concat(mut self, mut other: Self) -> Self {
pub(crate) fn concat(mut self, mut other: Self) -> Self {
if !other.vec.is_empty() {
let other_first = other.vec.remove(0);
self.push(other_first);
Expand Down
Loading

0 comments on commit acafc76

Please sign in to comment.