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