-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create sync car crate (#485)
Replaces iroh-car with ceramic-car that has both async and sync APIs. Additionally changes the Event::encode_car and Event::decode_car methods to be synchronous as we nearly always have the bytes in memory already and its cheap to copy them into memory when we do not.
- Loading branch information
1 parent
92b9c44
commit 01ec7c9
Showing
35 changed files
with
495 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
[package] | ||
name = "iroh-car" | ||
authors = ["dignifiedquire <[email protected]>"] | ||
description = "Implementation the car files for iroh" | ||
name = "ceramic-car" | ||
authors = [ | ||
"dignifiedquire <[email protected]>", | ||
"Nathaniel Cook <[email protected]>", | ||
] | ||
description = "Car V1 reading and writing API" | ||
version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
|
@@ -10,8 +13,8 @@ publish = false | |
|
||
[dependencies] | ||
cid.workspace = true | ||
futures.workspace = true | ||
integer-encoding = { workspace = true, features = ["tokio_async"] } | ||
futures.workspace = true | ||
serde_ipld_dagcbor.workspace = true | ||
serde.workspace = true | ||
thiserror.workspace = true | ||
|
@@ -20,13 +23,11 @@ tokio = { workspace = true, features = ["io-util"] } | |
[dev-dependencies] | ||
multihash.workspace = true | ||
multihash-codetable.workspace = true | ||
ipld-core.workspace = true | ||
tokio = { workspace = true, features = [ | ||
"macros", | ||
"sync", | ||
"rt", | ||
"fs", | ||
"io-util", | ||
] } | ||
ipld-core.workspace = true | ||
|
||
[features] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//! Implementation of the [car](https://ipld.io/specs/transport/car/) format. | ||
|
||
mod reader; | ||
mod util; | ||
mod writer; | ||
|
||
pub use crate::error::Error; | ||
pub use crate::header::{CarHeader, CarHeaderV1}; | ||
pub use reader::CarReader; | ||
pub use writer::CarWriter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::io::Read; | ||
|
||
use cid::Cid; | ||
|
||
use super::util::{ld_read, read_node}; | ||
use crate::{header::CarHeader, Error}; | ||
|
||
/// Reads CAR files that are in a BufReader | ||
#[derive(Debug)] | ||
pub struct CarReader<R> { | ||
reader: R, | ||
header: CarHeader, | ||
buffer: Vec<u8>, | ||
} | ||
|
||
impl<R> CarReader<R> | ||
where | ||
R: Read + Send + Unpin, | ||
{ | ||
/// Creates a new CarReader and parses the CarHeader | ||
pub fn new(mut reader: R) -> Result<Self, Error> { | ||
let mut buffer = Vec::new(); | ||
|
||
match ld_read(&mut reader, &mut buffer)? { | ||
Some(buf) => { | ||
let header = CarHeader::decode(buf)?; | ||
|
||
Ok(CarReader { | ||
reader, | ||
header, | ||
buffer, | ||
}) | ||
} | ||
None => Err(Error::Parsing( | ||
"failed to parse uvarint for header".to_string(), | ||
)), | ||
} | ||
} | ||
|
||
/// Returns the header of this car file. | ||
pub fn header(&self) -> &CarHeader { | ||
&self.header | ||
} | ||
|
||
/// Returns the next IPLD Block in the buffer | ||
pub fn next_block(&mut self) -> Result<Option<(Cid, Vec<u8>)>, Error> { | ||
read_node(&mut self.reader, &mut self.buffer) | ||
} | ||
} | ||
|
||
impl<R> Iterator for CarReader<R> | ||
where | ||
R: Read + Send + Unpin, | ||
{ | ||
type Item = Result<(Cid, Vec<u8>), Error>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
read_node(&mut self.reader, &mut self.buffer).transpose() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::io::Cursor; | ||
|
||
use cid::Cid; | ||
use ipld_core::{codec::Codec, ipld::Ipld}; | ||
use multihash_codetable::{Code, MultihashDigest}; | ||
use serde_ipld_dagcbor::codec::DagCborCodec; | ||
|
||
use crate::sync::*; | ||
|
||
#[test] | ||
fn car_write_read() { | ||
let digest_test = Code::Sha2_256.digest(b"test"); | ||
let cid_test = Cid::new_v1(<DagCborCodec as Codec<Ipld>>::CODE, digest_test); | ||
|
||
let digest_foo = Code::Sha2_256.digest(b"foo"); | ||
let cid_foo = Cid::new_v1(<DagCborCodec as Codec<Ipld>>::CODE, digest_foo); | ||
|
||
let header = CarHeader::V1(CarHeaderV1::from(vec![cid_foo])); | ||
|
||
let mut buffer = Vec::new(); | ||
let mut writer = CarWriter::new(header, &mut buffer); | ||
writer.write(cid_test, b"test").unwrap(); | ||
writer.write(cid_foo, b"foo").unwrap(); | ||
writer.finish().unwrap(); | ||
|
||
let reader = Cursor::new(&buffer); | ||
let car_reader = CarReader::new(reader).unwrap(); | ||
let files: Vec<_> = car_reader | ||
.into_iter() | ||
.collect::<Result<_, Error>>() | ||
.unwrap(); | ||
|
||
assert_eq!(files.len(), 2); | ||
assert_eq!(files[0].0, cid_test); | ||
assert_eq!(files[0].1, b"test"); | ||
assert_eq!(files[1].0, cid_foo); | ||
assert_eq!(files[1].1, b"foo"); | ||
} | ||
} |
Oops, something went wrong.