libradicl 0.16.0

support library for alevin-fry
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
/*
 * Copyright (c) 2020-2024 COMBINE-lab.
 *
 * This file is part of libradicl
 * (see https://www.github.com/COMBINE-lab/libradicl).
 *
 * License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
 */

//! Types and traits providing a high-level interface for reading and parsing
//! RAD files, including parsing RAD chunks in parallel.
//!
//! # Reading a RAD file in parallel
//!
//! [`ParallelRadReader`] (and [`ParallelChunkReader`], for when you already hold
//! a prelude or are reading from something that is not seekable) parses chunks
//! on one thread and hands **meta-chunks** to consumers through a shared queue.
//!
//! There are three levels of control. **Prefer the highest one that fits.**
//!
//! | level | API | use when |
//! | --- | --- | --- |
//! | high | [`ParallelRadReader::process_parallel`] | you just want the records and do not care to own the threads |
//! | **middle** | [`ParallelRadReader::chunk_iter`] | you want to drive the worker threads yourself — the common case |
//! | low | [`ParallelRadReader::get_queue`] + [`ParallelRadReader::is_done`] | neither of the above fits |
//!
//! ## The contract the low level asks of you
//!
//! The producer pushes **every** meta-chunk onto the queue and only *then* sets
//! the done-flag. Observing that flag therefore tells you nothing about whether
//! the queue is empty. A loop shaped like
//!
//! ```ignore
//! while !done.load(Ordering::SeqCst) {
//!     while let Some(meta_chunk) = queue.pop() { /* ... */ }
//! }
//! ```
//!
//! abandons whatever is still queued if the flag becomes visible before its next
//! check. It does not error — it just returns fewer records than the file holds.
//!
//! [`MetaChunkStream`], returned by [`ParallelRadReader::chunk_iter`], holds that
//! invariant for you, and [`ParallelRadReader::process_parallel`] additionally
//! owns the worker lifecycle. Both are drain-safe by construction; reach for
//! [`ParallelRadReader::get_queue`] only when you genuinely need the primitives.
//!
//! See `examples/read_chunk_single_cell_parallel.rs` for a complete program.

use crate::libradicl::chunk::Chunk;
use crate::libradicl::codec::{CHUNK_CODEC_TAG, ChunkCodec, decompress_payload};
use crate::libradicl::header::RadPrelude;
use crate::libradicl::rad_types::{TagMap, TagValue};
use crate::libradicl::record::{MappedRecord, RecordContext};
use crate::libradicl::utils;
use anyhow::Context;
use crossbeam_queue::ArrayQueue;
use crossbeam_utils::Backoff;
use scroll::Pwrite;
use std::io::{BufRead, Cursor, Seek};
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};

/// This represents an empty callback of the appropriate type for the [ParallelChunkReader] and
/// [ParallelRadReader] functions.  Use this when you want the callback to be a no-op.
pub const EMPTY_METACHUNK_CALLBACK: Option<Box<dyn FnMut(u64, u64)>> = None;

/// Determine the chunk compression codec advertised by a file-tag map.
/// An absent [`CHUNK_CODEC_TAG`] means [`ChunkCodec::None`] (every RAD file
/// written before chunk compression existed reads unchanged).
fn codec_from_tag_map(file_tag_map: &TagMap) -> anyhow::Result<ChunkCodec> {
    match file_tag_map.get(CHUNK_CODEC_TAG) {
        None => Ok(ChunkCodec::None),
        Some(TagValue::U8(v)) => ChunkCodec::from_u8(*v),
        Some(_) => anyhow::bail!("'{CHUNK_CODEC_TAG}' file tag must be a U8"),
    }
}

/// A [MetaChunk] consists of a series of [Chunk]s that may be grouped together
/// for efficiency.  One can easily iterate over the [Chunk]s of a [MetaChunk] by
/// calling the [MetaChunk::iter] method.
pub struct MetaChunk<R: MappedRecord> {
    pub first_chunk_index: usize,
    pub num_sub_chunks: usize,
    pub num_bytes: u32,
    pub num_records: u32,
    chunk_blob: Vec<u8>,
    record_context: <R as MappedRecord>::ParsingContext,
}

/// An iterator over the [Chunk]s of a [MetaChunk].
pub struct MetaChunkIterator<'a, 'b, R: MappedRecord> {
    curr_sub_chunk: usize,
    num_sub_chunks: usize,
    data: Cursor<&'a [u8]>,
    record_context: &'b <R as MappedRecord>::ParsingContext,
}

impl<'a, 'b, R: MappedRecord> Iterator for MetaChunkIterator<'a, 'b, R> {
    type Item = Chunk<R>;

    /// Return the next [Chunk] contained within this [MetaChunk], returns
    /// [None] when no chunks remain.
    fn next(&mut self) -> Option<Self::Item> {
        if self.curr_sub_chunk < self.num_sub_chunks {
            self.curr_sub_chunk += 1;
            //println!("number of bytes in data = {}", self.data.get_ref().len());
            let c = Chunk::<R>::from_bytes(&mut self.data, self.record_context);
            //println!("sub_chunk {} parsed, yielding it now", self.curr_sub_chunk - 1);
            Some(c)
        } else {
            None
        }
    }

    /// Since we know how many [Chunk]s compose each [MetaChunk], we provide the
    /// optimal `size_hint` directly
    fn size_hint(&self) -> (usize, Option<usize>) {
        let rem = self.num_sub_chunks - self.curr_sub_chunk;
        (rem, Some(rem))
    }
}

// We know exactly how many [Chunk]s a [MetaChunk] will yield, so this is also an
// [ExactSizeIterator].
impl<'a, 'b, R: MappedRecord> ExactSizeIterator for MetaChunkIterator<'a, 'b, R> {}

impl<R: MappedRecord> MetaChunk<R>
where
    <R as MappedRecord>::ParsingContext: RecordContext,
{
    /// Creates a new [MetaChunk]
    pub fn new(
        first_chunk_index: usize,
        num_sub_chunks: usize,
        num_bytes: u32,
        num_records: u32,
        record_context: <R as MappedRecord>::ParsingContext,
        chunk_blob: Vec<u8>,
    ) -> Self {
        Self {
            first_chunk_index,
            num_sub_chunks,
            num_bytes,
            num_records,
            chunk_blob,
            record_context,
        }
    }

    /// Returns a [MetaChunkIterator] that can iterate over the
    /// [Chunk]s of this [MetaChunk].
    pub fn iter(&self) -> MetaChunkIterator<'_, '_, R> {
        MetaChunkIterator {
            curr_sub_chunk: 0,
            num_sub_chunks: self.num_sub_chunks,
            data: Cursor::new(self.chunk_blob.as_slice()),
            record_context: &self.record_context,
        }
    }

    /// The number of records present in this entire [MetaChunk]
    pub fn num_records(&self) -> u32 {
        self.num_records
    }

    /// The number of bytes present in this entire [MetaChunk]
    pub fn num_bytes(&self) -> u32 {
        self.num_bytes
    }

    /// The id of the first chunk present in this [MetaChunk]
    pub fn first_chunk_index(&self) -> usize {
        self.first_chunk_index
    }
}

/// This free function is used within the [ParallelRadReader] and [ParallelChunkReader] to
/// fill a work queue with [MetaChunk]s from the current file position until the end of the
/// file is reached. It applies the "filter" function to each chunk to determine if the chunk
/// should be included in the output (`filter_fn` returns `true`) or not (`filter_fn` returns
/// `false`).
///
/// <div class="warning">
/// NOTE:: For this function to work correctly, it is assumed that, at the point this function is
/// invoked, the reader `br` is offset at the start of the first [Chunk] in the file (directly
/// after file-level tag values).
/// </div>
///
/// * `br` - The underlying reader from which the [Chunk]s are drawn
/// * `callback` - An optional callback to be invoked when each new [MetaChunk] is placed on the work
///   queue. The callback is given 2 values; the first is the number of bytes of the just-pushed
///   [MetaChunk] and the second is the number of records of the just-pushed [MetaChunk].
/// * `prelude` - A shared reference to the [RadPrelude] corresponding to the chunks in the file
/// * `meta_chunk_queue` - A parallel queue onto which the raw data for each [MetaChunk] will be
///   placed
/// * `done_var` - An [AtomicBool] that will be set to true only once all of the [Chunk]s of the
///   underlying file have been read and added to the work queue.
fn fill_work_queue_filtered<
    R: MappedRecord,
    T: BufRead,
    ChunkIt: Iterator<Item = usize> + BufReadProvider<T> + LastChunkSignaler,
    FilterF,
    F: FnMut(u64, u64),
>(
    mut chunk_iter: ChunkIt,
    filter_fn: FilterF,
    mut callback: Option<F>,
    prelude: &RadPrelude,
    codec: ChunkCodec,
    meta_chunk_queue: Arc<ArrayQueue<MetaChunk<R>>>,
    done_var: Arc<AtomicBool>,
) -> anyhow::Result<()>
where
    <R as MappedRecord>::ParsingContext: RecordContext,
    <R as MappedRecord>::ParsingContext: Clone,
    FilterF: Fn(&[u8], &<R as MappedRecord>::ParsingContext) -> bool,
{
    const BUFSIZE: usize = 524208;
    // the buffer that will hold our records
    let mut buf = vec![0u8; BUFSIZE];
    // scratch holding the compressed bytes of a chunk before decompression
    // (only used when `codec != ChunkCodec::None`)
    let mut scratch: Vec<u8> = Vec::new();
    // the number of bytes currently packed into the meta chunk
    let mut cbytes = 0u32;
    // the number of records currently packed into the meta chunk
    let mut crec = 0u32;
    // the number of chunks in the current meta chunk
    let mut chunks_in_meta_chunk = 0usize;
    // the offset of the first chunk in this chunk
    let mut first_chunk = 0usize;
    // if we had to expand the buffer already and should
    // forcibly push the current buffer onto the queue
    let mut force_push = false;
    // the number of bytes and records in the next chunk header
    let mut nbytes_chunk = 0u32;
    let mut nrec_chunk = 0u32;

    // we include the endpoint here because we will not actually
    // copy a chunk in the first iteration (since we have not yet
    // read the chunk header, which comes at the end of the loop).
    let record_context = prelude
        .get_record_context::<<R as MappedRecord>::ParsingContext>()
        .unwrap();
    while let Some(chunk_num) = chunk_iter.next() {
        // while until_fn(chunk_num, &mut br) {
        // in the first iteration we've not read a header yet
        // so we can't fill a chunk, otherwise we read the header
        // at the bottom of the previous iteration of this loop, and
        // we will fill in the buffer appropriately here.
        if chunk_num > 0 {
            // Decompress (if needed) into `buf` at `boffset`, yielding an
            // uncompressed `[eff_nbytes][nrec][records]` chunk; `eff_nbytes`
            // equals `nbytes_chunk` when codec is None. The filter then runs on
            // the uncompressed chunk bytes, exactly as before.
            let boffset = cbytes as usize;
            let eff_nbytes = if codec == ChunkCodec::None {
                if nbytes_chunk as usize > buf.len() {
                    force_push = true;
                    let chunk_resize = nbytes_chunk as usize + cbytes as usize;
                    buf.resize(chunk_resize, 0);
                }
                let br = chunk_iter.get_mut_buf_read();
                buf.pwrite::<u32>(nbytes_chunk, boffset)?;
                buf.pwrite::<u32>(nrec_chunk, boffset + 4)?;
                br.read_exact(&mut buf[(boffset + 8)..(boffset + nbytes_chunk as usize)])
                    .context("failed to read from work queue.")?;
                nbytes_chunk
            } else {
                let br = chunk_iter.get_mut_buf_read();
                scratch.resize(nbytes_chunk as usize - 8, 0);
                br.read_exact(&mut scratch)
                    .context("failed to read compressed chunk from work queue.")?;
                let decoded = decompress_payload(codec, &scratch)?;
                let eff = decoded.len() as u32 + 8;
                if boffset + eff as usize > buf.len() {
                    force_push = true;
                    buf.resize(boffset + eff as usize, 0);
                }
                buf.pwrite::<u32>(eff, boffset)?;
                buf.pwrite::<u32>(nrec_chunk, boffset + 4)?;
                buf[(boffset + 8)..(boffset + eff as usize)].copy_from_slice(&decoded);
                eff
            };
            // apply the filter
            if filter_fn(&buf[boffset..], &record_context) {
                chunks_in_meta_chunk += 1;
                cbytes += eff_nbytes;
                crec += nrec_chunk;
            } else {
                // if we are skipping this collated chunk, and it triggered a
                // force_push, then undo that.
                force_push = false;
            }
        }

        // in the last iteration of the loop, we will have read all headers already
        // and we are just filling up the buffer with the last chunk, and there will be no more
        // headers left to read
        let last_chunk = chunk_iter.is_last_chunk();
        if !last_chunk {
            let (nc, nr) = Chunk::<R>::read_header(chunk_iter.get_mut_buf_read());
            nbytes_chunk = nc;
            nrec_chunk = nr;
        }

        // determine if we should dump the current buffer to the work queue
        if force_push  // if we were told to push this chunk
                || // or if adding the next cell to this chunk would exceed the buffer size
                    ((cbytes + nbytes_chunk) as usize > buf.len() && chunks_in_meta_chunk > 0)
                    || // of if this was the last chunk
                    last_chunk
        {
            // launch off these cells on the queue
            let mut bclone = MetaChunk::<R>::new(
                first_chunk,
                chunks_in_meta_chunk,
                cbytes,
                crec,
                record_context.clone(),
                buf.clone(),
            );
            // keep trying until we can push this payload
            while let Err(t) = meta_chunk_queue.push(bclone) {
                bclone = t;
                // no point trying to push if the queue is full
                while meta_chunk_queue.is_full() {}
            }
            callback
                .iter_mut()
                .for_each(|f| f(cbytes as u64, chunks_in_meta_chunk as u64));

            // offset of the first cell in the next chunk
            first_chunk += chunks_in_meta_chunk;
            // reset the counters
            chunks_in_meta_chunk = 0;
            cbytes = 0;
            crec = 0;
            buf.resize(BUFSIZE, 0);
            force_push = false;
        }
    }
    done_var.store(true, Ordering::SeqCst);
    Ok(())
}

/// This free function is used within the [ParallelRadReader] and [ParallelChunkReader] to
/// fill a work queue with [MetaChunk]s from the current file position until the end of the
/// file is reached.
///
/// <div class="warning">
/// NOTE:: For this function to work correctly, it is assumed that, at the point this function is
/// invoked, the reader `br` is offset at the start of the first [Chunk] in the file (directly
/// after file-level tag values).
/// </div>
///
/// * `br` - The underlying reader from which the [Chunk]s are drawn
/// * `callback` - An optional callback to be invoked when each new [MetaChunk] is placed on the work
///   queue. The callback is given 2 values; the first is the number of bytes of the just-pushed
///   [MetaChunk] and the second is the number of records of the just-pushed [MetaChunk].
/// * `prelude` - A shared reference to the [RadPrelude] corresponding to the chunks in the file
/// * `meta_chunk_queue` - A parallel queue onto which the raw data for each [MetaChunk] will be
///   placed
/// * `done_var` - An [AtomicBool] that will be set to true only once all of the [Chunk]s of the
///   underlying file have been read and added to the work queue.
fn fill_work_queue<
    R: MappedRecord,
    T: BufRead,
    ChunkIt: Iterator<Item = usize> + BufReadProvider<T> + LastChunkSignaler,
    F: FnMut(u64, u64),
>(
    mut chunk_iter: ChunkIt,
    mut callback: Option<F>,
    prelude: &RadPrelude,
    codec: ChunkCodec,
    meta_chunk_queue: Arc<ArrayQueue<MetaChunk<R>>>,
    done_var: Arc<AtomicBool>,
) -> anyhow::Result<()>
where
    <R as MappedRecord>::ParsingContext: RecordContext,
    <R as MappedRecord>::ParsingContext: Clone,
{
    const BUFSIZE: usize = 524208;
    // the buffer that will hold our records
    let mut buf = vec![0u8; BUFSIZE];
    // scratch holding the compressed bytes of a chunk before decompression
    // (only used when `codec != ChunkCodec::None`)
    let mut scratch: Vec<u8> = Vec::new();
    // the number of bytes currently packed into the meta chunk
    let mut cbytes = 0u32;
    // the number of records currently packed into the meta chunk
    let mut crec = 0u32;
    // the number of chunks in the current meta chunk
    let mut chunks_in_meta_chunk = 0usize;
    // the offset of the first chunk in this chunk
    let mut first_chunk = 0usize;
    // if we had to expand the buffer already and should
    // forcibly push the current buffer onto the queue
    let mut force_push = false;
    // the number of bytes and records in the next chunk header
    let mut nbytes_chunk = 0u32;
    let mut nrec_chunk = 0u32;

    // we include the endpoint here because we will not actually
    // copy a chunk in the first iteration (since we have not yet
    // read the chunk header, which comes at the end of the loop).
    let record_context = prelude
        .get_record_context::<<R as MappedRecord>::ParsingContext>()
        .unwrap();

    while let Some(chunk_num) = chunk_iter.next() {
        //while until_fn(chunk_num, &mut br) {
        // in the first iteration we've not read a header yet
        // so we can't fill a chunk, otherwise we read the header
        // at the bottom of the previous iteration of this loop, and
        // we will fill in the buffer appropriately here.
        if chunk_num > 0 {
            if codec == ChunkCodec::None {
                // if the current chunk (the chunk whose header we read in the last iteration of
                // the loop) alone is too big for the buffer, then resize the buffer to be big enough
                if nbytes_chunk as usize > buf.len() {
                    // if we had to resize the buffer to fit this cell, then make sure we push
                    // immediately in the next round
                    force_push = true;
                    let chunk_resize = nbytes_chunk as usize + cbytes as usize;
                    buf.resize(chunk_resize, 0);
                }
                let br = chunk_iter.get_mut_buf_read();

                // copy the data for the current chunk into the buffer
                let boffset = cbytes as usize;
                buf.pwrite::<u32>(nbytes_chunk, boffset)?;
                buf.pwrite::<u32>(nrec_chunk, boffset + 4)?;
                // read everything from the end of the eader into the buffer
                br.read_exact(&mut buf[(boffset + 8)..(boffset + nbytes_chunk as usize)])
                    .context("failed to read from work queue.")?;
                chunks_in_meta_chunk += 1;
                cbytes += nbytes_chunk;
                crec += nrec_chunk;
            } else {
                // Compressed chunk: `nbytes_chunk` is the compressed framing size.
                // Read the compressed payload, decompress it, and write an
                // *uncompressed* `[eff_nbytes][nrec][records]` chunk into `buf`,
                // so downstream record parsing is identical to the codec=None case.
                let br = chunk_iter.get_mut_buf_read();
                scratch.resize(nbytes_chunk as usize - 8, 0);
                br.read_exact(&mut scratch)
                    .context("failed to read compressed chunk from work queue.")?;
                let decoded = decompress_payload(codec, &scratch)?;
                let eff_nbytes = decoded.len() as u32 + 8;
                let boffset = cbytes as usize;
                if boffset + eff_nbytes as usize > buf.len() {
                    // the decoded chunk doesn't fit; grow and push immediately next round
                    force_push = true;
                    buf.resize(boffset + eff_nbytes as usize, 0);
                }
                buf.pwrite::<u32>(eff_nbytes, boffset)?;
                buf.pwrite::<u32>(nrec_chunk, boffset + 4)?;
                buf[(boffset + 8)..(boffset + eff_nbytes as usize)].copy_from_slice(&decoded);
                chunks_in_meta_chunk += 1;
                cbytes += eff_nbytes;
                crec += nrec_chunk;
            }
        }

        // in the last iteration of the loop, we will have read all headers already
        // and we are just filling up the buffer with the last chunk, and there will be no more
        // headers left to read
        let last_chunk = chunk_iter.is_last_chunk();
        if !last_chunk {
            let (nc, nr) = Chunk::<R>::read_header(chunk_iter.get_mut_buf_read());
            nbytes_chunk = nc;
            nrec_chunk = nr;
        }

        // determine if we should dump the current buffer to the work queue
        if force_push  // if we were told to push this chunk
                || // or if adding the next cell to this chunk would exceed the buffer size
                    ((cbytes + nbytes_chunk) as usize > buf.len() && chunks_in_meta_chunk > 0)
                    || // of if this was the last chunk
                    last_chunk
        {
            // launch off these cells on the queue
            let mut bclone = MetaChunk::<R>::new(
                first_chunk,
                chunks_in_meta_chunk,
                cbytes,
                crec,
                record_context.clone(),
                buf.clone(),
            );
            // keep trying until we can push this payload
            while let Err(t) = meta_chunk_queue.push(bclone) {
                bclone = t;
                // no point trying to push if the queue is full
                while meta_chunk_queue.is_full() {}
            }
            callback
                .iter_mut()
                .for_each(|f| f(cbytes as u64, chunks_in_meta_chunk as u64));

            // offset of the first cell in the next chunk
            first_chunk += chunks_in_meta_chunk;
            // reset the counters
            chunks_in_meta_chunk = 0;
            cbytes = 0;
            crec = 0;
            buf.resize(BUFSIZE, 0);
            force_push = false;
        }
    }
    done_var.store(true, Ordering::SeqCst);
    Ok(())
}

/// Allows reading the underlying RAD file in parallel (for the chunks) by dedicating a single
/// thread (the one running functions on this structure) to filling
/// a work queue. The queue is filled with [MetaChunk]s, which themselves
/// provide an iterator over [Chunk]s. The [ParallelRadReader] first parses the
/// prelude and file tag values itself, and then the chunks.  The main distinction
/// between this type and [ParallelChunkReader] is that this takes care of parsing
/// the prelude and file-level tag values as well.
#[derive(Debug)]
pub struct ParallelRadReader<R: MappedRecord, T: BufRead + Seek> {
    pub prelude: RadPrelude,
    pub file_tag_map: TagMap,
    reader: T,
    pub meta_chunk_queue: Arc<ArrayQueue<MetaChunk<R>>>,
    done_var: Arc<AtomicBool>,
}

/// A drain-safe iterator over the [MetaChunk]s produced by a parallel reader.
///
/// This is the **recommended** way to consume meta-chunks. It encapsulates the
/// ordering contract between the producer and its consumers, which is easy to
/// get wrong when driving [`ArrayQueue`] and the done-flag directly:
///
/// > The producer pushes **every** meta-chunk onto the queue and only *then*
/// > sets the done-flag. A consumer that observes an empty queue, and then
/// > observes the flag, may be looking at a queue the producer filled in
/// > between those two observations.
///
/// A loop that breaks as soon as it sees the flag set can therefore abandon
/// chunks that are still queued, silently losing records. [`MetaChunkStream`]
/// makes one final pass over the queue after first observing the flag, which
/// closes that window.
///
/// # Sharing across threads
///
/// Construct **one iterator per consumer thread** — each is just two `Arc`
/// clones, exactly what [`ParallelRadReader::get_queue`] and
/// [`ParallelRadReader::is_done`] hand out today. All iterators pop from the
/// same queue, so the queue continues to distribute work atomically:
///
/// ```ignore
/// std::thread::scope(|s| {
///     for _ in 0..nworkers {
///         let chunks = reader.chunk_iter();   // one per thread
///         s.spawn(move || {
///             for meta_chunk in chunks {
///                 for chunk in meta_chunk.iter() { /* ... */ }
///             }
///         });
///     }
///     reader.start_chunk_parsing(None::<fn(u64, u64)>)
/// })?;
/// ```
///
/// A single iterator cannot be shared between threads ([`Iterator::next`] takes
/// `&mut self`); do not wrap one in a `Mutex`, as that would serialize the
/// consumers. Construct one each instead.
pub struct MetaChunkStream<R: MappedRecord> {
    queue: Arc<ArrayQueue<MetaChunk<R>>>,
    done: Arc<AtomicBool>,
}

impl<R: MappedRecord> MetaChunkStream<R> {
    /// Build an iterator over `queue`, terminating once `done` is set **and**
    /// the queue has been drained.
    pub fn new(queue: Arc<ArrayQueue<MetaChunk<R>>>, done: Arc<AtomicBool>) -> Self {
        Self { queue, done }
    }
}

impl<R: MappedRecord> Iterator for MetaChunkStream<R> {
    type Item = MetaChunk<R>;

    fn next(&mut self) -> Option<Self::Item> {
        // Waiting policy. The producer is I/O bound, so a consumer that finds
        // the queue empty may be waiting for a disk read rather than for a
        // hand-off that is microseconds away. Spinning through that is actively
        // harmful: it burns the cores the producer needs.
        //
        // `Backoff` ramps from short spins to `yield_now`, which covers the
        // fast case (a chunk is imminent) and stops a hot spin from monopolising
        // a core. Once it reports `is_completed`, though, further yielding does
        // nothing on an otherwise idle machine — `yield_now` returns
        // immediately when there is nothing else runnable, so the consumer is
        // back to a hot spin. That is the point at which the wait is clearly
        // I/O-bound and worth actually sleeping through.
        //
        // Measured with a stalling reader and 8 consumers (`process_parallel`,
        // ~0.94s of work), CPU time for the same wall time:
        //
        //                        all cores          pinned to 2 CPUs
        //   unconditional spin   —                  40.9s wall (43x slower)
        //   Backoff alone        0.944s / 7.54s     0.938s / 1.87s
        //   Backoff + sleep      0.945s / 0.08s     0.853s / 0.04s
        //
        // The sleep tier is what pays: ~100x less CPU on an idle machine, and
        // slightly *better* wall time when oversubscribed. It cannot penalise a
        // fast producer, because reaching it requires the whole `Backoff` ramp
        // to be exhausted first — a briefly empty queue never gets there.
        const IDLE_SLEEP: std::time::Duration = std::time::Duration::from_micros(50);
        let backoff = Backoff::new();

        loop {
            if let Some(meta_chunk) = self.queue.pop() {
                return Some(meta_chunk);
            }
            if self.done.load(Ordering::Acquire) {
                // The producer enqueues everything before setting the flag, so
                // anything pushed between our failed pop and that store is still
                // here. `None` from this final pop means genuinely exhausted.
                return self.queue.pop();
            }
            if backoff.is_completed() {
                std::thread::sleep(IDLE_SLEEP);
            } else {
                backoff.snooze();
            }
        }
    }
}

impl<R: MappedRecord, T: BufRead + Seek> ParallelRadReader<R, T> {
    /// Create a new [ParallelRadReader] over the contents provided by `reader`.
    /// This [ParallelRadReader] will expect to provide chunks to `num_consumers` different
    /// threads once the [Self::start_chunk_parsing()] method has been called.
    ///
    /// # Errors
    ///
    /// Returns an error if the prelude or file-level tag map cannot be parsed —
    /// an empty, truncated or otherwise malformed input, which is exactly what
    /// a partial download or an interrupted write looks like. Prefer this over
    /// [`Self::new`], which panics in that case.
    pub fn try_new(mut reader: T, num_consumers: std::num::NonZeroUsize) -> anyhow::Result<Self> {
        let prelude = RadPrelude::from_bytes(&mut reader).context(
            "could not parse the RAD prelude; the input may be truncated or not a RAD file",
        )?;
        let file_tag_map = prelude
            .file_tags
            .parse_tags_from_bytes(&mut reader)
            .context("could not parse the file-level tag map from the RAD prelude")?;

        Ok(Self {
            prelude,
            file_tag_map,
            reader,
            meta_chunk_queue: Arc::new(ArrayQueue::<MetaChunk<R>>::new(num_consumers.get() * 4)),
            done_var: Arc::new(AtomicBool::new(false)),
        })
    }

    /// Create a new [ParallelRadReader] over the contents provided by `reader`.
    /// This [ParallelRadReader] will expect to provide chunks to `num_consumers` different
    /// threads once the [Self::start_chunk_parsing()] method has been called.
    ///
    /// # Panics
    ///
    /// Panics if the prelude or file-level tag map cannot be parsed. Use
    /// [`Self::try_new`] to handle malformed input — reading a file the user
    /// supplied is not a situation where a panic is the useful outcome.
    pub fn new(reader: T, num_consumers: std::num::NonZeroUsize) -> Self {
        Self::try_new(reader, num_consumers).expect("could not create ParallelRadReader")
    }

    /// Create a new [ParallelRadReader] given the provided `prelude`. It is
    /// assumed that the input `reader` has been consumed up to the point of the end of the prelude.
    /// This function will read and parse the file_tag_map.
    /// This [ParallelRadReader] will expect to provide chunks to `num_consumers` different
    /// threads once the [Self::start_chunk_parsing()] method has been called.
    ///
    /// # Panics
    ///
    /// Panics if the file-level tag map cannot be parsed; see
    /// [`Self::try_from_prelude`] for the fallible form.
    pub fn from_prelude(
        reader: T,
        prelude: RadPrelude,
        num_consumers: std::num::NonZeroUsize,
    ) -> Self {
        Self::try_from_prelude(reader, prelude, num_consumers)
            .expect("could not create ParallelRadReader from prelude")
    }

    /// Create a new [ParallelRadReader] given the provided `prelude`. It is
    /// assumed that the input `reader` has been consumed up to the point of the end of the prelude.
    /// This function will read and parse the file_tag_map.
    ///
    /// # Errors
    ///
    /// Returns an error if the file-level tag map cannot be parsed.
    pub fn try_from_prelude(
        mut reader: T,
        prelude: RadPrelude,
        num_consumers: std::num::NonZeroUsize,
    ) -> anyhow::Result<Self> {
        let file_tag_map = prelude
            .file_tags
            .parse_tags_from_bytes(&mut reader)
            .context("could not parse the file-level tag map from the RAD prelude")?;
        Ok(Self {
            prelude,
            file_tag_map,
            reader,
            meta_chunk_queue: Arc::new(ArrayQueue::<MetaChunk<R>>::new(num_consumers.get() * 4)),
            done_var: Arc::new(AtomicBool::new(false)),
        })
    }

    /// Create a new [ParallelRadReader] given the provided `prelude` and `file_tag_map`.  It is
    /// assumed that the input `reader` has been consumed up to the point of the first chunk.
    /// This [ParallelRadReader] will expect to provide chunks to `num_consumers` different
    /// threads once the [Self::start_chunk_parsing()] method has been called.
    pub fn from_prelude_and_file_tag_map(
        reader: T,
        prelude: RadPrelude,
        file_tag_map: TagMap,
        num_consumers: std::num::NonZeroUsize,
    ) -> Self {
        Self {
            prelude,
            file_tag_map,
            reader,
            meta_chunk_queue: Arc::new(ArrayQueue::<MetaChunk<R>>::new(num_consumers.get() * 4)),
            done_var: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Get an `std::sync::Arc` holding the underlying `ArrayQueue` associated with this reader.
    /// This allows independent parser threads to obtain `MetaChunk`s, over which they can iterate
    /// to parse records.
    ///
    /// This is a **low-level** accessor. Consuming the queue correctly requires
    /// honouring the ordering contract documented on [`MetaChunkStream`]: the
    /// producer enqueues every meta-chunk *before* setting the done-flag, so a
    /// consumer that stops as soon as it observes the flag can abandon queued
    /// chunks and silently lose records. Prefer [`Self::chunk_iter`], which
    /// handles this, or [`Self::process_parallel`], which handles the worker
    /// threads too.
    pub fn get_queue(&self) -> Arc<ArrayQueue<MetaChunk<R>>> {
        self.meta_chunk_queue.clone()
    }

    /// Get an [std::sync::Arc] holding the [AtomicBool] that records the status of the parsing of
    /// the input file.  If the [AtomicBool] is false, parsing of the input file has not completed,
    /// and it is still possible that new [MetaChunk]s will be placed on the work queue.  However, once
    /// the contained [AtomicBool] has been set to true, the parsing is done and no further
    /// [MetaChunk]s will be placed on the queue, other than those that are already "in flight".
    pub fn is_done(&self) -> Arc<AtomicBool> {
        self.done_var.clone()
    }

    /// Obtain a drain-safe iterator over this reader's [MetaChunk]s.
    ///
    /// **Prefer this over [`Self::get_queue`] / [`Self::is_done`].** Those are
    /// the low-level primitives; using them correctly requires reproducing the
    /// producer/consumer ordering contract described on [`MetaChunkStream`], and
    /// getting it wrong silently drops records rather than failing loudly.
    ///
    /// Call once per consumer thread — see [`MetaChunkStream`] for an example.
    pub fn chunk_iter(&self) -> MetaChunkStream<R> {
        MetaChunkStream::new(self.meta_chunk_queue.clone(), self.done_var.clone())
    }

    /// Get the current byte offset into the underlying `reader` stream from which this
    /// RAD file is being consumed.
    pub fn get_byte_offset(&mut self) -> u64 {
        self.reader.stream_position().unwrap()
    }

    /// This function starts the process of parsing the [Chunk]s of the underlying RAD
    /// file into a work queue of [MetaChunk]s, which can then be consumed by multiple
    /// worker threads in parallel.
    /// <div class="warning">
    /// NOTE: This function will attempt to populate the queue until the
    /// file is exhausted (all Chunks have been placed on the queue). However, to control
    /// potential memory use, we use a bounded work queue.  Therefore, if the queue is not being
    /// emptied by workers, this function will spin endlessly waiting to put the next MetaChunk
    /// on the work queue. Since this is a blocking function, be sure to have the worker threads
    /// obtain a reference to the queue (via the get_queue() method) before calling this function!
    /// </div>
    /// Read the file and process every [MetaChunk] across `num_workers` threads,
    /// handling the worker lifecycle for you.
    ///
    /// This is the **highest-level** entry point: it spawns the consumers, runs
    /// the producer, drains the queue safely, and joins everything before
    /// returning. There is no ordering contract left for the caller to get
    /// wrong. Use it when you do not need to own the threading yourself.
    ///
    /// `process` is invoked once per meta-chunk and may run concurrently on any
    /// worker, so it must be `Sync`. Per-worker mutable state should live inside
    /// the closure (for example behind a thread-local or an accumulator you
    /// merge afterwards).
    ///
    /// ```ignore
    /// let seen = std::sync::atomic::AtomicUsize::new(0);
    /// reader.process_parallel(NonZeroUsize::new(8).unwrap(), |meta_chunk| {
    ///     for chunk in meta_chunk.iter() {
    ///         seen.fetch_add(chunk.reads.len(), Ordering::Relaxed);
    ///     }
    /// })?;
    /// ```
    ///
    /// For finer control — your own thread pool, scoped borrows, per-worker
    /// accumulators — use [`Self::chunk_iter`] instead and drive the threads
    /// yourself.
    pub fn process_parallel<P>(
        &mut self,
        num_workers: std::num::NonZeroUsize,
        process: P,
    ) -> anyhow::Result<()>
    where
        P: Fn(MetaChunk<R>) + Sync,
        R: Send,
        <R as MappedRecord>::ParsingContext: RecordContext,
        <R as MappedRecord>::ParsingContext: Clone + Send,
    {
        let queue = self.meta_chunk_queue.clone();
        let done = self.done_var.clone();
        let process = &process;

        std::thread::scope(|s| -> anyhow::Result<()> {
            for _ in 0..num_workers.get() {
                let chunks = MetaChunkStream::new(queue.clone(), done.clone());
                s.spawn(move || {
                    for meta_chunk in chunks {
                        process(meta_chunk);
                    }
                });
            }
            // Producer runs on this thread and sets the done-flag when finished;
            // the workers above drain whatever remains before exiting.
            self.start_chunk_parsing(None::<fn(u64, u64)>)
        })
    }

    pub fn start_chunk_parsing<F: FnMut(u64, u64)>(
        &mut self,
        callback: Option<F>,
    ) -> anyhow::Result<()>
    where
        <R as MappedRecord>::ParsingContext: RecordContext,
        <R as MappedRecord>::ParsingContext: Clone,
    {
        let mut pcr = ParallelChunkReader::<R> {
            prelude: &self.prelude,
            meta_chunk_queue: self.meta_chunk_queue.clone(),
            done_var: self.done_var.clone(),
            codec: codec_from_tag_map(&self.file_tag_map)?,
        };

        pcr.start(&mut self.reader, callback)
    }

    /// This function starts the process of parsing the [Chunk]s of the underlying RAD
    /// file into a work queue of [MetaChunk]s, which can then be consumed by multiple
    /// worker threads in parallel. **Note**: This variant of the function will apply
    /// the filter function `filter_fn` and the resulting iterators returned to the
    /// consumer will include only chunks for which `filter_fn(chunk)` is `true`.
    /// <div class="warning">
    /// NOTE: This function will attempt to populate the queue until the
    /// file is exhausted (all Chunks have been placed on the queue). However, to control
    /// potential memory use, we use a bounded work queue.  Therefore, if the queue is not being
    /// emptied by workers, this function will spin endlessly waiting to put the next MetaChunk
    /// on the work queue. Since this is a blocking function, be sure to have the worker threads
    /// obtain a reference to the queue (via the get_queue() method) before calling this function!
    /// </div>
    pub fn start_chunk_parsing_filtered<FilterFn, F: FnMut(u64, u64)>(
        &mut self,
        filter_fn: FilterFn,
        callback: Option<F>,
    ) -> anyhow::Result<()>
    where
        <R as MappedRecord>::ParsingContext: RecordContext,
        <R as MappedRecord>::ParsingContext: Clone,
        FilterFn: Fn(&[u8], &<R as MappedRecord>::ParsingContext) -> bool,
    {
        let mut pcr = ParallelChunkReader::<R> {
            prelude: &self.prelude,
            meta_chunk_queue: self.meta_chunk_queue.clone(),
            done_var: self.done_var.clone(),
            codec: codec_from_tag_map(&self.file_tag_map)?,
        };

        pcr.start_filtered(&mut self.reader, filter_fn, callback)
    }
}

/// This trait represents the behavior of being able to determine if we
/// are currently looking at the last chunk in a RAD file.
trait LastChunkSignaler {
    /// Returns true if the current chunk under consideration is the
    /// last chunk in a RAD file, and false otherwise.
    fn is_last_chunk(&mut self) -> bool;
}

/// This trait represents the behavior of being able to provide a shared
/// or mutable reference to some type 'T' such that `T :` [BufRead].
trait BufReadProvider<T: BufRead> {
    #[allow(dead_code)]
    /// return a shared reference to the [BufRead]
    fn get_buf_read(&self) -> &T;
    /// return a mutable reference to the [BufRead]
    fn get_mut_buf_read(&mut self) -> &mut T;
}

/// An iterator that will iterate over chunk IDs given the total number of
/// chunks to be parsed.
struct ChunkCountIterator<T: BufRead> {
    num_chunks: usize,
    current_chunk: usize,
    buf_reader: T,
}

impl<T: BufRead> Iterator for ChunkCountIterator<T> {
    type Item = usize;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        let c = self.current_chunk;
        self.current_chunk += 1;
        if c <= self.num_chunks { Some(c) } else { None }
    }

    #[inline(always)]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let rem = self.num_chunks + 1 - self.current_chunk;
        (rem, Some(rem))
    }
}

impl<T: BufRead> ExactSizeIterator for ChunkCountIterator<T> {}

impl<T: BufRead> LastChunkSignaler for ChunkCountIterator<T> {
    fn is_last_chunk(&mut self) -> bool {
        // this is > instead of == because we have already
        // incremented the chunk by the time we call this
        // that is, our iteration loop is c in 0..=num_chunks
        self.current_chunk > self.num_chunks
    }
}

impl<T: BufRead> BufReadProvider<T> for ChunkCountIterator<T> {
    fn get_buf_read(&self) -> &T {
        &self.buf_reader
    }
    fn get_mut_buf_read(&mut self) -> &mut T {
        &mut self.buf_reader
    }
}

/// An iterator that will iterate over chunk IDs until there is
/// no more data to be parsed from the underlying [BufRead]
/// object.
struct ReadUntilEOFIter<T: BufRead> {
    current_chunk: usize,
    buf_reader: T,
}

impl<T: BufRead> Iterator for ReadUntilEOFIter<T> {
    type Item = usize;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        let c = self.current_chunk;
        self.current_chunk += 1;
        if utils::has_data_left(&mut self.buf_reader).expect("encountered error reading input file")
        {
            Some(c)
        } else {
            None
        }
    }
}

impl<T: BufRead> BufReadProvider<T> for ReadUntilEOFIter<T> {
    fn get_buf_read(&self) -> &T {
        &self.buf_reader
    }
    fn get_mut_buf_read(&mut self) -> &mut T {
        &mut self.buf_reader
    }
}

impl<T: BufRead> LastChunkSignaler for ReadUntilEOFIter<T> {
    fn is_last_chunk(&mut self) -> bool {
        !utils::has_data_left(&mut self.buf_reader).expect("encountered error reading input file")
    }
}

/// Allows reading chunks from the underlying RAD file chunks
/// in parallel by dedicating a single thread (the one running
/// functions on this structure) to filling a work queue.
/// The queue is filled with [MetaChunk]s, which themselves
/// provide an iterator over [Chunk]s.  The [ParallelChunkReader]
/// takes a reference to the [RadPrelude] for this RAD file so
/// that it can produce [MetaChunk]s that know how to be properly
/// parsed into [Chunk]s.
#[derive(Debug)]
pub struct ParallelChunkReader<'a, R: MappedRecord> {
    pub prelude: &'a RadPrelude,
    pub meta_chunk_queue: Arc<ArrayQueue<MetaChunk<R>>>,
    pub done_var: Arc<AtomicBool>,
    /// Chunk compression codec (from the file-tag map); chunks are decompressed
    /// transparently in the reader thread so consumers see uncompressed records.
    /// Private so adding it stays a non-breaking change; set via [`Self::new`]
    /// (defaults to [`ChunkCodec::None`]) or by [`ParallelRadReader`].
    codec: ChunkCodec,
}

impl<'a, R: MappedRecord> ParallelChunkReader<'a, R> {
    /// `prelude`: The `RadPrelude` corresponding to the file that will be parsed
    /// `num_consumers`: The estimated number of consumer threads that will draw `MetaChunk`s from
    /// this `ParallelChunkReader`
    pub fn new(prelude: &'a RadPrelude, num_consumers: std::num::NonZeroUsize) -> Self {
        Self {
            prelude,
            meta_chunk_queue: Arc::new(ArrayQueue::<MetaChunk<R>>::new(num_consumers.get() * 4)),
            done_var: Arc::new(AtomicBool::new(false)),
            // This constructor has no file-tag map, so it assumes no chunk
            // compression. Use [ParallelRadReader] (which parses the file tags)
            // to read compressed RAD files.
            codec: ChunkCodec::None,
        }
    }

    /// Get an [std::sync::Arc] holding the underlying [ArrayQueue] associated with this reader.
    /// This allows independent parser threads to obtain [MetaChunk]s, over which they can iterate
    /// to parse records.
    pub fn get_queue(&self) -> Arc<ArrayQueue<MetaChunk<R>>> {
        self.meta_chunk_queue.clone()
    }

    /// Get an [std::sync::Arc] holding the [AtomicBool] that records the status of the parsing of
    /// the input file.  If the [AtomicBool] is false, parsing of the input file has not completed,
    /// and it is still possible that new [MetaChunk]s will be placed on the work queue.  However, once
    /// the contained [AtomicBool] has been set to true, the parsing is done and no further
    /// [MetaChunk]s will be placed on the queue, other than those that are already "in flight".
    pub fn is_done(&self) -> Arc<AtomicBool> {
        self.done_var.clone()
    }

    /// Obtain a drain-safe iterator over this reader's [MetaChunk]s.
    ///
    /// **Prefer this over [`Self::get_queue`] / [`Self::is_done`].** Those are
    /// the low-level primitives; using them correctly requires reproducing the
    /// producer/consumer ordering contract described on [`MetaChunkStream`], and
    /// getting it wrong silently drops records rather than failing loudly.
    ///
    /// Call once per consumer thread — see [`MetaChunkStream`] for an example.
    pub fn chunk_iter(&self) -> MetaChunkStream<R> {
        MetaChunkStream::new(self.meta_chunk_queue.clone(), self.done_var.clone())
    }
}

impl<'a, R: MappedRecord> ParallelChunkReader<'a, R> {
    /// Start this [ParallelChunkReader] processing input from the [BufRead] `br`.
    /// Note that this reader should be positioned at the start of the chunks for this
    /// RAD file, so that the prelude and file tag values have already been parsed/consumded.
    /// Read from `br` and process every [MetaChunk] across `num_workers` threads,
    /// handling the worker lifecycle for you.
    ///
    /// This is the **highest-level** entry point: it spawns the consumers, runs
    /// the producer, drains the queue safely, and joins everything before
    /// returning. There is no ordering contract left for the caller to get
    /// wrong. Use it when you do not need to own the threading yourself.
    ///
    /// `process` is invoked once per meta-chunk and may run concurrently on any
    /// worker, so it must be `Sync`. Per-worker mutable state should live inside
    /// the closure (for example behind a thread-local or an accumulator you
    /// merge afterwards).
    ///
    /// For finer control — your own thread pool, scoped borrows, per-worker
    /// accumulators — use [`Self::chunk_iter`] instead and drive the threads
    /// yourself.
    pub fn process_parallel<T: BufRead, P>(
        &mut self,
        br: T,
        num_workers: std::num::NonZeroUsize,
        process: P,
    ) -> anyhow::Result<()>
    where
        P: Fn(MetaChunk<R>) + Sync,
        R: Send,
        <R as MappedRecord>::ParsingContext: RecordContext,
        <R as MappedRecord>::ParsingContext: Clone + Send,
    {
        let queue = self.meta_chunk_queue.clone();
        let done = self.done_var.clone();
        let process = &process;

        std::thread::scope(|s| -> anyhow::Result<()> {
            for _ in 0..num_workers.get() {
                let chunks = MetaChunkStream::new(queue.clone(), done.clone());
                s.spawn(move || {
                    for meta_chunk in chunks {
                        process(meta_chunk);
                    }
                });
            }
            // Producer runs on this thread and sets the done-flag when finished;
            // the workers above drain whatever remains before exiting.
            self.start(br, None::<fn(u64, u64)>)
        })
    }

    pub fn start<T: BufRead, F: FnMut(u64, u64)>(
        &mut self,
        br: T,
        callback: Option<F>,
    ) -> anyhow::Result<()>
    where
        <R as MappedRecord>::ParsingContext: RecordContext,
        <R as MappedRecord>::ParsingContext: Clone,
    {
        if let Some(nchunks) = self.prelude.hdr.num_chunks() {
            let num_chunks: usize = nchunks.into();
            let chunk_iter = ChunkCountIterator::<T> {
                num_chunks,
                current_chunk: 0,
                buf_reader: br,
            };
            // fill queue known number of chunks
            fill_work_queue(
                chunk_iter,
                callback,
                self.prelude,
                self.codec,
                self.meta_chunk_queue.clone(),
                self.done_var.clone(),
            )?;
        } else {
            let chunk_iter = ReadUntilEOFIter::<T> {
                current_chunk: 0,
                buf_reader: br,
            };
            // fill queue unknown number of chunks
            fill_work_queue(
                chunk_iter,
                callback,
                self.prelude,
                self.codec,
                self.meta_chunk_queue.clone(),
                self.done_var.clone(),
            )?;
        }
        Ok(())
    }

    /// Start this [ParallelChunkReader] processing input from the [BufRead] `br`.
    /// Note that this reader should be positioned at the start of the chunks for this
    /// RAD file, so that the prelude and file tag values have already been parsed/consumded.
    /// The provided filter will be applied at the **chunk** level, and chunks passing the filter
    /// for which the filter function returns `true` will be retained; others will be
    /// discarded / skipped.
    pub fn start_filtered<T: BufRead, FilterF, F: FnMut(u64, u64)>(
        &mut self,
        br: T,
        filter_fn: FilterF,
        callback: Option<F>,
    ) -> anyhow::Result<()>
    where
        <R as MappedRecord>::ParsingContext: RecordContext,
        <R as MappedRecord>::ParsingContext: Clone,
        FilterF: Fn(&[u8], &<R as MappedRecord>::ParsingContext) -> bool,
    {
        if let Some(nchunks) = self.prelude.hdr.num_chunks() {
            let num_chunks: usize = nchunks.into();
            let chunk_iter = ChunkCountIterator::<T> {
                num_chunks,
                current_chunk: 0,
                buf_reader: br,
            };
            // fill queue known number of chunks filtered
            fill_work_queue_filtered(
                chunk_iter,
                filter_fn,
                callback,
                self.prelude,
                self.codec,
                self.meta_chunk_queue.clone(),
                self.done_var.clone(),
            )?;
        } else {
            let chunk_iter = ReadUntilEOFIter::<T> {
                current_chunk: 0,
                buf_reader: br,
            };
            // fill queue unknown number of chunks filtered
            fill_work_queue_filtered(
                chunk_iter,
                filter_fn,
                callback,
                self.prelude,
                self.codec,
                self.meta_chunk_queue.clone(),
                self.done_var.clone(),
            )?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rad_types::RadIntId;
    use crate::record::{PiscemBulkReadRecord, PiscemBulkRecordContext};
    use std::sync::atomic::AtomicUsize;

    fn dummy_meta_chunk(index: usize) -> MetaChunk<PiscemBulkReadRecord> {
        MetaChunk {
            first_chunk_index: index,
            num_sub_chunks: 0,
            num_bytes: 0,
            num_records: 0,
            chunk_blob: Vec::new(),
            record_context: PiscemBulkRecordContext {
                frag_map_t: RadIntId::U8,
            },
        }
    }

    /// The contract this iterator exists to enforce: the producer enqueues
    /// every meta-chunk *before* setting the done-flag, so observing the flag
    /// says nothing about whether the queue is empty. A consumer that stops as
    /// soon as it sees the flag — the natural `while !done { while let Some(..)
    /// = q.pop() }` shape — abandons everything still queued.
    ///
    /// Here the flag is already set before any consumer starts, which is the
    /// worst case that shape gets wrong 100% of the time and this iterator
    /// must get right.
    #[test]
    fn chunk_iter_drains_a_queue_that_is_already_done() {
        const NCHUNKS: usize = 500;

        for nconsumers in [1_usize, 4, 8] {
            let queue = Arc::new(ArrayQueue::<MetaChunk<PiscemBulkReadRecord>>::new(NCHUNKS));
            let done = Arc::new(AtomicBool::new(false));
            let seen = AtomicUsize::new(0);

            for i in 0..NCHUNKS {
                queue.push(dummy_meta_chunk(i)).ok().unwrap();
            }
            done.store(true, Ordering::SeqCst);

            std::thread::scope(|s| {
                for _ in 0..nconsumers {
                    let chunks = MetaChunkStream::new(queue.clone(), done.clone());
                    let seen = &seen;
                    s.spawn(move || {
                        for _meta_chunk in chunks {
                            seen.fetch_add(1, Ordering::SeqCst);
                        }
                    });
                }
            });

            assert_eq!(
                seen.load(Ordering::SeqCst),
                NCHUNKS,
                "{nconsumers} consumer(s) stopped at the done-flag with chunks still queued"
            );
            assert!(queue.is_empty(), "queue not fully drained");
        }
    }

    /// Concurrent smoke test: consumers spin on an empty queue first, so the
    /// producer's pushes and its done-store race against live `next()` calls.
    /// Nothing may be lost or double-counted.
    #[test]
    fn chunk_iter_loses_nothing_racing_a_live_producer() {
        const NCHUNKS: usize = 500;

        for nconsumers in [1_usize, 4, 8] {
            let queue = Arc::new(ArrayQueue::<MetaChunk<PiscemBulkReadRecord>>::new(NCHUNKS));
            let done = Arc::new(AtomicBool::new(false));
            let seen = AtomicUsize::new(0);
            let started = Arc::new(AtomicUsize::new(0));

            std::thread::scope(|s| {
                for _ in 0..nconsumers {
                    let chunks = MetaChunkStream::new(queue.clone(), done.clone());
                    let started = started.clone();
                    let seen = &seen;
                    s.spawn(move || {
                        started.fetch_add(1, Ordering::SeqCst);
                        for _meta_chunk in chunks {
                            seen.fetch_add(1, Ordering::SeqCst);
                        }
                    });
                }

                // Every consumer is already spinning on an empty queue before
                // the producer does anything.
                while started.load(Ordering::SeqCst) < nconsumers {
                    std::hint::spin_loop();
                }
                for i in 0..NCHUNKS {
                    queue.push(dummy_meta_chunk(i)).ok().unwrap();
                }
                done.store(true, Ordering::SeqCst);
            });

            assert_eq!(
                seen.load(Ordering::SeqCst),
                NCHUNKS,
                "{nconsumers} consumer(s)"
            );
        }
    }

    /// A malformed RAD stream must surface as an error, not a panic. Reading a
    /// file the user supplied is a normal fallible operation: a truncated
    /// download or an interrupted write should be reportable, and `new`'s
    /// `unwrap` made that impossible.
    #[test]
    fn try_new_rejects_malformed_input() {
        let n = std::num::NonZeroUsize::new(2).unwrap();
        for (what, bytes) in [
            ("truncated", vec![0_u8; 12]),
            ("empty", Vec::new()),
            ("not a rad file", b"@HD\tVN:1.6\nnot rad at all".to_vec()),
        ] {
            let res = ParallelRadReader::<PiscemBulkReadRecord, _>::try_new(
                std::io::BufReader::new(Cursor::new(bytes)),
                n,
            );
            assert!(
                res.is_err(),
                "{what} input was accepted as a valid RAD stream"
            );
        }
    }

    /// End-to-end coverage of the high-level driver over a real RAD stream:
    /// every record written must be handed to the closure exactly once, at any
    /// worker count.
    #[test]
    fn process_parallel_visits_every_record() {
        use crate::chunk::Chunk;
        use crate::header::RadPrelude;
        use crate::rad_types::{RadType, TagDesc, TagSection, TagSectionLabel};
        use crate::record::{AlevinFryReadRecord, AlevinFryRecordContext};
        use crate::writers::RadFileWriter;
        use std::io::Cursor;

        const NCHUNKS: usize = 64;
        const RECS_PER_CHUNK: u32 = 3;

        let hdr = crate::header::RadHeader {
            is_paired: 0,
            ref_count: 3,
            ref_names: vec!["tgt1".into(), "tgt2".into(), "tgt3".into()],
            num_chunks: 0,
        };
        let mut file_tags = TagSection::new_with_label(TagSectionLabel::FileTags);
        for name in ["bclen", "umilen"] {
            file_tags.add_tag_desc(TagDesc {
                name: name.to_string(),
                typeid: RadType::Int(RadIntId::U16),
            });
        }
        let mut read_tags = TagSection::new_with_label(TagSectionLabel::ReadTags);
        for name in ["b", "u"] {
            read_tags.add_tag_desc(TagDesc {
                name: name.to_string(),
                typeid: RadType::Int(RadIntId::U32),
            });
        }
        let mut aln_tags = TagSection::new_with_label(TagSectionLabel::AlignmentTags);
        aln_tags.add_tag_desc(TagDesc {
            name: "compressed_ori_refid".to_string(),
            typeid: RadType::Int(RadIntId::U32),
        });
        let prelude = RadPrelude {
            hdr,
            file_tags,
            read_tags,
            aln_tags,
        };
        let mut file_tag_map = crate::rad_types::TagMap::with_keyset(&prelude.file_tags.tags);
        file_tag_map.add(crate::rad_types::TagValue::U16(16));
        file_tag_map.add(crate::rad_types::TagValue::U16(12));

        let ctx = AlevinFryRecordContext::get_context_from_tag_section(
            &prelude.file_tags,
            &prelude.read_tags,
            &prelude.aln_tags,
        )
        .unwrap();
        let rec = AlevinFryReadRecord {
            bc: 12345,
            umi: 6789,
            dirs: vec![true, false, true],
            refs: vec![0, 1, 2],
        };
        let chunk = Chunk::<AlevinFryReadRecord> {
            nbytes: 0,
            nrec: RECS_PER_CHUNK,
            reads: vec![rec.clone(), rec.clone(), rec],
        };

        let mut fw = RadFileWriter::new(Cursor::new(Vec::new()), &prelude, &file_tag_map).unwrap();
        for _ in 0..NCHUNKS {
            fw.write_chunk(&chunk, &ctx).unwrap();
        }
        let bytes = fw.finalize().unwrap().into_inner();

        let expected = NCHUNKS * RECS_PER_CHUNK as usize;
        for nworkers in [1_usize, 2, 8] {
            let n = std::num::NonZeroUsize::new(nworkers).unwrap();
            let mut reader = ParallelRadReader::<AlevinFryReadRecord, _>::new(
                std::io::BufReader::new(Cursor::new(bytes.clone())),
                n,
            );
            let seen = AtomicUsize::new(0);
            reader
                .process_parallel(n, |meta_chunk| {
                    for c in meta_chunk.iter() {
                        seen.fetch_add(c.reads.len(), Ordering::SeqCst);
                    }
                })
                .unwrap();
            assert_eq!(
                seen.load(Ordering::SeqCst),
                expected,
                "process_parallel with {nworkers} worker(s) did not visit every record"
            );
        }
    }
}