Skip to main content

snix_store/pathinfoservice/
mod.rs

1mod cache;
2mod from_addr;
3mod grpc;
4mod lru;
5mod nix_http;
6mod redb;
7mod signing_wrapper;
8
9#[cfg(test)]
10mod tests;
11
12use std::sync::Arc;
13
14use auto_impl::auto_impl;
15use futures::stream::BoxStream;
16use snix_castore::composition::{Registry, ServiceBuilder};
17use tonic::async_trait;
18
19use crate::nar::NarCalculationService;
20pub use crate::path_info::PathInfo;
21
22pub use self::cache::{Cache as CachePathInfoService, CacheConfig as CachePathInfoServiceConfig};
23pub use self::from_addr::from_addr;
24pub use self::grpc::{GRPCPathInfoService, GRPCPathInfoServiceConfig};
25pub use self::lru::{LruPathInfoService, LruPathInfoServiceConfig};
26pub use self::nix_http::{NixHTTPPathInfoService, NixHTTPPathInfoServiceConfig};
27pub use self::redb::{RedbPathInfoService, RedbPathInfoServiceConfig};
28pub use self::signing_wrapper::{KeyFileSigningPathInfoServiceConfig, SigningPathInfoService};
29
30#[cfg(feature = "cloud")]
31mod bigtable;
32#[cfg(feature = "cloud")]
33pub use self::bigtable::{BigtableParameters, BigtablePathInfoService};
34
35#[cfg(feature = "fs")]
36mod fs;
37#[cfg(feature = "fs")]
38pub use self::fs::RootNodesWrapper;
39
40pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
41
42/// The base trait all PathInfo services need to implement.
43#[cfg_attr(any(test, feature = "mocks"), mockall::automock)]
44#[async_trait]
45#[auto_impl(&, &mut, Arc, Box)]
46pub trait PathInfoService: Send + Sync {
47    /// Retrieve a PathInfo by the output digest.
48    async fn get(&self, digest: [u8; 20]) -> Result<Option<PathInfo>, Error>;
49
50    /// Check if a PathInfo exists.
51    /// Has a naïve default impl, but store implementations may decide to
52    /// implement their own.
53    async fn has(&self, digest: [u8; 20]) -> Result<bool, Error> {
54        Ok(self.get(digest).await?.is_some())
55    }
56
57    /// Store a PathInfo.
58    async fn put(&self, path_info: PathInfo) -> Result<PathInfo, Error>;
59
60    /// Iterate over all PathInfo objects in the store.
61    /// Implementations can decide to disallow listing.
62    ///
63    /// This returns a pinned, boxed stream. The pinning allows for it to be polled easily,
64    /// and the box allows different underlying stream implementations to be returned since
65    /// Rust doesn't support this as a generic in traits yet. This is the same thing that
66    /// [async_trait] generates, but for streams instead of futures.
67    ///
68    /// Even though this function is not async, underlying implementations are
69    /// assumed to be nonblocking on IO, so they MUST use spawn_blocking when
70    /// doing IO.
71    /// Implementations can assume to be invoked in the context of a tokio runtime.
72    fn list(&self) -> BoxStream<'static, Result<PathInfo, Error>>;
73
74    /// Returns a (more) suitable NarCalculationService.
75    /// This can be used to offload NAR calculation to the remote side.
76    fn nar_calculation_service(&self) -> Option<Arc<dyn NarCalculationService>> {
77        None
78    }
79}
80
81/// Registers the builtin PathInfoService implementations with the registry
82pub(crate) fn register_pathinfo_services(reg: &mut Registry) {
83    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, CachePathInfoServiceConfig>("cache");
84    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, GRPCPathInfoServiceConfig>("grpc");
85    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, KeyFileSigningPathInfoServiceConfig>("keyfile-signing");
86    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, LruPathInfoServiceConfig>("lru");
87    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, NixHTTPPathInfoServiceConfig>("nix");
88    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, RedbPathInfoServiceConfig>("redb");
89    #[cfg(feature = "cloud")]
90    {
91        reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, BigtableParameters>(
92            "bigtable",
93        );
94    }
95}