@@ -15,23 +15,23 @@ use tracing::{error, info};
1515
1616pub struct Storage {
1717 pub provider : Box < dyn StorageProvider > ,
18+ block_size : u32 ,
1819}
1920
20- // TODO: Make this configurable
21- // Changing to 1MB to optimize for larger files
22- const BLOCK_SIZE : usize = 1024 * 100 ;
23-
2421impl Storage {
25- pub fn new ( provider : Box < dyn StorageProvider > ) -> Self {
26- Storage { provider }
22+ pub fn new ( provider : Box < dyn StorageProvider > , block_size : u32 ) -> Self {
23+ Storage {
24+ provider,
25+ block_size,
26+ }
2727 }
2828
2929 pub fn import_path ( & self , path : & Path ) -> Result < String > {
3030 let rt = tokio:: runtime:: Runtime :: new ( ) ?;
3131 let blocks: Result < Vec < Block > > = rt. block_on ( async {
3232 let file: File = FileBuilder :: new ( )
3333 . path ( path)
34- . fixed_chunker ( BLOCK_SIZE )
34+ . fixed_chunker ( self . block_size . try_into ( ) ? )
3535 . build ( )
3636 . await ?;
3737 let blocks: Vec < _ > = file. encode ( ) . await ?. try_collect ( ) . await ?;
@@ -40,8 +40,6 @@ impl Storage {
4040 let blocks = blocks?;
4141 let mut root_cid: Option < String > = None ;
4242
43- let mut stored_blocks = vec ! [ ] ;
44-
4543 blocks. iter ( ) . for_each ( |b| {
4644 let links = b
4745 . links ( )
@@ -61,14 +59,9 @@ impl Storage {
6159 error ! ( "Failed to import block {e}" ) ;
6260 }
6361 if !stored. links . is_empty ( ) {
64- root_cid = Some ( stored. cid . clone ( ) ) ;
62+ root_cid = Some ( stored. cid ) ;
6563 }
66- stored_blocks. push ( stored) ;
6764 } ) ;
68- info ! ( "Validating imported blocks {}" , blocks. len( ) ) ;
69- if let Err ( e) = crate :: block:: validate_dag ( & stored_blocks) {
70- error ! ( "Failed to validate dag on import: {e}" ) ;
71- }
7265 if blocks. len ( ) == 1 {
7366 if let Some ( first) = blocks. first ( ) {
7467 root_cid = Some ( first. cid ( ) . to_string ( ) ) ;
@@ -141,7 +134,6 @@ impl Storage {
141134 window_size : u32 ,
142135 window_num : u32 ,
143136 ) -> Result < Vec < StoredBlock > > {
144- println ! ( "offset = {} * {}" , window_size, window_num) ;
145137 let offset = window_size * window_num;
146138
147139 self . provider
@@ -156,6 +148,8 @@ pub mod tests {
156148 use assert_fs:: { fixture:: FileWriteBin , fixture:: PathChild , TempDir } ;
157149 use rand:: { thread_rng, RngCore } ;
158150
151+ const BLOCK_SIZE : usize = 1024 * 10 ;
152+
159153 struct TestHarness {
160154 storage : Storage ,
161155 _db_dir : TempDir ,
@@ -167,7 +161,7 @@ pub mod tests {
167161 let db_path = db_dir. child ( "storage.db" ) ;
168162 let provider = SqliteStorageProvider :: new ( db_path. path ( ) . to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
169163 provider. setup ( ) . unwrap ( ) ;
170- let storage = Storage :: new ( Box :: new ( provider) ) ;
164+ let storage = Storage :: new ( Box :: new ( provider) , BLOCK_SIZE . try_into ( ) . unwrap ( ) ) ;
171165 TestHarness {
172166 storage,
173167 _db_dir : db_dir,
@@ -264,17 +258,14 @@ pub mod tests {
264258 let cid = harness. storage . import_path ( test_file. path ( ) ) . unwrap ( ) ;
265259
266260 let window_size: u32 = 10 ;
267- let mut window_num = 0 ;
268-
269261 let all_dag_blocks = harness. storage . get_all_dag_blocks ( & cid) . unwrap ( ) ;
270262
271- for chunk in all_dag_blocks. chunks ( window_size as usize ) . into_iter ( ) {
263+ for ( window_num , chunk) in all_dag_blocks. chunks ( window_size as usize ) . enumerate ( ) {
272264 let window_blocks = harness
273265 . storage
274- . get_dag_blocks_by_window ( & cid, window_size, window_num)
266+ . get_dag_blocks_by_window ( & cid, window_size, window_num. try_into ( ) . unwrap ( ) )
275267 . unwrap ( ) ;
276268 assert_eq ! ( chunk, & window_blocks) ;
277- window_num += 1 ;
278269 }
279270 }
280271
0 commit comments