2023-05-14 17:10:23 +02:00
|
|
|
use super::PathInfoService;
|
|
|
|
use crate::{proto, Error};
|
2023-02-13 18:55:40 +01:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
|
|
|
|
2023-03-10 12:19:20 +01:00
|
|
|
#[derive(Default)]
|
2023-02-13 18:55:40 +01:00
|
|
|
pub struct MemoryPathInfoService {
|
2023-05-14 17:10:23 +02:00
|
|
|
db: Arc<RwLock<HashMap<[u8; 20], proto::PathInfo>>>,
|
2023-02-13 18:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PathInfoService for MemoryPathInfoService {
|
2023-05-14 17:10:23 +02:00
|
|
|
fn get(&self, digest: [u8; 20]) -> Result<Option<proto::PathInfo>, Error> {
|
|
|
|
let db = self.db.read().unwrap();
|
2023-02-13 18:55:40 +01:00
|
|
|
|
2023-05-14 17:10:23 +02:00
|
|
|
match db.get(&digest) {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(path_info) => Ok(Some(path_info.clone())),
|
2023-02-13 18:55:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2023-05-14 17:10:23 +02:00
|
|
|
db.insert(nix_path.digest, path_info.clone());
|
2023-02-13 18:55:40 +01:00
|
|
|
|
|
|
|
Ok(path_info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|