diff options
author | Yureka <tvl@yuka.dev> | 2024-07-29T12·34+0200 |
---|---|---|
committer | yuka <tvl@yuka.dev> | 2024-08-13T12·17+0000 |
commit | 3ca0b53840b352b24f3a315404df11458b0bdbbb (patch) | |
tree | 2635e8d67d0c334cc5760dc4205b7515c6283a77 /tvix/nar-bridge | |
parent | 5d3f3158d6102baee48d2772e85f05cfc1fac95e (diff) |
refactor(tvix/castore): use Directory struct separate from proto one r/8484
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>
Diffstat (limited to 'tvix/nar-bridge')
-rw-r--r-- | tvix/nar-bridge/src/nar.rs | 21 | ||||
-rw-r--r-- | tvix/nar-bridge/src/narinfo.rs | 22 |
2 files changed, 18 insertions, 25 deletions
diff --git a/tvix/nar-bridge/src/nar.rs b/tvix/nar-bridge/src/nar.rs index 9ee27c7df5ca..1dba05f5b38f 100644 --- a/tvix/nar-bridge/src/nar.rs +++ b/tvix/nar-bridge/src/nar.rs @@ -46,25 +46,20 @@ pub async fn get( } // parse the proto - let mut root_node: tvix_castore::proto::Node = Message::decode(Bytes::from(root_node_proto)) + let root_node: tvix_castore::proto::Node = Message::decode(Bytes::from(root_node_proto)) .map_err(|e| { warn!(err=%e, "unable to decode root node proto"); StatusCode::NOT_FOUND })?; + let root_node: tvix_castore::directoryservice::Node = (&root_node).try_into().map_err(|e| { + warn!(err=%e, "root node validation failed"); + StatusCode::BAD_REQUEST + })?; + // validate the node, but add a dummy node name, as we only send unnamed // nodes - if let Some(rn) = root_node.node { - root_node.node = Some(rn.rename("00000000000000000000000000000000-dummy".into())) - } - - let root_node = root_node - .validate() - .map_err(|e| { - warn!(err=%e, "root node validation failed"); - StatusCode::BAD_REQUEST - })? - .to_owned(); + let root_node = root_node.rename("00000000000000000000000000000000-dummy".into()); let (w, r) = tokio::io::duplex(1024 * 8); @@ -130,7 +125,7 @@ pub async fn put( // store mapping of narhash to root node into root_nodes. // we need it later to populate the root node when accepting the PathInfo. - root_nodes.write().put(nar_hash_actual, root_node); + root_nodes.write().put(nar_hash_actual, (&root_node).into()); Ok("") } diff --git a/tvix/nar-bridge/src/narinfo.rs b/tvix/nar-bridge/src/narinfo.rs index 4706be64d5b4..a883ac88ce18 100644 --- a/tvix/nar-bridge/src/narinfo.rs +++ b/tvix/nar-bridge/src/narinfo.rs @@ -61,22 +61,20 @@ pub async fn get( StatusCode::INTERNAL_SERVER_ERROR })?; - let mut narinfo = path_info.to_narinfo(store_path).ok_or_else(|| { + let mut narinfo = path_info.to_narinfo(store_path.as_ref()).ok_or_else(|| { warn!(path_info=?path_info, "PathInfo contained no NAR data"); StatusCode::INTERNAL_SERVER_ERROR })?; // encode the (unnamed) root node in the NAR url itself. - let root_node = path_info - .node - .as_ref() - .and_then(|n| n.node.as_ref()) - .expect("root node must not be none") - .clone() - .rename("".into()); + let root_node = tvix_castore::directoryservice::Node::try_from( + path_info.node.as_ref().expect("root node must not be none"), + ) + .unwrap() // PathInfo is validated + .rename("".into()); let mut buf = Vec::new(); - Node::encode(&root_node, &mut buf); + Node::encode(&(&root_node).into(), &mut buf); let url = format!( "nar/tvix-castore/{}?narsize={}", @@ -128,10 +126,10 @@ pub async fn put( // Lookup root node with peek, as we don't want to update the LRU list. // We need to be careful to not hold the RwLock across the await point. - let maybe_root_node = root_nodes + let maybe_root_node: Option<tvix_castore::directoryservice::Node> = root_nodes .read() .peek(&narinfo.nar_hash) - .map(|v| v.to_owned()); + .and_then(|v| v.try_into().ok()); match maybe_root_node { Some(root_node) => { @@ -139,7 +137,7 @@ pub async fn put( // We need to rename the node to the narinfo storepath basename, as // that's where it's stored in PathInfo. pathinfo.node = Some(castorepb::Node { - node: Some(root_node.rename(narinfo.store_path.to_string().into())), + node: Some((&root_node.rename(narinfo.store_path.to_string().into())).into()), }); // Persist the PathInfo. |