about summary refs log blame commit diff
path: root/tvix/store/src/pathinfoservice/memory.rs
blob: 5b48ed9efa34e0198de8ff028c5201a213f1d6d2 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                           



                                                                                                 




                         
                                                        
                                                        












                                                                                   

 
                                                                                  

                                                                               
 


                                                           














                                                                                                              
                                                              




                             








                                                                                              
 
use super::PathInfoService;
use crate::{
    blobservice::BlobService, directoryservice::DirectoryService, nar::calculate_size_and_sha256,
    proto, Error,
};
use std::{
    collections::HashMap,
    sync::{Arc, RwLock},
};

pub struct MemoryPathInfoService<DS: DirectoryService> {
    db: Arc<RwLock<HashMap<[u8; 20], proto::PathInfo>>>,

    blob_service: Box<dyn BlobService>,
    directory_service: DS,
}

impl<DS: DirectoryService> MemoryPathInfoService<DS> {
    pub fn new(blob_service: Box<dyn BlobService>, directory_service: DS) -> Self {
        Self {
            db: Default::default(),
            blob_service,
            directory_service,
        }
    }
}

impl<DS: DirectoryService + Clone> PathInfoService for MemoryPathInfoService<DS> {
    fn get(&self, digest: [u8; 20]) -> Result<Option<proto::PathInfo>, Error> {
        let db = self.db.read().unwrap();

        match db.get(&digest) {
            None => Ok(None),
            Some(path_info) => Ok(Some(path_info.clone())),
        }
    }

    fn put(&self, path_info: proto::PathInfo) -> Result<proto::PathInfo, Error> {
        // Call validate on the received PathInfo message.
        match path_info.validate() {
            Err(e) => Err(Error::InvalidRequest(format!(
                "failed to validate PathInfo: {}",
                e
            ))),

            // In case the PathInfo is valid, and we were able to extract a NixPath, store it in the database.
            // This overwrites existing PathInfo objects.
            Ok(nix_path) => {
                let mut db = self.db.write().unwrap();
                db.insert(nix_path.digest, path_info.clone());

                Ok(path_info)
            }
        }
    }

    fn calculate_nar(&self, root_node: &proto::node::Node) -> Result<(u64, [u8; 32]), Error> {
        calculate_size_and_sha256(
            root_node,
            &self.blob_service,
            self.directory_service.clone(),
        )
        .map_err(|e| Error::StorageError(e.to_string()))
    }
}