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