e815b680c0
We currently only support querying by the output hash digest. This makes the interface a bit simpler. Change-Id: I80b285373f1923e85cb0e404c4b15d51a7f259ef Reviewed-on: https://cl.tvl.fyi/c/depot/+/8570 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
use super::PathInfoService;
|
|
use crate::{proto, Error};
|
|
use std::{
|
|
collections::HashMap,
|
|
sync::{Arc, RwLock},
|
|
};
|
|
|
|
#[derive(Default)]
|
|
pub struct MemoryPathInfoService {
|
|
db: Arc<RwLock<HashMap<[u8; 20], proto::PathInfo>>>,
|
|
}
|
|
|
|
impl PathInfoService for MemoryPathInfoService {
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|