use anyhow::Result;
use rusqlite::{Connection, OptionalExtension, Transaction, params};
use super::SqliteStore;
use crate::cold::{content_digest, stub_digest};
use crate::schema::Memory;
impl SqliteStore {
pub fn ingest_memory_if_absent(&self, memory: &Memory) -> Result<bool> {
self.with_txn(|tx| {
if Self::memory_exists(tx, memory.id)? || blocked(tx, memory)? {
return Ok(false);
}
self.store_memory_on_tx(tx, memory)
})
}
pub fn ingest_blocked(&self, memory: &Memory) -> Result<bool> {
blocked(&self.reader(), memory)
}
}
pub(super) fn block_row(tx: &Transaction<'_>, id: &str, owner: Option<&str>) -> Result<()> {
let row: Option<(String, String, String, Option<String>)> = tx
.query_row(
"SELECT source, source_id, content, archived_segment FROM memories \
WHERE id = ?1 AND (?2 IS NULL OR machine_id = ?2)",
params![id, owner],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)),
)
.optional()?;
let Some((source, source_id, content, segment)) = row else {
return Ok(());
};
let digest = match (segment, stub_digest(&content)) {
(Some(_), Some(digest)) => digest.to_string(),
_ => content_digest(&content),
};
tx.execute(
"INSERT OR IGNORE INTO ingest_blocklist (source, source_id, content_sha256, blocked_at) \
VALUES (?1, ?2, ?3, ?4)",
params![
source,
source_id,
digest,
chrono::Utc::now().timestamp_micros()
],
)?;
Ok(())
}
fn blocked(conn: &Connection, memory: &Memory) -> Result<bool> {
Ok(conn.query_row(
"SELECT EXISTS (SELECT 1 FROM ingest_blocklist \
WHERE source = ?1 AND source_id = ?2 AND content_sha256 = ?3)",
params![
memory.source,
memory.source_id,
content_digest(&memory.content)
],
|r| r.get(0),
)?)
}