disk_backed_queue 0.1.3

A robust, crash-resistant queue implementation that persists all data to disk using SQLite
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
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
//! # disk-backed-queue
//!
//! A robust, crash-resistant queue implementation that persists all data to disk using SQLite.
//! Provides an mpsc-like channel API while ensuring messages survive application restarts and
//! system failures.
//!
//! ## Features
//!
//! - **Zero Message Loss**: All messages are persisted to SQLite before acknowledgment
//! - **Crash Recovery**: Messages survive application restarts and system crashes
//! - **MPSC-like API**: Familiar channel-based interface compatible with async Rust
//! - **Dead Letter Queue**: Corrupted messages automatically moved to separate database
//! - **Batch Operations**: High-performance bulk send/receive (~50x faster than single operations)
//! - **Backpressure Support**: Optional queue size limits prevent unbounded growth
//!
//! ## Quick Start
//!
//! ```no_run
//! use disk_backed_queue::disk_backed_channel;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct MyMessage {
//!     id: u64,
//!     content: String,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create a disk-backed channel
//!     let (tx, mut rx) = disk_backed_channel::<MyMessage, _>(
//!         "my_queue.db",
//!         "messages".to_string(),
//!         None  // No size limit
//!     ).await?;
//!
//!     // Send messages
//!     tx.send(MyMessage {
//!         id: 1,
//!         content: "Hello, world!".to_string(),
//!     }).await?;
//!
//!     // Receive messages
//!     if let Some(msg) = rx.recv().await? {
//!         println!("Received: {:?}", msg);
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Use Cases
//!
//! Perfect for scenarios where message loss is unacceptable:
//!
//! - Message queuing during database outages
//! - Event sourcing and audit logs
//! - Job queues that survive restarts
//! - Data pipelines requiring guaranteed delivery
//!
//! ## Performance
//!
//! Batch operations are approximately **50x faster** than single operations due to
//! reduced fsync overhead. For high-throughput scenarios, use [`DiskBackedSender::send_batch`]
//! and [`DiskBackedReceiver::recv_batch`].
//!
//! Run `cargo run --example throughput_demo --release` to see performance on your system.

use rusqlite::OptionalExtension;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tracing::{debug, error, instrument, warn};

#[derive(Debug)]
pub enum DiskQueueError {
    Database(rusqlite::Error),
    Serialization(postcard::Error),
    Deserialization(postcard::Error),
    InvalidTableName(String),
    TaskJoin(String),
    /// A worker thread panicked and poisoned an internal lock. Always indicates a bug.
    LockPoisoned(String),
    /// A corrupt item could not be moved to the dead letter queue. The original item is left
    /// in the main queue (so it can be retried) and the underlying error is reported here.
    DlqWriteFailed(String),
    QueueClosed,
    UnexpectedRowCount(String),
    QueueFull(usize),
}

impl std::fmt::Display for DiskQueueError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Database(e) => write!(f, "Database error: {e}"),
            Self::Serialization(e) => write!(f, "Serialization error: {e}"),
            Self::Deserialization(e) => write!(f, "Deserialization error: {e}"),
            Self::InvalidTableName(s) => write!(f, "Invalid table name: {s}"),
            Self::TaskJoin(s) => write!(f, "Internal task error: {s}"),
            Self::LockPoisoned(s) => write!(f, "Internal lock poisoned: {s}"),
            Self::DlqWriteFailed(s) => write!(f, "Failed to write to dead letter queue: {s}"),
            Self::QueueClosed => f.write_str("Queue is closed"),
            Self::UnexpectedRowCount(s) => write!(f, "Unexpected row count: {s}"),
            Self::QueueFull(n) => write!(f, "Queue is full (max size: {n})"),
        }
    }
}

impl std::error::Error for DiskQueueError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Database(e) => Some(e),
            Self::Serialization(e) | Self::Deserialization(e) => Some(e),
            _ => None,
        }
    }
}

impl From<rusqlite::Error> for DiskQueueError {
    fn from(e: rusqlite::Error) -> Self {
        Self::Database(e)
    }
}

pub type Result<T> = std::result::Result<T, DiskQueueError>;

/// Lock the SQLite mutex, mapping a poisoned lock to [`DiskQueueError::LockPoisoned`].
///
/// Mutex poisoning means a previous holder panicked while holding the lock; the database
/// state is intact (SQLite is transactional) but the panic itself is a bug worth surfacing.
fn lock_conn(
    db: &Mutex<rusqlite::Connection>,
) -> Result<std::sync::MutexGuard<'_, rusqlite::Connection>> {
    db.lock().map_err(|e| {
        error!("Internal lock poisoned: {}", e);
        DiskQueueError::LockPoisoned(e.to_string())
    })
}

/// Durability configuration for SQLite synchronous mode
///
/// This controls the trade-off between durability and performance.
#[derive(Debug, Clone, Copy, Default)]
pub enum DurabilityLevel {
    /// OFF (0) - No fsync, fastest but data loss on power failure
    /// **WARNING**: Only use for temporary/cache data where loss is acceptable
    Off,

    /// NORMAL (1) - Fsync only at critical moments (WAL checkpoints)
    /// Safe in WAL mode for most crashes, but power loss during checkpoint can corrupt
    /// Good balance of performance and safety for WAL mode
    Normal,

    /// FULL (2) - Fsync after every commit (DEFAULT)
    /// Maximum durability - survives power loss at any moment
    /// Recommended for critical data where message loss is unacceptable
    #[default]
    Full,

    /// EXTRA (3) - Even more paranoid than FULL
    /// Additional fsyncs for maximum durability
    /// Rarely needed, significant performance impact
    Extra,
}

impl DurabilityLevel {
    fn as_str(&self) -> &'static str {
        match self {
            DurabilityLevel::Off => "OFF",
            DurabilityLevel::Normal => "NORMAL",
            DurabilityLevel::Full => "FULL",
            DurabilityLevel::Extra => "EXTRA",
        }
    }
}

/// Cached SQL query strings for the queue operations.
///
/// Pre-formatted SQL queries are cached to avoid repeated string formatting
/// on every operation. The table name is validated and interpolated once at
/// queue creation time, ensuring SQL injection safety.
#[derive(Debug)]
struct CachedQueries {
    insert_sql: String,
    select_sql: String,
    delete_sql: String,
    count_sql: String,
    clear_sql: String,
}

/// A disk-backed queue that persists all messages to SQLite.
///
/// This is the core queue implementation. For most use cases, prefer using
/// [`disk_backed_channel`] which provides a more ergonomic sender/receiver API.
///
/// All operations are async and use `spawn_blocking` internally to avoid blocking
/// the async executor.
///
/// # Type Parameters
///
/// * `T` - The message type. Must implement `Serialize + Deserialize + Send + Sync + 'static`.
///
/// # Example
///
/// ```no_run
/// use disk_backed_queue::DiskBackedQueue;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// struct Message {
///     data: String,
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let queue = DiskBackedQueue::new(
///         "queue.db",
///         "messages".to_string(),
///         None,
///     ).await?;
///
///     queue.send(Message { data: "hello".to_string() }).await?;
///     let msg = queue.recv().await?;
///
///     Ok(())
/// }
/// ```
pub struct DiskBackedQueue<T> {
    db: Arc<Mutex<rusqlite::Connection>>,
    dlq_db: Arc<Mutex<rusqlite::Connection>>,
    queries: CachedQueries,
    dlq_insert_sql: String,
    table_name: String,
    max_size: Option<usize>,
    _phantom: PhantomData<T>,
}

impl<T> std::fmt::Debug for DiskBackedQueue<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DiskBackedQueue")
            .field("table_name", &self.table_name)
            .field("max_size", &self.max_size)
            .field("queries", &self.queries)
            .finish_non_exhaustive()
    }
}

impl<T> DiskBackedQueue<T>
where
    T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
{
    /// Create a new disk-backed queue with default durability (FULL)
    ///
    /// This is the recommended constructor for most use cases.
    #[instrument(skip_all, fields(db_path = %db_path.as_ref().display(), table_name = %table_name))]
    pub async fn new<P: AsRef<Path>>(
        db_path: P,
        table_name: String,
        max_size: Option<usize>,
    ) -> Result<Self> {
        Self::with_durability(db_path, table_name, max_size, DurabilityLevel::default()).await
    }

    /// Create a new disk-backed queue with custom durability level
    ///
    /// # Durability Levels
    ///
    /// - `DurabilityLevel::Full` (default) - Maximum safety, fsync after every commit
    /// - `DurabilityLevel::Normal` - Good balance in WAL mode, fsync at checkpoints
    /// - `DurabilityLevel::Off` - Fastest, but data loss on power failure (not recommended)
    /// - `DurabilityLevel::Extra` - Paranoid mode, additional fsyncs (rarely needed)
    ///
    /// # Example
    ///
    /// ```ignore
    /// // For critical data (default)
    /// let queue = DiskBackedQueue::with_durability(
    ///     "critical.db",
    ///     "messages".to_string(),
    ///     None,
    ///     DurabilityLevel::Full,
    /// ).await?;
    ///
    /// // For high-performance temporary data
    /// let queue = DiskBackedQueue::with_durability(
    ///     "cache.db",
    ///     "temp".to_string(),
    ///     None,
    ///     DurabilityLevel::Normal,
    /// ).await?;
    /// ```
    #[instrument(skip_all, fields(db_path = %db_path.as_ref().display(), table_name = %table_name, durability = ?durability))]
    pub async fn with_durability<P: AsRef<Path>>(
        db_path: P,
        table_name: String,
        max_size: Option<usize>,
        durability: DurabilityLevel,
    ) -> Result<Self> {
        // Validate table name to prevent SQL injection
        if table_name.is_empty() || table_name.len() > 128 {
            return Err(DiskQueueError::InvalidTableName(table_name));
        }
        if !table_name.chars().all(|c| c.is_alphanumeric() || c == '_') {
            return Err(DiskQueueError::InvalidTableName(table_name));
        }

        let conn = rusqlite::Connection::open(&db_path).map_err(|e| {
            error!(
                "Failed to open SQLite database at {}: {}",
                db_path.as_ref().display(),
                e
            );
            e
        })?;

        // Enable WAL mode for better concurrency and crash safety
        conn.pragma_update(None, "journal_mode", "WAL")
            .map_err(|e| {
                error!("Failed to enable WAL mode: {}", e);
                e
            })?;

        // Set synchronous mode for durability guarantees
        conn.pragma_update(None, "synchronous", durability.as_str())
            .map_err(|e| {
                error!(
                    "Failed to set synchronous mode to {}: {}",
                    durability.as_str(),
                    e
                );
                e
            })?;

        debug!(
            "Configured SQLite with WAL mode and synchronous={}",
            durability.as_str()
        );

        // Enable foreign key constraints
        conn.pragma_update(None, "foreign_keys", true)
            .map_err(|e| {
                error!("Failed to enable foreign keys: {}", e);
                e
            })?;

        // Set busy timeout to handle concurrent access
        conn.busy_timeout(Duration::from_secs(5)).map_err(|e| {
            error!("Failed to set busy timeout: {}", e);
            e
        })?;

        // Create table if it doesn't exist
        let create_table_sql = format!(
            "CREATE TABLE IF NOT EXISTS {table_name} (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                data BLOB NOT NULL,
                created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))
            )"
        );
        conn.execute(&create_table_sql, []).map_err(|e| {
            error!("Failed to create table {}: {}", table_name, e);
            e
        })?;

        // Create index on created_at for efficient ordering
        let create_index_sql = format!(
            "CREATE INDEX IF NOT EXISTS idx_{table_name}_created_at ON {table_name} (created_at, id)"
        );
        conn.execute(&create_index_sql, []).map_err(|e| {
            error!("Failed to create index for table {}: {}", table_name, e);
            e
        })?;

        debug!(
            "Successfully initialized disk-backed queue table: {}",
            table_name
        );

        // Open dead letter queue database
        let dlq_path = db_path.as_ref().with_extension("dlq.db");
        let dlq_conn = rusqlite::Connection::open(&dlq_path).map_err(|e| {
            error!(
                "Failed to open DLQ database at {}: {}",
                dlq_path.display(),
                e
            );
            e
        })?;

        // Configure DLQ database with same durability settings
        dlq_conn
            .pragma_update(None, "journal_mode", "WAL")
            .map_err(|e| {
                error!("Failed to enable WAL mode for DLQ: {}", e);
                e
            })?;

        dlq_conn
            .pragma_update(None, "synchronous", durability.as_str())
            .map_err(|e| {
                error!(
                    "Failed to set synchronous mode for DLQ to {}: {}",
                    durability.as_str(),
                    e
                );
                e
            })?;

        dlq_conn.busy_timeout(Duration::from_secs(5)).map_err(|e| {
            error!("Failed to set busy timeout for DLQ: {}", e);
            e
        })?;

        // Create DLQ table with error information
        let dlq_table_sql = format!(
            "CREATE TABLE IF NOT EXISTS {table_name}_dlq (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                original_id INTEGER,
                data BLOB NOT NULL,
                error_message TEXT NOT NULL,
                created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),
                moved_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))
            )"
        );
        dlq_conn.execute(&dlq_table_sql, []).map_err(|e| {
            error!("Failed to create DLQ table {}_dlq: {}", table_name, e);
            e
        })?;

        debug!(
            "Successfully initialized dead letter queue for table: {}",
            table_name
        );

        let queries = CachedQueries {
            insert_sql: format!("INSERT INTO {table_name} (data) VALUES (?)"),
            select_sql: format!(
                "SELECT id, data FROM {table_name} ORDER BY created_at ASC, id ASC LIMIT 1"
            ),
            delete_sql: format!("DELETE FROM {table_name} WHERE id = ?"),
            count_sql: format!("SELECT COUNT(*) FROM {table_name}"),
            clear_sql: format!("DELETE FROM {table_name}"),
        };

        let dlq_insert_sql = format!(
            "INSERT INTO {table_name}_dlq (original_id, data, error_message) VALUES (?, ?, ?)"
        );

        Ok(Self {
            db: Arc::new(Mutex::new(conn)),
            dlq_db: Arc::new(Mutex::new(dlq_conn)),
            queries,
            dlq_insert_sql,
            table_name,
            max_size,
            _phantom: PhantomData,
        })
    }

    /// Send a single message to the queue.
    ///
    /// The message is serialized using postcard and persisted to SQLite before this
    /// method returns. If the queue has a `max_size` configured and the queue is full,
    /// this method will block with exponential backoff until space becomes available.
    ///
    /// # Performance
    ///
    /// Single sends are limited by fsync overhead. For high throughput, use
    /// [`send_batch`](Self::send_batch) instead, which is approximately 50x faster.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::DiskBackedQueue;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let queue = DiskBackedQueue::<Message>::new("queue.db", "messages".to_string(), None).await?;
    /// queue.send(Message { data: "hello".to_string() }).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, fields(table_name = %self.table_name))]
    pub async fn send(&self, item: T) -> Result<()> {
        let serialized = postcard::to_allocvec(&item).map_err(|e| {
            error!("Failed to serialize item for queue: {}", e);
            DiskQueueError::Serialization(e)
        })?;

        let db = self.db.clone();
        let insert_sql = self.queries.insert_sql.clone();
        let count_sql = self.queries.count_sql.clone();
        let max_size = self.max_size;
        let table_name = self.table_name.clone();

        tokio::task::spawn_blocking(move || {
            // The capacity check and insert run in a single BEGIN IMMEDIATE transaction so
            // concurrent senders cannot both observe `count == max-1` and both insert.
            let mut backoff = Duration::from_millis(10);
            loop {
                let mut conn = lock_conn(&db)?;
                let tx = conn
                    .transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)
                    .map_err(|e| {
                        error!(
                            "Failed to start transaction for table {}: {}",
                            table_name, e
                        );
                        DiskQueueError::Database(e)
                    })?;

                if let Some(max) = max_size {
                    let count: i64 = tx
                        .query_row(&count_sql, [], |row| row.get(0))
                        .map_err(DiskQueueError::Database)?;

                    if usize::try_from(count).unwrap_or(usize::MAX) >= max {
                        // Queue is full: roll back the transaction, release the lock, and wait.
                        drop(tx);
                        drop(conn);
                        warn!(
                            table_name = %table_name,
                            current_size = count,
                            max_size = max,
                            "Queue is full, waiting for space..."
                        );
                        std::thread::sleep(backoff);
                        backoff = std::cmp::min(backoff * 2, Duration::from_secs(1));
                        continue;
                    }
                }

                let rows_affected = tx.execute(&insert_sql, [&serialized]).map_err(|e| {
                    error!(
                        "Failed to insert item into queue table {}: {}",
                        table_name, e
                    );
                    DiskQueueError::Database(e)
                })?;

                if rows_affected != 1 {
                    error!("Expected to insert 1 row, but inserted {}", rows_affected);
                    return Err(DiskQueueError::UnexpectedRowCount(format!(
                        "Insert affected {rows_affected} rows instead of 1"
                    )));
                }

                tx.commit().map_err(|e| {
                    error!(
                        "Failed to commit insert for table {}: {}",
                        table_name, e
                    );
                    DiskQueueError::Database(e)
                })?;

                debug!("Successfully enqueued item to disk queue");
                return Ok(());
            }
        })
        .await
        .map_err(|e| DiskQueueError::TaskJoin(e.to_string()))?
    }

    /// Send multiple messages in a single transaction for much better throughput.
    ///
    /// All messages in the batch are inserted atomically - either all succeed or all fail.
    /// This is significantly faster than individual sends (approximately 50x speedup) because
    /// it uses a single SQLite transaction and fsync.
    ///
    /// # Performance
    ///
    /// Batch operations amortize the fsync overhead across all messages in the batch.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::DiskBackedQueue;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let queue = DiskBackedQueue::<Message>::new("queue.db", "messages".to_string(), None).await?;
    /// let messages: Vec<Message> = (0..100)
    ///     .map(|i| Message { data: format!("msg_{}", i) })
    ///     .collect();
    ///
    /// queue.send_batch(messages).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, fields(table_name = %self.table_name, batch_size = items.len()))]
    pub async fn send_batch(&self, items: Vec<T>) -> Result<()> {
        if items.is_empty() {
            return Ok(());
        }

        // Reject batches larger than max_size up front so we don't spin forever waiting
        // for space that can never exist.
        if let Some(max) = self.max_size
            && items.len() > max
        {
            return Err(DiskQueueError::QueueFull(max));
        }

        let mut serialized_items = Vec::with_capacity(items.len());

        // Serialize all items first (outside spawn_blocking for error handling)
        for item in items {
            let serialized = postcard::to_allocvec(&item).map_err(|e| {
                error!("Failed to serialize item for batch queue: {}", e);
                DiskQueueError::Serialization(e)
            })?;
            serialized_items.push(serialized);
        }

        let db = self.db.clone();
        let insert_sql = self.queries.insert_sql.clone();
        let count_sql = self.queries.count_sql.clone();
        let max_size = self.max_size;
        let table_name = self.table_name.clone();
        let batch_size = serialized_items.len();

        tokio::task::spawn_blocking(move || {
            // Capacity check and inserts run in the same BEGIN IMMEDIATE transaction so the
            // queue cannot exceed `max_size` even with concurrent senders.
            let mut backoff = Duration::from_millis(10);
            loop {
                let mut conn = lock_conn(&db)?;
                let tx = conn
                    .transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)
                    .map_err(|e| {
                        error!(
                            "Failed to start transaction for batch insert on table {}: {}",
                            table_name, e
                        );
                        DiskQueueError::Database(e)
                    })?;

                if let Some(max) = max_size {
                    let count: i64 = tx
                        .query_row(&count_sql, [], |row| row.get(0))
                        .map_err(DiskQueueError::Database)?;

                    if usize::try_from(count).unwrap_or(usize::MAX) + batch_size > max {
                        drop(tx);
                        drop(conn);
                        warn!(
                            table_name = %table_name,
                            current_size = count,
                            max_size = max,
                            batch_size = batch_size,
                            "Queue is full, waiting for space for batch..."
                        );
                        std::thread::sleep(backoff);
                        backoff = std::cmp::min(backoff * 2, Duration::from_secs(1));
                        continue;
                    }
                }

                {
                    let mut stmt = tx.prepare_cached(&insert_sql).map_err(|e| {
                        error!(
                            "Failed to prepare INSERT for table {}: {}",
                            table_name, e
                        );
                        DiskQueueError::Database(e)
                    })?;
                    for serialized in &serialized_items {
                        stmt.execute([serialized]).map_err(|e| {
                            error!(
                                "Failed to insert item into queue table {} during batch: {}",
                                table_name, e
                            );
                            DiskQueueError::Database(e)
                        })?;
                    }
                }

                tx.commit().map_err(|e| {
                    error!(
                        "Failed to commit batch transaction for table {}: {}",
                        table_name, e
                    );
                    DiskQueueError::Database(e)
                })?;

                debug!(
                    "Successfully enqueued {} items to disk queue in batch",
                    batch_size
                );
                return Ok(());
            }
        })
        .await
        .map_err(|e| DiskQueueError::TaskJoin(e.to_string()))?
    }

    /// Receive a single message from the queue.
    ///
    /// Returns the oldest message in FIFO order and removes it from the queue atomically.
    /// If the queue is empty, returns `Ok(None)` immediately (non-blocking).
    ///
    /// # Dead Letter Queue
    ///
    /// If a message fails to deserialize (e.g., due to schema changes or corruption),
    /// it is automatically moved to the dead letter queue database (`.dlq.db` file)
    /// and a `Deserialization` error is returned. The queue is not blocked - subsequent
    /// calls will process the next message.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::DiskBackedQueue;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize, Debug)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let queue = DiskBackedQueue::<Message>::new("queue.db", "messages".to_string(), None).await?;
    /// match queue.recv().await? {
    ///     Some(msg) => println!("Received: {:?}", msg),
    ///     None => println!("Queue is empty"),
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, fields(table_name = %self.table_name))]
    pub async fn recv(&self) -> Result<Option<T>> {
        let db = self.db.clone();
        let dlq_db = self.dlq_db.clone();
        let select_sql = self.queries.select_sql.clone();
        let delete_sql = self.queries.delete_sql.clone();
        let dlq_insert_sql = self.dlq_insert_sql.clone();
        let table_name = self.table_name.clone();

        tokio::task::spawn_blocking(move || {
            let mut conn = lock_conn(&db)?;

            // Start a transaction for atomic read-delete
            let tx = conn.transaction().map_err(|e| {
                error!(
                    "Failed to start transaction for table {}: {}",
                    table_name, e
                );
                DiskQueueError::Database(e)
            })?;

            // Read the oldest item
            let result: Option<(i64, Vec<u8>)> = tx
                .query_row(&select_sql, [], |row| Ok((row.get(0)?, row.get(1)?)))
                .optional()
                .map_err(|e| {
                    error!(
                        "Failed to execute SELECT query on table {}: {}",
                        table_name, e
                    );
                    DiskQueueError::Database(e)
                })?;

            if let Some((id, data)) = result {
                // Try to deserialize BEFORE deleting
                let item: T = match postcard::from_bytes::<T>(&data) {
                    Ok(item) => item,
                    Err(e) => {
                        // Deserialization failed - move to dead letter queue.
                        // We require the DLQ insert to succeed before deleting from the main
                        // queue, otherwise a DLQ failure would silently drop the message.
                        error!(
                            "Failed to deserialize item from queue (id {}): {}. Moving to DLQ.",
                            id, e
                        );

                        let dlq_result = match dlq_db.lock() {
                            Ok(dlq_conn) => dlq_conn
                                .execute(
                                    &dlq_insert_sql,
                                    rusqlite::params![id, &data, e.to_string()],
                                )
                                .map_err(|dlq_err| dlq_err.to_string()),
                            Err(poison_err) => Err(format!("DLQ mutex poisoned: {poison_err}")),
                        };

                        if let Err(dlq_msg) = dlq_result {
                            // Leave the corrupt row in the main queue (transaction rolls back
                            // on drop) so the operator can intervene without losing data.
                            error!(
                                "Failed to write corrupt item {} from table {} to DLQ: {}. Item left in main queue.",
                                id, table_name, dlq_msg
                            );
                            return Err(DiskQueueError::DlqWriteFailed(dlq_msg));
                        }

                        // DLQ insert succeeded; safe to delete the corrupt row from main.
                        tx.execute(&delete_sql, [&id]).map_err(|err| {
                            error!(
                                "Failed to delete item {} from table {}: {}",
                                id, table_name, err
                            );
                            DiskQueueError::Database(err)
                        })?;

                        tx.commit().map_err(|err| {
                            error!(
                                "Failed to commit transaction after DLQ move for table {}: {}",
                                table_name, err
                            );
                            DiskQueueError::Database(err)
                        })?;

                        // Return deserialization error to signal corruption
                        return Err(DiskQueueError::Deserialization(e));
                    }
                };

                // Delete the item from the queue
                let rows_deleted = tx.execute(&delete_sql, [&id]).map_err(|e| {
                    error!(
                        "Failed to delete item {} from table {}: {}",
                        id, table_name, e
                    );
                    DiskQueueError::Database(e)
                })?;

                if rows_deleted != 1 {
                    error!(
                        "Expected to delete 1 row, but deleted {} rows for id {}",
                        rows_deleted, id
                    );
                    return Err(DiskQueueError::UnexpectedRowCount(format!(
                        "Delete affected {rows_deleted} rows instead of 1 for id {id}"
                    )));
                }

                // Commit the transaction - if this fails, message stays in queue
                tx.commit().map_err(|e| {
                    error!(
                        "Failed to commit transaction for table {}: {}",
                        table_name, e
                    );
                    DiskQueueError::Database(e)
                })?;

                debug!("Successfully dequeued item from disk queue");
                Ok(Some(item))
            } else {
                // No items in queue
                Ok(None)
            }
        })
        .await
        .map_err(|e| DiskQueueError::TaskJoin(e.to_string()))?
    }

    /// Receive multiple messages in a single transaction for much better throughput.
    ///
    /// Returns up to `limit` messages from the queue in FIFO order. May return fewer if
    /// there aren't enough items. Returns an empty `Vec` if the queue is empty.
    ///
    /// All messages in the batch are removed atomically - either all succeed or all fail.
    ///
    /// # Dead Letter Queue
    ///
    /// If any message fails to deserialize, it is moved to the DLQ and skipped.
    /// The batch will contain only successfully deserialized messages. The transaction
    /// still commits successfully.
    ///
    /// # Performance
    ///
    /// Batch operations are approximately 50x faster than individual receives due to
    /// reduced transaction overhead.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::DiskBackedQueue;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let queue = DiskBackedQueue::<Message>::new("queue.db", "messages".to_string(), None).await?;
    /// // Receive up to 100 messages at once
    /// let messages = queue.recv_batch(100).await?;
    /// println!("Received {} messages", messages.len());
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, fields(table_name = %self.table_name, limit = limit))]
    pub async fn recv_batch(&self, limit: usize) -> Result<Vec<T>> {
        if limit == 0 {
            return Ok(Vec::new());
        }

        let db = self.db.clone();
        let dlq_db = self.dlq_db.clone();
        let table_name = self.table_name.clone();
        let dlq_insert_sql = self.dlq_insert_sql.clone();

        tokio::task::spawn_blocking(move || {
            let mut conn = lock_conn(&db)?;

            // Start a transaction for atomic batch read-delete
            let tx = conn.transaction().map_err(|e| {
                error!("Failed to start transaction for table {}: {}", table_name, e);
                DiskQueueError::Database(e)
            })?;

            // Read up to `limit` oldest items.
            // Safety: `table_name` is validated at construction time to contain only
            // alphanumeric characters and underscores, preventing SQL injection.
            let select_batch_sql = format!(
                "SELECT id, data FROM {table_name} ORDER BY created_at ASC, id ASC LIMIT ?"
            );

            let mut stmt = tx.prepare_cached(&select_batch_sql).map_err(|e| {
                error!("Failed to prepare SELECT statement for table {}: {}", table_name, e);
                DiskQueueError::Database(e)
            })?;

            let limit_param = i64::try_from(limit).unwrap_or(i64::MAX);
            let rows = stmt
                .query_map([limit_param], |row| {
                    Ok((row.get::<_, i64>(0)?, row.get::<_, Vec<u8>>(1)?))
                })
                .map_err(|e| {
                    error!("Failed to execute SELECT query on table {}: {}", table_name, e);
                    DiskQueueError::Database(e)
                })?;

            let mut items = Vec::new();
            let mut ids_to_delete = Vec::new();
            let mut dlq_failures: usize = 0;

            for row_result in rows {
                let (id, data) = row_result.map_err(|e| {
                    error!("Failed to read row from table {}: {}", table_name, e);
                    DiskQueueError::Database(e)
                })?;

                // Try to deserialize
                match postcard::from_bytes::<T>(&data) {
                    Ok(item) => {
                        items.push(item);
                        ids_to_delete.push(id);
                    }
                    Err(e) => {
                        // Deserialization failed - move to dead letter queue.
                        // Only mark for deletion if the DLQ insert succeeds, otherwise the
                        // corrupt row stays in the main queue (no silent data loss).
                        error!(
                            "Failed to deserialize item from queue (id {}): {}. Moving to DLQ.",
                            id, e
                        );

                        let dlq_ok = match dlq_db.lock() {
                            Ok(dlq_conn) => match dlq_conn.execute(
                                &dlq_insert_sql,
                                rusqlite::params![id, &data, e.to_string()],
                            ) {
                                Ok(_) => true,
                                Err(dlq_err) => {
                                    error!(
                                        "Failed to insert corrupt item {} into DLQ for table {}: {}. Leaving item in main queue.",
                                        id, table_name, dlq_err
                                    );
                                    false
                                }
                            },
                            Err(poison_err) => {
                                error!(
                                    "DLQ mutex poisoned while handling corrupt item {}: {}. Leaving item in main queue.",
                                    id, poison_err
                                );
                                false
                            }
                        };

                        if dlq_ok {
                            ids_to_delete.push(id);
                        } else {
                            dlq_failures += 1;
                        }
                    }
                }
            }

            drop(stmt); // Release the statement before executing deletes

            // Delete all processed items
            if !ids_to_delete.is_empty() {
                // SQLite limit for host parameters is typically 999 or higher, but 900 is safe
                const BATCH_DELETE_LIMIT: usize = 900;

                for chunk in ids_to_delete.chunks(BATCH_DELETE_LIMIT) {
                    // Safety: `table_name` is validated at construction time to contain only
                    // alphanumeric characters and underscores, preventing SQL injection.
                    // The placeholders (?) are properly parameterized below.
                    let delete_batch_sql = format!(
                        "DELETE FROM {} WHERE id IN ({})",
                        table_name,
                        chunk
                            .iter()
                            .map(|_| "?")
                            .collect::<Vec<_>>()
                            .join(",")
                    );

                    let params: Vec<&dyn rusqlite::ToSql> = chunk
                        .iter()
                        .map(|id| id as &dyn rusqlite::ToSql)
                        .collect();

                    let rows_deleted = tx
                        .execute(&delete_batch_sql, params.as_slice())
                        .map_err(|e| {
                            error!("Failed to delete items from table {}: {}", table_name, e);
                            DiskQueueError::Database(e)
                        })?;

                    if rows_deleted != chunk.len() {
                        error!(
                            "Expected to delete {} rows, but deleted {} rows",
                            chunk.len(),
                            rows_deleted
                        );
                        return Err(DiskQueueError::UnexpectedRowCount(format!(
                            "Delete affected {rows_deleted} rows instead of {}",
                            chunk.len()
                        )));
                    }
                }
            }

            // Commit the transaction
            tx.commit().map_err(|e| {
                error!("Failed to commit batch transaction for table {}: {}", table_name, e);
                DiskQueueError::Database(e)
            })?;

            // If every item we read failed to land in the DLQ and we have nothing valid to
            // return, surface an error so the caller is not stuck silently spinning. If we
            // recovered at least some items, return them and let the caller observe the DLQ
            // failures via logs/metrics on the next call.
            if dlq_failures > 0 && items.is_empty() {
                return Err(DiskQueueError::DlqWriteFailed(format!(
                    "{dlq_failures} corrupt item(s) could not be moved to DLQ"
                )));
            }

            if dlq_failures > 0 {
                warn!(
                    table_name = %table_name,
                    dlq_failures = dlq_failures,
                    "Some corrupt items could not be moved to DLQ; they remain in the main queue"
                );
            }

            debug!("Successfully dequeued {} items from disk queue in batch", items.len());
            Ok(items)
        })
        .await
        .map_err(|e| DiskQueueError::TaskJoin(e.to_string()))?
    }

    /// Returns the number of messages currently in the queue.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::DiskBackedQueue;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let queue = DiskBackedQueue::<Message>::new("queue.db", "messages".to_string(), None).await?;
    /// let count = queue.len().await?;
    /// println!("Queue has {} messages", count);
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, fields(table_name = %self.table_name))]
    pub async fn len(&self) -> Result<usize> {
        let db = self.db.clone();
        let count_sql = self.queries.count_sql.clone();

        tokio::task::spawn_blocking(move || {
            let conn = lock_conn(&db)?;
            let count: i64 = conn
                .query_row(&count_sql, [], |row| row.get(0))
                .map_err(DiskQueueError::Database)?;
            Ok(usize::try_from(count).unwrap_or(usize::MAX))
        })
        .await
        .map_err(|e| DiskQueueError::TaskJoin(e.to_string()))?
    }

    /// Returns `true` if the queue contains no messages.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::DiskBackedQueue;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let queue = DiskBackedQueue::<Message>::new("queue.db", "messages".to_string(), None).await?;
    /// if queue.is_empty().await? {
    ///     println!("No messages to process");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, fields(table_name = %self.table_name))]
    pub async fn is_empty(&self) -> Result<bool> {
        let db = self.db.clone();
        // Cheap O(1) check that stops at the first row instead of doing a full COUNT(*).
        // Safety: `table_name` is validated at construction time.
        let exists_sql = format!(
            "SELECT EXISTS(SELECT 1 FROM {} LIMIT 1)",
            self.table_name
        );

        tokio::task::spawn_blocking(move || {
            let conn = lock_conn(&db)?;
            let exists: i64 = conn
                .query_row(&exists_sql, [], |row| row.get(0))
                .map_err(DiskQueueError::Database)?;
            Ok(exists == 0)
        })
        .await
        .map_err(|e| DiskQueueError::TaskJoin(e.to_string()))?
    }

    /// Removes all messages from the queue.
    ///
    /// This operation is atomic - either all messages are deleted or none are.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::DiskBackedQueue;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let queue = DiskBackedQueue::<Message>::new("queue.db", "messages".to_string(), None).await?;
    /// queue.clear().await?;
    /// assert!(queue.is_empty().await?);
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, fields(table_name = %self.table_name))]
    pub async fn clear(&self) -> Result<()> {
        let db = self.db.clone();
        let clear_sql = self.queries.clear_sql.clone();

        tokio::task::spawn_blocking(move || {
            let conn = lock_conn(&db)?;
            conn.execute(&clear_sql, [])
                .map_err(DiskQueueError::Database)?;
            debug!("Cleared disk queue");
            Ok(())
        })
        .await
        .map_err(|e| DiskQueueError::TaskJoin(e.to_string()))?
    }
}

/// The sending half of a disk-backed channel.
///
/// Messages sent through this sender are persisted to SQLite. The sender can be
/// cloned and shared across multiple tasks or threads safely.
///
/// Created via [`disk_backed_channel`] or [`disk_backed_channel_with_durability`].
///
/// # Example
///
/// ```no_run
/// # use disk_backed_queue::disk_backed_channel;
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize, Deserialize)]
/// # struct Message { data: String }
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = disk_backed_channel::<Message, _>(
///     "queue.db",
///     "messages".to_string(),
///     None,
/// ).await?;
///
/// // Sender can be cloned and moved to other tasks
/// let tx2 = tx.clone();
/// tokio::spawn(async move {
///     tx2.send(Message { data: "from another task".to_string() }).await
/// });
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct DiskBackedSender<T> {
    queue: std::sync::Arc<DiskBackedQueue<T>>,
}

impl<T> DiskBackedSender<T>
where
    T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
{
    /// Send a single message to the queue.
    ///
    /// See [`DiskBackedQueue::send`] for details.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::disk_backed_channel;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let (tx, mut rx) = disk_backed_channel::<Message, _>("queue.db", "messages".to_string(), None).await?;
    /// tx.send(Message { data: "hello".to_string() }).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send(&self, item: T) -> Result<()> {
        self.queue.send(item).await
    }

    /// Send multiple messages in a single transaction for much better throughput.
    ///
    /// See [`DiskBackedQueue::send_batch`] for details.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::disk_backed_channel;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let (tx, mut rx) = disk_backed_channel::<Message, _>("queue.db", "messages".to_string(), None).await?;
    /// let messages = vec![
    ///     Message { data: "msg1".to_string() },
    ///     Message { data: "msg2".to_string() },
    /// ];
    /// tx.send_batch(messages).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send_batch(&self, items: Vec<T>) -> Result<()> {
        self.queue.send_batch(items).await
    }

    /// Blocking send for use in synchronous contexts.
    ///
    /// This method blocks the current thread until the message is persisted.
    /// If called from within a Tokio runtime, it uses `block_in_place` to avoid
    /// blocking the async executor. If called outside a runtime, it creates a
    /// temporary runtime.
    ///
    /// # Performance
    ///
    /// This should be used sparingly as it blocks a thread. Prefer async `send`
    /// when possible.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::disk_backed_channel;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let (tx, mut rx) = disk_backed_channel::<Message, _>("queue.db", "messages".to_string(), None).await?;
    /// // In a synchronous context
    /// std::thread::spawn(move || {
    ///     tx.blocking_send(Message { data: "from sync".to_string() })
    /// });
    /// # Ok(())
    /// # }
    /// ```
    pub fn blocking_send(&self, item: T) -> Result<()> {
        // `tokio::task::block_in_place` only works on the multi-thread runtime; calling it
        // from a current-thread runtime panics. We probe the runtime flavor and fall back
        // to a temporary runtime when block_in_place is unavailable.
        match tokio::runtime::Handle::try_current() {
            Ok(handle) if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => {
                tokio::task::block_in_place(|| handle.block_on(self.send(item)))
            }
            _ => {
                let runtime = tokio::runtime::Runtime::new().map_err(|e| {
                    error!("Failed to create Tokio runtime: {}", e);
                    DiskQueueError::TaskJoin(format!("Runtime creation failed: {e}"))
                })?;
                runtime.block_on(self.send(item))
            }
        }
    }
}

impl<T> Clone for DiskBackedSender<T> {
    /// Clone the sender.
    ///
    /// This is a cheap operation that only clones an `Arc` pointer. All cloned
    /// senders share the same underlying queue and can be safely used from
    /// multiple tasks or threads.
    fn clone(&self) -> Self {
        Self {
            queue: self.queue.clone(),
        }
    }
}

/// The receiving half of a disk-backed channel.
///
/// Messages are received from SQLite in FIFO order. Unlike the sender, the receiver
/// cannot be cloned (similar to `tokio::sync::mpsc::Receiver`).
///
/// Created via [`disk_backed_channel`] or [`disk_backed_channel_with_durability`].
///
/// # Example
///
/// ```no_run
/// # use disk_backed_queue::disk_backed_channel;
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize, Deserialize, Debug)]
/// # struct Message { data: String }
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = disk_backed_channel::<Message, _>(
///     "queue.db",
///     "messages".to_string(),
///     None,
/// ).await?;
///
/// // Receive messages
/// while let Some(msg) = rx.recv().await? {
///     println!("Received: {:?}", msg);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct DiskBackedReceiver<T> {
    queue: std::sync::Arc<DiskBackedQueue<T>>,
}

impl<T> DiskBackedReceiver<T>
where
    T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
{
    /// Receive a single message from the queue.
    ///
    /// Returns `Ok(None)` if the queue is empty (non-blocking).
    /// See [`DiskBackedQueue::recv`] for details about dead letter queue handling.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::disk_backed_channel;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let (tx, mut rx) = disk_backed_channel::<Message, _>("queue.db", "messages".to_string(), None).await?;
    /// match rx.recv().await? {
    ///     Some(msg) => println!("Got message"),
    ///     None => println!("Queue is empty"),
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn recv(&mut self) -> Result<Option<T>> {
        self.queue.recv().await
    }

    /// Receive multiple messages in a single transaction for much better throughput.
    ///
    /// Returns up to `limit` messages from the queue. May return fewer if there aren't
    /// enough items. Returns an empty `Vec` if the queue is empty.
    ///
    /// See [`DiskBackedQueue::recv_batch`] for details.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::disk_backed_channel;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let (tx, mut rx) = disk_backed_channel::<Message, _>("queue.db", "messages".to_string(), None).await?;
    /// // Receive up to 100 messages at once
    /// let messages = rx.recv_batch(100).await?;
    /// for msg in messages {
    ///     // Process message
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn recv_batch(&mut self, limit: usize) -> Result<Vec<T>> {
        self.queue.recv_batch(limit).await
    }

    /// Returns the number of messages currently in the queue.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::disk_backed_channel;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let (tx, mut rx) = disk_backed_channel::<Message, _>("queue.db", "messages".to_string(), None).await?;
    /// let count = rx.len().await?;
    /// println!("Queue has {} messages", count);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn len(&self) -> Result<usize> {
        self.queue.len().await
    }

    /// Returns `true` if the queue contains no messages.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use disk_backed_queue::disk_backed_channel;
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Serialize, Deserialize)]
    /// # struct Message { data: String }
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let (tx, mut rx) = disk_backed_channel::<Message, _>("queue.db", "messages".to_string(), None).await?;
    /// if rx.is_empty().await? {
    ///     println!("No messages to process");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn is_empty(&self) -> Result<bool> {
        self.queue.is_empty().await
    }
}

/// Create a disk-backed channel with default durability (FULL).
///
/// This is the recommended way to create a disk-backed queue for most use cases.
/// It provides maximum data safety by fsyncing after every commit, ensuring messages
/// survive power loss.
///
/// # Arguments
///
/// * `db_path` - Path to the SQLite database file. Will be created if it doesn't exist.
/// * `table_name` - Name of the table to store messages. Must contain only alphanumeric
///   characters and underscores, and be between 1-128 characters.
/// * `max_size` - Optional maximum queue size. When set, senders will block when the
///   queue is full. Use `None` for unbounded queues.
///
/// # Returns
///
/// A tuple of `(sender, receiver)` where the sender can be cloned and shared across
/// tasks, while the receiver is single-threaded.
///
/// # Example
///
/// ```no_run
/// use disk_backed_queue::disk_backed_channel;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize, Debug)]
/// struct Message {
///     id: u64,
///     content: String,
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let (tx, mut rx) = disk_backed_channel::<Message, _>(
///         "my_queue.db",
///         "messages".to_string(),
///         Some(10_000),  // Max 10,000 messages
///     ).await?;
///
///     // Send from multiple tasks
///     let tx2 = tx.clone();
///     tokio::spawn(async move {
///         tx2.send(Message { id: 1, content: "hello".to_string() }).await
///     });
///
///     // Receive from single task
///     while let Some(msg) = rx.recv().await? {
///         println!("Received: {:?}", msg);
///     }
///
///     Ok(())
/// }
/// ```
pub async fn disk_backed_channel<T, P: AsRef<Path>>(
    db_path: P,
    table_name: String,
    max_size: Option<usize>,
) -> Result<(DiskBackedSender<T>, DiskBackedReceiver<T>)>
where
    T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
{
    disk_backed_channel_with_durability(db_path, table_name, max_size, DurabilityLevel::default())
        .await
}

/// Create a disk-backed channel with custom durability level.
///
/// This allows you to trade off between performance and data safety by choosing
/// different SQLite synchronous modes.
///
/// # Durability Levels
///
/// * [`DurabilityLevel::Full`] (default) - Maximum safety, survives power loss
/// * [`DurabilityLevel::Normal`] - Balanced performance, safe in most crashes
/// * [`DurabilityLevel::Off`] - Fastest, but data loss on power failure
/// * [`DurabilityLevel::Extra`] - Paranoid mode, additional fsyncs
///
/// # Example
///
/// ```no_run
/// use disk_backed_queue::{disk_backed_channel_with_durability, DurabilityLevel};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// struct Message { data: String }
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // For critical data (default)
///     let (tx, rx) = disk_backed_channel_with_durability::<Message, _>(
///         "critical.db",
///         "messages".to_string(),
///         None,
///         DurabilityLevel::Full,
///     ).await?;
///
///     // For high-performance temporary cache
///     let (tx_fast, rx_fast) = disk_backed_channel_with_durability::<Message, _>(
///         "cache.db",
///         "temp".to_string(),
///         None,
///         DurabilityLevel::Normal,
///     ).await?;
///
///     Ok(())
/// }
/// ```
pub async fn disk_backed_channel_with_durability<T, P: AsRef<Path>>(
    db_path: P,
    table_name: String,
    max_size: Option<usize>,
    durability: DurabilityLevel,
) -> Result<(DiskBackedSender<T>, DiskBackedReceiver<T>)>
where
    T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
{
    let queue = DiskBackedQueue::with_durability(db_path, table_name, max_size, durability).await?;
    let queue_arc = std::sync::Arc::new(queue);

    let sender = DiskBackedSender {
        queue: queue_arc.clone(),
    };

    let receiver = DiskBackedReceiver { queue: queue_arc };

    Ok((sender, receiver))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};
    use tempfile::NamedTempFile;

    #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
    struct TestMessage {
        id: u64,
        content: String,
    }

    #[tokio::test]
    async fn test_disk_backed_queue_basic() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "test_queue".to_string(), None)
            .await
            .unwrap();

        // Test empty queue
        assert!(queue.is_empty().await.unwrap());
        assert_eq!(queue.len().await.unwrap(), 0);
        assert!(queue.recv().await.unwrap().is_none());

        // Send some messages
        let msg1 = TestMessage {
            id: 1,
            content: "Hello".to_string(),
        };
        let msg2 = TestMessage {
            id: 2,
            content: "World".to_string(),
        };

        queue.send(msg1.clone()).await.unwrap();
        queue.send(msg2.clone()).await.unwrap();

        // Test queue state
        assert!(!queue.is_empty().await.unwrap());
        assert_eq!(queue.len().await.unwrap(), 2);

        // Receive messages in FIFO order
        let received1 = queue.recv().await.unwrap().unwrap();
        assert_eq!(received1, msg1);
        assert_eq!(queue.len().await.unwrap(), 1);

        let received2 = queue.recv().await.unwrap().unwrap();
        assert_eq!(received2, msg2);
        assert_eq!(queue.len().await.unwrap(), 0);

        // Queue should be empty now
        assert!(queue.is_empty().await.unwrap());
        assert!(queue.recv().await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_disk_backed_channel() {
        let temp_file = NamedTempFile::new().unwrap();
        let (sender, mut receiver) = disk_backed_channel::<TestMessage, _>(
            temp_file.path(),
            "test_channel".to_string(),
            None,
        )
        .await
        .unwrap();

        let msg = TestMessage {
            id: 42,
            content: "Channel test".to_string(),
        };

        sender.send(msg.clone()).await.unwrap();
        let received = receiver.recv().await.unwrap().unwrap();
        assert_eq!(received, msg);
    }

    #[tokio::test]
    async fn test_persistence() {
        let temp_file = NamedTempFile::new().unwrap();
        let temp_path = temp_file.path().to_path_buf();

        let msg = TestMessage {
            id: 99,
            content: "Persistent message".to_string(),
        };

        // Create queue, send message, and drop it
        {
            let queue = DiskBackedQueue::new(&temp_path, "persistence_test".to_string(), None)
                .await
                .unwrap();
            queue.send(msg.clone()).await.unwrap();
        }

        // Create new queue instance and verify message is still there
        {
            let queue: DiskBackedQueue<TestMessage> =
                DiskBackedQueue::new(&temp_path, "persistence_test".to_string(), None)
                    .await
                    .unwrap();
            assert_eq!(queue.len().await.unwrap(), 1);
            let received = queue.recv().await.unwrap().unwrap();
            assert_eq!(received, msg);
        }
    }

    #[tokio::test]
    async fn test_error_handling_database_corruption() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "test_queue".to_string(), None)
            .await
            .unwrap();

        // Send a valid message
        let msg = TestMessage {
            id: 1,
            content: "Test".to_string(),
        };
        queue.send(msg).await.unwrap();

        // Manually corrupt the database by writing invalid binary data
        {
            let db = queue.db.lock().unwrap();
            let garbage_data: Vec<u8> = vec![0xFF, 0xFE, 0xFD, 0xFC]; // Invalid postcard
            db.execute(
                "UPDATE test_queue SET data = ? WHERE id = 1",
                [&garbage_data],
            )
            .unwrap();
        }

        // Try to receive - should return deserialization error but message moved to DLQ
        let result = queue.recv().await;
        assert!(result.is_err(), "Expected error, got: {:?}", result);
        assert!(
            matches!(result, Err(DiskQueueError::Deserialization(_))),
            "Expected Deserialization error, got: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn test_concurrent_access() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = std::sync::Arc::new(
            DiskBackedQueue::new(temp_file.path(), "concurrent_test".to_string(), None)
                .await
                .unwrap(),
        );

        // Spawn multiple senders
        let mut send_handles = vec![];
        for i in 0..10 {
            let queue_clone = queue.clone();
            let handle = tokio::spawn(async move {
                for j in 0..10 {
                    let msg = TestMessage {
                        id: i * 10 + j,
                        content: format!("Message from sender {i}, iteration {j}"),
                    };
                    queue_clone.send(msg).await.unwrap();
                }
            });
            send_handles.push(handle);
        }

        // Spawn multiple receivers
        let mut recv_handles = vec![];
        let received_messages = std::sync::Arc::new(tokio::sync::Mutex::new(Vec::new()));

        for _ in 0..5 {
            let queue_clone = queue.clone();
            let messages_clone = received_messages.clone();
            let handle = tokio::spawn(async move {
                loop {
                    match queue_clone.recv().await {
                        Ok(Some(msg)) => {
                            messages_clone.lock().await.push(msg);
                        }
                        Ok(None) => {
                            // No more messages, small delay before checking again
                            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                        }
                        Err(_) => break,
                    }

                    // Stop when we've received all messages
                    if messages_clone.lock().await.len() >= 100 {
                        break;
                    }
                }
            });
            recv_handles.push(handle);
        }

        // Wait for all senders to complete
        for handle in send_handles {
            handle.await.unwrap();
        }

        // Wait for all messages to be received with timeout
        let start = tokio::time::Instant::now();
        let timeout = tokio::time::Duration::from_secs(10);

        loop {
            let count = received_messages.lock().await.len();
            if count >= 100 {
                break;
            }

            if start.elapsed() > timeout {
                panic!(
                    "Timeout: Only received {} messages after {:?}",
                    count, timeout
                );
            }

            tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
        }

        // Check that all messages were received
        let messages = received_messages.lock().await;
        assert_eq!(messages.len(), 100);

        // Clean up receivers
        for handle in recv_handles {
            handle.abort();
        }
    }

    #[tokio::test]
    async fn test_empty_queue_operations() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue: DiskBackedQueue<TestMessage> =
            DiskBackedQueue::new(temp_file.path(), "empty_test".to_string(), None)
                .await
                .unwrap();

        // Multiple recv calls on empty queue should all return None
        for _ in 0..5 {
            assert!(queue.recv().await.unwrap().is_none());
        }

        assert!(queue.is_empty().await.unwrap());
        assert_eq!(queue.len().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_large_messages() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "large_test".to_string(), None)
            .await
            .unwrap();

        // Create a large message (1MB of content)
        let large_content = "x".repeat(1024 * 1024);
        let large_msg = TestMessage {
            id: 42,
            content: large_content.clone(),
        };

        // Send and receive large message
        queue.send(large_msg.clone()).await.unwrap();
        let received = queue.recv().await.unwrap().unwrap();

        assert_eq!(received.id, 42);
        assert_eq!(received.content.len(), 1024 * 1024);
        assert_eq!(received.content, large_content);
    }

    #[tokio::test]
    async fn test_fifo_ordering() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "fifo_test".to_string(), None)
            .await
            .unwrap();

        // Send messages in order
        let messages: Vec<TestMessage> = (0..100)
            .map(|i| TestMessage {
                id: i,
                content: format!("Message {i}"),
            })
            .collect();

        for msg in &messages {
            queue.send(msg.clone()).await.unwrap();
        }

        // Receive messages and verify FIFO order
        for expected_msg in &messages {
            let received = queue.recv().await.unwrap().unwrap();
            assert_eq!(received, *expected_msg);
        }

        // Queue should be empty now
        assert!(queue.recv().await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_invalid_table_name() {
        let temp_file = NamedTempFile::new().unwrap();

        // Try to create queue with invalid table name
        let _result = DiskBackedQueue::<TestMessage>::new(
            temp_file.path(),
            "invalid-table-name-with-dashes".to_string(),
            None,
        )
        .await;

        // This should still work because SQLite is quite permissive
        // but let's test a truly invalid name
        let result2 = DiskBackedQueue::<TestMessage>::new(
            temp_file.path(),
            "".to_string(), // Empty table name should cause issues
            None,
        )
        .await;

        // Empty table name should cause an error
        assert!(result2.is_err());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_blocking_send() {
        let temp_file = NamedTempFile::new().unwrap();
        let (sender, mut receiver) = disk_backed_channel::<TestMessage, _>(
            temp_file.path(),
            "blocking_test".to_string(),
            None,
        )
        .await
        .unwrap();

        let msg = TestMessage {
            id: 123,
            content: "Blocking test".to_string(),
        };

        // Test blocking send
        sender.blocking_send(msg.clone()).unwrap();

        // Receive and verify
        let received = receiver.recv().await.unwrap().unwrap();
        assert_eq!(received, msg);
    }

    #[tokio::test]
    async fn test_database_file_permissions() {
        let temp_file = NamedTempFile::new().unwrap();
        let temp_path = temp_file.path().to_path_buf();

        // Create queue first
        let _queue = DiskBackedQueue::<TestMessage>::new(&temp_path, "perm_test".to_string(), None)
            .await
            .unwrap();

        // Check that database file was created
        assert!(temp_path.exists());

        // Basic metadata check (permissions test is platform-specific)
        let metadata = std::fs::metadata(&temp_path).unwrap();
        assert!(metadata.is_file());
        assert!(metadata.len() > 0); // Should have some content (SQLite header)
    }

    #[tokio::test]
    async fn test_dlq_file_created() {
        let temp_file = NamedTempFile::new().unwrap();
        let temp_path = temp_file.path();

        disk_backed_channel::<TestMessage, _>(temp_path, "test".to_string(), None)
            .await
            .unwrap();

        // Check DLQ file exists
        let dlq_path = temp_path.with_extension("dlq.db");
        assert!(dlq_path.exists());
    }

    #[tokio::test]
    async fn test_transaction_rollback_on_corruption() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "test_queue".to_string(), None)
            .await
            .unwrap();

        // Send a valid message
        let msg = TestMessage {
            id: 1,
            content: "Valid".to_string(),
        };
        queue.send(msg).await.unwrap();

        // Manually corrupt the database by writing invalid binary data
        {
            let db = queue.db.lock().unwrap();
            let garbage_data: Vec<u8> = vec![0xFF, 0xFE, 0xFD, 0xFC]; // Invalid postcard
            db.execute(
                "UPDATE test_queue SET data = ? WHERE id = 1",
                [&garbage_data],
            )
            .unwrap();
        }

        // Try to receive - should return deserialization error
        let result = queue.recv().await;
        assert!(result.is_err());
        assert!(matches!(result, Err(DiskQueueError::Deserialization(_))));

        // Verify message was moved to DLQ
        let dlq_path = temp_file.path().with_extension("dlq.db");
        let dlq_conn = rusqlite::Connection::open(&dlq_path).unwrap();
        let count: i64 = dlq_conn
            .query_row("SELECT COUNT(*) FROM test_queue_dlq", [], |row| row.get(0))
            .unwrap();
        assert_eq!(count, 1);

        // Verify main queue is empty
        assert_eq!(queue.len().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_max_size_blocks() {
        let temp_file = NamedTempFile::new().unwrap();
        let (tx, mut rx) = disk_backed_channel::<TestMessage, _>(
            temp_file.path(),
            "test".to_string(),
            Some(2), // Max 2 messages
        )
        .await
        .unwrap();

        let msg1 = TestMessage {
            id: 1,
            content: "Message 1".to_string(),
        };
        let msg2 = TestMessage {
            id: 2,
            content: "Message 2".to_string(),
        };
        let msg3 = TestMessage {
            id: 3,
            content: "Message 3".to_string(),
        };

        // Send 2 messages (should succeed immediately)
        tx.send(msg1.clone()).await.unwrap();
        tx.send(msg2.clone()).await.unwrap();

        // Send 3rd message in background (should block until space available)
        let tx_clone = tx.clone();
        let handle = tokio::spawn(async move { tx_clone.send(msg3).await });

        // Give it time to attempt send (should be blocked)
        tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
        assert!(!handle.is_finished());

        // Receive one message to make space
        let received = rx.recv().await.unwrap().unwrap();
        assert_eq!(received, msg1);

        // Now 3rd send should complete
        tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
        assert!(handle.is_finished());
        handle.await.unwrap().unwrap();

        // Verify queue has 2 messages
        assert_eq!(rx.len().await.unwrap(), 2);
    }

    #[tokio::test]
    async fn test_send_batch() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "batch_test".to_string(), None)
            .await
            .unwrap();

        // Create a batch of messages
        let messages: Vec<TestMessage> = (0..100)
            .map(|i| TestMessage {
                id: i,
                content: format!("Batch message {}", i),
            })
            .collect();

        // Send batch
        queue.send_batch(messages.clone()).await.unwrap();

        // Verify count
        assert_eq!(queue.len().await.unwrap(), 100);

        // Receive and verify all messages
        for expected in &messages {
            let received = queue.recv().await.unwrap().unwrap();
            assert_eq!(received, *expected);
        }

        assert!(queue.is_empty().await.unwrap());
    }

    #[tokio::test]
    async fn test_recv_batch() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "batch_recv_test".to_string(), None)
            .await
            .unwrap();

        // Send 100 individual messages
        for i in 0..100 {
            let msg = TestMessage {
                id: i,
                content: format!("Message {}", i),
            };
            queue.send(msg).await.unwrap();
        }

        // Receive in batches of 25
        let batch1 = queue.recv_batch(25).await.unwrap();
        assert_eq!(batch1.len(), 25);
        assert_eq!(batch1[0].id, 0);
        assert_eq!(batch1[24].id, 24);

        let batch2 = queue.recv_batch(25).await.unwrap();
        assert_eq!(batch2.len(), 25);
        assert_eq!(batch2[0].id, 25);
        assert_eq!(batch2[24].id, 49);

        // Receive remaining 50 items (but only request 100)
        let batch3 = queue.recv_batch(100).await.unwrap();
        assert_eq!(batch3.len(), 50); // Should only get 50
        assert_eq!(batch3[0].id, 50);
        assert_eq!(batch3[49].id, 99);

        // Queue should be empty
        assert!(queue.is_empty().await.unwrap());
        let empty_batch = queue.recv_batch(10).await.unwrap();
        assert!(empty_batch.is_empty());
    }

    #[tokio::test]
    async fn test_batch_with_channel_api() {
        let temp_file = NamedTempFile::new().unwrap();
        let (tx, mut rx) = disk_backed_channel::<TestMessage, _>(
            temp_file.path(),
            "batch_channel_test".to_string(),
            None,
        )
        .await
        .unwrap();

        // Send batch via sender
        let messages: Vec<TestMessage> = (0..50)
            .map(|i| TestMessage {
                id: i,
                content: format!("Batch {}", i),
            })
            .collect();

        tx.send_batch(messages.clone()).await.unwrap();

        // Receive batch via receiver
        let received = rx.recv_batch(50).await.unwrap();
        assert_eq!(received.len(), 50);
        assert_eq!(received, messages);
    }

    #[tokio::test]
    async fn test_batch_performance_comparison() {
        let temp_file1 = NamedTempFile::new().unwrap();
        let temp_file2 = NamedTempFile::new().unwrap();
        let queue_single = DiskBackedQueue::new(temp_file1.path(), "single".to_string(), None)
            .await
            .unwrap();
        let queue_batch = DiskBackedQueue::new(temp_file2.path(), "batch".to_string(), None)
            .await
            .unwrap();

        let message_count = 1000;
        let messages: Vec<TestMessage> = (0..message_count)
            .map(|i| TestMessage {
                id: i,
                content: format!("Perf test {}", i),
            })
            .collect();

        // Single send
        let start = std::time::Instant::now();
        for msg in messages.clone() {
            queue_single.send(msg).await.unwrap();
        }
        let single_duration = start.elapsed();

        // Batch send
        let start = std::time::Instant::now();
        queue_batch.send_batch(messages.clone()).await.unwrap();
        let batch_duration = start.elapsed();

        println!(
            "Single send: {:?}, Batch send: {:?}, Speedup: {:.2}x",
            single_duration,
            batch_duration,
            single_duration.as_secs_f64() / batch_duration.as_secs_f64()
        );

        // Batch should be significantly faster (at least 5x)
        assert!(batch_duration < single_duration / 5);
    }

    #[tokio::test]
    async fn test_batch_with_corrupted_data() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue = DiskBackedQueue::new(temp_file.path(), "batch_corrupt_test".to_string(), None)
            .await
            .unwrap();

        // Send 10 valid messages
        for i in 0..10 {
            let msg = TestMessage {
                id: i,
                content: format!("Valid {}", i),
            };
            queue.send(msg).await.unwrap();
        }

        // Corrupt message 5
        {
            let db = queue.db.lock().unwrap();
            let garbage_data: Vec<u8> = vec![0xFF, 0xFE, 0xFD, 0xFC];
            db.execute(
                "UPDATE batch_corrupt_test SET data = ? WHERE id = 6",
                [&garbage_data],
            )
            .unwrap();
        }

        // Batch receive should skip corrupted message and move it to DLQ
        let batch = queue.recv_batch(10).await.unwrap();
        assert_eq!(batch.len(), 9); // Should get 9 valid messages (10 - 1 corrupted)

        // Verify DLQ has 1 entry
        let dlq_path = temp_file.path().with_extension("dlq.db");
        let dlq_conn = rusqlite::Connection::open(&dlq_path).unwrap();
        let dlq_count: i64 = dlq_conn
            .query_row("SELECT COUNT(*) FROM batch_corrupt_test_dlq", [], |row| {
                row.get(0)
            })
            .unwrap();
        assert_eq!(dlq_count, 1);
    }

    /// Concurrent senders racing into a bounded queue must never exceed `max_size`.
    ///
    /// Before the BEGIN IMMEDIATE fix, two senders that both observed `count == max-1`
    /// could both insert and push the queue to `max+1`. With the atomic count+insert,
    /// `len()` should never exceed `max` from any observation point.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn test_max_size_never_exceeded_under_contention() {
        const MAX: usize = 10;
        const SENDERS: usize = 50;
        const PER_SENDER: usize = 5;

        let temp_file = NamedTempFile::new().unwrap();
        // Use the queue directly so both producer and consumer can share it via Arc.
        let queue = std::sync::Arc::new(
            DiskBackedQueue::<TestMessage>::new(
                temp_file.path(),
                "race_test".to_string(),
                Some(MAX),
            )
            .await
            .unwrap(),
        );

        // Spawn a sampler that watches `len()` while senders contend. If the bound is
        // ever violated, the assertion fires here.
        let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let sampler = {
            let queue = queue.clone();
            let stop = stop.clone();
            tokio::spawn(async move {
                while !stop.load(std::sync::atomic::Ordering::Relaxed) {
                    let len = queue.len().await.unwrap();
                    assert!(
                        len <= MAX,
                        "Queue length {len} exceeded max_size {MAX} — race fix regressed"
                    );
                    tokio::time::sleep(tokio::time::Duration::from_millis(2)).await;
                }
            })
        };

        // Spawn senders that race to fill the queue. Senders block when full and unblock
        // as a separate consumer task drains items.
        let mut send_handles = Vec::new();
        for sender_id in 0..SENDERS {
            let queue = queue.clone();
            send_handles.push(tokio::spawn(async move {
                for j in 0..PER_SENDER {
                    queue
                        .send(TestMessage {
                            id: (sender_id * PER_SENDER + j) as u64,
                            content: String::new(),
                        })
                        .await
                        .unwrap();
                }
            }));
        }

        // Drain in the background so senders eventually unblock.
        let drainer = {
            let queue = queue.clone();
            tokio::spawn(async move {
                let total = SENDERS * PER_SENDER;
                let mut received = 0;
                while received < total {
                    if queue.recv().await.unwrap().is_some() {
                        received += 1;
                    } else {
                        tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
                    }
                }
            })
        };

        for h in send_handles {
            h.await.unwrap();
        }
        drainer.await.unwrap();

        stop.store(true, std::sync::atomic::Ordering::Relaxed);
        sampler.await.unwrap();

        assert!(queue.is_empty().await.unwrap());
    }

    /// `blocking_send` from a current-thread runtime must not panic.
    ///
    /// `tokio::task::block_in_place` panics on the current-thread runtime, so the
    /// implementation falls back to a temporary runtime in that case.
    #[tokio::test] // default flavor is current_thread
    async fn test_blocking_send_on_current_thread_runtime() {
        let temp_file = NamedTempFile::new().unwrap();
        let (tx, mut rx) = disk_backed_channel::<TestMessage, _>(
            temp_file.path(),
            "blocking_ct_test".to_string(),
            None,
        )
        .await
        .unwrap();

        let msg = TestMessage {
            id: 7,
            content: "from current-thread".to_string(),
        };

        // Run blocking_send on a separate OS thread so we don't block the current-thread
        // runtime that's hosting the test (which would deadlock the recv below).
        let tx_clone = tx.clone();
        let msg_clone = msg.clone();
        let handle =
            std::thread::spawn(move || tx_clone.blocking_send(msg_clone));
        // Hand back to the runtime so the spawned thread can make progress on its own
        // temporary runtime without contention with this current_thread executor.
        tokio::task::yield_now().await;
        handle.join().expect("thread panicked").unwrap();

        let received = rx.recv().await.unwrap().unwrap();
        assert_eq!(received, msg);
    }

    /// `send_batch` rejects batches larger than `max_size` immediately rather than
    /// blocking forever waiting for space that can never exist.
    #[tokio::test]
    async fn test_send_batch_rejects_oversized_batch() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue =
            DiskBackedQueue::new(temp_file.path(), "oversize_test".to_string(), Some(5))
                .await
                .unwrap();

        let oversized: Vec<TestMessage> = (0..10)
            .map(|i| TestMessage {
                id: i,
                content: String::new(),
            })
            .collect();

        let result = queue.send_batch(oversized).await;
        assert!(
            matches!(result, Err(DiskQueueError::QueueFull(5))),
            "Expected QueueFull(5), got: {result:?}"
        );

        // Queue should still be empty and usable.
        assert!(queue.is_empty().await.unwrap());
        let small_batch = vec![
            TestMessage {
                id: 0,
                content: String::new(),
            };
            3
        ];
        queue.send_batch(small_batch).await.unwrap();
        assert_eq!(queue.len().await.unwrap(), 3);
    }

    /// `clear()` removes all messages, leaves the queue functional for further sends/recvs,
    /// and is a no-op when the queue is already empty.
    #[tokio::test]
    async fn test_clear_empties_queue_and_remains_usable() {
        let temp_file = NamedTempFile::new().unwrap();
        let queue =
            DiskBackedQueue::new(temp_file.path(), "clear_test".to_string(), None)
                .await
                .unwrap();

        // Clear on an empty queue should be a no-op.
        queue.clear().await.unwrap();
        assert!(queue.is_empty().await.unwrap());

        // Populate, clear, verify empty.
        for i in 0..25 {
            queue
                .send(TestMessage {
                    id: i,
                    content: format!("msg {i}"),
                })
                .await
                .unwrap();
        }
        assert_eq!(queue.len().await.unwrap(), 25);

        queue.clear().await.unwrap();
        assert!(queue.is_empty().await.unwrap());
        assert_eq!(queue.len().await.unwrap(), 0);
        assert!(queue.recv().await.unwrap().is_none());

        // Queue must remain usable after clear.
        let after = TestMessage {
            id: 999,
            content: "after-clear".to_string(),
        };
        queue.send(after.clone()).await.unwrap();
        assert_eq!(queue.recv().await.unwrap().unwrap(), after);
    }
}