Skip to content

Commit

Permalink
fix: move FsBlockStore::list outside of async_trait
Browse files Browse the repository at this point in the history
see comments in rs-ipfs#458. it is unknown why this is needed but it does work
away of the compiler error.
  • Loading branch information
koivunej committed Aug 2, 2021
1 parent d6838f1 commit afa0e47
Showing 1 changed file with 44 additions and 40 deletions.
84 changes: 44 additions & 40 deletions src/repo/fs/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,49 @@ impl FsBlockStore {
}
}
}

async fn list0(&self) -> Result<Vec<Cid>, Error> {
use futures::future::{ready, Either};
use futures::stream::{empty, TryStreamExt};
use tokio_stream::wrappers::ReadDirStream;

let span = tracing::trace_span!("listing blocks");

async move {
let stream = ReadDirStream::new(fs::read_dir(self.path.clone()).await?);

// FIXME: written as a stream to make the Vec be BoxStream<'static, Cid>
let vec = stream
.and_then(|d| async move {
// map over the shard directories
Ok(if d.file_type().await?.is_dir() {
Either::Left(ReadDirStream::new(fs::read_dir(d.path()).await?))
} else {
Either::Right(empty())
})
})
// flatten each
.try_flatten()
// convert the paths ending in ".data" into cid
.try_filter_map(|d| {
let name = d.file_name();
let path: &std::path::Path = name.as_ref();

ready(if path.extension() != Some("data".as_ref()) {
Ok(None)
} else {
let maybe_cid = filestem_to_block_cid(path.file_stem());
Ok(maybe_cid)
})
})
.try_collect::<Vec<_>>()
.await?;

Ok(vec)
}
.instrument(span)
.await
}
}

#[async_trait]
Expand Down Expand Up @@ -355,46 +398,7 @@ impl BlockStore for FsBlockStore {
}

async fn list(&self) -> Result<Vec<Cid>, Error> {
use futures::future::{ready, Either};
use futures::stream::{empty, TryStreamExt};
use tokio_stream::wrappers::ReadDirStream;

let span = tracing::trace_span!("listing blocks");

async move {
let stream = ReadDirStream::new(fs::read_dir(self.path.clone()).await?);

// FIXME: written as a stream to make the Vec be BoxStream<'static, Cid>
let vec = stream
.and_then(|d| async move {
// map over the shard directories
Ok(if d.file_type().await?.is_dir() {
Either::Left(ReadDirStream::new(fs::read_dir(d.path()).await?))
} else {
Either::Right(empty())
})
})
// flatten each
.try_flatten()
// convert the paths ending in ".data" into cid
.try_filter_map(|d| {
let name = d.file_name();
let path: &std::path::Path = name.as_ref();

ready(if path.extension() != Some("data".as_ref()) {
Ok(None)
} else {
let maybe_cid = filestem_to_block_cid(path.file_stem());
Ok(maybe_cid)
})
})
.try_collect::<Vec<_>>()
.await?;

Ok(vec)
}
.instrument(span)
.await
self.list0().await
}

async fn wipe(&self) {
Expand Down

0 comments on commit afa0e47

Please sign in to comment.