refactor(tvix/castore): use Directory struct separate from proto one
This uses our own data type to deal with Directories in the castore model. It makes some undesired states unrepresentable, removing the need for conversions and checking in various places: - In the protobuf, blake3 digests could have a wrong length, as proto doesn't know fixed-size fields. We now use `B3Digest`, which makes cloning cheaper, and removes the need to do size-checking everywhere. - In the protobuf, we had three different lists for `files`, `symlinks` and `directories`. This was mostly a protobuf size optimization, but made interacting with them a bit awkward. This has now been replaced with a list of enums, and convenience iterators to get various nodes, and add new ones. Change-Id: I7b92691bb06d77ff3f58a5ccea94a22c16f84f04 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12057 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
parent
5d3f3158d6
commit
3ca0b53840
53 changed files with 1429 additions and 1377 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{proto, B3Digest, Error};
|
||||
use crate::{B3Digest, Error};
|
||||
use futures::stream::BoxStream;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
|
@ -7,8 +7,9 @@ use tonic::async_trait;
|
|||
use tracing::{instrument, warn};
|
||||
|
||||
use super::utils::traverse_directory;
|
||||
use super::{DirectoryPutter, DirectoryService, SimplePutter};
|
||||
use super::{Directory, DirectoryPutter, DirectoryService, SimplePutter};
|
||||
use crate::composition::{CompositionContext, ServiceBuilder};
|
||||
use crate::proto;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct MemoryDirectoryService {
|
||||
|
|
@ -18,7 +19,7 @@ pub struct MemoryDirectoryService {
|
|||
#[async_trait]
|
||||
impl DirectoryService for MemoryDirectoryService {
|
||||
#[instrument(skip(self, digest), fields(directory.digest = %digest))]
|
||||
async fn get(&self, digest: &B3Digest) -> Result<Option<proto::Directory>, Error> {
|
||||
async fn get(&self, digest: &B3Digest) -> Result<Option<Directory>, Error> {
|
||||
let db = self.db.read().await;
|
||||
|
||||
match db.get(digest) {
|
||||
|
|
@ -37,35 +38,20 @@ impl DirectoryService for MemoryDirectoryService {
|
|||
)));
|
||||
}
|
||||
|
||||
// Validate the Directory itself is valid.
|
||||
if let Err(e) = directory.validate() {
|
||||
warn!("directory failed validation: {}", e.to_string());
|
||||
return Err(Error::StorageError(format!(
|
||||
"directory {} failed validation: {}",
|
||||
actual_digest, e,
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(Some(directory.clone()))
|
||||
Ok(Some(directory.clone().try_into().map_err(|e| {
|
||||
crate::Error::StorageError(format!("corrupted directory: {}", e))
|
||||
})?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, directory), fields(directory.digest = %directory.digest()))]
|
||||
async fn put(&self, directory: proto::Directory) -> Result<B3Digest, Error> {
|
||||
async fn put(&self, directory: Directory) -> Result<B3Digest, Error> {
|
||||
let digest = directory.digest();
|
||||
|
||||
// validate the directory itself.
|
||||
if let Err(e) = directory.validate() {
|
||||
return Err(Error::InvalidRequest(format!(
|
||||
"directory {} failed validation: {}",
|
||||
digest, e,
|
||||
)));
|
||||
}
|
||||
|
||||
// store it
|
||||
let mut db = self.db.write().await;
|
||||
db.insert(digest.clone(), directory);
|
||||
db.insert(digest.clone(), directory.into());
|
||||
|
||||
Ok(digest)
|
||||
}
|
||||
|
|
@ -74,7 +60,7 @@ impl DirectoryService for MemoryDirectoryService {
|
|||
fn get_recursive(
|
||||
&self,
|
||||
root_directory_digest: &B3Digest,
|
||||
) -> BoxStream<'static, Result<proto::Directory, Error>> {
|
||||
) -> BoxStream<'static, Result<Directory, Error>> {
|
||||
traverse_directory(self.clone(), root_directory_digest)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue