Skip to main content

second_brain_api/
sync_import.rs

1use anyhow::Result;
2use chrono::Utc;
3use second_brain_core::sqlite_store::SqliteStore;
4use second_brain_sync::import::{ImportStats, import_values};
5
6pub struct Applied {
7    pub stats: ImportStats,
8    /// The highest seq of the source's log applied here without a gap.
9    pub max_seq: u64,
10    pub reached: u64,
11}
12
13/// Imports records from `source`'s log. Records kept from earlier imports
14/// are retried first; a record that still fails is kept, retried on later
15/// imports and holds the gapless seq below it until it applies or settles.
16/// A stream that names no source keeps the per-call watermark.
17pub fn import_from(
18    sqlite: &SqliteStore,
19    source: Option<&str>,
20    records: &[serde_json::Value],
21) -> Result<Applied> {
22    let now = Utc::now().timestamp_micros();
23    if let Some(source) = source {
24        let retried = sqlite.retry_failed_records(source, now)?;
25        if retried.applied + retried.settled > 0 {
26            tracing::info!(
27                source,
28                applied = retried.applied,
29                settled = retried.settled,
30                "retried kept sync records"
31            );
32        }
33    }
34    let (mut stats, watermark) = import_values(sqlite, records)?;
35    let reached = stats.reached.max(watermark);
36    let Some(source) = source else {
37        return Ok(Applied {
38            stats,
39            max_seq: watermark,
40            reached,
41        });
42    };
43    for (record, error) in std::mem::take(&mut stats.failed) {
44        sqlite.note_failed_record(source, &record, &error, now)?;
45    }
46    let max_seq = sqlite.applied_through(source, reached)?;
47    Ok(Applied {
48        stats,
49        max_seq,
50        reached,
51    })
52}