Skip to main content

snix_castore/directoryservice/
mod.rs

1use crate::composition::{Registry, ServiceBuilder};
2use crate::{B3Digest, Directory};
3
4use auto_impl::auto_impl;
5use futures::stream::BoxStream;
6use tonic::async_trait;
7
8pub mod combinators;
9mod directory_graph;
10mod failing_putter;
11mod from_addr;
12mod grpc;
13mod object_store;
14pub mod order_validator;
15mod redb;
16mod simple_putter;
17pub mod traversal;
18
19#[cfg(test)]
20pub mod tests;
21
22pub use self::directory_graph::{DirectoryGraph, DirectoryGraphBuilder, DirectoryServiceGraphExt};
23pub use self::failing_putter::FailingPutter;
24pub use self::from_addr::from_addr;
25pub use self::grpc::{
26    GRPCDirectoryService, GRPCDirectoryServiceConfig,
27    MAX_DECODING_MESSAGE_SIZE as GRPC_MAX_DECODING_MESSAGE_SIZE,
28};
29pub use self::object_store::{ObjectStoreDirectoryService, ObjectStoreDirectoryServiceConfig};
30pub use self::redb::{RedbDirectoryService, RedbDirectoryServiceConfig};
31pub use self::simple_putter::SimplePutter;
32
33#[cfg(feature = "cloud")]
34mod bigtable;
35
36#[cfg(feature = "cloud")]
37pub use self::bigtable::{BigtableDirectoryService, BigtableParameters};
38
39#[derive(thiserror::Error, Debug)]
40pub struct Error(Box<dyn std::error::Error + Send + Sync + 'static>);
41
42impl std::fmt::Display for Error {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        write!(f, "{0}", self.0)
45    }
46}
47
48/// The base trait all Directory services need to implement.
49/// This is a simple get and put of [Directory], returning their
50/// digest.
51#[cfg_attr(any(test, feature = "mocks"), mockall::automock)]
52#[async_trait]
53#[auto_impl(&, &mut, Arc, Box)]
54pub trait DirectoryService: Send + Sync {
55    /// Looks up a single Directory message by its digest.
56    /// The returned Directory message *must* be valid.
57    /// In case the directory is not found, Ok(None) is returned.
58    ///
59    /// It is okay for certain implementations to only allow retrieval of
60    /// Directory digests that are at the "root", aka the last element that's
61    /// sent to a DirectoryPutter. This makes sense for implementations bundling
62    /// closures of directories together in batches.
63    async fn get(&self, digest: &B3Digest) -> Result<Option<Directory>, Error>;
64    /// Uploads a single Directory message, and returns the calculated
65    /// digest, or an error. An error *must* also be returned if the message is
66    /// not valid.
67    async fn put(&self, directory: Directory) -> Result<B3Digest, Error>;
68
69    /// Looks up a closure of [Directory].
70    /// Ideally this would be a `impl Stream<Item = Result<Directory, Error>>`,
71    /// and we'd be able to add a default implementation for it here, but
72    /// we can't have that yet.
73    ///
74    /// This returns a pinned, boxed stream. The pinning allows for it to be polled easily,
75    /// and the box allows different underlying stream implementations to be returned since
76    /// Rust doesn't support this as a generic in traits yet. This is the same thing that
77    /// [async_trait] generates, but for streams instead of futures.
78    ///
79    /// Directories are sent in an order from the root to the leaves, so that
80    /// the receiving side can validate each message to be connected to the root
81    /// that has initially been requested.
82    ///
83    /// In case the directory can not be found, this should return an empty stream.
84    fn get_recursive(
85        &self,
86        root_directory_digest: &B3Digest,
87    ) -> BoxStream<'_, Result<Directory, Error>>;
88
89    /// Allows persisting a closure of [Directory], which is a graph of
90    /// connected Directory messages.
91    fn put_multiple_start<'a>(&'a self) -> Box<dyn DirectoryPutter + 'a>;
92}
93
94/// Provides a handle to put a closure of connected [Directory] elements.
95///
96/// The consumer can periodically call [DirectoryPutter::put], starting from the
97/// leaves. Once the root is reached, [DirectoryPutter::close] can be called to
98/// retrieve the root digest (or an error).
99///
100/// DirectoryPutters might be created without a single [DirectoryPutter::put],
101/// and then dropped without calling [DirectoryPutter::close],
102/// for example when ingesting a path that ends up not pointing to a directory,
103/// but a single file or symlink.
104#[cfg_attr(any(test, feature = "mocks"), mockall::automock)]
105#[async_trait]
106pub trait DirectoryPutter: Send {
107    /// Put a individual [Directory] into the store.
108    /// Error semantics and behaviour is up to the specific implementation of
109    /// this trait.
110    /// Due to bursting, the returned error might refer to an object previously
111    /// sent via `put`.
112    async fn put(&mut self, directory: Directory) -> Result<(), Error>;
113
114    /// Close the stream, and wait for any errors.
115    /// If there's been any invalid Directory message uploaded, and error *must*
116    /// be returned.
117    async fn close(&mut self) -> Result<B3Digest, Error>;
118}
119
120/// Registers the builtin DirectoryService implementations with the registry
121pub(crate) fn register_directory_services(reg: &mut Registry) {
122    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::combinators::CacheConfig>("cache");
123    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::combinators::PriorityConfig>("priority");
124    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::combinators::RaceConfig>("race");
125    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::GRPCDirectoryServiceConfig>("grpc");
126    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::ObjectStoreDirectoryServiceConfig>("objectstore");
127    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::RedbDirectoryServiceConfig>("redb");
128    #[cfg(feature = "cloud")]
129    {
130        reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::BigtableParameters>("bigtable");
131    }
132}