Skip to main content

rostra_client_db/
social_post_scan_ops.rs

1use std::ops::Bound;
2
3use rostra_core::ShortEventId;
4use rostra_core::event::{EventContentUnsized, EventExt as _, EventKind, content_kind};
5use rostra_core::id::ToShort as _;
6
7use crate::social::EventPaginationCursor;
8use crate::social_post_scan::predecessor;
9use crate::{
10    Database, DbError, DbResult, SocialPostScanPage, SocialPostScanRequest, SocialPostScanRow,
11    SocialPostScanStop, content_store, events, events_content_state, social_posts_by_time,
12    social_posts_replaced_by,
13};
14
15impl Database {
16    /// Scan locally retained social posts in reverse authored-time order.
17    ///
18    /// Limits count examined index rows and raw CBOR bytes submitted to the
19    /// decoder, not accepted matches. Author/time filtering precedes decoding.
20    /// Replaced, unavailable and invalid posts are omitted just as in the
21    /// reverse timeline; all local authors (including self) are eligible.
22    /// Reply counts, replacement ancestry, attachments, private messages
23    /// and network reads are never accessed. There is no caller predicate:
24    /// matching and output policy belong to the caller.
25    ///
26    /// Each examined row requires only fixed-size index/envelope/state reads
27    /// and one replaced-by existence probe. Content is borrowed from
28    /// storage and its declared and stored lengths checked before owned
29    /// deserialization. The byte allowance is not an OS I/O, database
30    /// page-cache, decoder heap or CPU-time limit. Callers must bound their
31    /// requests, concurrency and output retention.
32    ///
33    /// A continuation is an inclusive range boundary, not necessarily an
34    /// existing key. Budget stops do not read ahead to establish
35    /// exhaustion. Oversized or byte-blocked candidates remain unconsumed
36    /// at their exact key and count as examined, but not decoded. Zero
37    /// row/result limits return immediately with the initial boundary; a
38    /// reversed time range is exhausted. Every call uses a fresh snapshot,
39    /// so concurrent edits/deletions can change later pages. Callers must
40    /// bind cursors to their account and filters.
41    ///
42    /// Storage and stored-record decoding errors propagate as errors; invalid
43    /// social-post CBOR is skipped and charged as a decode attempt.
44    pub async fn scan_social_posts_rev(
45        &self,
46        request: SocialPostScanRequest,
47    ) -> DbResult<SocialPostScanPage> {
48        self.read_with(|tx| {
49            let lower = EventPaginationCursor {
50                ts: request
51                    .authored_from
52                    .unwrap_or(rostra_core::Timestamp::ZERO),
53                event_id: ShortEventId::ZERO,
54            };
55            let upper =
56                request
57                    .cursor
58                    .unwrap_or(EventPaginationCursor::MAX)
59                    .min(EventPaginationCursor {
60                        ts: request
61                            .authored_through
62                            .unwrap_or(rostra_core::Timestamp::MAX),
63                        event_id: ShortEventId::MAX,
64                    });
65            let mut page = SocialPostScanPage {
66                rows: Vec::new(),
67                next_cursor: (lower <= upper).then_some(upper),
68                stop: SocialPostScanStop::Exhausted,
69                rows_scanned: 0,
70                content_bytes_decoded: 0,
71                contents_decoded: 0,
72            };
73            if lower > upper {
74                return Ok(page);
75            }
76            let index = tx.open_table(&social_posts_by_time::TABLE)?;
77            let events = tx.open_table(&events::TABLE)?;
78            let states = tx.open_table(&events_content_state::TABLE)?;
79            let replaced = tx.open_table(&social_posts_replaced_by::TABLE)?;
80            let store = tx.open_table(&content_store::TABLE)?;
81            let encode = |key: EventPaginationCursor| {
82                bincode::encode_to_vec((key.ts, key.event_id), redb_bincode::BINCODE_CONFIG)
83                    .map_err(|_| scan_invariant_error())
84            };
85            let lower_key = encode(lower)?;
86            let upper_key = encode(upper)?;
87            let mut entries = index
88                .as_raw()
89                .range::<&[u8]>((
90                    Bound::Included(lower_key.as_slice()),
91                    Bound::Included(upper_key.as_slice()),
92                ))?
93                .rev();
94            loop {
95                if page.next_cursor.is_none() {
96                    page.stop = SocialPostScanStop::Exhausted;
97                    break;
98                }
99                if page.rows.len() == request.max_records {
100                    page.stop = SocialPostScanStop::RecordBudget;
101                    break;
102                }
103                if page.rows_scanned == request.max_rows {
104                    page.stop = SocialPostScanStop::RowBudget;
105                    break;
106                }
107                let Some(entry) = entries.next() else {
108                    page.next_cursor = None;
109                    page.stop = SocialPostScanStop::Exhausted;
110                    break;
111                };
112                let (key, _) = entry?;
113                let ((ts, event_id), consumed) =
114                    bincode::decode_from_slice(key.value(), redb_bincode::BINCODE_CONFIG)?;
115                if consumed != key.value().len() {
116                    return Err(scan_invariant_error());
117                }
118                let cursor = EventPaginationCursor { ts, event_id };
119                page.rows_scanned += 1;
120                page.next_cursor = predecessor(cursor).filter(|cursor| *cursor >= lower);
121                let Some(event) = events
122                    .get(&event_id)?
123                    .map(|row| row.value_try())
124                    .transpose()?
125                else {
126                    continue;
127                };
128                if event.kind() != EventKind::SOCIAL_POST
129                    || event.timestamp() != ts
130                    || event.event().compute_id().to_short() != event_id
131                {
132                    return Err(scan_invariant_error());
133                }
134                let author = event.author();
135                if request.author.is_some_and(|wanted| wanted != author) {
136                    continue;
137                }
138                if Self::is_social_post_replaced_tx(author, event_id, &replaced)?
139                    || states
140                        .get(&event_id)?
141                        .map(|row| row.value_try())
142                        .transpose()?
143                        .is_some()
144                {
145                    continue;
146                }
147                let content_key = encode_content_hash_key(event.content_hash());
148                let Some(raw) = store.as_raw().get(content_key.as_slice())? else {
149                    continue;
150                };
151                let len = event.content_len() as usize;
152                let blocked = if len > request.max_content_bytes {
153                    Some(SocialPostScanStop::ContentTooLarge)
154                } else if len > request.max_content_bytes - page.content_bytes_decoded {
155                    Some(SocialPostScanStop::ContentByteBudget)
156                } else {
157                    None
158                };
159                if let Some(stop) = blocked {
160                    page.next_cursor = Some(cursor);
161                    page.stop = stop;
162                    break;
163                }
164                // ContentStoreRecord's single Cow field uses the borrowed
165                // EventContentUnsized encoding; no owned payload is needed here.
166                let (content, consumed) =
167                    bincode::borrow_decode_from_slice::<&EventContentUnsized, _>(
168                        raw.value(),
169                        redb_bincode::BINCODE_CONFIG,
170                    )?;
171                if consumed != raw.value().len() {
172                    return Err(bincode::error::DecodeError::Other(
173                        "Trailing bytes after encoded value",
174                    )
175                    .into());
176                }
177                if content.as_slice().len() != len {
178                    return Err(scan_invariant_error());
179                }
180                page.content_bytes_decoded += content.as_slice().len();
181                page.contents_decoded += 1;
182                let decoded = content.deserialize_cbor::<content_kind::SocialPost>();
183                let Ok(content) = decoded else {
184                    continue;
185                };
186                page.rows.push(SocialPostScanRow {
187                    ts,
188                    author,
189                    event_id,
190                    content,
191                });
192            }
193            Ok(page)
194        })
195        .await
196    }
197}
198
199pub(crate) fn encode_content_hash_key(hash: rostra_core::ContentHash) -> [u8; 32] {
200    let mut encoded = [0; 32];
201    let written = bincode::encode_into_slice(hash, &mut encoded, redb_bincode::BINCODE_CONFIG)
202        .expect("fixed-size content hash must fit its database key buffer");
203    assert_eq!(written, encoded.len());
204    encoded
205}
206
207fn scan_invariant_error() -> DbError {
208    bincode::error::DecodeError::Other("Social-post scan data does not match its retained event")
209        .into()
210}