T-97: M1.2o — tools/curate-etl: zero-copy raw BSON decoder for eodPrices documents → EodDailyBar (adds the bson crate only; no MongoDB connection) with fixture tests and local benchmark
This commit is contained in:
parent
ef37b88676
commit
a384c9e314
2 changed files with 110 additions and 8 deletions
|
|
@ -270,12 +270,13 @@ fn parse_ymd_strict(s: &str) -> Result<i32, BsonEodPriceError> {
|
|||
|
||||
#[inline]
|
||||
fn decode_element_value<'a>(
|
||||
field: &'static str,
|
||||
elem: RawElement<'a>,
|
||||
offset: usize,
|
||||
) -> Result<RawBsonRef<'a>, BsonEodPriceError> {
|
||||
elem.value().map_err(|err| BsonEodPriceError::CorruptBson {
|
||||
offset,
|
||||
detail: err.to_string(),
|
||||
detail: format!("field '{field}': {err}"),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -340,7 +341,7 @@ fn decode_eod_price_doc_at<'a>(
|
|||
let key = elem.key();
|
||||
|
||||
if key == cstr!("eodSearchCode") {
|
||||
match decode_element_value(elem, offset)? {
|
||||
match decode_element_value("eodSearchCode", elem, offset)? {
|
||||
RawBsonRef::String(s) => {
|
||||
if s.trim().is_empty() {
|
||||
return Err(BsonEodPriceError::EmptyField {
|
||||
|
|
@ -357,7 +358,7 @@ fn decode_eod_price_doc_at<'a>(
|
|||
}
|
||||
}
|
||||
} else if key == cstr!("date") {
|
||||
match decode_element_value(elem, offset)? {
|
||||
match decode_element_value("date", elem, offset)? {
|
||||
RawBsonRef::String(s) => {
|
||||
session_date = Some(parse_ymd_strict(s)?);
|
||||
}
|
||||
|
|
@ -375,19 +376,19 @@ fn decode_eod_price_doc_at<'a>(
|
|||
}
|
||||
}
|
||||
} else if key == cstr!("open") {
|
||||
let val = decode_element_value(elem, offset)?;
|
||||
let val = decode_element_value("open", elem, offset)?;
|
||||
open = Some(decode_non_negative_number("open", val)?);
|
||||
} else if key == cstr!("high") {
|
||||
let val = decode_element_value(elem, offset)?;
|
||||
let val = decode_element_value("high", elem, offset)?;
|
||||
high = Some(decode_non_negative_number("high", val)?);
|
||||
} else if key == cstr!("low") {
|
||||
let val = decode_element_value(elem, offset)?;
|
||||
let val = decode_element_value("low", elem, offset)?;
|
||||
low = Some(decode_non_negative_number("low", val)?);
|
||||
} else if key == cstr!("close") {
|
||||
let val = decode_element_value(elem, offset)?;
|
||||
let val = decode_element_value("close", elem, offset)?;
|
||||
close = Some(decode_non_negative_number("close", val)?);
|
||||
} else if key == cstr!("volume") {
|
||||
let val = decode_element_value(elem, offset)?;
|
||||
let val = decode_element_value("volume", elem, offset)?;
|
||||
adj_volume = Some(decode_non_negative_number("volume", val)?);
|
||||
}
|
||||
// Unknown extra fields are skipped without calling `elem.value()`.
|
||||
|
|
|
|||
|
|
@ -682,3 +682,104 @@ fn decoded_nvda_split_buffer_feeds_build_bars_1d_with_non_string_key_mapper() {
|
|||
assert_eq!(r2.close.to_bits(), 120.91_f64.to_bits());
|
||||
assert_eq!(r2.volume.to_bits(), 222_551_200.0_f64.to_bits());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_utf8_in_known_field_names_field_and_stream_continues() {
|
||||
let good_1 = make_valid_doc("AAPL.US", "2024-06-05", 194.0, 196.9, 193.5, 195.87, 54_000_000.0);
|
||||
let good_2 = make_valid_doc("MSFT.US", "2024-06-06", 423.0, 425.3, 420.5, 424.52, 14_800_000.0);
|
||||
|
||||
// Build a length-valid document and corrupt a byte inside the `eodSearchCode` string value
|
||||
// to invalid UTF-8 (`0xFF`) while leaving all framing and null terminators intact.
|
||||
let mut bad_utf8 = make_valid_doc("BADX.US", "2024-06-05", 10.0, 11.0, 9.0, 10.5, 100.0)
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
let needle = b"BADX.US";
|
||||
let pos = bad_utf8
|
||||
.windows(needle.len())
|
||||
.position(|w| w == needle)
|
||||
.expect("fixture should contain sentinel eodSearchCode bytes");
|
||||
bad_utf8[pos] = 0xFF;
|
||||
|
||||
// Single-document decode reports CorruptBson naming the field.
|
||||
let err = decode_eod_price_bytes(&bad_utf8).unwrap_err();
|
||||
match &err {
|
||||
BsonEodPriceError::CorruptBson { offset, detail } => {
|
||||
assert_eq!(*offset, 0);
|
||||
assert!(
|
||||
detail.contains("eodSearchCode"),
|
||||
"CorruptBson detail should name 'eodSearchCode', got: {detail}"
|
||||
);
|
||||
}
|
||||
other => panic!("expected CorruptBson for invalid UTF-8, got {other:?}"),
|
||||
}
|
||||
assert!(
|
||||
err.to_string().contains("eodSearchCode"),
|
||||
"Display output should name 'eodSearchCode', got: {err}"
|
||||
);
|
||||
|
||||
// Stream continues past the invalid-UTF-8 document and decodes the following document.
|
||||
let mut buffer = Vec::new();
|
||||
buffer.extend_from_slice(good_1.as_bytes());
|
||||
let bad_offset = buffer.len();
|
||||
buffer.extend_from_slice(&bad_utf8);
|
||||
buffer.extend_from_slice(good_2.as_bytes());
|
||||
|
||||
let mut decoded_codes: Vec<String> = Vec::new();
|
||||
let mut errors: Vec<(usize, BsonEodPriceError)> = Vec::new();
|
||||
let stats = decode_eod_price_buffer(
|
||||
&buffer,
|
||||
|bar| decoded_codes.push(bar.eod_search_code.to_string()),
|
||||
|offset, err| errors.push((offset, err)),
|
||||
);
|
||||
|
||||
assert_eq!(decoded_codes, vec!["AAPL.US", "MSFT.US"]);
|
||||
assert_eq!(stats.docs_in, 3);
|
||||
assert_eq!(stats.decoded, 2);
|
||||
assert_eq!(stats.rejected_corrupt_document, 1);
|
||||
assert_eq!(stats.rejected_total(), 1);
|
||||
assert_eq!(stats.decoded + stats.rejected_total(), stats.docs_in);
|
||||
assert_eq!(errors.len(), 1);
|
||||
assert_eq!(errors[0].0, bad_offset);
|
||||
assert!(
|
||||
errors[0].1.to_string().contains("eodSearchCode"),
|
||||
"stream error should name 'eodSearchCode', got: {}",
|
||||
errors[0].1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_length_prefix_in_middle_stops_stream_cleanly() {
|
||||
let good_1 = make_valid_doc("AAPL.US", "2024-06-05", 194.0, 196.9, 193.5, 195.87, 54_000_000.0);
|
||||
let good_2 = make_valid_doc("MSFT.US", "2024-06-06", 423.0, 425.3, 420.5, 424.52, 14_800_000.0);
|
||||
|
||||
let mut buffer = Vec::new();
|
||||
buffer.extend_from_slice(good_1.as_bytes());
|
||||
let bad_offset = buffer.len();
|
||||
buffer.extend_from_slice(&2_i32.to_le_bytes());
|
||||
buffer.extend_from_slice(good_2.as_bytes());
|
||||
|
||||
let mut decoded_codes: Vec<String> = Vec::new();
|
||||
let mut errors: Vec<(usize, BsonEodPriceError)> = Vec::new();
|
||||
let stats = decode_eod_price_buffer(
|
||||
&buffer,
|
||||
|bar| decoded_codes.push(bar.eod_search_code.to_string()),
|
||||
|offset, err| errors.push((offset, err)),
|
||||
);
|
||||
|
||||
assert_eq!(decoded_codes, vec!["AAPL.US"]);
|
||||
assert_eq!(stats.docs_in, 2);
|
||||
assert_eq!(stats.decoded, 1);
|
||||
assert_eq!(stats.rejected_corrupt_document, 1);
|
||||
assert_eq!(stats.rejected_total(), 1);
|
||||
assert_eq!(stats.decoded + stats.rejected_total(), stats.docs_in);
|
||||
assert_eq!(
|
||||
errors,
|
||||
vec![(
|
||||
bad_offset,
|
||||
BsonEodPriceError::CorruptDocumentLength {
|
||||
offset: bad_offset,
|
||||
declared_len: 2,
|
||||
}
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue