second-brain-core 0.13.2

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
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 {
    /// `store_memory_if_absent` for ingest, which also skips a memory whose
    /// text a delete removed from the same source, so a source file still
    /// on disk cannot bring it back under a new id.
    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)
        })
    }

    /// Whether a delete removed this memory's text from its source.
    pub fn ingest_blocked(&self, memory: &Memory) -> Result<bool> {
        blocked(&self.reader(), memory)
    }
}

/// Blocks the text of the memory row `id` from its source, read before the
/// row is deleted, when `owner` is None or ingested the row. A cold row's
/// stub carries the digest of its full text.
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),
    )?)
}