Skip to content

Commit ca88e16

Browse files
committed
chore: cleanup old codes
1 parent 117b8f2 commit ca88e16

File tree

6 files changed

+54
-182
lines changed

6 files changed

+54
-182
lines changed

Cargo.lock

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[workspace]
22

33
members = [
4-
"apps/doc_merger",
54
"apps/keck",
65
"libs/jwst-binding/jwst-jni",
76
"libs/jwst-binding/jwst-swift",

apps/doc_merger/Cargo.toml

Lines changed: 0 additions & 19 deletions
This file was deleted.

apps/doc_merger/src/main.rs

Lines changed: 0 additions & 139 deletions
This file was deleted.

libs/jwst-codec-utils/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ y-sync = "=0.3.1"
2525
yrs = "=0.16.5"
2626

2727
# ======= workspace dependencies =======
28+
jwst-codec = { workspace = true, features = ["debug", "large_refs"] }
2829
rand = { workspace = true }
29-
30-
jwst-codec = { workspace = true }
30+
serde_json = { workspace = true }
3131

3232
[dev-dependencies]
3333
criterion = { version = "0.5", features = ["html_reports"] }

libs/jwst-codec-utils/bin/doc_merger.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::{
2-
fs::read,
2+
fs::{read, write},
33
io::{Error, ErrorKind},
44
path::PathBuf,
55
time::Instant,
66
};
77

88
use clap::Parser;
99
use jwst_codec::Doc;
10+
use yrs::{types::ToJson, updates::decoder::Decode, ReadTxn, StateVector, Transact, Update};
1011

1112
/// ybinary merger
1213
#[derive(Parser, Debug)]
@@ -15,6 +16,10 @@ struct Args {
1516
/// Path of the ybinary to read
1617
#[arg(short, long)]
1718
path: String,
19+
20+
/// Output file
21+
#[arg(short, long)]
22+
output: Option<String>,
1823
}
1924

2025
fn load_path(path: &str) -> Result<Vec<Vec<u8>>, Error> {
@@ -48,16 +53,25 @@ fn load_path(path: &str) -> Result<Vec<Vec<u8>>, Error> {
4853

4954
fn main() {
5055
let args = Args::parse();
51-
jwst_merge(&args.path);
56+
jwst_merge(
57+
&args.path,
58+
&args.output.clone().unwrap_or_else(|| format!("{}.jwst", args.path)),
59+
);
60+
// std::io::stdin().read_line(&mut String::new()).unwrap();
61+
yrs_merge(
62+
&args.path,
63+
&args.output.clone().unwrap_or_else(|| format!("{}.yrs", args.path)),
64+
);
5265
}
5366

54-
fn jwst_merge(path: &str) {
67+
fn jwst_merge(path: &str, output: &str) {
5568
let updates = load_path(path).unwrap();
5669

5770
let mut doc = Doc::default();
5871
for (i, update) in updates.iter().enumerate() {
5972
println!("apply update{i} {} bytes", update.len());
6073
doc.apply_update_from_binary_v1(update.clone()).unwrap();
74+
println!("status: {:?}", doc.store_status());
6175
}
6276

6377
println!("press enter to continue");
@@ -71,21 +85,48 @@ fn jwst_merge(path: &str) {
7185

7286
doc.gc().unwrap();
7387

74-
let binary = {
88+
let (binary, json) = {
89+
let json = serde_json::to_string_pretty(&doc.get_map("space:blocks").unwrap()).unwrap();
7590
let binary = doc.encode_update_v1().unwrap();
7691

77-
println!("merged {} bytes", binary.len());
92+
println!("merged {} bytes, json {} bytes", binary.len(), json.len());
7893

79-
binary
94+
(binary, doc.get_map("space:blocks").unwrap())
8095
};
8196

8297
{
8398
let mut doc = Doc::default();
8499
doc.apply_update_from_binary_v1(binary.clone()).unwrap();
85100
let new_binary = doc.encode_update_v1().unwrap();
101+
let new_json = serde_json::to_string_pretty(&doc.get_map("space:blocks").unwrap()).unwrap();
102+
assert_json_diff::assert_json_eq!(doc.get_map("space:blocks").unwrap(), json);
103+
104+
println!(
105+
"re-encoded {} bytes, new json {} bytes",
106+
new_binary.len(),
107+
new_json.len()
108+
);
109+
}
110+
write(output, binary).unwrap();
111+
}
86112

87-
println!("re-encoded {} bytes", new_binary.len(),);
88-
};
113+
fn yrs_merge(path: &str, output: &str) {
114+
let updates = load_path(path).unwrap();
115+
116+
let doc = yrs::Doc::new();
117+
for (i, update) in updates.iter().enumerate() {
118+
println!("apply update{i} {} bytes", update.len());
119+
doc.transact_mut().apply_update(Update::decode_v1(update).unwrap())
120+
}
121+
let binary = doc
122+
.transact()
123+
.encode_state_as_update_v1(&StateVector::default())
124+
.unwrap();
125+
let map = doc.get_or_insert_map("space:blocks");
126+
let json = serde_json::to_string_pretty(&map.to_json(&doc.transact())).unwrap();
127+
128+
println!("merged {} bytes, json {} bytes", binary.len(), json.len());
129+
write(output, binary).unwrap();
89130
}
90131

91132
#[cfg(test)]
@@ -95,6 +136,7 @@ mod tests {
95136
#[test]
96137
#[ignore = "only for debug"]
97138
fn test_gc() {
98-
jwst_merge("/Users/ds/Downloads/out");
139+
jwst_merge("/Users/ds/Downloads/out2", "/Users/ds/Downloads/out.jwst");
140+
yrs_merge("/Users/ds/Downloads/out2", "/Users/ds/Downloads/out.yrs");
99141
}
100142
}

0 commit comments

Comments
 (0)