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    /// The individually returned Directory messages *must* be valid.
68    /// Directories are sent in an order from the root to the leaves, so that
69    /// the receiving side can validate each message to be a connected to the root
70    /// that has initially been requested.
71    ///
72    /// In case the directory can not be found, this should return an empty stream.
73    fn get_recursive(
74        &self,
75        root_directory_digest: &B3Digest,
76    ) -> BoxStream<'_, Result<Directory, Error>>;
77
78    /// Allows persisting a closure of [Directory], which is a graph of
79    /// connected Directory messages.
80    fn put_multiple_start(&self) -> Box<dyn DirectoryPutter + '_>;
81}
82
83/// Provides a handle to put a closure of connected [Directory] elements.
84///
85/// The consumer can periodically call [DirectoryPutter::put], starting from the
86/// leaves. Once the root is reached, [DirectoryPutter::close] can be called to
87/// retrieve the root digest (or an error).
88///
89/// DirectoryPutters might be created without a single [DirectoryPutter::put],
90/// and then dropped without calling [DirectoryPutter::close],
91/// for example when ingesting a path that ends up not pointing to a directory,
92/// but a single file or symlink.
93#[async_trait]
94pub trait DirectoryPutter: Send {
95    /// Put a individual [Directory] into the store.
96    /// Error semantics and behaviour is up to the specific implementation of
97    /// this trait.
98    /// Due to bursting, the returned error might refer to an object previously
99    /// sent via `put`.
100    async fn put(&mut self, directory: Directory) -> Result<(), Error>;
101
102    /// Close the stream, and wait for any errors.
103    /// If there's been any invalid Directory message uploaded, and error *must*
104    /// be returned.
105    async fn close(&mut self) -> Result<B3Digest, Error>;
106}
107
108/// Registers the builtin DirectoryService implementations with the registry
109pub(crate) fn register_directory_services(reg: &mut Registry) {
110    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::CacheConfig>("cache");
111    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::GRPCDirectoryServiceConfig>("grpc");
112    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::ObjectStoreDirectoryServiceConfig>("objectstore");
113    reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::RedbDirectoryServiceConfig>("redb");
114    #[cfg(feature = "cloud")]
115    {
116        reg.register::<Box<dyn ServiceBuilder<Output = dyn DirectoryService>>, super::directoryservice::BigtableParameters>("bigtable");
117    }
118}