diff --git a/chir-rs-castore/src/lib.rs b/chir-rs-castore/src/lib.rs index 1d438ac..db794d3 100644 --- a/chir-rs-castore/src/lib.rs +++ b/chir-rs-castore/src/lib.rs @@ -15,7 +15,7 @@ use chir_rs_config::ChirRs; use chir_rs_misc::{id_generator, lexicographic_base64}; use educe::Educe; use eyre::{Context as _, Result}; -use stretto::{AsyncCache, AsyncCacheBuilder}; +use stretto::AsyncCache; use tokio::{ fs::read_to_string, io::{AsyncRead, AsyncReadExt}, diff --git a/chir-rs-db/.sqlx/query-4eb1d9d2853313df1b2a2050de46d609d719ce7cf906df70d693ffc186468351.json b/chir-rs-db/.sqlx/query-4eb1d9d2853313df1b2a2050de46d609d719ce7cf906df70d693ffc186468351.json new file mode 100644 index 0000000..a82c7a8 --- /dev/null +++ b/chir-rs-db/.sqlx/query-4eb1d9d2853313df1b2a2050de46d609d719ce7cf906df70d693ffc186468351.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT COUNT(*) as amount FROM file_map WHERE \"b3hash\" = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "amount", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Bytea" + ] + }, + "nullable": [ + null + ] + }, + "hash": "4eb1d9d2853313df1b2a2050de46d609d719ce7cf906df70d693ffc186468351" +} diff --git a/chir-rs-db/migrations/20241130081151_add-file-b3hash-index.sql b/chir-rs-db/migrations/20241130081151_add-file-b3hash-index.sql new file mode 100644 index 0000000..4b3dc39 --- /dev/null +++ b/chir-rs-db/migrations/20241130081151_add-file-b3hash-index.sql @@ -0,0 +1,3 @@ +-- Add migration script here + +create index file_b3hash on file_map(b3hash); \ No newline at end of file diff --git a/chir-rs-db/src/file.rs b/chir-rs-db/src/file.rs index 72e7a57..1ff7d3a 100644 --- a/chir-rs-db/src/file.rs +++ b/chir-rs-db/src/file.rs @@ -208,7 +208,7 @@ impl File { /// Updates the file with new information /// /// # Errors - /// THis function returns an error if updating the entry in the database fails + /// This function returns an error if updating the entry in the database fails #[instrument(skip(db))] pub async fn update(&self, db: &Database) -> Result<()> { let id: i64 = self.id.try_into()?; @@ -231,4 +231,22 @@ impl File { })?; Ok(()) } + + /// Checks if a particular hash is in use + /// + /// # Errors + /// This function returns an error if updating the entry in the database fails + #[instrument(skip(db))] + pub async fn is_used(db: &Database, hash: Hash) -> Result { + #[expect(clippy::panic, reason = "sqlx silliness")] + let count = query!( + r#"SELECT COUNT(*) as amount FROM file_map WHERE "b3hash" = $1"#, + hash.as_bytes() + ) + .fetch_one(&*db.0) + .await + .with_context(|| format!("Checking if {hash:?} is still used."))?; + + Ok(count.amount.unwrap_or_default() != 0) + } }