Skip to main content

snix_store/pathinfoservice/
mod.rs

1pub mod combinators;
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::from_addr::from_addr;
23pub use self::grpc::{GRPCPathInfoService, GRPCPathInfoServiceConfig};
24pub use self::lru::{LruPathInfoService, LruPathInfoServiceConfig};
25pub use self::nix_http::{NixHTTPPathInfoService, NixHTTPPathInfoServiceConfig};
26pub use self::redb::{RedbPathInfoService, RedbPathInfoServiceConfig};
27pub use self::signing_wrapper::{KeyFileSigningPathInfoServiceConfig, SigningPathInfoService};
28
29#[cfg(feature = "cloud")]
30mod bigtable;
31#[cfg(feature = "cloud")]
32pub use self::bigtable::{BigtableParameters, BigtablePathInfoService};
33
34#[cfg(feature = "fs")]
35mod fs;
36#[cfg(feature = "fs")]
37pub use self::fs::RootNodesWrapper;
38
39pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
40
41/// The base trait all PathInfo services need to implement.
42#[cfg_attr(any(test, feature = "mocks"), mockall::automock)]
43#[async_trait]
44#[auto_impl(&, &mut, Arc, Box)]
45pub trait PathInfoService: Send + Sync {
46    /// Retrieve a PathInfo by the output digest.
47    async fn get(&self, digest: [u8; 20]) -> Result<Option<PathInfo>, Error>;
48
49    /// Check if a PathInfo exists.
50    /// Has a naïve default impl, but store implementations may decide to
51    /// implement their own.
52    async fn has(&self, digest: [u8; 20]) -> Result<bool, Error> {
53        Ok(self.get(digest).await?.is_some())
54    }
55
56    /// Store a PathInfo.
57    async fn put(&self, path_info: PathInfo) -> Result<PathInfo, Error>;
58
59    /// Iterate over all PathInfo objects in the store.
60    /// Implementations can decide to disallow listing.
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    /// Even though this function is not async, underlying implementations are
68    /// assumed to be nonblocking on IO, so they MUST use spawn_blocking when
69    /// doing IO.
70    /// Implementations can assume to be invoked in the context of a tokio runtime.
71    fn list(&self) -> BoxStream<'static, Result<PathInfo, Error>>;
72
73    /// Returns a (more) suitable NarCalculationService.
74    /// This can be used to offload NAR calculation to the remote side.
75    fn nar_calculation_service(&self) -> Option<Arc<dyn NarCalculationService>> {
76        None
77    }
78}
79
80/// Registers the builtin PathInfoService implementations with the registry
81pub(crate) fn register_pathinfo_services(reg: &mut Registry) {
82    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, combinators::CacheConfig>("cache");
83    reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, combinators::PriorityConfig>("priority");
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}