Skip to main content

hypercall_db/traits/
archiver.rs

1//! Archiver persistence traits (sync, r2d2 pool).
2//!
3//! These traits abstract the SQL layer for the db-archiver binary.
4//! All methods are synchronous because the archiver uses a blocking
5//! r2d2 connection pool.
6
7use anyhow::Result;
8use chrono::{DateTime, Utc};
9
10use crate::{
11    ArchivalBoundary, ArchivedCommand, ArchivedEvent, ArchivedOrderAction, ArchivedOrderUpdate,
12};
13
14/// Read operations for the archiver: boundary discovery and batch fetching.
15pub trait ArchiverReader: Send + Sync {
16    /// Find the safe archival boundary based on the engine snapshot's
17    /// `last_command_id` and the configured minimum age.
18    fn find_safe_boundary(&self, min_age_hours: i64) -> Result<ArchivalBoundary>;
19
20    /// Fetch a batch of engine events eligible for archival.
21    fn fetch_events_batch(
22        &self,
23        max_command_id: i64,
24        age_cutoff: &DateTime<Utc>,
25        batch_size: i64,
26        after_event_id: Option<i64>,
27    ) -> Result<Vec<ArchivedEvent>>;
28
29    /// Fetch engine commands that have no remaining engine events (orphans).
30    fn fetch_orphan_commands_batch(
31        &self,
32        max_command_id: i64,
33        age_cutoff: &DateTime<Utc>,
34        batch_size: i64,
35        after_command_id: Option<i64>,
36    ) -> Result<Vec<ArchivedCommand>>;
37
38    /// Fetch a batch of order actions eligible for archival.
39    fn fetch_order_actions_batch(
40        &self,
41        age_cutoff: &DateTime<Utc>,
42        batch_size: i64,
43        after_id: Option<i32>,
44    ) -> Result<Vec<ArchivedOrderAction>>;
45
46    /// Fetch a batch of order updates eligible for archival.
47    fn fetch_order_updates_batch(
48        &self,
49        age_cutoff: &DateTime<Utc>,
50        batch_size: i64,
51        after_id: Option<i32>,
52    ) -> Result<Vec<ArchivedOrderUpdate>>;
53}
54
55/// Write (delete) operations for the archiver: pruning archived rows.
56pub trait ArchiverWriter: Send + Sync {
57    /// Delete engine events by their IDs.
58    fn delete_events(&self, event_ids: &[i64]) -> Result<usize>;
59
60    /// Delete engine commands (and their digests) by their IDs.
61    fn delete_commands(&self, command_ids: &[i64]) -> Result<usize>;
62
63    /// Delete order actions by their IDs.
64    fn delete_order_actions(&self, ids: &[i32]) -> Result<usize>;
65
66    /// Delete order updates by their IDs.
67    fn delete_order_updates(&self, ids: &[i32]) -> Result<usize>;
68
69    /// Delete a batch of notifications older than `cutoff`.
70    fn delete_notifications_before(&self, cutoff: &DateTime<Utc>, batch_size: i64)
71        -> Result<usize>;
72
73    /// Delete notifications that exceed the per-user cap.
74    fn delete_notifications_over_per_user_cap(
75        &self,
76        max_per_user: i64,
77        batch_size: i64,
78    ) -> Result<usize>;
79}