Files
keep/src/meta_plugin/digest.rs
Andrew Phillips 81397c1319 feat: update meta plugin constructors to accept options and outputs
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
2025-08-19 13:56:33 -03:00

243 lines
6.8 KiB
Rust

use anyhow::Result;
use sha2::{Digest, Sha256};
use std::time::Instant;
use rusqlite::Connection;
use crate::meta_plugin::MetaPlugin;
#[derive(Debug, Clone, Default)]
pub struct DigestSha256MetaPlugin {
hasher: Sha256,
meta_name: String,
item_id: Option<i64>,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
}
impl DigestSha256MetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> DigestSha256MetaPlugin {
// Start with default outputs
let mut final_outputs = std::collections::HashMap::new();
let default_outputs = Self::default_outputs();
for output_name in default_outputs {
final_outputs.insert(output_name.clone(), serde_yaml::Value::String(output_name));
}
if let Some(outs) = outputs {
for (key, value) in outs {
final_outputs.insert(key, value);
}
}
DigestSha256MetaPlugin {
hasher: Sha256::new(),
meta_name: "digest_sha256".to_string(),
item_id: None,
outputs: final_outputs,
}
}
pub fn new_simple() -> DigestSha256MetaPlugin {
Self::new(None, None)
}
}
}
impl MetaPlugin for DigestSha256MetaPlugin {
fn is_internal(&self) -> bool {
true
}
fn initialize(&mut self, _conn: &Connection, item_id: i64) -> Result<()> {
self.item_id = Some(item_id);
Ok(())
}
fn finalize(&mut self, conn: &Connection) -> Result<()> {
if let Some(item_id) = self.item_id {
// Finalize the hash
let hash_result = self.hasher.finalize_reset();
let hex_string = format!("{:x}", hash_result);
// Save the hash as metadata using central output handler
let _ = self.save_meta(conn, item_id, "digest_sha256", hex_string);
}
Ok(())
}
fn update(&mut self, data: &[u8], _conn: &Connection) {
self.hasher.update(data);
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["digest_sha256".to_string()]
}
fn default_options(&self) -> std::collections::HashMap<String, serde_yaml::Value> {
std::collections::HashMap::new()
}
}
#[derive(Debug, Clone, Default)]
pub struct ReadTimeMetaPlugin {
start_time: Option<Instant>,
meta_name: String,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
}
impl ReadTimeMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> ReadTimeMetaPlugin {
// Start with default outputs
let mut final_outputs = std::collections::HashMap::new();
let default_outputs = Self::default_outputs();
for output_name in default_outputs {
final_outputs.insert(output_name.clone(), serde_yaml::Value::String(output_name));
}
if let Some(outs) = outputs {
for (key, value) in outs {
final_outputs.insert(key, value);
}
}
ReadTimeMetaPlugin {
start_time: None,
meta_name: "read_time".to_string(),
outputs: final_outputs,
}
}
pub fn new_simple() -> ReadTimeMetaPlugin {
Self::new(None, None)
}
}
}
impl MetaPlugin for ReadTimeMetaPlugin {
fn is_internal(&self) -> bool {
true
}
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
Ok(())
}
fn update(&mut self, _data: &[u8], _conn: &Connection) {
if self.start_time.is_none() {
self.start_time = Some(Instant::now());
}
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["read_time".to_string()]
}
fn default_options(&self) -> std::collections::HashMap<String, serde_yaml::Value> {
std::collections::HashMap::new()
}
}
#[derive(Debug, Clone, Default)]
pub struct ReadRateMetaPlugin {
start_time: Option<Instant>,
bytes_read: u64,
meta_name: String,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
}
impl ReadRateMetaPlugin {
pub fn new(
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> ReadRateMetaPlugin {
// Start with default outputs
let mut final_outputs = std::collections::HashMap::new();
let default_outputs = Self::default_outputs();
for output_name in default_outputs {
final_outputs.insert(output_name.clone(), serde_yaml::Value::String(output_name));
}
if let Some(outs) = outputs {
for (key, value) in outs {
final_outputs.insert(key, value);
}
}
ReadRateMetaPlugin {
start_time: None,
bytes_read: 0,
meta_name: "read_rate".to_string(),
outputs: final_outputs,
}
}
pub fn new_simple() -> ReadRateMetaPlugin {
Self::new(None, None)
}
}
}
impl MetaPlugin for ReadRateMetaPlugin {
fn is_internal(&self) -> bool {
true
}
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
Ok(())
}
fn update(&mut self, data: &[u8], _conn: &Connection) {
if self.start_time.is_none() {
self.start_time = Some(Instant::now());
}
self.bytes_read += data.len() as u64;
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["read_rate".to_string()]
}
fn default_options(&self) -> std::collections::HashMap<String, serde_yaml::Value> {
std::collections::HashMap::new()
}
}