T-109: M1.2y — tools/curate-etl: bounded-memory streaming reader for concatenated raw BSON (impl Read → whole-document chunks → existing eodPrices / qmSplits / qmDividends decoders) with tests and local benchmark (no new crates, no MongoDB connection)
This commit is contained in:
parent
c57ca3201c
commit
000847f458
1 changed files with 470 additions and 0 deletions
|
|
@ -1252,3 +1252,473 @@ fn corrupt_header_after_valid_doc_stops_fill_without_extra_read() {
|
|||
));
|
||||
assert_eq!(split_reader.into_inner().read_calls, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eof_is_sticky_and_does_not_reread_source() {
|
||||
struct CountingEofReader {
|
||||
data: Vec<u8>,
|
||||
pos: usize,
|
||||
eof_returned: bool,
|
||||
reads_after_eof: usize,
|
||||
}
|
||||
|
||||
impl Read for CountingEofReader {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
if self.eof_returned {
|
||||
self.reads_after_eof += 1;
|
||||
return Ok(0);
|
||||
}
|
||||
let remaining = self.data.len() - self.pos;
|
||||
if remaining == 0 {
|
||||
self.eof_returned = true;
|
||||
return Ok(0);
|
||||
}
|
||||
let take = remaining.min(buf.len());
|
||||
buf[..take].copy_from_slice(&self.data[self.pos..self.pos + take]);
|
||||
self.pos += take;
|
||||
Ok(take)
|
||||
}
|
||||
}
|
||||
|
||||
let doc = make_eod_doc("EOF.US", "2024-06-10", 10.0, 11.0, 9.0, 10.5, 100);
|
||||
let mut reader = RawBsonChunkReader::with_options(
|
||||
CountingEofReader {
|
||||
data: doc.clone(),
|
||||
pos: 0,
|
||||
eof_returned: false,
|
||||
reads_after_eof: 0,
|
||||
},
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 512,
|
||||
max_doc_bytes: 4096,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let chunk = reader.next_chunk().unwrap().unwrap();
|
||||
assert_eq!(chunk.bytes, doc.as_slice());
|
||||
assert!(reader.next_chunk().unwrap().is_none());
|
||||
for _ in 0..5 {
|
||||
assert!(reader.next_chunk().unwrap().is_none());
|
||||
}
|
||||
assert_eq!(reader.into_inner().reads_after_eof, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retries_many_consecutive_interrupted_reads() {
|
||||
struct BurstInterruptReader<'a> {
|
||||
data: &'a [u8],
|
||||
pos: usize,
|
||||
remaining_interrupts: usize,
|
||||
}
|
||||
|
||||
impl Read for BurstInterruptReader<'_> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
if self.remaining_interrupts > 0 {
|
||||
self.remaining_interrupts -= 1;
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Interrupted,
|
||||
"burst EINTR",
|
||||
));
|
||||
}
|
||||
let remaining = self.data.len() - self.pos;
|
||||
if remaining == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let take = remaining.min(buf.len());
|
||||
buf[..take].copy_from_slice(&self.data[self.pos..self.pos + take]);
|
||||
self.pos += take;
|
||||
Ok(take)
|
||||
}
|
||||
}
|
||||
|
||||
let doc = make_eod_doc("INTR.US", "2024-06-10", 20.0, 21.0, 19.0, 20.5, 500);
|
||||
let mut reader = RawBsonChunkReader::with_options(
|
||||
BurstInterruptReader {
|
||||
data: &doc,
|
||||
pos: 0,
|
||||
remaining_interrupts: 100,
|
||||
},
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 256,
|
||||
max_doc_bytes: 4096,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let chunk = reader.next_chunk().unwrap().unwrap();
|
||||
assert_eq!(chunk.bytes, doc.as_slice());
|
||||
assert!(reader.next_chunk().unwrap().is_none());
|
||||
assert_eq!(reader.docs_framed(), 1);
|
||||
assert_eq!(reader.bytes_read(), doc.len() as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_single_doc_buffer_yields_one_doc_per_chunk_without_growing() {
|
||||
let doc = make_eod_doc("EXACT.US", "2024-06-10", 10.0, 11.0, 9.0, 10.5, 100);
|
||||
let doc_len = doc.len();
|
||||
let count = 12usize;
|
||||
let mut input = Vec::with_capacity(doc_len * count);
|
||||
for _ in 0..count {
|
||||
input.extend_from_slice(&doc);
|
||||
}
|
||||
|
||||
let mut reader = RawBsonChunkReader::with_options(
|
||||
FixedStepReader::new(&input, 19),
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: doc_len,
|
||||
max_doc_bytes: 4096,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(reader.buffer_capacity(), doc_len);
|
||||
|
||||
let mut chunks = 0usize;
|
||||
while let Some(chunk) = reader.next_chunk().unwrap() {
|
||||
assert_eq!(chunk.bytes, doc.as_slice());
|
||||
assert_eq!(chunk.start_offset, (chunks * doc_len) as u64);
|
||||
assert_eq!(reader.buffer_capacity(), doc_len);
|
||||
chunks += 1;
|
||||
}
|
||||
assert_eq!(chunks, count);
|
||||
assert_eq!(reader.docs_framed(), count as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declared_length_one_over_max_rejects_without_growing_or_extra_read() {
|
||||
struct HeaderOnlyReader {
|
||||
header: [u8; 4],
|
||||
sent: bool,
|
||||
reads: usize,
|
||||
}
|
||||
|
||||
impl Read for HeaderOnlyReader {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.reads += 1;
|
||||
if self.sent {
|
||||
panic!("unexpected read #{} after sending oversized header", self.reads);
|
||||
}
|
||||
self.sent = true;
|
||||
let take = 4.min(buf.len());
|
||||
buf[..take].copy_from_slice(&self.header[..take]);
|
||||
Ok(take)
|
||||
}
|
||||
}
|
||||
|
||||
let max_doc = 512usize;
|
||||
let over_len = (max_doc + 1) as i32;
|
||||
let mut reader = RawBsonChunkReader::with_options(
|
||||
HeaderOnlyReader {
|
||||
header: over_len.to_le_bytes(),
|
||||
sent: false,
|
||||
reads: 0,
|
||||
},
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 64,
|
||||
max_doc_bytes: max_doc,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(reader.buffer_capacity(), 64);
|
||||
|
||||
let err = reader.next_chunk().unwrap_err();
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::CorruptDocumentLength {
|
||||
offset: 0,
|
||||
declared_len,
|
||||
max_doc_bytes,
|
||||
} if declared_len == over_len && max_doc_bytes == max_doc
|
||||
));
|
||||
assert_eq!(reader.buffer_capacity(), 64);
|
||||
assert_eq!(reader.into_inner().reads, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_truncated_document_reports_declared_len_within_memory_limit() {
|
||||
let max_doc = 2048usize;
|
||||
let declared_len = 1500i32;
|
||||
let mut truncated = Vec::with_capacity(900);
|
||||
truncated.extend_from_slice(&declared_len.to_le_bytes());
|
||||
truncated.resize(900, 0x11);
|
||||
|
||||
let mut reader = RawBsonChunkReader::with_options(
|
||||
FixedStepReader::new(&truncated, 127),
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 256,
|
||||
max_doc_bytes: max_doc,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let err = reader.next_chunk().unwrap_err();
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::TruncatedDocument {
|
||||
offset: 0,
|
||||
remaining: 900,
|
||||
declared_len: Some(1500),
|
||||
}
|
||||
));
|
||||
assert!(
|
||||
reader.buffer_capacity() <= max_doc,
|
||||
"capacity {} exceeded max_doc_bytes {max_doc}",
|
||||
reader.buffer_capacity()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stream_helpers_deliver_prefix_rows_before_terminal_error() {
|
||||
let eod_doc = make_eod_doc("PRE1.US", "2024-06-10", 10.0, 11.0, 9.0, 10.5, 100);
|
||||
let split_doc = {
|
||||
let mut d = RawDocumentBuf::new();
|
||||
d.append(cstr!("qmSearchCode"), "SPL1");
|
||||
d.append(cstr!("date"), "2024-06-10");
|
||||
d.append(cstr!("splitratio"), 2.0_f64);
|
||||
d.into_bytes()
|
||||
};
|
||||
let div_doc = {
|
||||
let mut d = RawDocumentBuf::new();
|
||||
d.append(cstr!("qmSearchCode"), "DIV1");
|
||||
d.append(cstr!("date"), "2024-06-10");
|
||||
d.append(cstr!("amount"), 0.5_f64);
|
||||
d.append(cstr!("currency"), "USD");
|
||||
d.into_bytes()
|
||||
};
|
||||
|
||||
let opts = BsonStreamOptions {
|
||||
chunk_capacity_bytes: 512,
|
||||
max_doc_bytes: 4096,
|
||||
};
|
||||
|
||||
// 1) Truncated tail after valid prefix.
|
||||
{
|
||||
let mut bytes = eod_doc.clone();
|
||||
bytes.extend_from_slice(&eod_doc[..10]);
|
||||
let mut seen = 0usize;
|
||||
let err = decode_eod_price_stream(Cursor::new(bytes), opts, |_| seen += 1, |_, _| {})
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::TruncatedDocument { offset, remaining: 10, .. }
|
||||
if offset == eod_doc.len() as u64
|
||||
));
|
||||
}
|
||||
{
|
||||
let mut bytes = split_doc.clone();
|
||||
bytes.extend_from_slice(&split_doc[..6]);
|
||||
let mut seen = 0usize;
|
||||
let err = decode_qm_split_stream(Cursor::new(bytes), opts, |_| seen += 1, |_, _| {})
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::TruncatedDocument { offset, remaining: 6, .. }
|
||||
if offset == split_doc.len() as u64
|
||||
));
|
||||
}
|
||||
{
|
||||
let mut bytes = div_doc.clone();
|
||||
bytes.extend_from_slice(&[1, 2]);
|
||||
let mut seen = 0usize;
|
||||
let err = decode_qm_dividend_stream(Cursor::new(bytes), opts, |_| seen += 1, |_, _| {})
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::TruncatedDocument { offset, remaining: 2, declared_len: None }
|
||||
if offset == div_doc.len() as u64
|
||||
));
|
||||
}
|
||||
|
||||
// 2) I/O error after valid prefix.
|
||||
{
|
||||
let mut seen = 0usize;
|
||||
let err = decode_eod_price_stream(
|
||||
FailingReader::new(eod_doc.clone(), io::ErrorKind::ConnectionReset, "reset"),
|
||||
opts,
|
||||
|_| seen += 1,
|
||||
|_, _| {},
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::Io { offset, ref source }
|
||||
if offset == eod_doc.len() as u64 && source.kind() == io::ErrorKind::ConnectionReset
|
||||
));
|
||||
}
|
||||
{
|
||||
let mut seen = 0usize;
|
||||
let err = decode_qm_split_stream(
|
||||
FailingReader::new(split_doc.clone(), io::ErrorKind::ConnectionReset, "reset"),
|
||||
opts,
|
||||
|_| seen += 1,
|
||||
|_, _| {},
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::Io { offset, .. } if offset == split_doc.len() as u64
|
||||
));
|
||||
}
|
||||
{
|
||||
let mut seen = 0usize;
|
||||
let err = decode_qm_dividend_stream(
|
||||
FailingReader::new(div_doc.clone(), io::ErrorKind::ConnectionReset, "reset"),
|
||||
opts,
|
||||
|_| seen += 1,
|
||||
|_, _| {},
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::Io { offset, .. } if offset == div_doc.len() as u64
|
||||
));
|
||||
}
|
||||
|
||||
// 3) Corrupt length header after valid prefix.
|
||||
{
|
||||
let mut bytes = eod_doc.clone();
|
||||
bytes.extend_from_slice(&3_i32.to_le_bytes());
|
||||
let mut seen = 0usize;
|
||||
let err = decode_eod_price_stream(Cursor::new(bytes), opts, |_| seen += 1, |_, _| {})
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::CorruptDocumentLength { offset, declared_len: 3, .. }
|
||||
if offset == eod_doc.len() as u64
|
||||
));
|
||||
}
|
||||
{
|
||||
let mut bytes = split_doc.clone();
|
||||
bytes.extend_from_slice(&(-7_i32).to_le_bytes());
|
||||
let mut seen = 0usize;
|
||||
let err = decode_qm_split_stream(Cursor::new(bytes), opts, |_| seen += 1, |_, _| {})
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::CorruptDocumentLength { offset, declared_len: -7, .. }
|
||||
if offset == split_doc.len() as u64
|
||||
));
|
||||
}
|
||||
{
|
||||
let mut bytes = div_doc.clone();
|
||||
bytes.extend_from_slice(&(5000_i32).to_le_bytes());
|
||||
let mut seen = 0usize;
|
||||
let err = decode_qm_dividend_stream(Cursor::new(bytes), opts, |_| seen += 1, |_, _| {})
|
||||
.unwrap_err();
|
||||
assert_eq!(seen, 1);
|
||||
assert!(matches!(
|
||||
err,
|
||||
BsonStreamError::CorruptDocumentLength { offset, declared_len: 5000, .. }
|
||||
if offset == div_doc.len() as u64
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stream_helpers_reject_invalid_options_before_reading() {
|
||||
struct PanicOnRead;
|
||||
impl Read for PanicOnRead {
|
||||
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
|
||||
panic!("reader must not be read when options are invalid");
|
||||
}
|
||||
}
|
||||
|
||||
let bad_cap = BsonStreamOptions {
|
||||
chunk_capacity_bytes: 0,
|
||||
max_doc_bytes: DEFAULT_MAX_DOC_BYTES,
|
||||
};
|
||||
let bad_max = BsonStreamOptions {
|
||||
chunk_capacity_bytes: 64,
|
||||
max_doc_bytes: 4,
|
||||
};
|
||||
|
||||
assert!(matches!(
|
||||
decode_eod_price_stream(PanicOnRead, bad_cap, |_| {}, |_, _| {}),
|
||||
Err(BsonStreamError::InvalidChunkCapacity)
|
||||
));
|
||||
assert!(matches!(
|
||||
decode_eod_price_stream(PanicOnRead, bad_max, |_| {}, |_, _| {}),
|
||||
Err(BsonStreamError::InvalidMaxDocBytes)
|
||||
));
|
||||
assert!(matches!(
|
||||
decode_qm_split_stream(PanicOnRead, bad_cap, |_| {}, |_, _| {}),
|
||||
Err(BsonStreamError::InvalidChunkCapacity)
|
||||
));
|
||||
assert!(matches!(
|
||||
decode_qm_split_stream(PanicOnRead, bad_max, |_| {}, |_, _| {}),
|
||||
Err(BsonStreamError::InvalidMaxDocBytes)
|
||||
));
|
||||
assert!(matches!(
|
||||
decode_qm_dividend_stream(PanicOnRead, bad_cap, |_| {}, |_, _| {}),
|
||||
Err(BsonStreamError::InvalidChunkCapacity)
|
||||
));
|
||||
assert!(matches!(
|
||||
decode_qm_dividend_stream(PanicOnRead, bad_max, |_| {}, |_, _| {}),
|
||||
Err(BsonStreamError::InvalidMaxDocBytes)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn randomized_buffer_and_read_size_sweep_matches_buffer_decoder() {
|
||||
let fixture = build_mixed_eod_fixture();
|
||||
|
||||
let mut expected_bars = Vec::new();
|
||||
let mut expected_errs = Vec::new();
|
||||
let expected_stats = decode_eod_price_buffer(
|
||||
&fixture,
|
||||
|bar| {
|
||||
expected_bars.push(OwnedEodBar {
|
||||
code: bar.eod_search_code.to_owned(),
|
||||
session_date: bar.session_date,
|
||||
open: bar.open.to_bits(),
|
||||
high: bar.high.to_bits(),
|
||||
low: bar.low.to_bits(),
|
||||
close: bar.close.to_bits(),
|
||||
adj_volume: bar.adj_volume.to_bits(),
|
||||
});
|
||||
},
|
||||
|offset, err| expected_errs.push((offset as u64, err)),
|
||||
);
|
||||
|
||||
let mut rng = SplitMix64::new(0x5EED_6464_B501_2026);
|
||||
for case in 0..64 {
|
||||
let cap = (rng.next_u64() as usize % (fixture.len() + 256)) + 1;
|
||||
let step = (rng.next_u64() as usize % 512) + 1;
|
||||
let opts = BsonStreamOptions {
|
||||
chunk_capacity_bytes: cap,
|
||||
max_doc_bytes: DEFAULT_MAX_DOC_BYTES,
|
||||
};
|
||||
|
||||
let mut got_bars = Vec::new();
|
||||
let mut got_errs = Vec::new();
|
||||
let got_stats = decode_eod_price_stream(
|
||||
FixedStepReader::new(&fixture, step),
|
||||
opts,
|
||||
|bar| {
|
||||
got_bars.push(OwnedEodBar {
|
||||
code: bar.eod_search_code.to_owned(),
|
||||
session_date: bar.session_date,
|
||||
open: bar.open.to_bits(),
|
||||
high: bar.high.to_bits(),
|
||||
low: bar.low.to_bits(),
|
||||
close: bar.close.to_bits(),
|
||||
adj_volume: bar.adj_volume.to_bits(),
|
||||
});
|
||||
},
|
||||
|offset, err| got_errs.push((offset, err)),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("case {case} (cap={cap}, step={step}) failed: {e}"));
|
||||
|
||||
assert_eq!(got_stats, expected_stats, "case {case} stats mismatch");
|
||||
assert_eq!(got_bars, expected_bars, "case {case} bars mismatch");
|
||||
assert_eq!(got_errs, expected_errs, "case {case} errs mismatch");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue