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