skybase 0.1.11

The Open-Source Backend & Developer Platform for the AT Protocol (Firebase for ATProto, powered by skyauth)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
//! Embedded SQLite record store providing WAL storage, JSON1 indexing,
//! and reactive change notifications.
//!
//! Handles database initialization, schema migration, atomic upserts, soft-deletions,
//! point lookups, and raw SQL queries for the query builder.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;

use crate::error::{Result, SkybaseError};
use crate::index::broadcast::{BroadcastBus, ChangeNotification, DEFAULT_BROADCAST_CAPACITY};
use crate::index::query::QueryBuilder;

/// Input payload for creating or updating a record in [`RecordStore`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RecordInput {
    /// Canonical AT-URI (`at://{did}/{collection}/{rkey}`).
    pub uri: String,
    /// Content identifier CID string (e.g., `bafyreih3...`).
    pub cid: String,
    /// Decentralized identifier of the repository owner (`did:plc:...`).
    pub did: String,
    /// Lexicon NSID collection name (e.g., `app.bsky.feed.post`).
    pub collection: String,
    /// Record key within the collection (e.g., `3k6babc123`).
    pub rkey: String,
    /// Arbitrary Lexicon JSON payload.
    pub record_json: serde_json::Value,
    /// Monotonic timestamp in microseconds or milliseconds.
    pub indexed_at: u64,
}

impl RecordInput {
    /// Constructs a new [`RecordInput`], automatically generating the canonical `uri`
    /// from `did`, `collection`, and `rkey`.
    #[must_use]
    pub fn new(
        did: impl Into<String>,
        collection: impl Into<String>,
        rkey: impl Into<String>,
        cid: impl Into<String>,
        record_json: serde_json::Value,
        indexed_at: u64,
    ) -> Self {
        let did = did.into();
        let collection = collection.into();
        let rkey = rkey.into();
        let uri = format!("at://{did}/{collection}/{rkey}");
        Self {
            uri,
            cid: cid.into(),
            did,
            collection,
            rkey,
            record_json,
            indexed_at,
        }
    }

    /// Constructs a [`RecordInput`] with an explicit pre-formatted URI.
    #[must_use]
    pub fn with_uri(
        uri: impl Into<String>,
        cid: impl Into<String>,
        did: impl Into<String>,
        collection: impl Into<String>,
        rkey: impl Into<String>,
        record_json: serde_json::Value,
        indexed_at: u64,
    ) -> Self {
        Self {
            uri: uri.into(),
            cid: cid.into(),
            did: did.into(),
            collection: collection.into(),
            rkey: rkey.into(),
            record_json,
            indexed_at,
        }
    }
}

/// A canonical record row retrieved from [`RecordStore`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RecordRow {
    /// Canonical AT-URI (`at://{did}/{collection}/{rkey}`).
    pub uri: String,
    /// Content identifier CID string.
    pub cid: String,
    /// Decentralized identifier of the repository owner.
    pub did: String,
    /// Lexicon NSID collection name.
    pub collection: String,
    /// Record key within the collection.
    pub rkey: String,
    /// Arbitrary Lexicon JSON payload.
    pub record_json: serde_json::Value,
    /// Ingestion timestamp.
    pub indexed_at: u64,
    /// Soft-delete tombstone status (`true` if deleted).
    pub is_deleted: bool,
}

/// An operation to execute within an atomic batch on [`RecordStore`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum StoreOperation {
    /// Upsert a record.
    Upsert(RecordInput),
    /// Soft delete a record at the specified monotonic timestamp.
    Delete {
        /// Canonical AT-URI.
        uri: String,
        /// Monotonic timestamp in microseconds.
        indexed_at: u64,
    },
}

/// Statistics for operations applied in an atomic batch.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct BatchStats {
    /// Number of records inserted or updated.
    pub upserted: usize,
    /// Number of records transitioned to deleted.
    pub deleted: usize,
    /// Number of invalid records skipped.
    pub skipped: usize,
}

/// Configuration options for initializing a [`RecordStore`].
#[derive(Debug, Clone)]
pub struct RecordStoreConfig {
    /// Optional filesystem path to SQLite database. If `None`, `:memory:` is used.
    pub path: Option<PathBuf>,
    /// Capacity of the in-memory broadcast channel. Default: 1024.
    pub broadcast_capacity: usize,
    /// SQLite busy wait timeout in milliseconds. Default: 5000.
    pub busy_timeout_ms: u32,
}

impl Default for RecordStoreConfig {
    fn default() -> Self {
        Self {
            path: None,
            broadcast_capacity: DEFAULT_BROADCAST_CAPACITY,
            busy_timeout_ms: 5000,
        }
    }
}

impl RecordStoreConfig {
    /// Creates an in-memory database configuration.
    #[must_use]
    pub fn in_memory() -> Self {
        Self::default()
    }

    /// Creates a persistent database configuration pointing to a file path.
    #[must_use]
    pub fn persistent(path: impl Into<PathBuf>) -> Self {
        Self {
            path: Some(path.into()),
            ..Self::default()
        }
    }
}

/// Embedded SQLite record store providing WAL storage, JSON1 indexing,
/// and reactive change notifications.
#[derive(Clone)]
pub struct RecordStore {
    inner: Arc<RecordStoreInner>,
}

struct RecordStoreInner {
    conn: Mutex<rusqlite::Connection>,
    bus: BroadcastBus,
}

impl RecordStore {
    /// Opens or creates a persistent SQLite record store at the given path.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] if SQLite fails to open the database file.
    pub fn open(path: impl AsRef<Path>) -> Result<Self> {
        let config = RecordStoreConfig::persistent(path.as_ref());
        Self::with_config(config)
    }

    /// Opens or creates a persistent SQLite record store at the given path (alias for [`RecordStore::open`]).
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] if SQLite fails to open the database file.
    pub fn open_file_backed(path: impl AsRef<Path>) -> Result<Self> {
        Self::open(path)
    }

    /// Opens an isolated in-memory SQLite record store (ideal for unit and integration testing).
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] if SQLite initialization fails.
    pub fn open_in_memory() -> Result<Self> {
        let config = RecordStoreConfig::in_memory();
        Self::with_config(config)
    }

    /// Opens a record store with custom configuration.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] or [`SkybaseError::Config`] on initialization failure.
    pub fn with_config(config: RecordStoreConfig) -> Result<Self> {
        let conn = match &config.path {
            Some(path) => {
                if let Some(parent) = path.parent() {
                    if !parent.as_os_str().is_empty() {
                        std::fs::create_dir_all(parent).map_err(|e| {
                            SkybaseError::Storage(format!(
                                "Failed to create database directory: {e}"
                            ))
                        })?;
                    }
                }
                rusqlite::Connection::open(path)?
            }
            None => rusqlite::Connection::open_in_memory()?,
        };

        Self::apply_pragmas(&conn, config.busy_timeout_ms)?;
        Self::init_schema(&conn)?;

        let bus = BroadcastBus::new(config.broadcast_capacity)?;

        Ok(Self {
            inner: Arc::new(RecordStoreInner {
                conn: Mutex::new(conn),
                bus,
            }),
        })
    }

    fn apply_pragmas(conn: &rusqlite::Connection, busy_timeout_ms: u32) -> Result<()> {
        conn.pragma_update(None, "journal_mode", "WAL")?;
        conn.pragma_update(None, "synchronous", "NORMAL")?;
        conn.pragma_update(None, "busy_timeout", busy_timeout_ms)?;
        conn.pragma_update(None, "temp_store", "MEMORY")?;
        conn.pragma_update(None, "mmap_size", 268435456_i64)?;
        conn.pragma_update(None, "foreign_keys", "ON")?;
        Ok(())
    }

    fn init_schema(conn: &rusqlite::Connection) -> Result<()> {
        conn.execute_batch(
            "BEGIN;
             CREATE TABLE IF NOT EXISTS records (
                 uri TEXT PRIMARY KEY,
                 cid TEXT NOT NULL,
                 did TEXT NOT NULL,
                 collection TEXT NOT NULL,
                 rkey TEXT NOT NULL,
                 record_json TEXT NOT NULL,
                 indexed_at INTEGER NOT NULL,
                 is_deleted INTEGER NOT NULL DEFAULT 0
             );
             CREATE TABLE IF NOT EXISTS _skybase_meta (
                 key TEXT PRIMARY KEY,
                 value TEXT NOT NULL
             );
             CREATE INDEX IF NOT EXISTS idx_records_collection ON records(collection);
             CREATE INDEX IF NOT EXISTS idx_records_did ON records(did);
             CREATE INDEX IF NOT EXISTS idx_records_indexed_at ON records(indexed_at);
             CREATE INDEX IF NOT EXISTS idx_records_collection_active ON records(collection, is_deleted, indexed_at DESC);
             COMMIT;",
        )?;
        Ok(())
    }

    /// Inserts or updates a record atomically, unmarking any previous soft-delete,
    /// and emits an `Upsert` change notification if the record was inserted or updated.
    ///
    /// Stale updates (where `input.indexed_at < records.indexed_at`) are discarded to prevent
    /// out-of-order firehose event delivery from clobbering newer state.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] or [`SkybaseError::Serialization`] on error.
    pub fn upsert_record(&self, input: &RecordInput) -> Result<()> {
        let record_json_str =
            serde_json::to_string(&input.record_json).map_err(SkybaseError::Serialization)?;
        let indexed_at_i64 = i64::try_from(input.indexed_at)
            .map_err(|e| SkybaseError::Index(format!("indexed_at out of i64 range: {e}")))?;

        let rows_affected = {
            let conn = self.inner.conn.lock();
            let mut stmt = conn.prepare_cached(
                "INSERT INTO records (uri, cid, did, collection, rkey, record_json, indexed_at, is_deleted)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, 0)
                 ON CONFLICT(uri) DO UPDATE SET
                     cid = excluded.cid,
                     did = excluded.did,
                     collection = excluded.collection,
                     rkey = excluded.rkey,
                     record_json = excluded.record_json,
                     indexed_at = excluded.indexed_at,
                     is_deleted = 0
                 WHERE excluded.indexed_at >= records.indexed_at;",
            )?;

            stmt.execute(rusqlite::params![
                input.uri,
                input.cid,
                input.did,
                input.collection,
                input.rkey,
                record_json_str,
                indexed_at_i64,
            ])?
        };

        if rows_affected > 0 {
            let row = RecordRow {
                uri: input.uri.clone(),
                cid: input.cid.clone(),
                did: input.did.clone(),
                collection: input.collection.clone(),
                rkey: input.rkey.clone(),
                record_json: input.record_json.clone(),
                indexed_at: input.indexed_at,
                is_deleted: false,
            };
            self.inner.bus.publish_upsert(row);
        }

        Ok(())
    }

    /// Applies a batch of [`StoreOperation`] mutations atomically in exact arrival order.
    ///
    /// Stale creates and stale deletes are dropped via monotonic `indexed_at` LWW barriers.
    /// If `cursor` is provided, it is persisted to `_skybase_meta` within the same transaction.
    ///
    /// Invalid timestamps or JSON payloads are quarantined/skipped without aborting the batch.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database transaction failure.
    pub fn apply_operations_batch(
        &self,
        operations: &[StoreOperation],
        cursor: Option<u64>,
    ) -> Result<BatchStats> {
        if operations.is_empty() && cursor.is_none() {
            return Ok(BatchStats::default());
        }

        let mut stats = BatchStats::default();
        let mut notifications = Vec::new();

        {
            let mut conn = self.inner.conn.lock();
            let tx = conn.transaction()?;

            {
                let mut upsert_stmt = tx.prepare_cached(
                    "INSERT INTO records (uri, cid, did, collection, rkey, record_json, indexed_at, is_deleted)
                     VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, 0)
                     ON CONFLICT(uri) DO UPDATE SET
                         cid = excluded.cid,
                         did = excluded.did,
                         collection = excluded.collection,
                         rkey = excluded.rkey,
                         record_json = excluded.record_json,
                         indexed_at = excluded.indexed_at,
                         is_deleted = 0
                     WHERE excluded.indexed_at >= records.indexed_at;",
                )?;

                let mut delete_stmt = tx.prepare_cached(
                    "UPDATE records
                     SET is_deleted = 1,
                         indexed_at = ?2
                     WHERE uri = ?1 AND ?2 >= indexed_at AND is_deleted = 0
                     RETURNING did, collection, rkey;",
                )?;

                let mut advance_tombstone_stmt = tx.prepare_cached(
                    "UPDATE records
                     SET indexed_at = ?2
                     WHERE uri = ?1 AND ?2 > indexed_at AND is_deleted = 1;",
                )?;

                for op in operations {
                    match op {
                        StoreOperation::Upsert(input) => {
                            let record_json_str = match serde_json::to_string(&input.record_json) {
                                Ok(s) => s,
                                Err(err) => {
                                    stats.skipped += 1;
                                    tracing::warn!(uri = %input.uri, "Skipping upsert with invalid JSON in batch: {err}");
                                    continue;
                                }
                            };
                            let indexed_at_i64 = match i64::try_from(input.indexed_at) {
                                Ok(t) => t,
                                Err(err) => {
                                    stats.skipped += 1;
                                    tracing::warn!(uri = %input.uri, "Skipping upsert with out-of-range timestamp in batch: {err}");
                                    continue;
                                }
                            };

                            let rows_affected = upsert_stmt.execute(rusqlite::params![
                                input.uri,
                                input.cid,
                                input.did,
                                input.collection,
                                input.rkey,
                                record_json_str,
                                indexed_at_i64,
                            ])?;

                            if rows_affected > 0 {
                                stats.upserted += 1;
                                notifications.push(ChangeNotification::Upsert(RecordRow {
                                    uri: input.uri.clone(),
                                    cid: input.cid.clone(),
                                    did: input.did.clone(),
                                    collection: input.collection.clone(),
                                    rkey: input.rkey.clone(),
                                    record_json: input.record_json.clone(),
                                    indexed_at: input.indexed_at,
                                    is_deleted: false,
                                }));
                            }
                        }
                        StoreOperation::Delete { uri, indexed_at } => {
                            let indexed_at_i64 = match i64::try_from(*indexed_at) {
                                Ok(t) => t,
                                Err(err) => {
                                    stats.skipped += 1;
                                    tracing::warn!(uri = %uri, "Skipping delete with out-of-range timestamp in batch: {err}");
                                    continue;
                                }
                            };

                            let mut rows =
                                delete_stmt.query(rusqlite::params![uri, indexed_at_i64])?;
                            if let Some(row) = rows.next()? {
                                stats.deleted += 1;
                                let did: String = row.get(0)?;
                                let collection: String = row.get(1)?;
                                let rkey: String = row.get(2)?;
                                notifications.push(ChangeNotification::Delete {
                                    uri: uri.clone(),
                                    did,
                                    collection,
                                    rkey,
                                });
                            } else {
                                drop(rows);
                                let _ = advance_tombstone_stmt
                                    .execute(rusqlite::params![uri, indexed_at_i64])?;
                            }
                        }
                    }
                }

                if let Some(cur) = cursor {
                    let mut meta_stmt = tx.prepare_cached(
                        "INSERT INTO _skybase_meta (key, value) VALUES ('cursor', ?1)
                         ON CONFLICT(key) DO UPDATE SET value = excluded.value
                         WHERE CAST(excluded.value AS INTEGER) >= CAST(_skybase_meta.value AS INTEGER);",
                    )?;
                    meta_stmt.execute(rusqlite::params![cur.to_string()])?;
                }
            }

            tx.commit()?;
        }

        for notification in notifications {
            match notification {
                ChangeNotification::Upsert(row) => {
                    self.inner.bus.publish_upsert(row);
                }
                ChangeNotification::Delete {
                    uri,
                    did,
                    collection,
                    rkey,
                } => {
                    self.inner.bus.publish_delete(uri, did, collection, rkey);
                }
            }
        }

        Ok(stats)
    }

    /// Upserts a slice of records atomically in a single SQLite transaction.
    ///
    /// Records with invalid timestamps or JSON are skipped without aborting the transaction.
    /// Only records that successfully insert or advance state emit `Upsert` notifications.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database transaction failure.
    pub fn upsert_records_batch(&self, records: &[RecordInput]) -> Result<()> {
        for record in records {
            i64::try_from(record.indexed_at)
                .map_err(|e| SkybaseError::Index(format!("indexed_at out of i64 range: {e}")))?;
            serde_json::to_string(&record.record_json)?;
        }
        let ops: Vec<StoreOperation> = records
            .iter()
            .cloned()
            .map(StoreOperation::Upsert)
            .collect();
        self.apply_operations_batch(&ops, None)?;
        Ok(())
    }

    /// Soft-deletes multiple records atomically with monotonic LWW timestamps.
    ///
    /// Each item is a tuple of `(uri, indexed_at)`.
    /// Emits `Delete` notifications only for records that were actively transitioned to deleted.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database failure.
    pub fn soft_delete_records_batch(&self, items: &[(&str, u64)]) -> Result<usize> {
        let ops: Vec<StoreOperation> = items
            .iter()
            .map(|&(uri, indexed_at)| StoreOperation::Delete {
                uri: uri.to_string(),
                indexed_at,
            })
            .collect();
        let stats = self.apply_operations_batch(&ops, None)?;
        Ok(stats.deleted)
    }

    /// Soft-deletes a record by setting `is_deleted = 1` and updating `indexed_at`
    /// if `indexed_at >= records.indexed_at`.
    ///
    /// Emits a `Delete` notification if the record was active and transitioned to deleted.
    /// Stale delete calls (`indexed_at < records.indexed_at`) or repeated calls for already-deleted
    /// records are idempotent no-ops that return `Ok(false)`.
    ///
    /// Returns `true` if the record transitioned from active to deleted, `false` otherwise.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] or [`SkybaseError::Index`] on database failure.
    pub fn soft_delete_record(&self, uri: &str, indexed_at: u64) -> Result<bool> {
        let indexed_at_i64 = i64::try_from(indexed_at)
            .map_err(|e| SkybaseError::Index(format!("indexed_at out of i64 range: {e}")))?;

        let deleted_meta: Option<(String, String, String)> = {
            let conn = self.inner.conn.lock();
            let mut stmt = conn.prepare_cached(
                "UPDATE records
                 SET is_deleted = 1,
                     indexed_at = ?2
                 WHERE uri = ?1 AND ?2 >= indexed_at AND is_deleted = 0
                 RETURNING did, collection, rkey;",
            )?;

            let mut rows = stmt.query(rusqlite::params![uri, indexed_at_i64])?;

            if let Some(row) = rows.next()? {
                let did: String = row.get(0)?;
                let collection: String = row.get(1)?;
                let rkey: String = row.get(2)?;
                Some((did, collection, rkey))
            } else {
                drop(rows);
                drop(stmt);
                let mut advance_stmt = conn.prepare_cached(
                    "UPDATE records
                     SET indexed_at = ?2
                     WHERE uri = ?1 AND ?2 > indexed_at AND is_deleted = 1;",
                )?;
                let _ = advance_stmt.execute(rusqlite::params![uri, indexed_at_i64])?;
                None
            }
        };

        if let Some((did, collection, rkey)) = deleted_meta {
            self.inner.bus.publish_delete(uri, did, collection, rkey);
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Convenience soft-delete without explicit timestamp, setting `is_deleted = 1`
    /// without modifying `indexed_at`.
    ///
    /// Primarily intended for manual testing or administrative deletions.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database failure.
    pub fn soft_delete_record_unversioned(&self, uri: &str) -> Result<bool> {
        let deleted_meta: Option<(String, String, String)> = {
            let conn = self.inner.conn.lock();
            let mut stmt = conn.prepare_cached(
                "UPDATE records
                 SET is_deleted = 1
                 WHERE uri = ?1 AND is_deleted = 0
                 RETURNING did, collection, rkey;",
            )?;

            let mut rows = stmt.query(rusqlite::params![uri])?;

            if let Some(row) = rows.next()? {
                let did: String = row.get(0)?;
                let collection: String = row.get(1)?;
                let rkey: String = row.get(2)?;
                Some((did, collection, rkey))
            } else {
                None
            }
        };

        if let Some((did, collection, rkey)) = deleted_meta {
            self.inner.bus.publish_delete(uri, did, collection, rkey);
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Retrieves a metadata string value by key from `_skybase_meta`.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database error.
    pub fn get_meta(&self, key: &str) -> Result<Option<String>> {
        let conn = self.inner.conn.lock();
        let mut stmt = conn.prepare_cached("SELECT value FROM _skybase_meta WHERE key = ?1;")?;
        let mut rows = stmt.query(rusqlite::params![key])?;
        if let Some(row) = rows.next()? {
            let val: String = row.get(0)?;
            Ok(Some(val))
        } else {
            Ok(None)
        }
    }

    /// Sets a metadata string value by key in `_skybase_meta`.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database error.
    pub fn set_meta(&self, key: &str, value: &str) -> Result<()> {
        let conn = self.inner.conn.lock();
        let mut stmt = conn.prepare_cached(
            "INSERT INTO _skybase_meta (key, value) VALUES (?1, ?2)
             ON CONFLICT(key) DO UPDATE SET value = excluded.value;",
        )?;
        stmt.execute(rusqlite::params![key, value])?;
        Ok(())
    }

    /// Retrieves the persisted Jetstream sequence cursor from SQLite.
    ///
    /// Returns `None` if no cursor has been recorded yet.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database or parsing error.
    pub fn get_cursor(&self) -> Result<Option<u64>> {
        match self.get_meta("cursor")? {
            Some(s) => {
                let cursor = s.parse::<u64>().map_err(|e| {
                    SkybaseError::Storage(format!("Corrupted cursor value in _skybase_meta: {e}"))
                })?;
                Ok(Some(cursor))
            }
            None => Ok(None),
        }
    }

    /// Persists the Jetstream sequence cursor to SQLite monotonically.
    ///
    /// If a larger cursor is already stored, this call is a no-op.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database error.
    pub fn set_cursor(&self, cursor: u64) -> Result<()> {
        let conn = self.inner.conn.lock();
        let mut stmt = conn.prepare_cached(
            "INSERT INTO _skybase_meta (key, value) VALUES ('cursor', ?1)
             ON CONFLICT(key) DO UPDATE SET value = excluded.value
             WHERE CAST(excluded.value AS INTEGER) >= CAST(_skybase_meta.value AS INTEGER);",
        )?;
        stmt.execute(rusqlite::params![cursor.to_string()])?;
        Ok(())
    }

    /// Retrieves an active (non-deleted) record by its canonical URI.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] or [`SkybaseError::Serialization`] on error.
    pub fn get_record(&self, uri: &str) -> Result<Option<RecordRow>> {
        let conn = self.inner.conn.lock();
        let mut stmt = conn.prepare_cached(
            "SELECT uri, cid, did, collection, rkey, record_json, indexed_at, is_deleted
             FROM records
             WHERE uri = ?1 AND is_deleted = 0;",
        )?;

        let mut rows = stmt.query(rusqlite::params![uri])?;
        if let Some(row) = rows.next()? {
            let record = map_record_row(row)?;
            Ok(Some(record))
        } else {
            Ok(None)
        }
    }

    /// Retrieves a record by URI regardless of its soft-delete status.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] or [`SkybaseError::Serialization`] on error.
    pub fn get_record_including_deleted(&self, uri: &str) -> Result<Option<RecordRow>> {
        let conn = self.inner.conn.lock();
        let mut stmt = conn.prepare_cached(
            "SELECT uri, cid, did, collection, rkey, record_json, indexed_at, is_deleted
             FROM records
             WHERE uri = ?1;",
        )?;

        let mut rows = stmt.query(rusqlite::params![uri])?;
        if let Some(row) = rows.next()? {
            let record = map_record_row(row)?;
            Ok(Some(record))
        } else {
            Ok(None)
        }
    }

    /// Physically deletes a record from the database (hard delete).
    ///
    /// Returns `true` if a record was removed, or `false` if it did not exist.
    /// Emits a `Delete` notification if a record was removed.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] on database failure.
    pub fn hard_delete_record(&self, uri: &str) -> Result<bool> {
        let deleted_meta: Option<(String, String, String)> = {
            let conn = self.inner.conn.lock();
            let mut stmt = conn.prepare_cached(
                "DELETE FROM records WHERE uri = ?1 RETURNING did, collection, rkey;",
            )?;
            let mut rows = stmt.query(rusqlite::params![uri])?;
            if let Some(row) = rows.next()? {
                let did: String = row.get(0)?;
                let collection: String = row.get(1)?;
                let rkey: String = row.get(2)?;
                Some((did, collection, rkey))
            } else {
                None
            }
        };

        if let Some((did, collection, rkey)) = deleted_meta {
            self.inner.bus.publish_delete(uri, did, collection, rkey);
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Subscribes to the live change notification broadcast bus.
    #[must_use]
    pub fn subscribe(&self) -> broadcast::Receiver<ChangeNotification> {
        self.inner.bus.subscribe()
    }

    /// Returns a reference to the internal [`BroadcastBus`].
    #[must_use]
    pub fn broadcast_bus(&self) -> &BroadcastBus {
        &self.inner.bus
    }

    /// Initiates a structured [`QueryBuilder`] scoped to the given collection NSID.
    #[must_use]
    pub fn query(&self, collection: impl Into<String>) -> QueryBuilder<'_> {
        QueryBuilder::new(self, collection)
    }

    /// Initiates a structured [`QueryBuilder`] scoped to the given collection NSID (alias for [`query`](Self::query)).
    #[must_use]
    pub fn collection(&self, collection: impl Into<String>) -> QueryBuilder<'_> {
        self.query(collection)
    }

    /// Executes a raw SQL query with parameters and returns hydrated [`RecordRow`] items.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] or [`SkybaseError::Serialization`] on error.
    pub(crate) fn query_raw(
        &self,
        sql: &str,
        params: &[rusqlite::types::Value],
    ) -> Result<Vec<RecordRow>> {
        let conn = self.inner.conn.lock();
        let mut stmt = conn.prepare(sql)?;
        let mut rows = stmt.query(rusqlite::params_from_iter(params.iter()))?;
        let mut results = Vec::new();
        while let Some(row) = rows.next()? {
            results.push(map_record_row(row)?);
        }
        Ok(results)
    }

    /// Executes a closure with a reference to the underlying SQLite connection.
    ///
    /// # Errors
    /// Returns the result of `f`, or [`SkybaseError::Storage`] on internal error.
    #[doc(hidden)]
    pub fn with_conn<R, F>(&self, f: F) -> Result<R>
    where
        F: FnOnce(&rusqlite::Connection) -> Result<R>,
    {
        let conn = self.inner.conn.lock();
        f(&conn)
    }
}

/// Helper function to parse `did`, `collection`, and `rkey` from a canonical `at://` URI.
#[must_use]
pub fn parse_at_uri(uri: &str) -> Option<(String, String, String)> {
    let stripped = uri.strip_prefix("at://")?;
    let mut parts = stripped.split('/');
    let did = parts.next()?;
    let collection = parts.next()?;
    let rkey = parts.next()?;
    if parts.next().is_some() || did.is_empty() || collection.is_empty() || rkey.is_empty() {
        return None;
    }
    Some((did.to_string(), collection.to_string(), rkey.to_string()))
}

/// Maps a rusqlite row containing the 8 canonical record columns to a [`RecordRow`].
pub(crate) fn map_record_row(row: &rusqlite::Row<'_>) -> Result<RecordRow> {
    let uri: String = row.get(0)?;
    let cid: String = row.get(1)?;
    let did: String = row.get(2)?;
    let collection: String = row.get(3)?;
    let rkey: String = row.get(4)?;
    let record_json_str: String = row.get(5)?;
    let indexed_at_i64: i64 = row.get(6)?;
    let is_deleted_i64: i64 = row.get(7)?;

    let record_json: serde_json::Value =
        serde_json::from_str(&record_json_str).map_err(SkybaseError::Serialization)?;

    let indexed_at = u64::try_from(indexed_at_i64)
        .map_err(|e| SkybaseError::Index(format!("negative timestamp in database: {e}")))?;

    Ok(RecordRow {
        uri,
        cid,
        did,
        collection,
        rkey,
        record_json,
        indexed_at,
        is_deleted: is_deleted_i64 != 0,
    })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
    use super::*;

    #[test]
    fn test_open_in_memory_and_schema() {
        let store = RecordStore::open_in_memory().unwrap();
        let table_exists: bool = store
            .with_conn(|conn| {
                let count: i64 = conn.query_row(
                    "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='records'",
                    [],
                    |r| r.get(0),
                )?;
                Ok(count == 1)
            })
            .unwrap();
        assert!(table_exists);

        let index_count: i64 = store
            .with_conn(|conn| {
                let count: i64 = conn.query_row(
                    "SELECT count(*) FROM sqlite_master WHERE type='index' AND tbl_name='records'",
                    [],
                    |r| r.get(0),
                )?;
                Ok(count)
            })
            .unwrap();
        assert!(index_count >= 3);
    }

    #[test]
    fn test_upsert_and_get_record() {
        let store = RecordStore::open_in_memory().unwrap();
        let input = RecordInput::new(
            "did:plc:alice",
            "app.bsky.feed.post",
            "3k6b123",
            "bafyreih1",
            serde_json::json!({"text": "Hello Bluesky!"}),
            1_700_000_000,
        );

        store.upsert_record(&input).unwrap();

        let row = store
            .get_record(&input.uri)
            .unwrap()
            .expect("Record should exist");
        assert_eq!(row.uri, "at://did:plc:alice/app.bsky.feed.post/3k6b123");
        assert_eq!(row.cid, "bafyreih1");
        assert_eq!(row.did, "did:plc:alice");
        assert_eq!(row.collection, "app.bsky.feed.post");
        assert_eq!(row.rkey, "3k6b123");
        assert_eq!(row.record_json["text"], "Hello Bluesky!");
        assert_eq!(row.indexed_at, 1_700_000_000);
        assert!(!row.is_deleted);
    }

    #[test]
    fn test_upsert_conflict_update_and_resurrection() {
        let store = RecordStore::open_in_memory().unwrap();
        let input = RecordInput::new(
            "did:plc:bob",
            "app.bsky.feed.post",
            "1",
            "cid1",
            serde_json::json!({"v": 1}),
            100,
        );
        store.upsert_record(&input).unwrap();

        // Soft delete with LWW timestamp 150 (greater than 100)
        let deleted = store.soft_delete_record(&input.uri, 150).unwrap();
        assert!(deleted);
        assert!(store.get_record(&input.uri).unwrap().is_none());
        assert!(
            store
                .get_record_including_deleted(&input.uri)
                .unwrap()
                .unwrap()
                .is_deleted
        );

        // Re-upsert (resurrection with newer timestamp 200 >= 150)
        let input2 = RecordInput::new(
            "did:plc:bob",
            "app.bsky.feed.post",
            "1",
            "cid2",
            serde_json::json!({"v": 2}),
            200,
        );
        store.upsert_record(&input2).unwrap();

        let row = store
            .get_record(&input.uri)
            .unwrap()
            .expect("Resurrected record should exist");
        assert_eq!(row.cid, "cid2");
        assert_eq!(row.record_json["v"], 2);
        assert_eq!(row.indexed_at, 200);
        assert!(!row.is_deleted);
    }

    #[test]
    fn test_broadcast_notifications() {
        let store = RecordStore::open_in_memory().unwrap();
        let mut rx = store.subscribe();

        let input = RecordInput::new(
            "did:plc:carol",
            "app.bsky.feed.post",
            "xyz",
            "cid1",
            serde_json::json!({}),
            10,
        );
        store.upsert_record(&input).unwrap();

        let event = rx.try_recv().expect("Should receive upsert notification");
        match event {
            ChangeNotification::Upsert(row) => assert_eq!(row.uri, input.uri),
            _ => panic!("Expected Upsert event"),
        }

        store.soft_delete_record(&input.uri, 20).unwrap();
        let event2 = rx.try_recv().expect("Should receive delete notification");
        match event2 {
            ChangeNotification::Delete {
                uri,
                did,
                collection,
                rkey,
            } => {
                assert_eq!(uri, input.uri);
                assert_eq!(did, "did:plc:carol");
                assert_eq!(collection, "app.bsky.feed.post");
                assert_eq!(rkey, "xyz");
            }
            _ => panic!("Expected Delete event"),
        }
    }

    #[test]
    fn test_zero_subscribers_safe() {
        let store = RecordStore::open_in_memory().unwrap();
        let input = RecordInput::new(
            "did:plc:dave",
            "app.bsky.feed.post",
            "1",
            "cid",
            serde_json::json!({}),
            1,
        );
        assert!(store.upsert_record(&input).is_ok());
        assert!(store.soft_delete_record(&input.uri, 2).is_ok());
    }

    #[test]
    fn test_batch_upsert() {
        let store = RecordStore::open_in_memory().unwrap();
        let mut records = Vec::new();
        for i in 0..50 {
            records.push(RecordInput::new(
                "did:plc:bulk",
                "app.bsky.feed.post",
                format!("{i}"),
                format!("cid{i}"),
                serde_json::json!({"seq": i}),
                1000 + i,
            ));
        }

        store.upsert_records_batch(&records).unwrap();

        for i in 0..50 {
            let uri = format!("at://did:plc:bulk/app.bsky.feed.post/{i}");
            let row = store
                .get_record(&uri)
                .unwrap()
                .expect("Batch row should exist");
            assert_eq!(row.record_json["seq"], i);
        }
    }

    #[test]
    fn test_hard_delete() {
        let store = RecordStore::open_in_memory().unwrap();
        let input = RecordInput::new(
            "did:plc:hard",
            "app.bsky.feed.post",
            "1",
            "cid",
            serde_json::json!({}),
            1,
        );
        store.upsert_record(&input).unwrap();
        assert!(store.get_record(&input.uri).unwrap().is_some());

        let deleted = store.hard_delete_record(&input.uri).unwrap();
        assert!(deleted);
        assert!(store
            .get_record_including_deleted(&input.uri)
            .unwrap()
            .is_none());

        let deleted_again = store.hard_delete_record(&input.uri).unwrap();
        assert!(!deleted_again);
    }

    #[test]
    fn test_parse_at_uri() {
        let (did, coll, rkey) =
            parse_at_uri("at://did:plc:alice/app.bsky.feed.post/3k6b123").unwrap();
        assert_eq!(did, "did:plc:alice");
        assert_eq!(coll, "app.bsky.feed.post");
        assert_eq!(rkey, "3k6b123");

        assert!(parse_at_uri("https://example.com").is_none());
        assert!(parse_at_uri("at://did:plc:alice").is_none());
        assert!(parse_at_uri("at://did:plc:alice/collection").is_none());
        assert!(parse_at_uri("at://did:plc:alice/collection/rkey/extra").is_none());
    }

    #[test]
    fn test_wal_pragma_verification_on_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test_wal.db");
        let store = RecordStore::open(&db_path).unwrap();

        store
            .with_conn(|conn| {
                let journal_mode: String =
                    conn.query_row("PRAGMA journal_mode;", [], |r| r.get(0))?;
                assert_eq!(journal_mode.to_lowercase(), "wal");

                let busy_timeout: u32 = conn.query_row("PRAGMA busy_timeout;", [], |r| r.get(0))?;
                assert_eq!(busy_timeout, 5000);
                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn test_soft_delete_monotonic_barrier() {
        let store = RecordStore::open_in_memory().unwrap();
        let input = RecordInput::new(
            "did:plc:lww",
            "app.bsky.feed.post",
            "post_1",
            "cid_1",
            serde_json::json!({"text": "original"}),
            100,
        );
        store.upsert_record(&input).unwrap();

        // 1. Soft delete at t=200
        let res = store.soft_delete_record(&input.uri, 200).unwrap();
        assert!(res, "Should successfully soft delete");
        assert!(store.get_record(&input.uri).unwrap().is_none());

        // 2. Replayed create at t=100 (stale) must NOT resurrect the record
        store.upsert_record(&input).unwrap();
        assert!(
            store.get_record(&input.uri).unwrap().is_none(),
            "Stale create at t=100 must not resurrect record deleted at t=200"
        );

        // 3. Stale delete at t=150 must be a no-op and return false
        let stale_delete = store.soft_delete_record(&input.uri, 150).unwrap();
        assert!(!stale_delete, "Stale delete must return false");

        // 4. Newer create at t=300 resurrects the record
        let input_new = RecordInput::new(
            "did:plc:lww",
            "app.bsky.feed.post",
            "post_1",
            "cid_2",
            serde_json::json!({"text": "resurrected"}),
            300,
        );
        store.upsert_record(&input_new).unwrap();
        let row = store
            .get_record(&input.uri)
            .unwrap()
            .expect("Newer create at t=300 must resurrect");
        assert_eq!(row.cid, "cid_2");
        assert_eq!(row.indexed_at, 300);
    }

    #[test]
    fn test_persisted_cursor_and_meta() {
        let store = RecordStore::open_in_memory().unwrap();
        assert_eq!(store.get_cursor().unwrap(), None);

        store.set_cursor(1_700_000_123_456).unwrap();
        assert_eq!(store.get_cursor().unwrap(), Some(1_700_000_123_456));

        // Update cursor
        store.set_cursor(1_700_000_999_999).unwrap();
        assert_eq!(store.get_cursor().unwrap(), Some(1_700_000_999_999));

        // Arbitrary meta
        assert_eq!(store.get_meta("some_key").unwrap(), None);
        store.set_meta("some_key", "some_value").unwrap();
        assert_eq!(
            store.get_meta("some_key").unwrap(),
            Some("some_value".to_string())
        );
    }

    #[test]
    fn test_apply_operations_batch_order_and_quarantine() {
        let store = RecordStore::open_in_memory().unwrap();
        let uri = "at://did:plc:batch/app.bsky.feed.post/1";

        let op1 = StoreOperation::Upsert(RecordInput::new(
            "did:plc:batch",
            "app.bsky.feed.post",
            "1",
            "cid1",
            serde_json::json!({"v": 1}),
            100,
        ));
        let op2 = StoreOperation::Delete {
            uri: uri.to_string(),
            indexed_at: 200,
        };
        let op3 = StoreOperation::Upsert(RecordInput::new(
            "did:plc:batch",
            "app.bsky.feed.post",
            "1",
            "cid2",
            serde_json::json!({"v": 2}),
            300,
        ));

        // Execute batch in exact arrival order
        let stats = store
            .apply_operations_batch(&[op1, op2, op3], Some(500))
            .unwrap();

        assert_eq!(stats.upserted, 2);
        assert_eq!(stats.deleted, 1);
        assert_eq!(stats.skipped, 0);

        // Record must be alive with v=2 at t=300
        let record = store
            .get_record(uri)
            .unwrap()
            .expect("Record must be alive");
        assert_eq!(record.cid, "cid2");
        assert_eq!(record.indexed_at, 300);

        // Cursor must be persisted
        assert_eq!(store.get_cursor().unwrap(), Some(500));
    }
}