Skip to main content

Module decode

Module decode 

Source
Expand description

Decoding DBN and Zstd-compressed DBN files and streams.

The primary entry point is DbnDecoder, which reads DBN data from any io::Read source (files, network streams, in-memory buffers). When the format or compression is unknown at compile time, use DynDecoder to auto-detect from the first few bytes.

Sync decoders implement the DecodeDbn trait. With the async feature flag, async variants are also available.

§Examples

Decode a DBN file, dispatching on record type:

use dbn::decode::{DbnDecoder, DecodeRecordRef, DbnMetadata};
use dbn::{TradeMsg, OhlcvMsg, Record};

let mut decoder = DbnDecoder::from_zstd_file("20241007.dbn.zst")?;
println!("schema: {:?}", decoder.metadata().schema);

while let Some(rec_ref) = decoder.decode_record_ref()? {
    if let Some(trade) = rec_ref.get::<TradeMsg>() {
        println!("trade: instrument={} price={}", trade.hd.instrument_id, trade.price);
    } else if let Some(bar) = rec_ref.get::<OhlcvMsg>() {
        println!("bar: instrument={} close={}", bar.hd.instrument_id, bar.close);
    }
}

Decode all records of a known type into a Vec:

use dbn::decode::{DbnDecoder, DecodeRecord};
use dbn::MboMsg;

let decoder = DbnDecoder::from_zstd_file("20241007.mbo.dbn.zst")?;
let records: Vec<MboMsg> = decoder.decode_records()?;
println!("{} MBO records", records.len());

Re-exports§

pub use self::dbn::Decoder as DbnDecoder;
pub use self::dbn::MetadataDecoder as DbnMetadataDecoder;
pub use self::dbn::RecordDecoder as DbnRecordDecoder;
pub use self::dbn::AsyncDecoder as AsyncDbnDecoder;async
pub use self::dbn::AsyncMetadataDecoder as AsyncDbnMetadataDecoder;async
pub use self::dbn::AsyncRecordDecoder as AsyncDbnRecordDecoder;async

Modules§

dbn
Decoding of DBN files.
dbzDeprecated
Decoding of legacy DBZ files, a precursor to DBN.

Structs§

AsyncDynReaderasync
A type for runtime polymorphism on compressed and uncompressed input. The async version of DynReader.
DynDecoder
A decoder whose Encoding and Compression are determined at runtime by peeking at the first few bytes.
DynReader
Type for runtime polymorphism over whether decoding uncompressed or Zstd-compressed DBN records. Implements std::io::Write.
MergeDecoder
Merges the DBN decoding streams from one or more other decoders. Both metadata and the record streams are merged.
MergeRecordDecoder
Merges the record decoding streams from one or more other decoders, performing a k-merge based on Record::index_ts().
StreamIterDecoder
A consuming iterator wrapping a DecodeRecord. Lazily decodes the contents of the file or other input stream.

Traits§

AsyncDecodeRecordasync
Async trait for types that decode DBN records of a particular type.
AsyncDecodeRecordRefasync
Async trait for types that decode references to DBN records of a dynamic type.
AsyncSkipBytesasync
Like AsyncSeek, but only allows seeking forward from the current position.
DbnMetadata
Trait for decoders with metadata about what’s being decoded.
DecodeDbn
A trait alias for DBN decoders with metadata.
DecodeRecord
Trait for types that decode DBN records of a particular type.
DecodeRecordRef
Trait for types that decode references to DBN records of a dynamic type.
DecodeStream
A trait for decoders that can be converted to streaming iterators.
SkipBytes
Like Seek, but only allows seeking forward from the current position.