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
//! ReadonlyRefgetStore struct definition and core methods.
use super::*;
use super::alias::AliasManager;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};
use indexmap::IndexMap;
use anyhow::{anyhow, Context, Result};
use crate::collection::{read_rgsi_file, SequenceMetadataExt, SequenceRecordExt};
use crate::digest::lookup_alphabet;
use crate::digest::{
SequenceCollectionMetadata, SequenceCollectionRecord, SequenceMetadata,
SequenceRecord,
};
use crate::digest::{decode_string_from_bytes, decode_substring_from_bytes, encode_sequence, StreamingDecoder};
use crate::digest::{byte_range_for_bases, decode_substring_from_bytes_at_offset};
use crate::hashkeyable::{DigestKey, HashKeyable, key_to_digest_string};
use crate::seqcol::metadata_matches_attribute;
use std::fs::{self, create_dir_all, File};
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::sync::{Arc, Mutex};
/// Default capacity of the per-store open-file-descriptor cache used by the
/// partial-read path. MUST stay well under typical OS fd limits (often 1024):
/// stores can have huge sequence counts (e.g. ~122k for a transcriptome), so an
/// unbounded cache would exhaust file descriptors. Mirrors seqrepo's
/// `fd_cache_size`.
const SEQ_FD_CACHE_CAP: usize = 256;
/// A tiny bounded LRU cache mapping a sequence digest to an open, shared
/// `.seq` file handle. `.seq` content for a digest is immutable, so a cached
/// handle never goes stale and needs no invalidation. When the cache is full,
/// inserting a new handle evicts (closes) the least-recently-used one.
///
/// Recency is tracked with a monotonically increasing counter stamped on each
/// access; eviction scans for the minimum stamp. With a small cap (default 256)
/// this O(n) eviction is negligible compared to the avoided `open`/`seek`
/// syscalls on the hot path.
#[derive(Debug)]
struct FdCache {
cap: usize,
tick: u64,
/// digest key -> (shared file handle, last-use stamp)
entries: HashMap<DigestKey, (Arc<File>, u64)>,
}
impl FdCache {
fn new(cap: usize) -> Self {
FdCache {
cap: cap.max(1),
tick: 0,
entries: HashMap::with_capacity(cap.max(1)),
}
}
/// Return a cloned handle on hit, bumping recency. `None` on miss.
fn get(&mut self, key: &DigestKey) -> Option<Arc<File>> {
self.tick += 1;
let tick = self.tick;
if let Some(slot) = self.entries.get_mut(key) {
slot.1 = tick;
Some(Arc::clone(&slot.0))
} else {
None
}
}
/// Insert a freshly opened handle, evicting (closing) the LRU entry if full.
fn insert(&mut self, key: DigestKey, file: Arc<File>) {
self.tick += 1;
let tick = self.tick;
if !self.entries.contains_key(&key) && self.entries.len() >= self.cap {
// Evict the least-recently-used entry. Dropping the Arc closes the
// fd once no in-flight read still holds a clone.
if let Some(lru_key) = self
.entries
.iter()
.min_by_key(|(_, (_, stamp))| *stamp)
.map(|(k, _)| *k)
{
self.entries.remove(&lru_key);
}
}
self.entries.insert(key, (file, tick));
}
}
/// A `Read` adapter that yields a byte range from a shared `Arc<Vec<u8>>`
/// buffer without cloning the backing data.
///
/// Used by `stream_sequence` when the target sequence is already resident
/// in memory as `SequenceRecord::Full`. The alternative of slicing+cloning
/// would allocate a second copy of the (possibly chromosome-sized) buffer
/// and defeat the O(1) memory guarantee of streaming.
pub(crate) struct ArcSliceReader {
buf: Arc<Vec<u8>>,
pos: usize,
end: usize,
}
impl ArcSliceReader {
pub(crate) fn new(buf: Arc<Vec<u8>>, start: usize, end: usize) -> Self {
debug_assert!(start <= end);
debug_assert!(end <= buf.len());
Self { buf, pos: start, end }
}
}
impl Read for ArcSliceReader {
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
let remaining = self.end - self.pos;
if remaining == 0 {
return Ok(0);
}
let n = remaining.min(out.len());
out[..n].copy_from_slice(&self.buf[self.pos..self.pos + n]);
self.pos += n;
Ok(n)
}
}
/// Open a bounded HTTP byte range against a remote refget store and return
/// a `Read` yielding exactly `byte_end - byte_start` bytes.
#[cfg(feature = "http")]
fn open_remote_range(
remote: &str,
relpath: &Path,
byte_start: u64,
byte_end: u64,
) -> Result<Box<dyn Read + Send>> {
let relpath_str = relpath
.to_str()
.ok_or_else(|| anyhow!("Non-UTF8 sequence path"))?;
let url = if remote.ends_with('/') {
format!("{}{}", remote, relpath_str)
} else {
format!("{}/{}", remote, relpath_str)
};
let byte_len = byte_end - byte_start;
let last_byte = byte_end.saturating_sub(1);
let agent: ureq::Agent = ureq::Agent::config_builder()
.timeout_connect(Some(std::time::Duration::from_secs(30)))
.timeout_recv_response(Some(std::time::Duration::from_secs(60)))
.build()
.into();
let response = agent
.get(&url)
.header("Range", &format!("bytes={}-{}", byte_start, last_byte))
.call()
.map_err(|e| anyhow!("Failed to open remote byte range: {}", e))?;
// Guard against servers that ignore the Range header and return the full
// body with HTTP 200 instead of a 206 Partial Content slice. Wrapping the
// full body here would silently produce wrong bases, so refuse outright.
let status = response.status();
if status.as_u16() != 206 {
return Err(anyhow!(
"Remote server did not honor Range header (got HTTP {}); \
refusing to stream to avoid silent data corruption. URL: {}",
status,
url
));
}
Ok(Box::new(BufReader::with_capacity(
64 * 1024,
response.into_body().into_reader().take(byte_len),
)))
}
#[cfg(not(feature = "http"))]
fn open_remote_range(
_remote: &str,
_relpath: &Path,
_byte_start: u64,
_byte_end: u64,
) -> Result<Box<dyn Read + Send>> {
Err(anyhow!(
"Remote streaming requires the 'http' feature to be enabled"
))
}
/// Core refget store with `&self` read methods, suitable for `Arc` sharing in servers.
///
/// Mutating methods are used during the setup/loading phase; once wrapped in `Arc`,
/// only `&self` reads are accessible, making concurrent access thread-safe.
///
/// Holds a global sequence_store with all sequences (across collections) deduplicated.
/// This allows lookup by sequence digest directly (bypassing collection information).
/// Also holds a collections hashmap, to provide lookup by collection+name.
#[derive(Debug)]
pub struct ReadonlyRefgetStore {
/// SHA512t24u digest -> SequenceRecord (metadata + optional data)
pub(crate) sequence_store: HashMap<DigestKey, SequenceRecord>,
/// MD5 digest -> SHA512t24u digest lookup
pub(crate) md5_lookup: HashMap<DigestKey, DigestKey>,
/// Collection digest -> {name -> SHA512t24u digest} (IndexMap preserves FASTA insertion order)
pub(crate) name_lookup: HashMap<DigestKey, IndexMap<String, DigestKey>>,
/// Active sequence collections (now using SequenceCollectionRecord for Stub/Full pattern)
pub(crate) collections: HashMap<DigestKey, SequenceCollectionRecord>,
/// Storage strategy for sequences
pub(crate) mode: StorageMode,
/// Where the store lives on disk (local store or cache directory)
pub(crate) local_path: Option<PathBuf>,
/// Where to pull sequences from (if remote-backed)
pub(crate) remote_source: Option<String>,
/// Template for sequence file paths (e.g., "sequences/%s2/%s.seq")
pub(crate) seqdata_path_template: Option<String>,
/// Whether to persist sequences to disk (write-through caching)
pub(crate) persist_to_disk: bool,
/// Whether to suppress progress output
pub(crate) quiet: bool,
/// Whether to compute ancillary digests (nlp, snlp, sorted_sequences).
/// Default: true for new stores.
pub(crate) ancillary_digests: bool,
/// Whether on-disk attribute reverse index is enabled.
/// Default: false. Part 2 implements the indexed path.
pub(crate) attribute_index: bool,
/// Human-readable aliases for sequences and collections.
pub(crate) aliases: AliasManager,
/// FHR metadata for collections, keyed by collection digest.
pub(crate) fhr_metadata: HashMap<DigestKey, super::fhr_metadata::FhrMetadata>,
/// Available sequence alias namespaces (from manifest, for remote discovery).
pub(crate) available_sequence_alias_namespaces: Vec<String>,
/// Available collection alias namespaces (from manifest, for remote discovery).
pub(crate) available_collection_alias_namespaces: Vec<String>,
/// Bounded LRU cache of open `.seq` file handles for the partial-read path,
/// behind a `Mutex` because `get_substring` takes `&self`. Avoids re-opening
/// the same chromosome file on every per-region `get_substring` call.
seq_fd_cache: Mutex<FdCache>,
/// Whether the sequence index (sequences.rgsi) has been loaded.
/// For remote stores, this starts as `false` and is lazily loaded on first
/// sequence access, avoiding the costly download when only browsing collections.
pub(crate) sequence_index_loaded: bool,
/// The relative path to the sequence index file (from rgstore.json),
/// stored for deferred loading in remote stores.
pub(crate) sequence_index_path: Option<String>,
}
impl ReadonlyRefgetStore {
/// Generic constructor. Creates a new, empty `ReadonlyRefgetStore`.
/// Internal only - users should go through RefgetStore.
pub(crate) fn new(mode: StorageMode) -> Self {
ReadonlyRefgetStore {
sequence_store: HashMap::new(),
md5_lookup: HashMap::new(),
name_lookup: HashMap::new(),
collections: HashMap::new(),
mode,
local_path: None,
remote_source: None,
seqdata_path_template: None,
persist_to_disk: false,
quiet: false,
ancillary_digests: true,
attribute_index: false,
aliases: AliasManager::default(),
fhr_metadata: HashMap::new(),
available_sequence_alias_namespaces: Vec::new(),
available_collection_alias_namespaces: Vec::new(),
seq_fd_cache: Mutex::new(FdCache::new(SEQ_FD_CACHE_CAP)),
sequence_index_loaded: true,
sequence_index_path: None,
}
}
/// Test-only: shrink the partial-read fd-cache to a tiny capacity so the
/// eviction/reopen path is exercised deterministically. Only used by the
/// filesystem-gated partial-read tests.
#[cfg(all(test, feature = "filesystem"))]
pub(crate) fn set_seq_fd_cache_cap_for_test(&self, cap: usize) {
let mut cache = self.seq_fd_cache.lock().unwrap();
*cache = FdCache::new(cap);
}
/// Set whether to suppress progress output.
pub fn set_quiet(&mut self, quiet: bool) {
self.quiet = quiet;
}
/// Returns whether the store is in quiet mode.
pub fn is_quiet(&self) -> bool {
self.quiet
}
/// Check whether a valid RefgetStore exists at the given path.
pub fn store_exists<P: AsRef<Path>>(path: P) -> bool {
path.as_ref().join("rgstore.json").exists()
}
/// Change the storage mode, re-encoding/decoding existing sequences as needed.
pub fn set_encoding_mode(&mut self, new_mode: StorageMode) {
if self.mode == new_mode {
return;
}
for record in self.sequence_store.values_mut() {
match record {
SequenceRecord::Full { metadata, sequence } => {
match (self.mode, new_mode) {
(StorageMode::Raw, StorageMode::Encoded) => {
let alphabet = lookup_alphabet(&metadata.alphabet);
*sequence = std::sync::Arc::new(encode_sequence(&sequence[..], alphabet));
}
(StorageMode::Encoded, StorageMode::Raw) => {
let alphabet = lookup_alphabet(&metadata.alphabet);
*sequence = std::sync::Arc::new(decode_string_from_bytes(
&sequence[..],
metadata.length,
alphabet,
));
}
_ => {}
}
}
SequenceRecord::Stub(_) => {}
}
}
self.mode = new_mode;
}
/// Enable 2-bit encoding for space efficiency.
pub fn enable_encoding(&mut self) {
self.set_encoding_mode(StorageMode::Encoded);
}
/// Disable encoding, use raw byte storage.
pub fn disable_encoding(&mut self) {
self.set_encoding_mode(StorageMode::Raw);
}
/// Enable disk persistence for this store.
pub fn enable_persistence<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let path = path.as_ref();
self.local_path = Some(path.to_path_buf());
self.persist_to_disk = true;
self.seqdata_path_template
.get_or_insert_with(|| DEFAULT_SEQDATA_PATH_TEMPLATE.to_string());
create_dir_all(path.join("sequences"))?;
create_dir_all(path.join("collections"))?;
let keys: Vec<DigestKey> = self.sequence_store.keys().cloned().collect();
for key in keys {
if let Some(SequenceRecord::Full { metadata, sequence }) = self.sequence_store.get(&key)
{
self.write_sequence_to_disk_single(metadata, sequence)?;
let stub = SequenceRecord::Stub(metadata.clone());
self.sequence_store.insert(key, stub);
}
}
for record in self.collections.values() {
self.write_collection_to_disk_single(record)?;
}
self.write_index_files()?;
Ok(())
}
/// Disable disk persistence for this store.
pub fn disable_persistence(&mut self) {
self.persist_to_disk = false;
}
/// Check if persistence to disk is enabled.
pub fn is_persisting(&self) -> bool {
self.persist_to_disk
}
/// Adds a sequence to the Store
pub fn add_sequence<T: Into<Option<DigestKey>>>(
&mut self,
sequence_record: SequenceRecord,
collection_digest: T,
force: bool,
) -> Result<()> {
let collection_digest = collection_digest
.into()
.ok_or_else(|| anyhow::anyhow!("Collection digest is required"))?;
self.collections.get(&collection_digest).ok_or_else(|| {
anyhow::anyhow!("Collection not found for digest: {:?}", collection_digest)
})?;
let metadata = sequence_record.metadata();
self.name_lookup
.entry(collection_digest)
.or_default()
.insert(metadata.name.clone(), metadata.sha512t24u.to_key());
self.add_sequence_record(sequence_record, force)?;
Ok(())
}
/// Adds a collection, and all sequences in it, to the store.
pub fn add_sequence_collection(
&mut self,
collection: crate::digest::SequenceCollection,
) -> Result<()> {
self.add_sequence_collection_internal(collection, false)
}
/// Adds a collection, overwriting existing data.
pub fn add_sequence_collection_force(
&mut self,
collection: crate::digest::SequenceCollection,
) -> Result<()> {
self.add_sequence_collection_internal(collection, true)
}
/// Internal implementation for adding a sequence collection.
pub(crate) fn add_sequence_collection_internal(
&mut self,
collection: crate::digest::SequenceCollection,
force: bool,
) -> Result<()> {
let coll_digest = collection.metadata.digest.to_key();
if !force && self.collections.contains_key(&coll_digest) {
return Ok(());
}
let crate::digest::SequenceCollection { metadata, sequences } = collection;
let record = SequenceCollectionRecord::Full {
metadata: metadata.clone(),
sequences: sequences.iter().map(|s| SequenceRecord::Stub(s.metadata().clone())).collect(),
};
if self.persist_to_disk && self.local_path.is_some() {
self.write_collection_to_disk_single(&record)?;
}
self.collections.insert(coll_digest, record);
for sequence_record in sequences {
self.add_sequence(sequence_record, coll_digest, force)?;
}
if self.persist_to_disk && self.local_path.is_some() {
self.write_index_files()?;
}
Ok(())
}
/// Adds a SequenceRecord directly to the store without collection association.
pub fn add_sequence_record(&mut self, sr: SequenceRecord, force: bool) -> Result<()> {
let metadata = sr.metadata();
let key = metadata.sha512t24u.to_key();
// CONTRACT: ingested sequence bytes are ASCII (the refget digest and the
// on-the-fly decode paths assume one byte == one residue). Enforced in
// debug builds only to keep the ingestion hot path allocation/branch
// free in release.
debug_assert!(
match &sr {
SequenceRecord::Full { sequence, .. } => sequence.is_ascii(),
SequenceRecord::Stub(_) => true,
},
"add_sequence_record: sequence bytes must be ASCII"
);
if !force && self.sequence_store.contains_key(&key) {
return Ok(());
}
self.md5_lookup
.insert(metadata.md5.to_key(), metadata.sha512t24u.to_key());
if self.persist_to_disk && self.local_path.is_some() {
match &sr {
SequenceRecord::Full { metadata, sequence } => {
self.write_sequence_to_disk_single(metadata, sequence)?;
let stub = SequenceRecord::Stub(metadata.clone());
self.sequence_store.insert(key, stub);
return Ok(());
}
SequenceRecord::Stub(_) => {}
}
}
self.sequence_store.insert(key, sr);
Ok(())
}
/// Inserter-side half of [`add_sequence_record`](Self::add_sequence_record)
/// for the parallel streaming import: do the FAST in-memory dedup decision
/// and map/`md5_lookup` insert here (single inserter, so no race on the
/// dedup truth), but DO NOT write the `.seq` to disk. Instead return the
/// expanded full path + bytes so the caller can dispatch the (independent,
/// per-digest) disk write to a bounded writer pool.
///
/// Returns `Some((full_path, bytes))` when a `.seq` write must be performed
/// (disk-backed store, `Full` record, and either a first occurrence or a
/// forced overwrite), or `None` when nothing needs to be written to disk
/// (in-memory mode, a `Stub`, or a dedup hit). The in-memory state is fully
/// updated before returning, so dedup stays the single source of truth on
/// the inserter thread.
#[cfg_attr(not(feature = "filesystem"), allow(dead_code))]
pub(crate) fn add_sequence_record_deferred_write(
&mut self,
sr: SequenceRecord,
force: bool,
) -> Result<Option<(PathBuf, Vec<u8>)>> {
let metadata = sr.metadata();
let key = metadata.sha512t24u.to_key();
if !force && self.sequence_store.contains_key(&key) {
return Ok(None);
}
self.md5_lookup
.insert(metadata.md5.to_key(), metadata.sha512t24u.to_key());
if self.persist_to_disk && self.local_path.is_some() {
match sr {
SequenceRecord::Full { metadata, sequence } => {
// Insert the stub into the map NOW (reserve), so the dedup
// decision is committed on the inserter thread. The actual
// byte write is dispatched to the writer pool by the caller.
let full_path = self
.sequence_file_path(&metadata.sha512t24u)
.context("seqdata_path_template/local_path not set")?;
let stub = SequenceRecord::Stub(metadata);
self.sequence_store.insert(key, stub);
// INVARIANT: strong_count == 1 here — the Arc was just
// constructed in the import pipeline (Arc::new in import.rs)
// and no other owner exists at the point of this call, so
// try_unwrap always succeeds and reclaims the buffer with zero
// copy. The clone fallback is dead code / future-proofing only.
debug_assert_eq!(
std::sync::Arc::strong_count(&sequence),
1,
"sequence Arc must be uniquely owned for zero-copy reclaim"
);
let bytes = std::sync::Arc::try_unwrap(sequence)
.unwrap_or_else(|arc| (*arc).clone());
return Ok(Some((full_path, bytes)));
}
SequenceRecord::Stub(s) => {
self.sequence_store.insert(key, SequenceRecord::Stub(s));
return Ok(None);
}
}
}
self.sequence_store.insert(key, sr);
Ok(None)
}
// =========================================================================
// Sequence query methods
// =========================================================================
/// Returns an iterator over all sequence digests in the store
pub fn sequence_digests(&self) -> impl Iterator<Item = DigestKey> + '_ {
self.sequence_store.keys().cloned()
}
/// Returns an iterator over sequence metadata for all sequences in the store.
pub fn sequence_metadata(&self) -> impl Iterator<Item = &SequenceMetadata> + '_ {
self.sequence_store.values().map(|rec| rec.metadata())
}
/// Calculate the total disk size of all sequences in the store
pub fn total_disk_size(&self) -> usize {
self.sequence_store
.values()
.map(|rec| rec.metadata().disk_size(&self.mode))
.sum()
}
/// Returns the actual disk usage of the store directory.
pub fn actual_disk_usage(&self) -> usize {
let Some(path) = &self.local_path else {
return 0;
};
fn dir_size(path: &std::path::Path) -> usize {
let mut total = 0;
if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
total += entry.metadata().map(|m| m.len() as usize).unwrap_or(0);
} else if path.is_dir() {
total += dir_size(&path);
}
}
}
total
}
dir_size(path)
}
// =========================================================================
// Collection API
// =========================================================================
/// List collections with pagination and optional attribute filtering.
pub fn list_collections(
&self,
page: usize,
page_size: usize,
filters: &[(&str, &str)],
) -> Result<PagedResult<SequenceCollectionMetadata>> {
let mut filtered: Vec<SequenceCollectionMetadata> = Vec::new();
for record in self.collections.values() {
let meta = record.metadata();
let mut passes = true;
for &(attr_name, attr_digest) in filters {
if !metadata_matches_attribute(meta, attr_name, attr_digest)? {
passes = false;
break;
}
}
if passes {
filtered.push(meta.clone());
}
}
filtered.sort_by(|a, b| a.digest.cmp(&b.digest));
let total = filtered.len();
let start = page * page_size;
let results = if start < total {
filtered.into_iter().skip(start).take(page_size).collect()
} else {
Vec::new()
};
Ok(PagedResult {
results,
pagination: Pagination {
page,
page_size,
total,
},
})
}
/// Get metadata for a single collection by digest (no sequence data).
pub fn get_collection_metadata<K: AsRef<[u8]>>(
&self,
collection_digest: K,
) -> Option<&SequenceCollectionMetadata> {
let key = collection_digest.to_key();
self.collections.get(&key).map(|record| record.metadata())
}
/// Get a collection with all its sequences loaded.
pub fn get_collection(&self, collection_digest: &str) -> Result<crate::digest::SequenceCollection> {
let key = collection_digest.to_key();
if !self.name_lookup.contains_key(&key) {
return Err(anyhow!(
"Collection not loaded: {}. Call load_collection() or load_all_collections() first.",
collection_digest
));
}
let metadata = self
.collections
.get(&key)
.ok_or_else(|| anyhow!("Collection not found: {}", collection_digest))?
.metadata()
.clone();
// Iterate name_lookup for (name, digest) pairs so each record gets the
// correct per-collection name, not the last-written global name.
let sequences: Vec<SequenceRecord> = self
.name_lookup
.get(&key)
.map(|name_map| {
name_map
.iter()
.map(|(name, seq_key)| {
let record = self.sequence_store.get(seq_key).ok_or_else(|| {
anyhow!(
"Sequence {} not found in store for collection {}",
key_to_digest_string(seq_key),
collection_digest,
)
})?;
let mut meta = record.metadata().clone();
meta.name = name.clone();
Ok(match record.sequence_arc() {
Some(seq) => SequenceRecord::Full {
metadata: meta,
sequence: seq,
},
None => SequenceRecord::Stub(meta),
})
})
.collect::<Result<Vec<_>>>()
})
.transpose()?
.unwrap_or_default();
Ok(crate::digest::SequenceCollection {
metadata,
sequences,
})
}
/// Remove a collection from the store.
pub fn remove_collection(
&mut self,
digest: &str,
remove_orphan_sequences: bool,
) -> Result<bool> {
let key = digest.to_key();
if self.collections.remove(&key).is_none() {
return Ok(false);
}
let orphan_candidates: Vec<DigestKey> = self
.name_lookup
.get(&key)
.map(|name_map| name_map.values().cloned().collect())
.unwrap_or_default();
self.name_lookup.remove(&key);
self.fhr_metadata.remove(&key);
// Remove collection aliases pointing to this digest
let alias_pairs = self.aliases.reverse_lookup_collection(digest);
let affected_namespaces: std::collections::HashSet<String> = alias_pairs
.iter()
.map(|(ns, _)| ns.clone())
.collect();
for (ns, alias) in &alias_pairs {
self.aliases.remove_collection(ns, alias);
}
for ns in &affected_namespaces {
self.persist_alias_namespace(AliasKind::Collection, ns)?;
}
if remove_orphan_sequences && !orphan_candidates.is_empty() {
let mut still_referenced: std::collections::HashSet<DigestKey> =
std::collections::HashSet::new();
for name_map in self.name_lookup.values() {
for seq_key in name_map.values() {
still_referenced.insert(*seq_key);
}
}
let orphans: Vec<DigestKey> = orphan_candidates
.into_iter()
.filter(|k| !still_referenced.contains(k))
.collect();
for orphan_key in &orphans {
self.sequence_store.remove(orphan_key);
self.md5_lookup.retain(|_, v| v != orphan_key);
}
if self.persist_to_disk {
if let (Some(local_path), Some(template)) =
(&self.local_path, &self.seqdata_path_template)
{
for orphan_key in &orphans {
let orphan_digest = key_to_digest_string(orphan_key);
let seq_file_path = Self::expand_template(&orphan_digest, template);
let full_path = local_path.join(&seq_file_path);
let _ = fs::remove_file(&full_path);
if let Some(parent) = full_path.parent() {
let _ = fs::remove_dir(parent);
}
}
}
}
}
if self.persist_to_disk {
if let Some(local_path) = &self.local_path {
let rgsi_path = local_path.join(format!("collections/{}.rgsi", digest));
let _ = fs::remove_file(&rgsi_path);
let fhr_path = local_path.join(format!("fhr/{}.fhr.json", digest));
let _ = fs::remove_file(&fhr_path);
}
self.write_index_files()?;
}
Ok(true)
}
/// Remove any sequence from `sequence_store` (and its `.seq` file on disk)
/// that is not referenced by any entry in `self.collections`.
///
/// Used as a best-effort cleanup after a failed import:
/// [`add_sequence_collections_from_fastas`](Self::add_sequence_collections_from_fastas)
/// may have inserted sequences for a partially-built collection that never
/// received an `End` message (and was therefore never added to
/// `self.collections`). Those sequences are orphans — they have no
/// owning collection — and this method removes them so the store stays
/// internally consistent. The operation is O(sequences + collections).
///
/// Sequences shared with a successfully-finalized collection are preserved.
#[cfg_attr(not(feature = "filesystem"), allow(dead_code))]
pub(crate) fn remove_orphan_seq_files(&mut self) {
// Build the set of all sequence digest keys that are referenced by at
// least one surviving (finalized) collection.
let mut referenced: std::collections::HashSet<DigestKey> =
std::collections::HashSet::new();
for name_map in self.name_lookup.values() {
for &seq_key in name_map.values() {
referenced.insert(seq_key);
}
}
// Collect the orphan keys (present in sequence_store but not referenced).
let orphans: Vec<DigestKey> = self
.sequence_store
.keys()
.filter(|k| !referenced.contains(*k))
.copied()
.collect();
if orphans.is_empty() {
return;
}
// Remove from in-memory structures.
for key in &orphans {
self.sequence_store.remove(key);
self.md5_lookup.retain(|_, v| v != key);
}
// Best-effort remove on-disk `.seq` files.
if self.persist_to_disk {
if let (Some(local_path), Some(template)) =
(&self.local_path, &self.seqdata_path_template)
{
for key in &orphans {
let digest_str = key_to_digest_string(key);
let rel = Self::expand_template(&digest_str, template);
let full = local_path.join(&rel);
let _ = fs::remove_file(&full);
if let Some(parent) = full.parent() {
let _ = fs::remove_dir(parent); // ignore if non-empty
}
}
}
}
}
// =========================================================================
// Import from another store
// =========================================================================
/// Import a single collection (with all its sequences, aliases, and FHR
/// metadata) from another store into this store.
///
/// Both stores must be disk-backed with matching storage modes.
/// The source store must have the collection loaded (call
/// `load_collection()` or `load_all_collections()` first).
pub fn import_collection(&mut self, source: &ReadonlyRefgetStore, digest: &str) -> Result<()> {
// Both stores must be disk-backed with same mode
if source.local_path.is_none() || self.local_path.is_none() || !self.persist_to_disk {
return Err(anyhow!("import_collection requires both stores to be disk-backed"));
}
if source.mode != self.mode {
return Err(anyhow!(
"import_collection requires matching storage modes (source={:?}, dest={:?})",
source.mode,
self.mode,
));
}
self.import_collection_file_copy(source, digest)?;
// Copy sequence aliases for every sequence in the imported collection
let coll_key = digest.to_key();
if let Some(name_map) = source.name_lookup.get(&coll_key) {
for seq_key in name_map.values() {
let seq_digest = key_to_digest_string(seq_key);
for (ns, alias) in source.aliases.reverse_lookup_sequence(&seq_digest) {
self.add_sequence_alias(&ns, &alias, &seq_digest)?;
}
}
}
// Copy collection aliases
for (ns, alias) in source.aliases.reverse_lookup_collection(digest) {
self.add_collection_alias(&ns, &alias, digest)?;
}
// Copy FHR metadata
if let Some(fhr) = source.get_fhr_metadata(digest) {
self.set_fhr_metadata(digest, fhr.clone())?;
}
Ok(())
}
/// File-copy based import: copies RGSI and .seq files directly from
/// source to destination, then registers the collection in memory.
fn import_collection_file_copy(
&mut self,
source: &ReadonlyRefgetStore,
digest: &str,
) -> Result<()> {
use crate::collection::read_rgsi_file;
// 1. Read the source RGSI file to get collection metadata
let src_rgsi_path = source
.collection_file_path(digest)
.ok_or_else(|| anyhow!("Source store has no local path for collection {}", digest))?;
let collection = read_rgsi_file(&src_rgsi_path)
.with_context(|| format!("Failed to read source RGSI file: {}", src_rgsi_path.display()))?;
let mut metadata = collection.metadata.clone();
// 2. Handle ancillary digests and copy/write the RGSI file
let dst_rgsi_path = self
.collection_file_path(digest)
.ok_or_else(|| anyhow!("Dest store has no local path for collection {}", digest))?;
if let Some(parent) = dst_rgsi_path.parent() {
create_dir_all(parent)?;
}
let needs_ancillary_rewrite = self.ancillary_digests
&& metadata.name_length_pairs_digest.is_none();
if needs_ancillary_rewrite {
// Source lacks ancillary digests but destination wants them --
// compute them and write a new RGSI file.
metadata.compute_ancillary_digests(&collection.sequences);
use crate::collection::SequenceCollectionRecordExt;
let record = SequenceCollectionRecord::Full {
metadata: metadata.clone(),
sequences: collection.sequences.iter()
.map(|s| SequenceRecord::Stub(s.metadata().clone()))
.collect(),
};
record.write_collection_rgsi(&dst_rgsi_path)?;
} else {
// Byte-for-byte copy of the RGSI file
fs::copy(&src_rgsi_path, &dst_rgsi_path)
.with_context(|| format!(
"Failed to copy RGSI {} -> {}",
src_rgsi_path.display(),
dst_rgsi_path.display(),
))?;
}
// 3. Copy sequence data files
for seq_record in &collection.sequences {
let seq_meta = seq_record.metadata();
let seq_digest = &seq_meta.sha512t24u;
let src_seq_path = source
.sequence_file_path(seq_digest)
.ok_or_else(|| anyhow!("Source has no path for sequence {}", seq_digest))?;
let dst_seq_path = self
.sequence_file_path(seq_digest)
.ok_or_else(|| anyhow!("Dest has no path for sequence {}", seq_digest))?;
// Skip if destination already has this sequence (dedup across collections)
if dst_seq_path.exists() {
// Still need to register in memory below
} else {
if let Some(parent) = dst_seq_path.parent() {
create_dir_all(parent)?;
}
fs::copy(&src_seq_path, &dst_seq_path).with_context(|| {
format!(
"Failed to copy sequence {} -> {}",
src_seq_path.display(),
dst_seq_path.display(),
)
})?;
}
}
// 4. Register in memory
let coll_key = digest.to_key();
// Build the collection record with stub sequences
let stub_sequences: Vec<SequenceRecord> = collection
.sequences
.iter()
.map(|s| SequenceRecord::Stub(s.metadata().clone()))
.collect();
let record = SequenceCollectionRecord::Full {
metadata: metadata.clone(),
sequences: stub_sequences,
};
self.collections.insert(coll_key, record);
// Register sequences and populate name_lookup
let mut name_map = IndexMap::new();
for seq_record in &collection.sequences {
let seq_meta = seq_record.metadata();
let seq_key = seq_meta.sha512t24u.to_key();
name_map.insert(seq_meta.name.clone(), seq_key);
// Insert stub into sequence_store (skip if already present -- dedup)
if !self.sequence_store.contains_key(&seq_key) {
self.sequence_store
.insert(seq_key, SequenceRecord::Stub(seq_meta.clone()));
self.md5_lookup
.insert(seq_meta.md5.to_key(), seq_key);
}
}
self.name_lookup.insert(coll_key, name_map);
// 5. Update index files
self.write_index_files()?;
Ok(())
}
// =========================================================================
// Sequence API
// =========================================================================
/// List all sequences in the store (metadata only, no sequence data).
pub fn list_sequences(&self) -> Vec<SequenceMetadata> {
let mut result: Vec<_> = self
.sequence_store
.values()
.map(|rec| rec.metadata().clone())
.collect();
result.sort_by(|a, b| a.sha512t24u.cmp(&b.sha512t24u));
result
}
/// Get metadata for a single sequence by digest (no sequence data).
pub fn get_sequence_metadata<K: AsRef<[u8]>>(
&self,
seq_digest: K,
) -> Option<&SequenceMetadata> {
let key = seq_digest.to_key();
self.sequence_store.get(&key).map(|rec| rec.metadata())
}
/// Get a sequence by its SHA512t24u digest.
pub fn get_sequence<K: AsRef<[u8]>>(&self, seq_digest: K) -> Result<&SequenceRecord> {
let digest_key = seq_digest.to_key();
let actual_key = self
.md5_lookup
.get(&digest_key)
.copied()
.unwrap_or(digest_key);
self.sequence_store.get(&actual_key).ok_or_else(|| {
anyhow!(
"Sequence not found: {}",
String::from_utf8_lossy(seq_digest.as_ref())
)
})
}
/// Clear sequence data from the store to free memory.
///
/// Removes all sequence records. Metadata is preserved:
/// collections, name lookups, MD5 lookups, aliases, and FHR
/// metadata remain intact.
///
/// For on-disk stores, data on disk is unaffected.
pub fn clear(&mut self) {
self.sequence_store.clear();
if let Ok(mut cache) = self.seq_fd_cache.lock() {
cache.entries.clear();
}
}
/// Check whether a sequence's record is loaded (Full) rather
/// than a metadata-only Stub.
pub fn is_sequence_loaded<K: AsRef<[u8]>>(&self, seq_digest: K) -> bool {
let digest_key = seq_digest.to_key();
let actual_key = self
.md5_lookup
.get(&digest_key)
.copied()
.unwrap_or(digest_key);
match self.sequence_store.get(&actual_key) {
Some(SequenceRecord::Stub(_)) | None => false,
Some(SequenceRecord::Full { .. }) => true,
}
}
/// Get a sequence by collection digest and name.
pub fn get_sequence_by_name<K: AsRef<[u8]>>(
&self,
collection_digest: K,
sequence_name: &str,
) -> Result<&SequenceRecord> {
let collection_key = collection_digest.to_key();
if !self.name_lookup.contains_key(&collection_key) {
return Err(anyhow!(
"Collection not loaded. Call load_collection() or load_all_collections() first."
));
}
let digest_key = self.name_lookup.get(&collection_key)
.and_then(|name_map| name_map.get(sequence_name).cloned())
.ok_or_else(|| anyhow!("Sequence '{}' not found in collection", sequence_name))?;
let record = self.sequence_store.get(&digest_key).ok_or_else(|| {
anyhow!("Sequence record not found for '{}'. Call load_sequence() first.", sequence_name)
})?;
Ok(record)
}
// =========================================================================
// Loading methods
// =========================================================================
/// Ensure the sequence index (sequences.rgsi) is loaded.
/// For remote stores this is deferred until first sequence access.
/// Returns Ok(()) immediately if already loaded.
pub(crate) fn ensure_sequence_index_loaded(&mut self) -> Result<()> {
if self.sequence_index_loaded {
return Ok(());
}
let seq_index_path = self.sequence_index_path.clone()
.ok_or_else(|| anyhow!("No sequence_index_path set for deferred loading"))?;
if !self.quiet {
eprintln!("Downloading sequence index {}...", seq_index_path);
}
let data = Self::fetch_file(
&self.local_path,
&self.remote_source,
&seq_index_path,
true,
false,
)?;
let data_str = String::from_utf8(data)
.context("sequence index contains invalid UTF-8")?;
Self::load_sequences_from_reader(self, data_str.as_bytes())?;
self.sequence_index_loaded = true;
Ok(())
}
/// Explicitly load the sequence index. For servers that want to preload
/// all data during startup before converting to `ReadonlyRefgetStore`.
pub fn load_sequence_index(&mut self) -> Result<()> {
self.ensure_sequence_index_loaded()
}
/// Eagerly load all Stub collections to Full.
pub fn load_all_collections(&mut self) -> Result<()> {
let keys: Vec<DigestKey> = self.collections.keys().cloned().collect();
for key in keys {
self.ensure_collection_loaded(&key)?;
}
Ok(())
}
/// Eagerly load all Stub sequences to Full.
pub fn load_all_sequences(&mut self) -> Result<()> {
let keys: Vec<DigestKey> = self.sequence_store.keys().cloned().collect();
for key in keys {
self.ensure_sequence_loaded(&key)?;
}
Ok(())
}
/// Load a single collection by digest.
pub fn load_collection(&mut self, digest: &str) -> Result<()> {
let key = digest.to_key();
self.ensure_collection_loaded(&key)
}
/// Load a single sequence by digest.
pub fn load_sequence(&mut self, digest: &str) -> Result<()> {
let key = digest.to_key();
self.ensure_sequence_loaded(&key)
}
/// Iterate over all collections with their sequences loaded.
pub fn iter_collections(&self) -> impl Iterator<Item = crate::digest::SequenceCollection> + '_ {
let mut digests: Vec<String> = self
.collections
.values()
.map(|rec| rec.metadata().digest.clone())
.collect();
digests.sort();
digests.into_iter().filter_map(move |digest| {
self.get_collection(&digest).ok()
})
}
/// Iterate over all sequences with their data loaded.
pub fn iter_sequences(&self) -> impl Iterator<Item = SequenceRecord> + '_ {
let mut records: Vec<_> = self.sequence_store.values().cloned().collect();
records.sort_by(|a, b| a.metadata().sha512t24u.cmp(&b.metadata().sha512t24u));
records.into_iter()
}
/// Check if a collection is fully loaded.
pub fn is_collection_loaded<K: AsRef<[u8]>>(&self, collection_digest: K) -> bool {
let key = collection_digest.to_key();
self.collections
.get(&key)
.map_or(false, |record| record.has_sequences())
}
/// Returns the local path where the store is located (if any)
pub fn local_path(&self) -> Option<&PathBuf> {
self.local_path.as_ref()
}
/// Returns the remote source URL (if any)
pub fn remote_source(&self) -> Option<&str> {
self.remote_source.as_deref()
}
/// Returns the storage mode used by this store
pub fn storage_mode(&self) -> StorageMode {
self.mode
}
// =========================================================================
// Substring retrieval
// =========================================================================
/// Retrieves a substring from an encoded sequence by its SHA512t24u digest.
pub fn get_substring<K: AsRef<[u8]>>(
&self,
sha512_digest: K,
start: usize,
end: usize,
) -> Result<String> {
let digest_key = sha512_digest.to_key();
let record = self.sequence_store.get(&digest_key).ok_or_else(|| {
anyhow!(
"Sequence not found: {}",
String::from_utf8_lossy(sha512_digest.as_ref())
)
})?;
let (metadata, sequence): (&SequenceMetadata, &[u8]) = match record {
// Not resident: for a disk-backed store, read only the bytes covering
// [start, end) directly from the `.seq` file instead of loading the
// whole sequence into RAM. This is the random-access extract path:
// sparse region extraction touches a tiny fraction of each sequence,
// so a per-query partial read avoids the cost of `load_sequence`
// pulling entire chromosomes into memory.
SequenceRecord::Stub(meta) => {
if self.local_path.is_some() && self.seqdata_path_template.is_some() {
return self.get_substring_from_disk(meta, start, end);
}
return Err(anyhow!("Sequence data not loaded (stub only)"));
}
SequenceRecord::Full { metadata, sequence } => (metadata, sequence.as_slice()),
};
if start >= metadata.length || end > metadata.length || start >= end {
return Err(anyhow!(
"Invalid substring range: start={}, end={}, sequence length={}",
start,
end,
metadata.length
));
}
match self.mode {
StorageMode::Encoded => {
let alphabet = lookup_alphabet(&metadata.alphabet);
let decoded_sequence = decode_substring_from_bytes(sequence, start, end, alphabet);
// SAFETY: the Encoded decoder emits only bytes drawn from
// `alphabet.decoding_array`, which are ASCII sequence symbols
// (A/C/G/T/N/IUPAC/protein/ASCII), hence valid UTF-8. Skipping
// the O(n) `from_utf8` validation matters on multi-GB extracts.
Ok(unsafe { String::from_utf8_unchecked(decoded_sequence) })
}
StorageMode::Raw => {
let raw_slice: &[u8] = &sequence[start..end];
// SAFETY: Raw mode stores one ASCII sequence byte per base per the
// refget data model, so the slice is valid UTF-8.
Ok(unsafe { String::from_utf8_unchecked(raw_slice.to_vec()) })
}
}
}
/// Batch substring retrieval for many ranges of ONE sequence.
///
/// Resolves the record once and validates all ranges up front, then:
/// - **Resident `Full`**: decode/slice each range from the resident bytes.
/// - **Disk-backed `Stub`**: partial-read each range independently via the
/// fd-cached [`get_substring_from_disk`](Self::get_substring_from_disk)
/// path (reads only the bytes covering each range; reuses the open `.seq`
/// handle across ranges).
///
/// The main benefit over a loop of [`get_substring`](Self::get_substring) is
/// for FFI callers (e.g. the Python binding): resolving the record once and
/// crossing the boundary once per sequence instead of once per range.
///
/// A whole-sequence "decode once, slice all" strategy was tried for
/// wide/overlapping range sets but measured *slower* on real chromosome-scale
/// sequences (decoding a 250 Mbp chromosome into one buffer, plus the large
/// allocation, outweighs the avoided re-decode of scattered spans), so the
/// partial path is used uniformly. Callers wanting resident behavior should
/// load the sequence (`load_sequence`) first; the `Full` arm then serves it.
///
/// Output strings are byte-identical to a loop of
/// [`get_substring`](Self::get_substring).
pub fn get_substrings<K: AsRef<[u8]>>(
&self,
sha512_digest: K,
ranges: &[(usize, usize)],
) -> Result<Vec<String>> {
let digest_key = sha512_digest.to_key();
let actual_key = self
.md5_lookup
.get(&digest_key)
.copied()
.unwrap_or(digest_key);
let record = self.sequence_store.get(&actual_key).ok_or_else(|| {
anyhow!(
"Sequence not found: {}",
String::from_utf8_lossy(sha512_digest.as_ref())
)
})?;
let metadata = record.metadata();
let seq_length = metadata.length;
// Validate every range up front (same rule as get_substring).
for &(start, end) in ranges {
if start >= seq_length || end > seq_length || start >= end {
return Err(anyhow!(
"Invalid substring range: start={}, end={}, sequence length={}",
start,
end,
seq_length
));
}
}
match record {
SequenceRecord::Full { metadata, sequence } => {
let seq: &[u8] = sequence.as_slice();
let mut out = Vec::with_capacity(ranges.len());
match self.mode {
StorageMode::Encoded => {
let alphabet = lookup_alphabet(&metadata.alphabet);
for &(start, end) in ranges {
let decoded =
decode_substring_from_bytes(seq, start, end, alphabet);
// SAFETY: see get_substring (ASCII alphabet bytes).
out.push(unsafe { String::from_utf8_unchecked(decoded) });
}
}
StorageMode::Raw => {
for &(start, end) in ranges {
// SAFETY: see get_substring (Raw stores ASCII bases).
out.push(unsafe {
String::from_utf8_unchecked(seq[start..end].to_vec())
});
}
}
}
Ok(out)
}
SequenceRecord::Stub(meta) => {
if self.local_path.is_none() || self.seqdata_path_template.is_none() {
return Err(anyhow!("Sequence data not loaded (stub only)"));
}
let mut out = Vec::with_capacity(ranges.len());
for &(start, end) in ranges {
out.push(self.get_substring_from_disk(meta, start, end)?);
}
Ok(out)
}
}
}
/// Read a substring directly from the on-disk `.seq` file, fetching only the
/// bytes that cover `[start, end)`.
///
/// `.seq` files are headerless raw byte arrays (bit-packed in Encoded mode,
/// one ASCII byte per base in Raw mode), so byte offsets map directly to base
/// positions. We compute the covering byte range with [`byte_range_for_bases`],
/// read just that window with a positioned read, and decode it with
/// [`decode_substring_from_bytes_at_offset`]. This is the partial-read path
/// that makes random-access extraction cheap: it never materializes the whole
/// sequence in memory.
fn get_substring_from_disk(
&self,
metadata: &SequenceMetadata,
start: usize,
end: usize,
) -> Result<String> {
if start >= metadata.length || end > metadata.length || start >= end {
return Err(anyhow!(
"Invalid substring range: start={}, end={}, sequence length={}",
start,
end,
metadata.length
));
}
// Fetch (or open + cache) the shared `.seq` file handle. The critical
// section is kept tiny: lock -> get-or-open -> clone Arc -> unlock. The
// actual positioned read happens OUTSIDE the lock, so concurrent reads
// are never serialized by the cache.
let file = self.get_cached_seq_file(&metadata.sha512t24u)?;
// Compute the covering byte window, then read it with a positioned read.
let (byte_start, byte_end) = match self.mode {
StorageMode::Encoded => {
let alphabet = lookup_alphabet(&metadata.alphabet);
byte_range_for_bases(start, end, alphabet.bits_per_symbol)
}
StorageMode::Raw => (start, end),
};
let buf = crate::posread::read_exact_window(&file, byte_start, byte_end - byte_start)?;
let decoded: Vec<u8> = match self.mode {
StorageMode::Encoded => {
let alphabet = lookup_alphabet(&metadata.alphabet);
decode_substring_from_bytes_at_offset(&buf, byte_start, start, end, alphabet)
}
StorageMode::Raw => buf,
};
// SAFETY: Encoded decode emits only ASCII alphabet bytes; Raw stores
// ASCII sequence bytes. Either way `decoded` is valid UTF-8, so we skip
// the O(n) validation. See `get_substring` for the full justification.
Ok(unsafe { String::from_utf8_unchecked(decoded) })
}
/// Return a shared handle to the `.seq` file for `digest`, opening and
/// caching it on a miss. Bounded LRU; the LRU handle is evicted (closed)
/// when the cache is full.
fn get_cached_seq_file(&self, digest: &str) -> Result<Arc<File>> {
let key = digest.to_key();
// Fast path: hit. Hold the lock only for the tiny map op.
{
let mut cache = self
.seq_fd_cache
.lock()
.map_err(|_| anyhow!("seq fd cache mutex poisoned"))?;
if let Some(f) = cache.get(&key) {
return Ok(f);
}
}
// Miss: build the path and open OUTSIDE the lock (the open syscall must
// not serialize other readers).
let local_path = self
.local_path
.as_ref()
.ok_or_else(|| anyhow!("Partial read requires a local disk-backed store"))?;
let template = self
.seqdata_path_template
.as_ref()
.ok_or_else(|| anyhow!("No sequence data path template configured"))?;
let full_path = local_path.join(Self::expand_template(digest, template));
let file = Arc::new(
File::open(&full_path)
.with_context(|| format!("Failed to open seq file {}", full_path.display()))?,
);
// Insert under the lock. A concurrent miss may have inserted the same
// digest meanwhile; that is harmless (both handles read the same
// immutable file) -- prefer the already-cached one to avoid duplicating.
let mut cache = self
.seq_fd_cache
.lock()
.map_err(|_| anyhow!("seq fd cache mutex poisoned"))?;
if let Some(existing) = cache.get(&key) {
return Ok(existing);
}
cache.insert(key, Arc::clone(&file));
Ok(file)
}
// =========================================================================
// Streaming retrieval
// =========================================================================
/// Resolve the relative on-disk/remote path for a sequence's encoded bytes.
fn resolve_seq_file_relpath(&self, digest: &str) -> Result<PathBuf> {
let template = self
.seqdata_path_template
.as_deref()
.unwrap_or(DEFAULT_SEQDATA_PATH_TEMPLATE);
let relpath = Self::expand_template(digest, template);
let relpath_str = relpath
.to_str()
.ok_or_else(|| anyhow!("Non-UTF8 sequence path"))?;
Self::sanitize_relative_path(relpath_str)?;
Ok(relpath)
}
/// Stream a (sub)sequence as decoded ASCII bases without loading the full
/// sequence into memory.
///
/// Opens a bounded byte window on the backing store (local file via
/// `seek`+`take`, or remote via HTTP `Range: bytes=`), and wraps it in a
/// [`StreamingDecoder`] when the store is in `Encoded` mode. In `Raw`
/// mode the bounded byte source is returned directly, since bytes and
/// bases are 1:1.
///
/// Memory use is O(1) in the sequence length.
pub fn stream_sequence<K: AsRef<[u8]>>(
&self,
sha512_digest: K,
start: Option<u64>,
end: Option<u64>,
) -> Result<Box<dyn Read + Send>> {
// 1. Resolve digest (MD5 -> SHA512t24u fallback).
let digest_key = sha512_digest.to_key();
let actual_key = self
.md5_lookup
.get(&digest_key)
.copied()
.unwrap_or(digest_key);
// 2. Fetch metadata only (do not load bytes).
let record = self.sequence_store.get(&actual_key).ok_or_else(|| {
anyhow!(
"Sequence not found: {}",
String::from_utf8_lossy(sha512_digest.as_ref())
)
})?;
let metadata = record.metadata();
// 3. Resolve bounds.
let length = metadata.length as u64;
let start = start.unwrap_or(0);
let end = end.unwrap_or(length);
if start > end || end > length {
return Err(anyhow!(
"Invalid stream range: start={}, end={}, length={}",
start,
end,
length
));
}
// 4. Look up alphabet and compute bits-per-base.
let alphabet = crate::digest::lookup_alphabet(&metadata.alphabet);
let bps = match self.mode {
StorageMode::Encoded => alphabet.bits_per_symbol as u64,
StorageMode::Raw => 8,
};
// 5. Compute byte range + leading-skip bits.
let start_bit = start * bps;
let end_bit = end * bps;
let byte_start = start_bit / 8;
let byte_end = end_bit.div_ceil(8);
let leading_skip_bits = (start_bit - byte_start * 8) as u8;
let bases_to_emit = end - start;
let byte_len = byte_end - byte_start;
// Fast path for empty range.
if bases_to_emit == 0 {
return Ok(Box::new(std::io::empty()));
}
let sha = metadata.sha512t24u.clone();
// 6. Build the source reader. In-memory Full records are streamed
// directly from their encoded byte buffer. Otherwise try the
// on-disk path (local_path / remote_source).
if let SequenceRecord::Full { sequence, .. } = record {
// Share ownership of the already-resident buffer instead of
// cloning the byte range. This keeps peak allocation O(1) in
// the sequence size.
let shared = Arc::clone(sequence);
let source: Box<dyn Read + Send> = Box::new(ArcSliceReader::new(
shared,
byte_start as usize,
byte_end as usize,
));
return match self.mode {
StorageMode::Encoded => Ok(Box::new(StreamingDecoder::new(
source,
alphabet,
leading_skip_bits,
bases_to_emit,
))),
StorageMode::Raw => {
debug_assert_eq!(leading_skip_bits, 0);
Ok(source)
}
};
}
let relpath = self.resolve_seq_file_relpath(&sha)?;
let source: Box<dyn Read + Send> = if let Some(local) = self.local_path.as_ref() {
let full = local.join(&relpath);
if full.exists() {
let mut file = File::open(&full)
.with_context(|| format!("Failed to open local seq file: {}", full.display()))?;
file.seek(SeekFrom::Start(byte_start))
.context("Failed to seek in local seq file")?;
let file_len = file.metadata()
.with_context(|| format!("Failed to stat local seq file: {}", full.display()))?.len();
if file_len < byte_start + byte_len {
return Err(anyhow!(
"Local seq file is too short to satisfy the requested byte range \
[{}..{}) (file has {} bytes): {}",
byte_start, byte_start + byte_len,
file_len,
full.display()
));
}
Box::new(BufReader::new(file).take(byte_len))
} else if let Some(remote) = self.remote_source.as_ref() {
open_remote_range(remote, &relpath, byte_start, byte_end)?
} else {
return Err(anyhow!(
"Sequence file missing locally and no remote source: {}",
full.display()
));
}
} else if let Some(remote) = self.remote_source.as_ref() {
open_remote_range(remote, &relpath, byte_start, byte_end)?
} else {
return Err(anyhow!("No backing source configured for sequence {}", sha));
};
// 7. Dispatch on mode.
match self.mode {
StorageMode::Encoded => Ok(Box::new(StreamingDecoder::new(
source,
alphabet,
leading_skip_bits,
bases_to_emit,
))),
StorageMode::Raw => {
debug_assert_eq!(leading_skip_bits, 0);
Ok(source)
}
}
}
// =========================================================================
// Internal helpers
// =========================================================================
/// Expand a path template by substituting digest-based placeholders.
pub(crate) fn expand_template(digest_str: &str, template: &str) -> PathBuf {
debug_assert!(
digest_str.len() >= 4,
"Digest string must be at least 4 characters for template expansion, got {} chars",
digest_str.len()
);
let path_str = template
.replace("%s2", digest_str.get(0..2).unwrap_or(digest_str))
.replace("%s4", digest_str.get(0..4).unwrap_or(digest_str))
.replace("%s", digest_str);
PathBuf::from(path_str)
}
/// Return the full filesystem path to a sequence `.seq` file for the given digest.
///
/// Returns `None` if the store has no local path or no seqdata path template.
pub fn sequence_file_path(&self, seq_digest: &str) -> Option<PathBuf> {
let local_path = self.local_path.as_ref()?;
let template = self.seqdata_path_template.as_ref()?;
Some(local_path.join(Self::expand_template(seq_digest, template)))
}
/// Return the full filesystem path to a collection RGSI file for the given digest.
///
/// Returns `None` if the store has no local path.
pub fn collection_file_path(&self, coll_digest: &str) -> Option<PathBuf> {
let local_path = self.local_path.as_ref()?;
Some(local_path.join(format!("collections/{}.rgsi", coll_digest)))
}
/// Validate a relative path to prevent directory traversal attacks.
pub(crate) fn sanitize_relative_path(path: &str) -> Result<()> {
if path.starts_with('/') || path.starts_with('\\') {
return Err(anyhow!("Absolute paths not allowed: {}", path));
}
if path.contains("..") {
return Err(anyhow!("Directory traversal not allowed: {}", path));
}
if path.contains('\0') {
return Err(anyhow!("Null bytes not allowed in path"));
}
Ok(())
}
/// Helper function to fetch a file from local path or remote source
pub(crate) fn fetch_file(
local_path: &Option<PathBuf>,
remote_source: &Option<String>,
relative_path: &str,
persist_to_disk: bool,
force_refresh: bool,
) -> Result<Vec<u8>> {
Self::sanitize_relative_path(relative_path)?;
if persist_to_disk && !force_refresh {
if let Some(local_path) = local_path {
let full_local_path = local_path.join(relative_path);
if full_local_path.exists() {
return fs::read(&full_local_path).context(format!(
"Failed to read local file: {}",
full_local_path.display()
));
}
}
}
#[cfg(feature = "http")]
if let Some(remote_url) = remote_source {
let full_remote_url = if remote_url.ends_with('/') {
format!("{}{}", remote_url, relative_path)
} else {
format!("{}/{}", remote_url, relative_path)
};
let response = ureq::get(&full_remote_url)
.call()
.map_err(|e| anyhow!("Failed to fetch from remote: {}", e))?;
let mut data = Vec::new();
response
.into_body()
.into_reader()
.read_to_end(&mut data)
.context("Failed to read response body")?;
if persist_to_disk {
if let Some(local_path) = local_path {
let full_local_path = local_path.join(relative_path);
if let Some(parent) = full_local_path.parent() {
create_dir_all(parent)?;
}
fs::write(&full_local_path, &data).context(format!(
"Failed to cache file to: {}",
full_local_path.display()
))?;
}
}
return Ok(data);
}
let _ = remote_source;
Err(anyhow!(
"File not found locally and no remote source configured: {}",
relative_path
))
}
/// Ensure a collection is loaded into the store
pub(crate) fn ensure_collection_loaded(&mut self, collection_digest: &DigestKey) -> Result<()> {
if self.name_lookup.contains_key(collection_digest) {
return Ok(());
}
let needs_fetch = match self.collections.get(collection_digest) {
Some(SequenceCollectionRecord::Stub(_)) => true,
Some(SequenceCollectionRecord::Full { .. }) => false,
None => true,
};
if needs_fetch {
let digest_str = if let Some(SequenceCollectionRecord::Stub(meta)) =
self.collections.get(collection_digest)
{
meta.digest.clone()
} else {
key_to_digest_string(collection_digest)
};
let relative_path = format!("collections/{}.rgsi", digest_str);
if !self.quiet {
let cached = self
.local_path
.as_ref()
.map(|p| p.join(&relative_path).exists())
.unwrap_or(false);
let verb = if cached { "Loading" } else { "Downloading" };
eprintln!("{} collection metadata {}...", verb, digest_str);
}
let _collection_data =
Self::fetch_file(&self.local_path, &self.remote_source, &relative_path, true, false)?;
let local_path = self
.local_path
.as_ref()
.ok_or_else(|| anyhow!("No local path configured"))?;
let collection_file_path = local_path.join(&relative_path);
let collection = read_rgsi_file(&collection_file_path)?;
let loaded_digest = collection.metadata.digest.to_key();
if loaded_digest != *collection_digest {
return Err(anyhow!(
"Collection digest mismatch: expected {}, got {}",
key_to_digest_string(collection_digest),
key_to_digest_string(&loaded_digest)
));
}
let mut name_map = IndexMap::new();
for sequence_record in &collection.sequences {
let metadata = sequence_record.metadata();
let sha512_key = metadata.sha512t24u.to_key();
name_map.insert(metadata.name.clone(), sha512_key);
if !self.sequence_store.contains_key(&sha512_key) {
self.sequence_store
.insert(sha512_key, SequenceRecord::Stub(metadata.clone()));
let md5_key = metadata.md5.to_key();
self.md5_lookup.insert(md5_key, sha512_key);
}
}
self.name_lookup.insert(*collection_digest, name_map);
let record = SequenceCollectionRecord::from(collection);
self.collections.insert(*collection_digest, record);
} else {
let sequences_data: Vec<(SequenceMetadata, DigestKey, DigestKey)> =
if let Some(SequenceCollectionRecord::Full { sequences, .. }) =
self.collections.get(collection_digest)
{
sequences
.iter()
.map(|seq| {
let metadata = seq.metadata().clone();
let sha512_key = metadata.sha512t24u.to_key();
let md5_key = metadata.md5.to_key();
(metadata, sha512_key, md5_key)
})
.collect()
} else {
Vec::new()
};
let mut name_map = IndexMap::new();
for (metadata, sha512_key, md5_key) in sequences_data {
name_map.insert(metadata.name.clone(), sha512_key);
if !self.sequence_store.contains_key(&sha512_key) {
self.sequence_store
.insert(sha512_key, SequenceRecord::Stub(metadata));
self.md5_lookup.insert(md5_key, sha512_key);
}
}
self.name_lookup.insert(*collection_digest, name_map);
}
Ok(())
}
/// Ensure a sequence is loaded into memory
pub(crate) fn ensure_sequence_loaded(&mut self, digest: &DigestKey) -> Result<()> {
let record = self
.sequence_store
.get(digest)
.ok_or_else(|| anyhow!("Sequence not found in store"))?;
// Already loaded (Full or Mmap)
if record.is_loaded() {
return Ok(());
}
let digest_str = &record.metadata().sha512t24u;
let template = self
.seqdata_path_template
.as_ref()
.ok_or_else(|| anyhow!("No sequence data path template configured"))?;
let relative_path = Self::expand_template(digest_str, template)
.to_string_lossy()
.into_owned();
if !self.quiet {
let cached = self
.local_path
.as_ref()
.map(|p| p.join(&relative_path).exists())
.unwrap_or(false);
let verb = if cached { "Loading" } else { "Downloading" };
eprintln!("{} sequence {}...", verb, digest_str);
}
let data = Self::fetch_file(
&self.local_path,
&self.remote_source,
&relative_path,
self.persist_to_disk,
false,
)?;
self.sequence_store.entry(*digest).and_modify(|r| {
r.load_data(data);
});
Ok(())
}
// =========================================================================
// Write methods
// =========================================================================
/// Write the store using its configured paths.
pub fn write(&self) -> Result<()> {
if !self.persist_to_disk {
return Err(anyhow!(
"write() only works with disk-backed stores - use write_store_to_dir() instead"
));
}
self.write_index_files()
}
/// Write a RefgetStore object to a directory
pub fn write_store_to_dir<P: AsRef<Path>>(
&self,
root_path: P,
seqdata_path_template: Option<&str>,
) -> Result<()> {
let root_path = root_path.as_ref();
let template = seqdata_path_template
.or(self.seqdata_path_template.as_deref())
.unwrap_or(DEFAULT_SEQDATA_PATH_TEMPLATE);
if !self.quiet {
eprintln!(
"Writing store to directory: {}; Using seqdata path template: {}",
root_path.display(),
template
);
}
fs::create_dir_all(root_path)?;
let sequences_dir = root_path.join("sequences");
fs::create_dir_all(&sequences_dir)?;
let collections_dir = root_path.join("collections");
fs::create_dir_all(&collections_dir)?;
for record in self.sequence_store.values() {
match record {
SequenceRecord::Full { metadata, .. } => {
let rel_path = Self::expand_template(&metadata.sha512t24u, template);
let full_path = root_path.join(&rel_path);
record.to_file(full_path)?;
}
SequenceRecord::Stub(_) => {
continue;
}
}
}
for record in self.collections.values() {
let collection_file_path =
root_path.join(format!("collections/{}.rgsi", record.metadata().digest));
record.write_collection_rgsi(&collection_file_path)?;
}
let sequence_index_path = root_path.join("sequences.rgsi");
self.write_sequences_rgsi(&sequence_index_path)?;
let collection_index_path = root_path.join("collections.rgci");
self.write_collections_rgci(&collection_index_path)?;
let aliases_dir = root_path.join("aliases");
self.aliases.write_to_dir(&aliases_dir)?;
super::fhr_metadata::write_sidecars(&root_path.join("fhr"), &self.fhr_metadata)?;
self.write_rgstore_json(root_path, template)?;
Ok(())
}
/// Returns statistics about the store
pub fn stats(&self) -> StoreStats {
let n_sequences = self.sequence_store.len();
let n_sequences_loaded = self
.sequence_store
.values()
.filter(|record| record.is_loaded())
.count();
let n_collections = self.collections.len();
let n_collections_loaded = self
.collections
.values()
.filter(|record| record.has_sequences())
.count();
let mode_str = match self.mode {
StorageMode::Raw => "Raw",
StorageMode::Encoded => "Encoded",
};
StoreStats {
n_sequences,
n_sequences_loaded,
n_collections,
n_collections_loaded,
storage_mode: mode_str.to_string(),
}
}
/// List alias namespaces available on this store (from manifest).
pub fn available_alias_namespaces(&self) -> AvailableAliases<'_> {
AvailableAliases {
sequences: &self.available_sequence_alias_namespaces,
collections: &self.available_collection_alias_namespaces,
}
}
}
impl Display for ReadonlyRefgetStore {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let total_size = self.total_disk_size();
let size_str = format_bytes(total_size);
writeln!(f, "ReadonlyRefgetStore object:")?;
writeln!(f, " Mode: {:?}", self.mode)?;
writeln!(f, " Disk size: {} ({} bytes)", size_str, total_size)?;
writeln!(f, ">Sequences (n={}):", self.sequence_store.len())?;
for (i, (sha512_digest, sequence_record)) in self.sequence_store.iter().take(10).enumerate()
{
let metadata = sequence_record.metadata();
let first_8_chars = match sequence_record {
SequenceRecord::Stub(_) => "<stub>".to_string(),
SequenceRecord::Full {
metadata,
sequence: seq,
} => {
match self.mode {
StorageMode::Encoded => {
let alphabet = lookup_alphabet(&metadata.alphabet);
let decoded = decode_substring_from_bytes(
seq,
0,
8.min(metadata.length),
alphabet,
);
String::from_utf8(decoded).unwrap_or_else(|_| "???".to_string())
}
StorageMode::Raw => String::from_utf8(seq[0..8.min(seq.len())].to_vec())
.unwrap_or_else(|_| "???".to_string()),
}
}
};
writeln!(
f,
" - {}. {:02x?}, MD5: {:02x?}, Length: {}, Alphabet: {:?}, Start: {}",
i + 1,
key_to_digest_string(sha512_digest),
&metadata.md5,
&metadata.length,
&metadata.alphabet,
first_8_chars
)?;
}
writeln!(f, ">Collections (n={:?}):", self.name_lookup.len())?;
for (i, (digest, name_map)) in self.name_lookup.iter().enumerate() {
let seqcol_digest_str = key_to_digest_string(digest);
writeln!(
f,
" {}. Collection Digest: {:02x?} ({} sequences)",
i + 1,
seqcol_digest_str,
name_map.len()
)?;
for (name, sha512_digest) in name_map.iter().take(5) {
let sha512_str = key_to_digest_string(sha512_digest);
writeln!(f, " - Name: {}, SHA512: {:02x?}", name, sha512_str)?;
}
if name_map.len() > 5 {
writeln!(f, " - ... and {} more", name_map.len() - 5)?;
}
}
Ok(())
}
}
// Extension traits used by collection.rs
use crate::collection::SequenceCollectionRecordExt;
#[cfg(all(test, feature = "http"))]
mod open_remote_range_tests {
use super::*;
use std::net::TcpListener;
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
/// Spin up an HTTP server that DELIBERATELY IGNORES the Range header,
/// responding 200 OK with the full body. Models a misbehaving CDN/origin.
/// Returns (base_url, shutdown_fn).
fn start_range_ignoring_server(body: Vec<u8>) -> (String, impl FnOnce()) {
use std::io::{Read as _, Write as _};
let listener = TcpListener::bind("127.0.0.1:0").expect("bind");
let port = listener.local_addr().unwrap().port();
let base_url = format!("http://127.0.0.1:{}", port);
let stop = Arc::new(AtomicBool::new(false));
let stop_clone = Arc::clone(&stop);
std::thread::spawn(move || {
listener.set_nonblocking(false).ok();
while !stop_clone.load(Ordering::Relaxed) {
match listener.accept() {
Ok((mut stream, _)) => {
let mut buf = [0u8; 4096];
let _ = stream.read(&mut buf).unwrap_or(0);
// Respond 200 OK with the full body, ignoring the Range header.
let header = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);
let _ = stream.write_all(header.as_bytes());
let _ = stream.write_all(&body);
}
Err(_) => break,
}
}
});
let shutdown = move || {
stop.store(true, Ordering::Relaxed);
let _ = std::net::TcpStream::connect(format!("127.0.0.1:{}", port));
};
(base_url, shutdown)
}
/// When a remote server ignores the Range header and returns 200 + full body,
/// `open_remote_range` MUST return an error rather than silently wrapping the
/// wrong bytes in a StreamingDecoder. Otherwise callers get silent corruption.
#[test]
fn test_open_remote_range_rejects_non_206_response() {
let full_body: Vec<u8> = (0u8..=255).collect();
let (base_url, shutdown) = start_range_ignoring_server(full_body.clone());
// Request only a middle slice: bytes 10..20.
let result = open_remote_range(
&base_url,
std::path::Path::new("seq.dat"),
10,
20,
);
shutdown();
match result {
Err(e) => {
let msg = format!("{}", e);
assert!(
msg.contains("Range") || msg.contains("206") || msg.contains("status"),
"expected a Range/206/status error, got: {}",
msg
);
}
Ok(mut reader) => {
use std::io::Read as _;
let mut got = Vec::new();
reader.read_to_end(&mut got).unwrap();
// If we got here, the bug is live: the reader yielded bytes
// that are NOT the requested 10..20 slice. The `.take(byte_len)`
// in the buggy code returns the FIRST `byte_len` bytes of the
// full body, i.e. 0..10, instead of the requested 10..20.
assert_eq!(
got,
full_body[10..20],
"BUG: open_remote_range silently returned wrong bytes \
when server ignored the Range header. Got {:?}, expected {:?}. \
The function should have returned an error instead.",
got,
&full_body[10..20]
);
}
}
}
}