1
1
use std:: {
2
- fs:: read,
2
+ fs:: { read, write } ,
3
3
io:: { Error , ErrorKind } ,
4
4
path:: PathBuf ,
5
5
time:: Instant ,
6
6
} ;
7
7
8
8
use clap:: Parser ;
9
9
use jwst_codec:: Doc ;
10
+ use yrs:: { types:: ToJson , updates:: decoder:: Decode , ReadTxn , StateVector , Transact , Update } ;
10
11
11
12
/// ybinary merger
12
13
#[ derive( Parser , Debug ) ]
@@ -15,6 +16,10 @@ struct Args {
15
16
/// Path of the ybinary to read
16
17
#[ arg( short, long) ]
17
18
path : String ,
19
+
20
+ /// Output file
21
+ #[ arg( short, long) ]
22
+ output : Option < String > ,
18
23
}
19
24
20
25
fn load_path ( path : & str ) -> Result < Vec < Vec < u8 > > , Error > {
@@ -48,16 +53,25 @@ fn load_path(path: &str) -> Result<Vec<Vec<u8>>, Error> {
48
53
49
54
fn main ( ) {
50
55
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
+ ) ;
52
65
}
53
66
54
- fn jwst_merge ( path : & str ) {
67
+ fn jwst_merge ( path : & str , output : & str ) {
55
68
let updates = load_path ( path) . unwrap ( ) ;
56
69
57
70
let mut doc = Doc :: default ( ) ;
58
71
for ( i, update) in updates. iter ( ) . enumerate ( ) {
59
72
println ! ( "apply update{i} {} bytes" , update. len( ) ) ;
60
73
doc. apply_update_from_binary_v1 ( update. clone ( ) ) . unwrap ( ) ;
74
+ println ! ( "status: {:?}" , doc. store_status( ) ) ;
61
75
}
62
76
63
77
println ! ( "press enter to continue" ) ;
@@ -71,21 +85,48 @@ fn jwst_merge(path: &str) {
71
85
72
86
doc. gc ( ) . unwrap ( ) ;
73
87
74
- let binary = {
88
+ let ( binary, json) = {
89
+ let json = serde_json:: to_string_pretty ( & doc. get_map ( "space:blocks" ) . unwrap ( ) ) . unwrap ( ) ;
75
90
let binary = doc. encode_update_v1 ( ) . unwrap ( ) ;
76
91
77
- println ! ( "merged {} bytes" , binary. len( ) ) ;
92
+ println ! ( "merged {} bytes, json {} bytes " , binary. len ( ) , json . len( ) ) ;
78
93
79
- binary
94
+ ( binary, doc . get_map ( "space:blocks" ) . unwrap ( ) )
80
95
} ;
81
96
82
97
{
83
98
let mut doc = Doc :: default ( ) ;
84
99
doc. apply_update_from_binary_v1 ( binary. clone ( ) ) . unwrap ( ) ;
85
100
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
+ }
86
112
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 ( ) ;
89
130
}
90
131
91
132
#[ cfg( test) ]
@@ -95,6 +136,7 @@ mod tests {
95
136
#[ test]
96
137
#[ ignore = "only for debug" ]
97
138
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" ) ;
99
141
}
100
142
}
0 commit comments