T-178: M2.2r — tools/curate-etl: bounded-memory teData stream decoder (decode_te_data_chunks / decode_te_data_stream in bson_stream.rs over decode_te_data_buffer, absolute error offsets) with tests (no new crates, no CLI/bench/runner changes, no live Mongo)
This commit is contained in:
parent
817c0e3aa9
commit
580663da1c
4 changed files with 627 additions and 3 deletions
|
|
@ -748,6 +748,7 @@ Command (`2026-11-28`, two back-to-back runs with identical checksum `0x6d48cd59
|
|||
- `decode_eod_price_stream` / `decode_eod_price_chunks` → `Result<BsonEodDecodeStats, BsonStreamError>`
|
||||
- `decode_qm_split_stream` / `decode_qm_split_chunks` → `Result<BsonQmActionDecodeStats, BsonStreamError>`
|
||||
- `decode_qm_dividend_stream` / `decode_qm_dividend_chunks` → `Result<BsonQmActionDecodeStats, BsonStreamError>`
|
||||
- `decode_te_data_stream` / `decode_te_data_chunks` → `Result<BsonTeDataDecodeStats, BsonStreamError>` (borrowed historical `TeDataPoint<'a>` points only — `forecast` unread — with absolute `u64` error offsets)
|
||||
- Because borrowed zero-copy views (`RawEodDailyBar<'a>`, `RawQmSplit<'a>`, `RawQmDividend<'a>`) borrow from the reused chunk buffer, callbacks are higher-ranked (`for<'a> FnMut(...)`). Per-document decode errors are skipped and reported with absolute `u64` stream offsets (rebasing embedded `offset` fields on `CorruptBson` errors), and chunk stats sum via `AddAssign` so `decoded + rejected_total() == docs_in` holds across the entire stream.
|
||||
|
||||
### `bench-bson-stream`
|
||||
|
|
|
|||
|
|
@ -54,6 +54,9 @@ use crate::bson_qm_intraday::{
|
|||
use crate::bson_qm_prices::{
|
||||
decode_qm_price_buffer, BsonQmPriceDecodeStats, BsonQmPriceError, RawQmPrice,
|
||||
};
|
||||
use crate::bson_te_data::{
|
||||
decode_te_data_buffer, BsonTeDataDecodeStats, BsonTeDataError, TeDataPoint,
|
||||
};
|
||||
use crate::bson_te_market_data::{
|
||||
decode_te_market_data_buffer, BsonTeMarketDataDecodeStats, BsonTeMarketDataError,
|
||||
};
|
||||
|
|
@ -983,6 +986,92 @@ where
|
|||
decode_te_market_data_chunks(&mut chunk_reader, target_symbol, on_bar, on_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rebase_te_data_error(base: u64, err: BsonTeDataError) -> BsonTeDataError {
|
||||
match err {
|
||||
BsonTeDataError::TruncatedDocument {
|
||||
offset,
|
||||
remaining,
|
||||
declared_len,
|
||||
} => BsonTeDataError::TruncatedDocument {
|
||||
offset: rebase_offset(base, offset),
|
||||
remaining,
|
||||
declared_len,
|
||||
},
|
||||
BsonTeDataError::CorruptDocumentLength {
|
||||
offset,
|
||||
declared_len,
|
||||
} => BsonTeDataError::CorruptDocumentLength {
|
||||
offset: rebase_offset(base, offset),
|
||||
declared_len,
|
||||
},
|
||||
BsonTeDataError::CorruptBson { offset, detail } => BsonTeDataError::CorruptBson {
|
||||
offset: rebase_offset(base, offset),
|
||||
detail,
|
||||
},
|
||||
BsonTeDataError::CorruptPointBson {
|
||||
offset,
|
||||
point_index,
|
||||
detail,
|
||||
} => BsonTeDataError::CorruptPointBson {
|
||||
offset: rebase_offset(base, offset),
|
||||
point_index,
|
||||
detail,
|
||||
},
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
/// Streams chunks from an existing [`RawBsonChunkReader`] through [`decode_te_data_buffer`],
|
||||
/// accumulating [`BsonTeDataDecodeStats`] and rebasing per-document and per-point error offsets
|
||||
/// to absolute stream byte offsets.
|
||||
///
|
||||
/// Emits historical `data` observations as borrowed [`TeDataPoint<'a>`] values; `forecast` is
|
||||
/// never read. Per-document and per-point data-quality errors are reported to `on_error` and do
|
||||
/// not abort the stream; only stream-level framing or I/O errors return `Err(BsonStreamError)`.
|
||||
pub fn decode_te_data_chunks<R, F, E>(
|
||||
reader: &mut RawBsonChunkReader<R>,
|
||||
mut on_point: F,
|
||||
mut on_error: E,
|
||||
) -> Result<BsonTeDataDecodeStats, BsonStreamError>
|
||||
where
|
||||
R: io::Read,
|
||||
F: for<'a> FnMut(TeDataPoint<'a>),
|
||||
E: FnMut(u64, BsonTeDataError),
|
||||
{
|
||||
let mut total = BsonTeDataDecodeStats::default();
|
||||
while let Some(chunk) = reader.next_chunk()? {
|
||||
let base = chunk.start_offset;
|
||||
let chunk_stats = decode_te_data_buffer(
|
||||
chunk.bytes,
|
||||
&mut on_point,
|
||||
|local_offset, err| {
|
||||
let abs_offset = base + local_offset as u64;
|
||||
on_error(abs_offset, rebase_te_data_error(base, err));
|
||||
},
|
||||
);
|
||||
total += chunk_stats;
|
||||
}
|
||||
Ok(total)
|
||||
}
|
||||
|
||||
/// Streams a concatenated raw BSON `teData` [`std::io::Read`] source through
|
||||
/// [`decode_te_data_buffer`] with bounded memory.
|
||||
pub fn decode_te_data_stream<R, F, E>(
|
||||
reader: R,
|
||||
options: BsonStreamOptions,
|
||||
on_point: F,
|
||||
on_error: E,
|
||||
) -> Result<BsonTeDataDecodeStats, BsonStreamError>
|
||||
where
|
||||
R: io::Read,
|
||||
F: for<'a> FnMut(TeDataPoint<'a>),
|
||||
E: FnMut(u64, BsonTeDataError),
|
||||
{
|
||||
let mut chunk_reader = RawBsonChunkReader::with_options(reader, options)?;
|
||||
decode_te_data_chunks(&mut chunk_reader, on_point, on_error)
|
||||
}
|
||||
|
||||
/// Terminal error returned by [`decode_ceo_posts_chunks`] and [`decode_ceo_posts_stream`].
|
||||
#[derive(Debug)]
|
||||
pub enum BsonCeoPostsStreamError {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::error::Error as _;
|
||||
use std::io::{self, Cursor, Read};
|
||||
|
||||
use bson::raw::{cstr, RawDocumentBuf};
|
||||
use bson::raw::{cstr, RawArrayBuf, RawDocumentBuf};
|
||||
use bson::DateTime;
|
||||
|
||||
use crate::bson_eod_intraday::{
|
||||
|
|
@ -25,9 +25,13 @@ use crate::bson_stream::{
|
|||
decode_eod_intraday_chunks, decode_eod_intraday_stream, decode_eod_price_stream,
|
||||
decode_qm_dividend_stream, decode_qm_financials_chunks, decode_qm_financials_stream,
|
||||
decode_qm_intraday_chunks, decode_qm_intraday_stream, decode_qm_price_stream,
|
||||
decode_qm_split_stream, BsonQmFinancialsStreamError, BsonStreamError, BsonStreamOptions,
|
||||
decode_qm_split_stream, decode_te_data_chunks, decode_te_data_stream,
|
||||
BsonQmFinancialsStreamError, BsonStreamError, BsonStreamOptions,
|
||||
RawBsonChunkReader, DEFAULT_CHUNK_CAPACITY_BYTES, DEFAULT_MAX_DOC_BYTES, MIN_BSON_DOC_BYTES,
|
||||
};
|
||||
use crate::bson_te_data::{
|
||||
decode_te_data_buffer, BsonTeDataDecodeStats, BsonTeDataError, TeDataPoint,
|
||||
};
|
||||
use crate::fundamentals::FinancialObservation;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
@ -3092,3 +3096,532 @@ fn qm_financials_stream_rejects_invalid_config_before_read() {
|
|||
BsonQmFinancialsStreamError::Decode(BsonQmFinancialsError::InvalidFiscalPeriodConfig { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct OwnedTeDataPoint {
|
||||
te_symbol: String,
|
||||
country: String,
|
||||
category: String,
|
||||
period_date: i32,
|
||||
value_bits: u64,
|
||||
}
|
||||
|
||||
fn owned_te_data_point(p: TeDataPoint<'_>) -> OwnedTeDataPoint {
|
||||
OwnedTeDataPoint {
|
||||
te_symbol: p.te_symbol.to_owned(),
|
||||
country: p.country.to_owned(),
|
||||
category: p.category.to_owned(),
|
||||
period_date: p.period_date,
|
||||
value_bits: p.value.to_bits(),
|
||||
}
|
||||
}
|
||||
|
||||
fn make_te_data_doc(
|
||||
te_symbol: Option<&str>,
|
||||
country: &str,
|
||||
category: &str,
|
||||
data_points: &[(&str, f64)],
|
||||
forecast_points: &[(&str, f64)],
|
||||
) -> Vec<u8> {
|
||||
let mut doc = RawDocumentBuf::new();
|
||||
if let Some(sym) = te_symbol {
|
||||
doc.append(cstr!("teSymbol"), sym);
|
||||
}
|
||||
doc.append(cstr!("country"), country);
|
||||
doc.append(cstr!("category"), category);
|
||||
|
||||
let mut data_arr = RawArrayBuf::new();
|
||||
for &(d, v) in data_points {
|
||||
let mut pt = RawDocumentBuf::new();
|
||||
pt.append(cstr!("d"), d);
|
||||
pt.append(cstr!("v"), v);
|
||||
data_arr.push(pt);
|
||||
}
|
||||
doc.append(cstr!("data"), data_arr);
|
||||
|
||||
if !forecast_points.is_empty() {
|
||||
let mut fc_arr = RawArrayBuf::new();
|
||||
for &(d, v) in forecast_points {
|
||||
let mut pt = RawDocumentBuf::new();
|
||||
pt.append(cstr!("d"), d);
|
||||
pt.append(cstr!("v"), v);
|
||||
fc_arr.push(pt);
|
||||
}
|
||||
doc.append(cstr!("forecast"), fc_arr);
|
||||
}
|
||||
|
||||
doc.into_bytes()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn te_data_stream_and_chunks_match_buffer_across_tiny_chunks_and_short_reads() {
|
||||
let mut fixture = Vec::new();
|
||||
fixture.extend_from_slice(&make_te_data_doc(
|
||||
Some("USACPI"),
|
||||
"United States",
|
||||
"Inflation Rate",
|
||||
&[("2024-01-31", 3.1), ("2024-02-29", 3.2)],
|
||||
&[("2025-06-30", 2.4)],
|
||||
));
|
||||
fixture.extend_from_slice(&make_te_data_doc(
|
||||
Some("CACPI"),
|
||||
"Canada",
|
||||
"Inflation Rate",
|
||||
&[("2024-01-31", 2.9), ("2024-02-29", 2.8), ("2024-03-31", 2.9)],
|
||||
&[],
|
||||
));
|
||||
// Include a rejected doc (missing teSymbol) and a doc with a bad point date + valid trailing point.
|
||||
fixture.extend_from_slice(&make_te_data_doc(
|
||||
None,
|
||||
"United States",
|
||||
"GDP",
|
||||
&[("2024-03-31", 2.1)],
|
||||
&[],
|
||||
));
|
||||
fixture.extend_from_slice(&make_te_data_doc(
|
||||
Some("USAGDP"),
|
||||
"United States",
|
||||
"GDP Growth Rate",
|
||||
&[("2024-01-31", 1.6), ("2023-02-29", 9.9), ("2024-06-30", 3.0)],
|
||||
&[("2025-12-31", 2.0)],
|
||||
));
|
||||
fixture.extend_from_slice(&make_te_data_doc(
|
||||
Some("CAPOLICY"),
|
||||
"Canada",
|
||||
"Interest Rate",
|
||||
&[("2024-06-05", 4.75)],
|
||||
&[],
|
||||
));
|
||||
|
||||
let mut expected_points = Vec::new();
|
||||
let mut expected_errs = Vec::new();
|
||||
let expected_stats = decode_te_data_buffer(
|
||||
&fixture,
|
||||
|p| expected_points.push(owned_te_data_point(p)),
|
||||
|off, err| expected_errs.push((off as u64, err)),
|
||||
);
|
||||
assert_eq!(expected_stats.docs_in, 5);
|
||||
assert_eq!(expected_stats.docs_accepted, 4);
|
||||
assert_eq!(expected_stats.docs_rejected_total(), 1);
|
||||
assert_eq!(expected_stats.points_in, 9);
|
||||
assert_eq!(expected_stats.points_decoded, 8);
|
||||
assert_eq!(expected_stats.rejected_total(), 1);
|
||||
assert_eq!(
|
||||
expected_stats.docs_in,
|
||||
expected_stats.docs_accepted + expected_stats.docs_rejected_total()
|
||||
);
|
||||
assert_eq!(
|
||||
expected_stats.points_in,
|
||||
expected_stats.points_decoded + expected_stats.rejected_total()
|
||||
);
|
||||
|
||||
// Tiny chunk capacities force multiple whole-document chunks; short FixedStepReader splits
|
||||
// both 4-byte length prefixes and document bodies across reads.
|
||||
let max_doc_bytes = 4096;
|
||||
for (cap, step) in [(1, 1), (3, 2), (17, 3), (64, 7), (128, 17)] {
|
||||
let opts = BsonStreamOptions {
|
||||
chunk_capacity_bytes: cap,
|
||||
max_doc_bytes,
|
||||
};
|
||||
let mut got_points = Vec::new();
|
||||
let mut got_errs = Vec::new();
|
||||
let got_stats = decode_te_data_stream(
|
||||
FixedStepReader::new(&fixture, step),
|
||||
opts,
|
||||
|p| got_points.push(owned_te_data_point(p)),
|
||||
|off, err| got_errs.push((off, err)),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("teData stream cap={cap} step={step} failed: {e}"));
|
||||
|
||||
assert_eq!(got_stats, expected_stats, "stats mismatch cap={cap} step={step}");
|
||||
assert_eq!(got_points, expected_points, "points mismatch cap={cap} step={step}");
|
||||
assert_eq!(got_errs, expected_errs, "errors mismatch cap={cap} step={step}");
|
||||
}
|
||||
|
||||
// Exercise decode_te_data_chunks directly with tiny buffer + FixedStepReader and verify
|
||||
// buffer_capacity stays within max_doc_bytes.
|
||||
let chunk_opts = BsonStreamOptions {
|
||||
chunk_capacity_bytes: 32,
|
||||
max_doc_bytes,
|
||||
};
|
||||
let mut chunk_reader = RawBsonChunkReader::with_options(
|
||||
RandomInterruptReader::new(&fixture, 0x7E_DA7A, true),
|
||||
chunk_opts,
|
||||
)
|
||||
.unwrap();
|
||||
let mut chunk_points = Vec::new();
|
||||
let mut chunk_errs = Vec::new();
|
||||
let chunk_stats = decode_te_data_chunks(
|
||||
&mut chunk_reader,
|
||||
|p| chunk_points.push(owned_te_data_point(p)),
|
||||
|off, err| chunk_errs.push((off, err)),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(chunk_stats, expected_stats);
|
||||
assert_eq!(chunk_points, expected_points);
|
||||
assert_eq!(chunk_errs, expected_errs);
|
||||
assert!(chunk_reader.buffer_capacity() <= max_doc_bytes);
|
||||
assert_eq!(chunk_reader.docs_framed(), 5);
|
||||
assert_eq!(chunk_reader.bytes_read(), fixture.len() as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn te_data_stream_later_chunk_rejects_continue_with_absolute_offsets() {
|
||||
let doc1 = make_te_data_doc(
|
||||
Some("USACPI"),
|
||||
"United States",
|
||||
"Inflation Rate",
|
||||
&[("2024-01-31", 3.1)],
|
||||
&[],
|
||||
);
|
||||
let doc2_missing_sym = make_te_data_doc(
|
||||
None,
|
||||
"United States",
|
||||
"Unemployment Rate",
|
||||
&[("2024-01-31", 3.7)],
|
||||
&[],
|
||||
);
|
||||
let doc3_bad_point = make_te_data_doc(
|
||||
Some("CACPI"),
|
||||
"Canada",
|
||||
"Inflation Rate",
|
||||
&[
|
||||
("2024-01-31", 2.9),
|
||||
("not-a-date", 99.0),
|
||||
("2024-03-31", 2.8),
|
||||
],
|
||||
&[],
|
||||
);
|
||||
let doc4_trailing_valid = make_te_data_doc(
|
||||
Some("USAGDP"),
|
||||
"United States",
|
||||
"GDP Growth Rate",
|
||||
&[("2024-06-30", 3.0)],
|
||||
&[],
|
||||
);
|
||||
|
||||
let mut fixture = Vec::new();
|
||||
fixture.extend_from_slice(&doc1);
|
||||
let missing_sym_offset = fixture.len() as u64;
|
||||
assert!(missing_sym_offset > 0);
|
||||
fixture.extend_from_slice(&doc2_missing_sym);
|
||||
let bad_point_doc_offset = fixture.len() as u64;
|
||||
assert!(bad_point_doc_offset > missing_sym_offset);
|
||||
fixture.extend_from_slice(&doc3_bad_point);
|
||||
fixture.extend_from_slice(&doc4_trailing_valid);
|
||||
|
||||
// chunk_capacity_bytes == doc1.len() guarantees doc2 and doc3 start in non-first chunks.
|
||||
let opts = BsonStreamOptions {
|
||||
chunk_capacity_bytes: doc1.len(),
|
||||
max_doc_bytes: 4096,
|
||||
};
|
||||
let mut points = Vec::new();
|
||||
let mut errs = Vec::new();
|
||||
let stats = decode_te_data_stream(
|
||||
FixedStepReader::new(&fixture, 9),
|
||||
opts,
|
||||
|p| points.push(owned_te_data_point(p)),
|
||||
|off, err| errs.push((off, err)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Valid prefix, valid point before and after bad point, and trailing valid doc all emit.
|
||||
assert_eq!(points.len(), 4);
|
||||
assert_eq!(points[0].te_symbol, "USACPI");
|
||||
assert_eq!(points[1].te_symbol, "CACPI");
|
||||
assert_eq!(points[1].value_bits, 2.9_f64.to_bits());
|
||||
assert_eq!(points[2].te_symbol, "CACPI");
|
||||
assert_eq!(points[2].value_bits, 2.8_f64.to_bits());
|
||||
assert_eq!(points[3].te_symbol, "USAGDP");
|
||||
|
||||
assert_eq!(
|
||||
errs,
|
||||
vec![
|
||||
(missing_sym_offset, BsonTeDataError::MissingSymbol),
|
||||
(
|
||||
bad_point_doc_offset,
|
||||
BsonTeDataError::InvalidPointDate { point_index: 1 },
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
assert_eq!(stats.docs_in, 4);
|
||||
assert_eq!(stats.docs_accepted, 3);
|
||||
assert_eq!(stats.rejected_missing_symbol, 1);
|
||||
assert_eq!(stats.docs_rejected_total(), 1);
|
||||
assert_eq!(stats.points_in, 5);
|
||||
assert_eq!(stats.points_decoded, 4);
|
||||
assert_eq!(stats.rejected_invalid_date, 1);
|
||||
assert_eq!(stats.rejected_total(), 1);
|
||||
assert_eq!(
|
||||
stats.docs_in,
|
||||
stats.docs_accepted + stats.docs_rejected_total()
|
||||
);
|
||||
assert_eq!(stats.points_in, stats.points_decoded + stats.rejected_total());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn te_data_stream_rebases_corrupt_bson_and_corrupt_point_bson_in_later_chunks() {
|
||||
let doc1 = make_te_data_doc(
|
||||
Some("USACPI"),
|
||||
"United States",
|
||||
"Inflation Rate",
|
||||
&[("2024-01-31", 3.1)],
|
||||
&[],
|
||||
);
|
||||
// Length-framed corrupt top-level BSON doc.
|
||||
let doc2_corrupt_top = make_corrupt_bson_doc(37);
|
||||
// Length-framed teData doc whose second point (point_index = 1) has a corrupt element type tag.
|
||||
let mut doc3_corrupt_point = make_te_data_doc(
|
||||
Some("CACPI"),
|
||||
"Canada",
|
||||
"Inflation Rate",
|
||||
&[("2024-01-31", 2.9), ("2024-02-29", 2.8)],
|
||||
&[],
|
||||
);
|
||||
let second_d_pos = doc3_corrupt_point
|
||||
.windows(3)
|
||||
.enumerate()
|
||||
.filter(|(_, w)| *w == b"\x02d\0")
|
||||
.nth(1)
|
||||
.map(|(idx, _)| idx)
|
||||
.expect("locate second point's 'd' element");
|
||||
doc3_corrupt_point[second_d_pos] = 0x7E;
|
||||
|
||||
let doc4_after = make_te_data_doc(
|
||||
Some("USAGDP"),
|
||||
"United States",
|
||||
"GDP Growth Rate",
|
||||
&[("2024-06-30", 3.0)],
|
||||
&[],
|
||||
);
|
||||
|
||||
let mut fixture = Vec::new();
|
||||
fixture.extend_from_slice(&doc1);
|
||||
let corrupt_top_offset = fixture.len() as u64;
|
||||
fixture.extend_from_slice(&doc2_corrupt_top);
|
||||
let corrupt_point_offset = fixture.len() as u64;
|
||||
fixture.extend_from_slice(&doc3_corrupt_point);
|
||||
fixture.extend_from_slice(&doc4_after);
|
||||
|
||||
let opts = BsonStreamOptions {
|
||||
chunk_capacity_bytes: doc1.len(),
|
||||
max_doc_bytes: 4096,
|
||||
};
|
||||
let mut points = Vec::new();
|
||||
let mut errs = Vec::new();
|
||||
let stats = decode_te_data_stream(
|
||||
FixedStepReader::new(&fixture, 11),
|
||||
opts,
|
||||
|p| points.push(owned_te_data_point(p)),
|
||||
|off, err| errs.push((off, err)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// doc1 point + doc3's valid first point + doc4 point all emit.
|
||||
assert_eq!(points.len(), 3);
|
||||
assert_eq!(points[0].te_symbol, "USACPI");
|
||||
assert_eq!(points[1].te_symbol, "CACPI");
|
||||
assert_eq!(points[2].te_symbol, "USAGDP");
|
||||
|
||||
assert_eq!(errs.len(), 2);
|
||||
assert!(
|
||||
matches!(
|
||||
&errs[0],
|
||||
(abs_off, BsonTeDataError::CorruptBson { offset, .. })
|
||||
if *abs_off == corrupt_top_offset && *offset as u64 == corrupt_top_offset
|
||||
),
|
||||
"expected rebased CorruptBson at {corrupt_top_offset}, got {:?}",
|
||||
errs[0]
|
||||
);
|
||||
assert!(
|
||||
matches!(
|
||||
&errs[1],
|
||||
(
|
||||
abs_off,
|
||||
BsonTeDataError::CorruptPointBson {
|
||||
offset,
|
||||
point_index: 1,
|
||||
..
|
||||
}
|
||||
) if *abs_off == corrupt_point_offset && *offset as u64 == corrupt_point_offset
|
||||
),
|
||||
"expected rebased CorruptPointBson at {corrupt_point_offset}, got {:?}",
|
||||
errs[1]
|
||||
);
|
||||
|
||||
assert_eq!(stats.docs_in, 4);
|
||||
assert_eq!(stats.docs_accepted, 3);
|
||||
assert_eq!(stats.rejected_corrupt_document, 1);
|
||||
assert_eq!(stats.points_in, 4);
|
||||
assert_eq!(stats.points_decoded, 3);
|
||||
assert_eq!(stats.rejected_corrupt_point, 1);
|
||||
assert_eq!(
|
||||
stats.docs_in,
|
||||
stats.docs_accepted + stats.docs_rejected_total()
|
||||
);
|
||||
assert_eq!(stats.points_in, stats.points_decoded + stats.rejected_total());
|
||||
|
||||
// Directly exercise rebase_te_data_error for the two framing-offset variants intercepted by
|
||||
// RawBsonChunkReader, plus non-offset variants.
|
||||
assert_eq!(
|
||||
super::rebase_te_data_error(
|
||||
1_000,
|
||||
BsonTeDataError::TruncatedDocument {
|
||||
offset: 42,
|
||||
remaining: 3,
|
||||
declared_len: Some(64),
|
||||
},
|
||||
),
|
||||
BsonTeDataError::TruncatedDocument {
|
||||
offset: 1_042,
|
||||
remaining: 3,
|
||||
declared_len: Some(64),
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
super::rebase_te_data_error(
|
||||
2_000,
|
||||
BsonTeDataError::CorruptDocumentLength {
|
||||
offset: 17,
|
||||
declared_len: 3,
|
||||
},
|
||||
),
|
||||
BsonTeDataError::CorruptDocumentLength {
|
||||
offset: 2_017,
|
||||
declared_len: 3,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
super::rebase_te_data_error(500, BsonTeDataError::InvalidPointDate { point_index: 4 }),
|
||||
BsonTeDataError::InvalidPointDate { point_index: 4 }
|
||||
);
|
||||
assert_eq!(
|
||||
super::rebase_te_data_error(500, BsonTeDataError::MissingSymbol),
|
||||
BsonTeDataError::MissingSymbol
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn te_data_stream_ignores_forecast_and_handles_empty_input() {
|
||||
// Empty input via both decode_te_data_stream and decode_te_data_chunks.
|
||||
let opts = BsonStreamOptions {
|
||||
chunk_capacity_bytes: 64,
|
||||
max_doc_bytes: 1024,
|
||||
};
|
||||
let empty_stats = decode_te_data_stream(
|
||||
Cursor::new(&[][..]),
|
||||
opts,
|
||||
|_| panic!("unexpected point on empty input"),
|
||||
|_, _| panic!("unexpected error on empty input"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(empty_stats, BsonTeDataDecodeStats::default());
|
||||
|
||||
let mut empty_reader = RawBsonChunkReader::with_options(Cursor::new(&[][..]), opts).unwrap();
|
||||
let empty_chunk_stats = decode_te_data_chunks(
|
||||
&mut empty_reader,
|
||||
|_| panic!("unexpected point on empty chunks"),
|
||||
|_, _| panic!("unexpected error on empty chunks"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(empty_chunk_stats, BsonTeDataDecodeStats::default());
|
||||
|
||||
// Document with distinct historical `data` and forward-looking `forecast` arrays.
|
||||
let doc = make_te_data_doc(
|
||||
Some("USACPI"),
|
||||
"United States",
|
||||
"Inflation Rate",
|
||||
&[("2024-01-31", 3.1), ("2024-02-29", 3.2)],
|
||||
&[("2026-01-31", 99.1), ("2026-06-30", 99.2), ("2027-01-31", 99.3)],
|
||||
);
|
||||
let mut points = Vec::new();
|
||||
let stats = decode_te_data_stream(
|
||||
FixedStepReader::new(&doc, 7),
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 32,
|
||||
max_doc_bytes: 4096,
|
||||
},
|
||||
|p| points.push(owned_te_data_point(p)),
|
||||
|off, err| panic!("unexpected error at {off}: {err}"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(stats.docs_in, 1);
|
||||
assert_eq!(stats.docs_accepted, 1);
|
||||
assert_eq!(stats.points_in, 2);
|
||||
assert_eq!(stats.points_decoded, 2);
|
||||
assert_eq!(stats.rejected_total(), 0);
|
||||
assert_eq!(points.len(), 2);
|
||||
assert_eq!(points[0].value_bits, 3.1_f64.to_bits());
|
||||
assert_eq!(points[1].value_bits, 3.2_f64.to_bits());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn te_data_stream_returns_terminal_errors_for_truncated_and_oversized_docs() {
|
||||
let prefix_doc = make_te_data_doc(
|
||||
Some("USACPI"),
|
||||
"United States",
|
||||
"Inflation Rate",
|
||||
&[("2024-01-31", 3.1)],
|
||||
&[],
|
||||
);
|
||||
|
||||
// 1. Truncated trailing document after valid prefix.
|
||||
let mut truncated = prefix_doc.clone();
|
||||
let trunc_offset = truncated.len() as u64;
|
||||
truncated.extend_from_slice(&120_i32.to_le_bytes());
|
||||
truncated.extend_from_slice(&[0x02, b'a', 0x00]);
|
||||
|
||||
let mut trunc_points = Vec::new();
|
||||
let trunc_err = decode_te_data_stream(
|
||||
FixedStepReader::new(&truncated, 5),
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 32,
|
||||
max_doc_bytes: 4096,
|
||||
},
|
||||
|p| trunc_points.push(owned_te_data_point(p)),
|
||||
|off, err| panic!("terminal stream error must not reach on_error ({off}: {err})"),
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert_eq!(trunc_points.len(), 1);
|
||||
assert_eq!(trunc_points[0].te_symbol, "USACPI");
|
||||
assert!(matches!(
|
||||
trunc_err,
|
||||
BsonStreamError::TruncatedDocument {
|
||||
offset,
|
||||
remaining: 7,
|
||||
declared_len: Some(120),
|
||||
} if offset == trunc_offset
|
||||
));
|
||||
|
||||
// 2. Oversized trailing document (declared length > max_doc_bytes) after valid prefix.
|
||||
let mut oversized = prefix_doc;
|
||||
let oversize_offset = oversized.len() as u64;
|
||||
oversized.extend_from_slice(&1024_i32.to_le_bytes());
|
||||
oversized.extend_from_slice(&[0x00; 16]);
|
||||
|
||||
let mut oversize_points = Vec::new();
|
||||
let oversize_err = decode_te_data_stream(
|
||||
FixedStepReader::new(&oversized, 5),
|
||||
BsonStreamOptions {
|
||||
chunk_capacity_bytes: 32,
|
||||
max_doc_bytes: 512,
|
||||
},
|
||||
|p| oversize_points.push(owned_te_data_point(p)),
|
||||
|off, err| panic!("terminal stream error must not reach on_error ({off}: {err})"),
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert_eq!(oversize_points.len(), 1);
|
||||
assert_eq!(oversize_points[0].te_symbol, "USACPI");
|
||||
assert!(matches!(
|
||||
oversize_err,
|
||||
BsonStreamError::CorruptDocumentLength {
|
||||
offset,
|
||||
declared_len: 1024,
|
||||
max_doc_bytes: 512,
|
||||
} if offset == oversize_offset
|
||||
));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,7 +105,8 @@ pub use bson_stream::{
|
|||
decode_qm_filings_chunks, decode_qm_filings_stream, decode_qm_financials_chunks,
|
||||
decode_qm_financials_stream, decode_qm_intraday_chunks, decode_qm_intraday_stream,
|
||||
decode_qm_price_chunks, decode_qm_price_stream, decode_qm_split_chunks,
|
||||
decode_qm_split_stream, decode_te_market_data_chunks, decode_te_market_data_stream,
|
||||
decode_qm_split_stream, decode_te_data_chunks, decode_te_data_stream,
|
||||
decode_te_market_data_chunks, decode_te_market_data_stream,
|
||||
BsonCeoPostsStreamError, BsonCeoShortsStreamError, BsonQmEventsStreamError,
|
||||
BsonQmFinancialsStreamError, BsonStreamError, BsonStreamOptions, RawBsonChunk,
|
||||
RawBsonChunkReader, DEFAULT_CHUNK_CAPACITY_BYTES, DEFAULT_MAX_DOC_BYTES, MIN_BSON_DOC_BYTES,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue