feat: add sha256 hash metadata storage in finalize

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-18 09:20:07 -03:00
parent 9e62356a30
commit 18a3f1b36e

View File

@@ -1,6 +1,7 @@
use anyhow::Result;
use sha2::{Digest, Sha256};
use std::time::Instant;
use rusqlite::Connection;
use crate::meta_plugin::MetaPlugin;
@@ -8,6 +9,8 @@ use crate::meta_plugin::MetaPlugin;
pub struct DigestSha256MetaPlugin {
hasher: Sha256,
meta_name: String,
item_id: Option<i64>,
conn: Option<*mut Connection>,
}
impl DigestSha256MetaPlugin {
@@ -15,6 +18,8 @@ impl DigestSha256MetaPlugin {
DigestSha256MetaPlugin {
hasher: Sha256::new(),
meta_name: "digest_sha256".to_string(),
item_id: None,
conn: None,
}
}
}
@@ -24,7 +29,30 @@ impl MetaPlugin for DigestSha256MetaPlugin {
true
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
self.item_id = Some(item_id);
// Store raw pointer to connection - unsafe but necessary for this design
self.conn = Some(conn as *const _ as *mut Connection);
Ok(())
}
fn finalize(&mut self) -> Result<()> {
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
// Convert raw pointer back to reference (unsafe)
let conn_ref = unsafe { &*conn };
// Finalize the hash
let hash_result = self.hasher.finalize_reset();
let hex_string = format!("{:x}", hash_result);
// Save the hash as metadata
let meta = crate::db::Meta {
id: item_id,
name: self.meta_name.clone(),
value: hex_string,
};
crate::db::store_meta(conn_ref, meta)?;
}
Ok(())
}