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