about summary refs log tree commit diff
diff options
context:
space:
mode:
authoredef <edef@edef.eu>2023-10-27T11·35+0000
committeredef <edef@edef.eu>2023-10-27T13·56+0000
commit7f7c1ae7be97a54e8a52bda29e6d2c22f2efb822 (patch)
treec05a0fa85a32a1561c85c44e3ee51e561a60081a
parent36f2b69de59ddd9f64c1f37c9ef1422661643245 (diff)
refactor(nix-compat/store_path): make digest and name private r/6888
Change-Id: I62cbe883afcf3dd0c8d4de0e3b845069eb750c97
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9855
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
-rw-r--r--tvix/cli/src/tvix_store_io.rs8
-rw-r--r--tvix/nix-compat/src/store_path/mod.rs22
-rw-r--r--tvix/store/src/fs/mod.rs7
-rw-r--r--tvix/store/src/pathinfoservice/memory.rs2
-rw-r--r--tvix/store/src/pathinfoservice/sled.rs5
-rw-r--r--tvix/store/src/proto/mod.rs4
6 files changed, 27 insertions, 21 deletions
diff --git a/tvix/cli/src/tvix_store_io.rs b/tvix/cli/src/tvix_store_io.rs
index 3ecae98cf3..9be896ffc4 100644
--- a/tvix/cli/src/tvix_store_io.rs
+++ b/tvix/cli/src/tvix_store_io.rs
@@ -61,10 +61,10 @@ impl TvixStoreIO {
         sub_path: &Path,
     ) -> Result<Option<Node>, io::Error> {
         let path_info_service = self.path_info_service.clone();
-        let digest = store_path.digest;
-        let task = self
-            .tokio_handle
-            .spawn(async move { path_info_service.get(digest).await });
+        let task = self.tokio_handle.spawn({
+            let digest = *store_path.digest();
+            async move { path_info_service.get(digest).await }
+        });
         let path_info = match self.tokio_handle.block_on(task).unwrap()? {
             // If there's no PathInfo found, early exit
             None => return Ok(None),
diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs
index c1df442adc..93b1d80bbf 100644
--- a/tvix/nix-compat/src/store_path/mod.rs
+++ b/tvix/nix-compat/src/store_path/mod.rs
@@ -52,8 +52,18 @@ pub enum Error {
 /// path.
 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
 pub struct StorePath {
-    pub digest: [u8; DIGEST_SIZE],
-    pub name: String,
+    digest: [u8; DIGEST_SIZE],
+    name: String,
+}
+
+impl StorePath {
+    pub fn digest(&self) -> &[u8; DIGEST_SIZE] {
+        &self.digest
+    }
+
+    pub fn name(&self) -> &str {
+        self.name.as_ref()
+    }
 }
 
 impl PartialOrd for StorePath {
@@ -187,14 +197,6 @@ pub(crate) fn validate_name(s: &[u8]) -> Result<String, Error> {
     Ok(String::from_utf8(s.to_vec()).unwrap())
 }
 
-/// Ensures the StorePath fulfils the requirements for store paths.
-/// Useful when populating the struct manually instead of parsing.
-pub fn validate(s: &StorePath) -> Result<(), Error> {
-    validate_name(s.name.as_bytes())?;
-
-    Ok(())
-}
-
 impl fmt::Display for StorePath {
     /// The string representation of a store path starts with a digest (20
     /// bytes), [crate::nixbase32]-encoded, followed by a `-`,
diff --git a/tvix/store/src/fs/mod.rs b/tvix/store/src/fs/mod.rs
index 542f626676..84de6ebe8d 100644
--- a/tvix/store/src/fs/mod.rs
+++ b/tvix/store/src/fs/mod.rs
@@ -169,9 +169,10 @@ impl TvixStoreFs {
         } else {
             // If we don't have it, look it up in PathInfoService.
             let path_info_service = self.path_info_service.clone();
-            let task = self
-                .tokio_handle
-                .spawn(async move { path_info_service.get(store_path.digest).await });
+            let task = self.tokio_handle.spawn({
+                let digest = *store_path.digest();
+                async move { path_info_service.get(digest).await }
+            });
             match self.tokio_handle.block_on(task).unwrap()? {
                 // the pathinfo doesn't exist, so the file doesn't exist.
                 None => Ok(None),
diff --git a/tvix/store/src/pathinfoservice/memory.rs b/tvix/store/src/pathinfoservice/memory.rs
index dbb4b02dd0..d18453477a 100644
--- a/tvix/store/src/pathinfoservice/memory.rs
+++ b/tvix/store/src/pathinfoservice/memory.rs
@@ -74,7 +74,7 @@ impl PathInfoService for MemoryPathInfoService {
             // This overwrites existing PathInfo objects.
             Ok(nix_path) => {
                 let mut db = self.db.write().unwrap();
-                db.insert(nix_path.digest, path_info.clone());
+                db.insert(*nix_path.digest(), path_info.clone());
 
                 Ok(path_info)
             }
diff --git a/tvix/store/src/pathinfoservice/sled.rs b/tvix/store/src/pathinfoservice/sled.rs
index bac384ea09..fce0b7f441 100644
--- a/tvix/store/src/pathinfoservice/sled.rs
+++ b/tvix/store/src/pathinfoservice/sled.rs
@@ -119,7 +119,10 @@ impl PathInfoService for SledPathInfoService {
             ))),
             // 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) => match self.db.insert(nix_path.digest, path_info.encode_to_vec()) {
+            Ok(nix_path) => match self
+                .db
+                .insert(*nix_path.digest(), path_info.encode_to_vec())
+            {
                 Ok(_) => Ok(path_info),
                 Err(e) => {
                     warn!("failed to insert PathInfo: {}", e);
diff --git a/tvix/store/src/proto/mod.rs b/tvix/store/src/proto/mod.rs
index 4b5bf4f594..fe942ac266 100644
--- a/tvix/store/src/proto/mod.rs
+++ b/tvix/store/src/proto/mod.rs
@@ -130,12 +130,12 @@ impl PathInfo {
                     // This is safe, because we ensured the proper length earlier already.
                     let reference_digest = self.references[i].to_vec().try_into().unwrap();
 
-                    if reference_names_store_path.digest != reference_digest {
+                    if reference_names_store_path.digest() != &reference_digest {
                         return Err(
                             ValidatePathInfoError::InconsistentNarinfoReferenceNameDigest(
                                 i,
                                 reference_digest,
-                                reference_names_store_path.digest,
+                                *reference_names_store_path.digest(),
                             ),
                         );
                     }