···8888 }
8989}
90909191-pub enum Vehicle<R: AsyncRead + Unpin, T: Processable> {
9191+pub enum Driver<R: AsyncRead + Unpin, T: Processable> {
9292 Lil(Commit, MemDriver<T>),
9393 Big(BigCar<R, T>),
9494}
95959696-pub async fn load_car<R: AsyncRead + Unpin, T: Processable>(
9797- reader: R,
9898- process: fn(Vec<u8>) -> T,
9999- max_size: usize,
100100-) -> Result<Vehicle<R, T>, DriveError> {
101101- let mut mem_blocks = HashMap::new();
9696+impl<R: AsyncRead + Unpin, T: Processable> Driver<R, T> {
9797+ pub async fn load_car(
9898+ reader: R,
9999+ process: fn(Vec<u8>) -> T,
100100+ max_size: usize,
101101+ ) -> Result<Driver<R, T>, DriveError> {
102102+ let mut mem_blocks = HashMap::new();
102103103103- let mut car = CarReader::new(reader).await?;
104104+ let mut car = CarReader::new(reader).await?;
104105105105- let root = *car
106106- .header()
107107- .roots()
108108- .first()
109109- .ok_or(DriveError::MissingRoot)?;
110110- log::debug!("root: {root:?}");
106106+ let root = *car
107107+ .header()
108108+ .roots()
109109+ .first()
110110+ .ok_or(DriveError::MissingRoot)?;
111111+ log::debug!("root: {root:?}");
111112112112- let mut commit = None;
113113+ let mut commit = None;
113114114114- // try to load all the blocks into memory
115115- let mut mem_size = 0;
116116- while let Some((cid, data)) = car.next_block().await? {
117117- // the root commit is a Special Third Kind of block that we need to make
118118- // sure not to optimistically send to the processing function
119119- if cid == root {
120120- let c: Commit = serde_ipld_dagcbor::from_slice(&data)?;
121121- commit = Some(c);
122122- continue;
123123- }
115115+ // try to load all the blocks into memory
116116+ let mut mem_size = 0;
117117+ while let Some((cid, data)) = car.next_block().await? {
118118+ // the root commit is a Special Third Kind of block that we need to make
119119+ // sure not to optimistically send to the processing function
120120+ if cid == root {
121121+ let c: Commit = serde_ipld_dagcbor::from_slice(&data)?;
122122+ commit = Some(c);
123123+ continue;
124124+ }
124125125125- // remaining possible types: node, record, other. optimistically process
126126- let maybe_processed = if Node::could_be(&data) {
127127- MaybeProcessedBlock::Raw(data)
128128- } else {
129129- MaybeProcessedBlock::Processed(process(data))
130130- };
126126+ // remaining possible types: node, record, other. optimistically process
127127+ let maybe_processed = if Node::could_be(&data) {
128128+ MaybeProcessedBlock::Raw(data)
129129+ } else {
130130+ MaybeProcessedBlock::Processed(process(data))
131131+ };
131132132132- // stash (maybe processed) blocks in memory as long as we have room
133133- mem_size += std::mem::size_of::<Cid>() + maybe_processed.get_size();
134134- mem_blocks.insert(cid, maybe_processed);
135135- if mem_size >= max_size {
136136- return Ok(Vehicle::Big(BigCar {
137137- car,
138138- root,
139139- process,
140140- max_size,
141141- mem_blocks,
142142- commit,
143143- }));
133133+ // stash (maybe processed) blocks in memory as long as we have room
134134+ mem_size += std::mem::size_of::<Cid>() + maybe_processed.get_size();
135135+ mem_blocks.insert(cid, maybe_processed);
136136+ if mem_size >= max_size {
137137+ return Ok(Driver::Big(BigCar {
138138+ car,
139139+ root,
140140+ process,
141141+ max_size,
142142+ mem_blocks,
143143+ commit,
144144+ }));
145145+ }
144146 }
145145- }
146147147147- // all blocks loaded and we fit in memory! hopefully we found the commit...
148148- let commit = commit.ok_or(DriveError::MissingCommit)?;
148148+ // all blocks loaded and we fit in memory! hopefully we found the commit...
149149+ let commit = commit.ok_or(DriveError::MissingCommit)?;
149150150150- let walker = Walker::new(commit.data);
151151+ let walker = Walker::new(commit.data);
151152152152- Ok(Vehicle::Lil(
153153- commit,
154154- MemDriver {
155155- blocks: mem_blocks,
156156- walker,
157157- process,
158158- },
159159- ))
153153+ Ok(Driver::Lil(
154154+ commit,
155155+ MemDriver {
156156+ blocks: mem_blocks,
157157+ walker,
158158+ process,
159159+ },
160160+ ))
161161+ }
160162}
161163162164/// a paritally memory-loaded car file that needs disk spillover to continue
+4
src/lib.rs
···77pub mod mst;
88pub mod process;
99pub mod walk;
1010+1111+pub use disk::SqliteStore;
1212+pub use drive::{DriveError, Driver};
1313+pub use process::{Processable, noop};