fix: tree fuzz sort value (#351)

This commit is contained in:
Leon Zhao 2024-05-07 19:39:38 +08:00 committed by GitHub
parent fffd49b5fa
commit 51a673822e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 47 additions and 33 deletions

12
Cargo.lock generated
View file

@ -665,6 +665,7 @@ dependencies = [
"enum-as-inner 0.5.1",
"enum_dispatch",
"fxhash",
"itertools 0.12.1",
"loro",
"loro-internal",
"rand",
@ -913,6 +914,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.5"
@ -1044,7 +1054,7 @@ dependencies = [
"generic-btree",
"getrandom",
"im",
"itertools 0.11.0",
"itertools 0.12.1",
"js-sys",
"leb128",
"loro-common",

View file

@ -24,3 +24,4 @@ tracing = { version = "0.1", features = [
"release_max_level_warn",
] }
serde_columnar = { version = "0.3.4" }
itertools = "0.12.1"

View file

@ -9,13 +9,14 @@ publish = false
[dependencies]
loro = { path = "../loro" }
loro-internal = { path = "../loro-internal", features = ["test_utils"] }
arbitrary = "1"
tabled = "0.10"
fxhash = { workspace = true }
enum_dispatch = { workspace = true }
enum-as-inner = { workspace = true }
rand = "0.8.5"
tracing = { workspace = true }
itertools = { workspace = true }
arbitrary = "1"
tabled = "0.10"
rand = "0.8.5"
[dev-dependencies]
ctor = "0.2"

View file

@ -224,6 +224,7 @@ dependencies = [
"enum-as-inner 0.5.1",
"enum_dispatch",
"fxhash",
"itertools 0.12.1",
"loro",
"loro-internal",
"rand",
@ -257,7 +258,7 @@ dependencies = [
"arref",
"fxhash",
"heapless 0.7.17",
"itertools",
"itertools 0.11.0",
"loro-thunderdome",
"proc-macro2",
]
@ -372,6 +373,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.10"
@ -422,7 +432,7 @@ dependencies = [
[[package]]
name = "loro"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"either",
"enum-as-inner 0.6.0",
@ -434,7 +444,7 @@ dependencies = [
[[package]]
name = "loro-common"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"arbitrary",
"enum-as-inner 0.6.0",
@ -449,7 +459,7 @@ dependencies = [
[[package]]
name = "loro-delta"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"arrayvec",
"enum-as-inner 0.5.1",
@ -460,7 +470,7 @@ dependencies = [
[[package]]
name = "loro-internal"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"append-only-bytes",
"arbitrary",
@ -473,7 +483,7 @@ dependencies = [
"generic-btree",
"getrandom",
"im",
"itertools",
"itertools 0.12.1",
"leb128",
"loro-common",
"loro-delta",
@ -496,7 +506,7 @@ dependencies = [
[[package]]
name = "loro-rle"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"append-only-bytes",
"arref",
@ -827,7 +837,7 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5d54dd7e7a1ec134c842f8a3bdb5a1fc662d002682e0457f976f3046cf9ccf8"
dependencies = [
"itertools",
"itertools 0.11.0",
"postcard",
"serde",
"serde_columnar_derive",

View file

@ -6,6 +6,7 @@ use std::{
};
use fxhash::FxHashMap;
use itertools::Itertools;
use loro::{
event::Diff, Container, ContainerID, ContainerType, LoroDoc, LoroError, LoroTree, LoroValue,
TreeExternalDiff, TreeID,
@ -447,23 +448,17 @@ impl ApplyDiff for TreeTracker {
fn to_value(&self) -> LoroValue {
let mut list: Vec<FxHashMap<_, _>> = Vec::new();
for (i, node) in self.iter().enumerate() {
node.to_value(i, &mut list);
let mut q = VecDeque::from_iter(
self.iter()
.sorted_unstable_by_key(|x| &x.position)
.enumerate(),
);
while let Some((i, node)) = q.pop_front() {
list.push(node.to_value(i));
q.extend(node.children.iter().enumerate());
}
list.sort_by_key(|x| {
let parent = if let LoroValue::String(p) = x.get("parent").unwrap() {
Some(p.clone())
} else {
None
};
(
parent,
*x.get("index").unwrap().as_i64().unwrap(),
x.get("id").unwrap().as_string().unwrap().clone(),
)
});
list.into()
}
}
@ -500,10 +495,7 @@ impl TreeNode {
}
}
fn to_value(&self, index: usize, list: &mut Vec<FxHashMap<String, LoroValue>>) {
for (i, child) in self.children.iter().enumerate() {
child.to_value(i, list);
}
fn to_value(&self, index: usize) -> FxHashMap<String, LoroValue> {
let mut map = FxHashMap::default();
map.insert("id".to_string(), self.id.to_string().into());
map.insert("meta".to_string(), self.meta.to_value());
@ -516,6 +508,6 @@ impl TreeNode {
);
map.insert("position".to_string(), self.position.clone().into());
map.insert("index".to_string(), (index as i64).into());
list.push(map);
map
}
}

View file

@ -35,7 +35,7 @@ serde_json = { version = "1" }
arref = "0.1.0"
serde_columnar = { workspace = true }
append-only-bytes = { version = "0.1.12", features = ["u32_range"] }
itertools = "0.11.0"
itertools = { workspace = true }
enum_dispatch = { workspace = true }
im = "15.1.0"
generic-btree = { version = "^0.10.5" }