Skip to content

Commit a87f9f1

Browse files
committed
more work
1 parent 86c7204 commit a87f9f1

File tree

3 files changed

+99
-19
lines changed

3 files changed

+99
-19
lines changed

grovedb/src/lib.rs

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub struct GroveDb {
247247
version: i32
248248
}
249249

250-
pub struct state_sync_info<'db/*, S*/> {
250+
pub struct StateSyncInfo<'db/*, S*/> {
251251
restorer: Option<Restorer<PrefixedRocksDbImmediateStorageContext<'db>>>,
252252
tx: Option<Transaction<'db>>,
253253
pending_chunks :BTreeSet<Vec<u8>>,
@@ -256,20 +256,77 @@ pub struct state_sync_info<'db/*, S*/> {
256256
version: i32,
257257
}
258258

259-
impl/*<S>*/ state_sync_info<'_/*, S*/> {
260-
pub fn new() -> state_sync_info<'static/*, S*/> {
259+
impl/*<S>*/ StateSyncInfo<'_/*, S*/> {
260+
/*
261+
pub fn new<'a>() -> StateSyncInfo<'a/*, S*/> {
261262
let pending_chunks = BTreeSet::new();
262263
let processed_prefixes = BTreeSet::new();
263-
state_sync_info {
264+
StateSyncInfo {
264265
restorer: None,
265266
tx: None,
266-
pending_chunks: pending_chunks,
267-
processed_prefixes: processed_prefixes,
267+
pending_chunks,
268+
processed_prefixes,
268269
//current_subtree_opt: None,
269270
current_prefix: None,
270271
version: 1
271272
}
272273
}
274+
275+
*/
276+
/*
277+
pub fn start_syncing(
278+
&self,
279+
source_db: &GroveDb,
280+
target_db: &GroveDb,
281+
) -> Result<(), Error> {
282+
let app_hash = source_db.root_hash(None).value.unwrap();
283+
target_db.w_start_snapshot_syncing(&mut self, app_hash).expect("TODO: panic message");
284+
Ok(())
285+
}
286+
287+
*/
288+
/*
289+
pub fn w_start_snapshot_syncing<'db>(
290+
&'db mut self,
291+
grovedb: &'db GroveDb,
292+
app_hash: CryptoHash,
293+
) -> Result<Vec<Vec<u8>>, Error>{
294+
let mut res = vec![];
295+
296+
match (&mut self.restorer, &self.tx, &self.current_prefix) {
297+
(None, None, None) => {
298+
if self.pending_chunks.is_empty() && self.processed_prefixes.is_empty() {
299+
let root_prefix = [0u8; 32];
300+
self.tx = Some(grovedb.start_transaction());
301+
if let Some(ref_tx) = self.tx.as_ref() {
302+
let merk = grovedb.open_merk_for_replication(SubtreePath::empty(), ref_tx).unwrap();
303+
let restorer = Restorer::new(merk, app_hash, None);
304+
self.restorer = Some(restorer);
305+
self.current_prefix = Some(root_prefix);
306+
self.pending_chunks.insert(root_prefix.to_vec());
307+
308+
res.push(root_prefix.to_vec());
309+
}
310+
else {
311+
return Err(Error::InternalError(
312+
"Unable to start a tx",
313+
));
314+
}
315+
} else {
316+
return Err(Error::InternalError(
317+
"Invalid internal state sync info",
318+
));
319+
}
320+
},
321+
_ => {
322+
return Err(Error::InternalError(
323+
"GroveDB has already started a snapshot syncing",
324+
));
325+
}
326+
}
327+
328+
Ok(res)
329+
}*/
273330
}
274331

275332
pub(crate) type SubtreePrefix = [u8; blake3::OUT_LEN];
@@ -308,6 +365,21 @@ pub type TransactionArg<'db, 'a> = Option<&'a Transaction<'db>>;
308365

309366
#[cfg(feature = "full")]
310367
impl GroveDb {
368+
pub fn create_state_sync_info(
369+
&self,
370+
) -> StateSyncInfo {
371+
let pending_chunks = BTreeSet::new();
372+
let processed_prefixes = BTreeSet::new();
373+
StateSyncInfo {
374+
restorer: None,
375+
tx: None,
376+
pending_chunks,
377+
processed_prefixes,
378+
//current_subtree_opt: None,
379+
current_prefix: None,
380+
version: 1
381+
}
382+
}
311383
/// Opens a given path
312384
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
313385

@@ -387,7 +459,7 @@ impl GroveDb {
387459
fn open_merk_for_replication<'db, 'b, B>(
388460
&'db self,
389461
path: SubtreePath<'b, B>,
390-
tx: &'db Transaction,
462+
tx: &'b Transaction<'db>,
391463
) -> Result<Merk<PrefixedRocksDbImmediateStorageContext<'db>>, Error>
392464
where
393465
B: AsRef<[u8]> + 'b,
@@ -1316,13 +1388,20 @@ impl GroveDb {
13161388

13171389
// pub fn w_start_snapshot_syncing<'db: 'a, 'a/*, S: StorageContext<'db>*/>(
13181390
// &'a self,
1319-
// state_sync_info: &'db mut state_sync_info<'a/*, S*/>,
1391+
// StateSyncInfo: &'db mut StateSyncInfo<'a/*, S*/>,
13201392
// app_hash: CryptoHash,
13211393
// )
13221394

1323-
pub fn w_start_snapshot_syncing<'db: 'a, 'a/*, S: StorageContext<'db>*/>(
1324-
&'a self,
1325-
state_sync_info: &'db mut state_sync_info<'a/*, S*/>,
1395+
// pub fn w_start_snapshot_syncing<'db: 'a, 'a/*, S: StorageContext<'db>*/>(
1396+
// &'a self,
1397+
// StateSyncInfo: &'db mut StateSyncInfo<'a/*, S*/>,
1398+
// app_hash: CryptoHash,
1399+
// )
1400+
1401+
pub fn w_start_snapshot_syncing<'db>(
1402+
&'db self,
1403+
//state_sync_info: &'db mut StateSyncInfo<'db>,
1404+
mut state_sync_info: StateSyncInfo<'db>,
13261405
app_hash: CryptoHash,
13271406
) -> Result<Vec<Vec<u8>>, Error>{
13281407
let mut res = vec![];
@@ -1364,7 +1443,7 @@ impl GroveDb {
13641443

13651444
pub fn w_apply_chunk<'db: 'a, 'a/*, S: StorageContext<'db>*/>(
13661445
&'a self,
1367-
state_sync_info: &'db mut state_sync_info<'a/*, S*/>,
1446+
state_sync_info: &'db mut StateSyncInfo<'a/*, S*/>,
13681447
chunk: (Vec<u8>, Vec<Op>)
13691448
) -> Result<Vec<Vec<u8>>, Error>{
13701449
let mut res = vec![];

merk/src/merk/restore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'db, S: StorageContext<'db>> Restorer<S> {
121121
}
122122

123123
/// Process multi chunks (space optimized chunk proofs that can contain
124-
/// multiple singluar chunks)
124+
/// multiple singular chunks)
125125
pub fn process_multi_chunk(&mut self, multi_chunk: Vec<ChunkOp>) -> Result<Vec<String>, Error> {
126126
let mut expect_chunk_id = true;
127127
let mut chunk_ids = vec![];

tutorials/src/bin/replication.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::VecDeque;
22
use std::ops::Range;
33
use std::path::Path;
4-
use grovedb::{operations::insert::InsertOptions, Element, GroveDb, PathQuery, Query, Transaction, state_sync_info};
4+
use grovedb::{operations::insert::InsertOptions, Element, GroveDb, PathQuery, Query, Transaction, StateSyncInfo};
55
use grovedb::reference_path::ReferencePathType;
66
use rand::{distributions::Alphanumeric, Rng, thread_rng};
77
use rand::prelude::SliceRandom;
@@ -78,7 +78,7 @@ fn main() {
7878
let db_checkpoint_0 = GroveDb::open(path_checkpoint).expect("cannot open grovedb from checkpoint");
7979

8080
let path_copy = generate_random_path("../tutorial-storage/", "/db_copy", 24);
81-
let mut db_copy = create_empty_db(path_copy.clone());
81+
let db_copy = create_empty_db(path_copy.clone());
8282

8383
println!("\n######### root_hashes:");
8484
let root_hash_0 = db_0.root_hash(None).unwrap().unwrap();
@@ -89,7 +89,8 @@ fn main() {
8989
println!("root_hash_copy: {:?}", hex::encode(root_hash_copy));
9090

9191
println!("\n######### db_checkpoint_0 -> db_copy state sync");
92-
sync_db_demo(&db_checkpoint_0, &db_copy).unwrap();
92+
let mut state_info = db_copy.create_state_sync_info();
93+
sync_db_demo(&db_checkpoint_0, &db_copy, &mut state_info).unwrap();
9394
//db_copy.w_sync_db_demo(&db_checkpoint_0).unwrap();
9495
return;
9596

@@ -219,11 +220,11 @@ fn query_db(db: &GroveDb, path: &[&[u8]], key: Vec<u8>) {
219220

220221
fn sync_db_demo(
221222
source_db: &GroveDb,
222-
target_db: &GroveDb,
223+
target_db: & GroveDb,
224+
state_sync_info: &mut StateSyncInfo,
223225
) -> Result<(), grovedb::Error> {
224-
let mut state_sync_inf = state_sync_info::new();
225226
let app_hash = source_db.root_hash(None).value.unwrap();
226-
//target_db.w_start_snapshot_syncing(&mut state_sync_inf, app_hash);
227+
target_db.w_start_snapshot_syncing(state_sync_info, app_hash).expect("TODO: panic message");
227228
Ok(())
228229
}
229230

0 commit comments

Comments
 (0)