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