1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use async_stream::try_stream;
use futures::stream::BoxStream;
use lru::LruCache;
use nix_compat::nixbase32;
use std::num::NonZeroUsize;
use std::sync::Arc;
use tokio::sync::RwLock;
use tonic::async_trait;
use tracing::instrument;

use crate::proto::PathInfo;
use tvix_castore::composition::{CompositionContext, ServiceBuilder};
use tvix_castore::Error;

use super::PathInfoService;

pub struct LruPathInfoService {
    lru: Arc<RwLock<LruCache<[u8; 20], PathInfo>>>,
}

impl LruPathInfoService {
    pub fn with_capacity(capacity: NonZeroUsize) -> Self {
        Self {
            lru: Arc::new(RwLock::new(LruCache::new(capacity))),
        }
    }
}

#[async_trait]
impl PathInfoService for LruPathInfoService {
    #[instrument(level = "trace", skip_all, fields(path_info.digest = nixbase32::encode(&digest)))]
    async fn get(&self, digest: [u8; 20]) -> Result<Option<PathInfo>, Error> {
        Ok(self.lru.write().await.get(&digest).cloned())
    }

    #[instrument(level = "trace", skip_all, fields(path_info.root_node = ?path_info.node))]
    async fn put(&self, path_info: PathInfo) -> Result<PathInfo, Error> {
        // call validate
        let store_path = path_info
            .validate()
            .map_err(|e| Error::InvalidRequest(format!("invalid PathInfo: {}", e)))?;

        self.lru
            .write()
            .await
            .put(*store_path.digest(), path_info.clone());

        Ok(path_info)
    }

    fn list(&self) -> BoxStream<'static, Result<PathInfo, Error>> {
        let lru = self.lru.clone();
        Box::pin(try_stream! {
            let lru = lru.read().await;
            let it = lru.iter();

            for (_k,v) in it {
                yield v.clone()
            }
        })
    }
}

#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct LruPathInfoServiceConfig {
    capacity: NonZeroUsize,
}

impl TryFrom<url::Url> for LruPathInfoServiceConfig {
    type Error = Box<dyn std::error::Error + Send + Sync>;
    fn try_from(_url: url::Url) -> Result<Self, Self::Error> {
        Err(Error::StorageError(
            "Instantiating a LruPathInfoService from a url is not supported".into(),
        )
        .into())
    }
}

#[async_trait]
impl ServiceBuilder for LruPathInfoServiceConfig {
    type Output = dyn PathInfoService;
    async fn build<'a>(
        &'a self,
        _instance_name: &str,
        _context: &CompositionContext,
    ) -> Result<Arc<dyn PathInfoService>, Box<dyn std::error::Error + Send + Sync + 'static>> {
        Ok(Arc::new(LruPathInfoService::with_capacity(self.capacity)))
    }
}

#[cfg(test)]
mod test {
    use std::num::NonZeroUsize;

    use crate::{
        pathinfoservice::{LruPathInfoService, PathInfoService},
        proto::PathInfo,
        tests::fixtures::PATH_INFO_WITH_NARINFO,
    };
    use lazy_static::lazy_static;
    use tvix_castore::proto as castorepb;

    lazy_static! {
        static ref PATHINFO_1: PathInfo = PATH_INFO_WITH_NARINFO.clone();
        static ref PATHINFO_1_DIGEST: [u8; 20] = [0; 20];
        static ref PATHINFO_2: PathInfo = {
            let mut p = PATHINFO_1.clone();
            let root_node = p.node.as_mut().unwrap();
            if let castorepb::Node { node: Some(node) } = root_node {
                match node {
                    castorepb::node::Node::Directory(n) => {
                        n.name = "11111111111111111111111111111111-dummy2".into()
                    }
                    castorepb::node::Node::File(n) => {
                        n.name = "11111111111111111111111111111111-dummy2".into()
                    }
                    castorepb::node::Node::Symlink(n) => {
                        n.name = "11111111111111111111111111111111-dummy2".into()
                    }
                }
            } else {
                unreachable!()
            }
            p
        };
        static ref PATHINFO_2_DIGEST: [u8; 20] = *(PATHINFO_2.validate().unwrap()).digest();
    }

    #[tokio::test]
    async fn evict() {
        let svc = LruPathInfoService::with_capacity(NonZeroUsize::new(1).unwrap());

        // pathinfo_1 should not be there
        assert!(svc
            .get(*PATHINFO_1_DIGEST)
            .await
            .expect("no error")
            .is_none());

        // insert it
        svc.put(PATHINFO_1.clone()).await.expect("no error");

        // now it should be there.
        assert_eq!(
            Some(PATHINFO_1.clone()),
            svc.get(*PATHINFO_1_DIGEST).await.expect("no error")
        );

        // insert pathinfo_2. This will evict pathinfo 1
        svc.put(PATHINFO_2.clone()).await.expect("no error");

        // now pathinfo 2 should be there.
        assert_eq!(
            Some(PATHINFO_2.clone()),
            svc.get(*PATHINFO_2_DIGEST).await.expect("no error")
        );

        // … but pathinfo 1 not anymore.
        assert!(svc
            .get(*PATHINFO_1_DIGEST)
            .await
            .expect("no error")
            .is_none());
    }
}