Skip to content

Commit

Permalink
chore: cleanup old codes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Dec 28, 2023
1 parent d5d7287 commit 6f66dc2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 169 deletions.
19 changes: 0 additions & 19 deletions apps/doc_merger/Cargo.toml

This file was deleted.

139 changes: 0 additions & 139 deletions apps/doc_merger/src/main.rs

This file was deleted.

4 changes: 2 additions & 2 deletions libs/jwst-codec-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ y-sync = "=0.3.1"
yrs = "=0.16.5"

# ======= workspace dependencies =======
jwst-codec = { workspace = true, features = ["debug", "large_refs"] }
rand = { workspace = true }

jwst-codec = { workspace = true }
serde_json = { workspace = true }

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
60 changes: 51 additions & 9 deletions libs/jwst-codec-utils/bin/doc_merger.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{
fs::read,
fs::{read, write},
io::{Error, ErrorKind},
path::PathBuf,
time::Instant,
};

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

/// ybinary merger
#[derive(Parser, Debug)]
Expand All @@ -15,6 +16,10 @@ struct Args {
/// Path of the ybinary to read
#[arg(short, long)]
path: String,

/// Output file
#[arg(short, long)]
output: Option<String>,
}

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

fn main() {
let args = Args::parse();
jwst_merge(&args.path);
jwst_merge(
&args.path,
&args.output.clone().unwrap_or_else(|| format!("{}.jwst", args.path)),
);
// std::io::stdin().read_line(&mut String::new()).unwrap();
yrs_merge(
&args.path,
&args.output.clone().unwrap_or_else(|| format!("{}.yrs", args.path)),
);
}

fn jwst_merge(path: &str) {
fn jwst_merge(path: &str, output: &str) {
let updates = load_path(path).unwrap();

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

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

doc.gc().unwrap();

let binary = {
let (binary, json) = {
let json = serde_json::to_string_pretty(&doc.get_map("space:blocks").unwrap()).unwrap();
let binary = doc.encode_update_v1().unwrap();

println!("merged {} bytes", binary.len());
println!("merged {} bytes, json {} bytes", binary.len(), json.len());

binary
(binary, doc.get_map("space:blocks").unwrap())
};

{
let mut doc = Doc::default();
doc.apply_update_from_binary_v1(binary.clone()).unwrap();
let new_binary = doc.encode_update_v1().unwrap();
let new_json = serde_json::to_string_pretty(&doc.get_map("space:blocks").unwrap()).unwrap();
assert_json_diff::assert_json_eq!(doc.get_map("space:blocks").unwrap(), json);

println!(
"re-encoded {} bytes, new json {} bytes",
new_binary.len(),
new_json.len()
);
}
write(output, binary).unwrap();
}

println!("re-encoded {} bytes", new_binary.len(),);
};
fn yrs_merge(path: &str, output: &str) {
let updates = load_path(path).unwrap();

let doc = yrs::Doc::new();
for (i, update) in updates.iter().enumerate() {
println!("apply update{i} {} bytes", update.len());
doc.transact_mut().apply_update(Update::decode_v1(update).unwrap())
}
let binary = doc
.transact()
.encode_state_as_update_v1(&StateVector::default())
.unwrap();
let map = doc.get_or_insert_map("space:blocks");
let json = serde_json::to_string_pretty(&map.to_json(&doc.transact())).unwrap();

println!("merged {} bytes, json {} bytes", binary.len(), json.len());
write(output, binary).unwrap();
}

#[cfg(test)]
Expand All @@ -95,6 +136,7 @@ mod tests {
#[test]
#[ignore = "only for debug"]
fn test_gc() {
jwst_merge("/Users/ds/Downloads/out");
jwst_merge("/Users/ds/Downloads/out2", "/Users/ds/Downloads/out.jwst");
yrs_merge("/Users/ds/Downloads/out2", "/Users/ds/Downloads/out.yrs");
}
}

0 comments on commit 6f66dc2

Please sign in to comment.