Skip to main content

snix_castore/blobservice/
mod.rs

1use std::io;
2
3use auto_impl::auto_impl;
4use tonic::async_trait;
5
6use crate::B3Digest;
7use crate::composition::{Registry, ServiceBuilder};
8use crate::proto::stat_blob_response::ChunkMeta;
9
10mod chunked_reader;
11mod combinator;
12mod from_addr;
13mod grpc;
14mod memory;
15mod object_store;
16
17#[cfg(test)]
18pub mod tests;
19
20#[cfg(any(test, feature = "mocks"))]
21mod mocks;
22#[cfg(any(test, feature = "mocks"))]
23pub use mocks::TestBlobWriter;
24
25pub use self::chunked_reader::ChunkedReader;
26pub use self::combinator::{Cache, CacheBlobServiceConfig};
27pub use self::from_addr::from_addr;
28pub use self::grpc::{GRPCBlobService, GRPCBlobServiceConfig};
29pub use self::memory::{MemoryBlobService, MemoryBlobServiceConfig};
30pub use self::object_store::{ObjectStoreBlobService, ObjectStoreBlobServiceConfig};
31
32/// The base trait all BlobService services need to implement.
33/// It provides functions to check whether a given blob exists,
34/// a way to read (and seek) a blob, and a method to create a blobwriter handle,
35/// which will implement a writer interface, and also provides a close function,
36/// to finalize a blob and get its digest.
37#[cfg_attr(any(test, feature = "mocks"), mockall::automock)]
38#[async_trait]
39#[auto_impl(&, &mut, Arc, Box)]
40pub trait BlobService: Send + Sync {
41    /// Check if the service has the blob, by its content hash.
42    /// On implementations returning chunks, this must also work for chunks.
43    async fn has(&self, digest: &B3Digest) -> io::Result<bool>;
44
45    /// Request a blob from the store, by its content hash.
46    /// On implementations returning chunks, this must also work for chunks.
47    async fn open_read(&self, digest: &B3Digest) -> io::Result<Option<Box<dyn BlobReader>>>;
48
49    /// Insert a new blob into the store. Returns a [BlobWriter], which
50    /// implements [tokio::io::AsyncWrite] and a [BlobWriter::close] to finalize
51    /// the blob and get its digest.
52    async fn open_write(&self) -> Box<dyn BlobWriter>;
53
54    /// Return a list of chunks for a given blob.
55    /// There's a distinction between returning Ok(None) and Ok(Some(vec![])).
56    /// The former return value is sent in case the blob is not present at all,
57    /// while the second one is sent in case there's no more granular chunks (or
58    /// the backend does not support chunking).
59    /// A default implementation checking for existence and then returning it
60    /// does not have more granular chunks available is provided.
61    async fn chunks(&self, digest: &B3Digest) -> io::Result<Option<Vec<ChunkMeta>>> {
62        if !self.has(digest).await? {
63            return Ok(None);
64        }
65        // default implementation, signalling the backend does not have more
66        // granular chunks available.
67        Ok(Some(vec![]))
68    }
69}
70
71/// A [tokio::io::AsyncWrite] that the user needs to close() afterwards to persist.
72/// On success, it returns the digest of the written blob.
73#[async_trait]
74pub trait BlobWriter: tokio::io::AsyncWrite + Send + Unpin {
75    /// Signal there's no more data to be written, and return the digest of the
76    /// contents written.
77    ///
78    /// Closing an already-closed BlobWriter is a no-op.
79    async fn close(&mut self) -> io::Result<B3Digest>;
80}
81
82/// BlobReader is a [tokio::io::AsyncRead] that also allows seeking.
83pub trait BlobReader: tokio::io::AsyncRead + tokio::io::AsyncSeek + Send + Unpin + 'static {}
84
85/// A [`io::Cursor<Vec<u8>>`] can be used as a BlobReader.
86impl BlobReader for io::Cursor<&'static [u8]> {}
87impl BlobReader for io::Cursor<&'static [u8; 0]> {}
88impl BlobReader for io::Cursor<Vec<u8>> {}
89impl BlobReader for io::Cursor<bytes::Bytes> {}
90impl BlobReader for tokio::fs::File {}
91
92/// Registers the builtin BlobService implementations with the registry
93pub(crate) fn register_blob_services(reg: &mut Registry) {
94    reg.register::<Box<dyn ServiceBuilder<Output = dyn BlobService>>, super::blobservice::ObjectStoreBlobServiceConfig>("objectstore");
95    reg.register::<Box<dyn ServiceBuilder<Output = dyn BlobService>>, super::blobservice::MemoryBlobServiceConfig>("memory");
96    reg.register::<Box<dyn ServiceBuilder<Output = dyn BlobService>>, super::blobservice::CacheBlobServiceConfig>("cache");
97    reg.register::<Box<dyn ServiceBuilder<Output = dyn BlobService>>, super::blobservice::GRPCBlobServiceConfig>("grpc");
98}