Skip to content

Commit 0992f42

Browse files
authored
feat(mater): remove filestore abstraction in favor of blockwriter (#773)
1 parent 53fa203 commit 0992f42

File tree

9 files changed

+44
-176
lines changed

9 files changed

+44
-176
lines changed

mater/cli/src/convert.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::PathBuf;
22

3-
use mater::{create_filestore, Cid, Config, Error};
3+
use mater::{Blockwriter, Cid, Error};
44
use tokio::fs::File;
55

66
/// Converts a file at location `input_path` to a CARv2 file at `output_path`
@@ -16,10 +16,10 @@ pub(crate) async fn convert_file_to_car(
1616
}?;
1717

1818
if input_path.as_os_str() == "-" {
19-
create_filestore(tokio::io::stdin(), output_file, Config::default()).await
19+
Blockwriter::import(tokio::io::stdin(), output_file).await
2020
} else {
2121
let source_file = File::open(input_path).await?;
22-
create_filestore(source_file, output_file, Config::default()).await
22+
Blockwriter::import(source_file, output_file).await
2323
}
2424
}
2525

mater/lib/benches/benchmark.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
};
77

88
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
9-
use mater::{create_filestore, Blockwriter, Config};
9+
use mater::Blockwriter;
1010
use rand::{prelude::SliceRandom, rngs::ThreadRng, Rng};
1111
use tempfile::{tempdir, TempDir};
1212
use tokio::{
@@ -169,29 +169,27 @@ fn prepare_source_file(content: &[u8]) -> (TempDir, PathBuf) {
169169
(temp_dir, file)
170170
}
171171

172-
/// Create a filestore. This function is benchmarked.
173-
async fn create_filestore_benched(source: &Path, target: &Path) {
172+
/// Import a source into a writer. This function is benchmarked.
173+
async fn blockwriter_import(source: &Path, target: &Path) {
174174
let source_file = File::open(source).await.unwrap();
175175
let output_file = File::create(target).await.unwrap();
176176

177-
create_filestore(source_file, output_file, Config::default())
178-
.await
179-
.unwrap();
177+
Blockwriter::import(source_file, output_file).await.unwrap();
180178
}
181179

182-
fn filestore(c: &mut Criterion) {
180+
fn import(c: &mut Criterion) {
183181
let files = get_source_files();
184182

185183
for (params, source_file, temp_dir) in files {
186184
let target_file = temp_dir.path().join("target");
187185

188186
c.bench_with_input(BenchmarkId::new("filestore", params), &(), |b, _: &()| {
189187
b.to_async(TokioExecutor::new().unwrap())
190-
.iter(|| create_filestore_benched(&source_file, &target_file));
188+
.iter(|| blockwriter_import(&source_file, &target_file));
191189
});
192190
}
193191
}
194192

195193
criterion_group!(bench_reading, read_write);
196-
criterion_group!(bench_filestore, filestore);
197-
criterion_main!(bench_reading, bench_filestore);
194+
criterion_group!(bench_import, import);
195+
criterion_main!(bench_reading, bench_import);

mater/lib/src/file_reader.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,18 @@ mod test {
178178

179179
#[tokio::test]
180180
async fn read_duplicated_blocks() {
181-
let raw_input = tokio::fs::read("tests/fixtures/original/zero")
182-
.await
183-
.unwrap();
184-
185181
let mut loader = CarExtractor::from_path("tests/fixtures/car_v2/zero.car")
186182
.await
187183
.unwrap();
188184
let root = loader.roots().await.unwrap()[0];
189185
let mut out_check = Cursor::new(vec![1u8; 4096]);
190186
loader.copy_tree(&root, &mut out_check).await.unwrap();
191187

192-
assert_eq!(raw_input, out_check.into_inner());
188+
let expected = [0u8; 524288].as_slice();
189+
let inner = out_check.into_inner();
190+
let result = inner.as_slice();
191+
192+
assert_eq!(expected, result);
193193
}
194194

195195
async fn load_and_compare<P1, P2>(original: P1, path: P2)

mater/lib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use cid::{CidExt, MultihashExt};
2626
pub use file_reader::CarExtractor;
2727
pub use ipld_core::cid::Cid;
2828
pub use multicodec::{DAG_PB_CODE, IDENTITY_CODE, RAW_CODE};
29-
pub use stores::{create_filestore, Blockwriter, Config, FileBlockstore};
29+
pub use stores::{Blockwriter, Config, FileBlockstore};
3030
pub use v1::{BlockMetadata, Header as CarV1Header, Reader as CarV1Reader, Writer as CarV1Writer};
3131
pub use v2::{
3232
verify_cid, Characteristics, Header as CarV2Header, Index, IndexEntry, IndexSorted,

mater/lib/src/stores/blockstore.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ use futures::stream::StreamExt;
77
use ipld_core::cid::Cid;
88
use tokio::io::{AsyncRead, AsyncSeek, AsyncSeekExt, AsyncWrite};
99

10-
use super::{DEFAULT_BLOCK_SIZE, DEFAULT_TREE_WIDTH};
1110
use crate::{
1211
unixfs::stream_balanced_tree,
1312
v1::{self},
14-
v2, BlockMetadata, Error, Index, IndexEntry, IndexSorted, SingleWidthIndex,
13+
v2, BlockMetadata, Config, Error, Index, IndexEntry, IndexSorted, SingleWidthIndex,
1514
};
1615

1716
/// CAR file writer.
@@ -20,6 +19,7 @@ pub struct Blockwriter<W> {
2019
index: HashMap<Cid, BlockMetadata>,
2120
roots: Vec<Cid>,
2221
started: bool,
22+
config: Config,
2323
}
2424

2525
impl<W> Blockwriter<W> {
@@ -30,6 +30,7 @@ impl<W> Blockwriter<W> {
3030
index: HashMap::new(),
3131
roots: Vec::with_capacity(1),
3232
started: false,
33+
config: Default::default(),
3334
}
3435
}
3536

@@ -66,6 +67,7 @@ impl Blockwriter<Cursor<Vec<u8>>> {
6667
index: HashMap::new(),
6768
roots: Vec::with_capacity(1),
6869
started: false,
70+
config: Default::default(),
6971
}
7072
}
7173
}
@@ -74,6 +76,19 @@ impl<W> Blockwriter<W>
7476
where
7577
W: AsyncWrite + AsyncSeek + Unpin,
7678
{
79+
/// Convert `source` into a CAR file, writing it to `writer`.
80+
/// Returns the root [`Cid`].
81+
pub async fn import<S>(source: S, writer: W) -> Result<Cid, Error>
82+
where
83+
S: AsyncRead + Unpin,
84+
{
85+
let mut writer = Self::new(writer);
86+
writer.write_from(source).await?;
87+
let root = *writer.roots.first().ok_or(Error::EmptyRootsError)?;
88+
writer.finish().await?;
89+
Ok(root)
90+
}
91+
7792
/// Writes the contents from `source`, adding a new root to [`Blockwriter`].
7893
pub async fn write_from<S>(&mut self, source: S) -> Result<(), Error>
7994
where
@@ -87,8 +102,15 @@ where
87102

88103
let mut current_position = self.writer.get_inner_mut().stream_position().await?;
89104

90-
let chunker = crate::chunker::byte_stream_chunker(source, DEFAULT_BLOCK_SIZE);
91-
let nodes = stream_balanced_tree(chunker, DEFAULT_TREE_WIDTH).peekable();
105+
let nodes = match self.config {
106+
Config::Balanced {
107+
chunk_size,
108+
tree_width,
109+
} => {
110+
let chunker = crate::chunker::byte_stream_chunker(source, chunk_size);
111+
stream_balanced_tree(chunker, tree_width).peekable()
112+
}
113+
};
92114
tokio::pin!(nodes);
93115

94116
let mut root = None;

mater/lib/src/stores/filestore.rs

Lines changed: 0 additions & 150 deletions
This file was deleted.

mater/lib/src/stores/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
mod blockstore;
22
mod file;
3-
mod filestore;
43

54
pub use blockstore::Blockwriter;
65
pub use file::FileBlockstore;
7-
pub use filestore::create_filestore;
86

97
/// The default block size, as defined in
108
/// [boxo](https://github.com/ipfs/boxo/blob/f4fe8997dcbeb39b3a4842d8f08b34739bfd84a4/chunker/parse.go#L13).
-512 KB
Binary file not shown.

storage-provider/server/src/storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ where
404404
// Stream the body from source to the temp file.
405405
let file = File::create(&temp_file_path).await?;
406406
let writer = BufWriter::new(file);
407-
let cid = mater::create_filestore(source, writer, mater::Config::default()).await?;
407+
let cid = mater::Blockwriter::import(source, writer).await?;
408408
tracing::trace!("finished writing the CAR archive");
409409

410410
// If the file is successfully written, we can now move it to the final

0 commit comments

Comments
 (0)