Available languages: [Python](https://databento.com/docs/api-historical/helpers/dbn-store-new.python.md) · [C++](https://databento.com/docs/api-historical/helpers/dbn-store-new.cpp.md) · [Rust](https://databento.com/docs/api-historical/helpers/dbn-store-new.rust.md) · [HTTP](https://databento.com/docs/api-historical/helpers/dbn-store-new.http.md)

Source: https://databento.com/docs/api-historical/helpers/dbn-store-new


### AsyncDbnStore::new

Create a new decoder from a DBN stream that implements [AsyncReadExt](https://docs.rs/tokio/latest/tokio/io/trait.AsyncReadExt.html).
Immediately decodes the DBN [Metadata](https://databento.com/docs/api-historical/helpers/metadata.md).

#### API method

```rust
pub async fn new<R>(mut reader: R) -> dbn::Result<Self>
where
    R: tokio::io::AsyncReadExt + Unpin;
```

#### Parameters

| Parameter | Type | Description |
| `reader` | R: tokio::io::AsyncReadExt + Unpin | A readable DBN byte stream. |

#### Returns

An [AsyncDbnStore](https://databento.com/docs/api-historical/helpers/dbn-store.md) object.

This function will return an error if it is unable to parse the metadata in `reader` or the input is encoded in a newer version of DBN.

#### Example usage

```rust
use std::io::SeekFrom;

use databento::{
    HistoricalClient,
    dbn::{
        Dataset, Schema, TradeMsg,
        decode::{AsyncDbnDecoder, DbnMetadata},
        encode::{AsyncDbnEncoder, AsyncEncodeRecord},
    },
    historical::timeseries::GetRangeParams,
};
use time::macros::datetime;
use tokio::{fs::OpenOptions, io::AsyncSeekExt};

let mut client =
    HistoricalClient::builder().key("$YOUR_API_KEY")?.build()?;
let mut decoder = client
    .timeseries()
    .get_range(
        &GetRangeParams::builder()
            .dataset(Dataset::GlbxMdp3)
            .date_time_range(
                datetime!(2022-06-06 00:00 UTC)
                    ..datetime!(2022-06-07 00:00 UTC),
            )
            .symbols("ESM2")
            .schema(Schema::Trades)
            .build(),
    )
    .await?;

// Save streamed data to .dbn
let path = "GLBX-ESM2-20220606.trades.dbn.zst";
let mut file = OpenOptions::new()
    .read(true)
    .write(true)
    .create(true)
    .truncate(true)
    .open(path)
    .await?;
let mut encoder =
    AsyncDbnEncoder::new(&mut file, decoder.metadata()).await?;
while let Some(trade) =
    decoder.decode_record::<TradeMsg>().await?
{
    encoder.encode_record(trade).await?;
}
encoder.flush().await?;

// Open saved data
file.seek(SeekFrom::Start(0)).await?;
let mut decoder = AsyncDbnDecoder::new(file).await?;
for i in 0..5 {
    let trade =
        decoder.decode_record::<TradeMsg>().await?.unwrap();
    println!("{trade:?}");
}
```
#### Example response

```plaintext
TradeMsg { hd: RecordHeader { length: 12, rtype: Mbp0, publisher_id: GlbxMdp3Glbx, instrument_id: 3403, ts_event: 1654473600070033767 }, price: 4108.500000000, size: 1, action: 'T', side: 'A', flags: 0, depth: 0, ts_recv: 1654473600070314216, ts_in_delta: 18681, sequence: 157862 }
TradeMsg { hd: RecordHeader { length: 12, rtype: Mbp0, publisher_id: GlbxMdp3Glbx, instrument_id: 3403, ts_event: 1654473600089830441 }, price: 4108.250000000, size: 1, action: 'T', side: 'A', flags: 0, depth: 0, ts_recv: 1654473600090544076, ts_in_delta: 18604, sequence: 157922 }
TradeMsg { hd: RecordHeader { length: 12, rtype: Mbp0, publisher_id: GlbxMdp3Glbx, instrument_id: 3403, ts_event: 1654473600807018955 }, price: 4108.250000000, size: 4, action: 'T', side: 'B', flags: 0, depth: 0, ts_recv: 1654473600807324169, ts_in_delta: 18396, sequence: 158072 }
TradeMsg { hd: RecordHeader { length: 12, rtype: Mbp0, publisher_id: GlbxMdp3Glbx, instrument_id: 3403, ts_event: 1654473601317385867 }, price: 4108.000000000, size: 1, action: 'T', side: 'A', flags: 0, depth: 0, ts_recv: 1654473601317722490, ts_in_delta: 22043, sequence: 158111 }
TradeMsg { hd: RecordHeader { length: 12, rtype: Mbp0, publisher_id: GlbxMdp3Glbx, instrument_id: 3403, ts_event: 1654473601317385867 }, price: 4108.000000000, size: 7, action: 'T', side: 'A', flags: 0, depth: 0, ts_recv: 1654473601317736158, ts_in_delta: 17280, sequence: 158112 }
```
