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