1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Record data types for encoding different Databento [`Schema`](crate::enums::Schema)s
//! in the upcoming DBN version 4.
//!
//! In development and may change.
pub mod rtype;
use crate::{record::HasRType, Publisher, RType};
pub mod fields;
pub mod layouts;
#[cfg(test)]
mod layouts_tests;
mod methods;
mod records;
pub mod types;
pub use records::*;
/// A non-owning immutable reference to a DBN version 4 record.
pub type RecordRef<'a> = crate::RecordRef<'a, RecordHeader>;
/// A non-owning mutable reference to a DBN version 4 record.
pub type RecordRefMut<'a> = crate::RecordRefMut<'a, RecordHeader>;
/// The length in bytes of the largest DBN version 4 record type.
///
/// Provisionally the v3 value.
pub const MAX_RECORD_LEN: usize = crate::MAX_RECORD_LEN;
/// An owned buffer holding any DBN version 4 record of a dynamic type.
pub type RecordBuf<const CAP: usize = MAX_RECORD_LEN> = crate::RecordBuf<CAP, RecordHeader>;
/// Contains common data across all DBN version 4 records including framing and type (`rtype`)
/// fields. Always found at the beginning of the record.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct RecordHeader {
/// The number of bytes following the header.
length: u16,
/// The record type. Record types implement the trait [`HasRType`], and the
/// [`has_rtype`][HasRType::has_rtype] function can be used to check if that type can
/// be used to decode a message with a given rtype. The set of possible values is
/// defined in [`rtype`].
pub rtype: u16,
/// The publisher ID assigned by Databento, which denotes the dataset and venue.
///
/// See [Publishers](https://databento.com/docs/standards-and-conventions/common-fields-enums-types#publishers-datasets-and-venues).
pub publisher_id: u16,
/// Reserved for future use. Always zero.
_reserved: [u8; 2],
/// The numeric instrument ID. See [Instrument identifiers](https://databento.com/docs/standards-and-conventions/common-fields-enums-types#instrument-identifiers).
pub instrument_id: u64,
/// The matching-engine-received timestamp expressed as the number of nanoseconds
/// since the UNIX epoch.
///
/// See [ts_event](https://databento.com/docs/standards-and-conventions/common-fields-enums-types#ts-event).
pub ts_event: u64,
}
impl RecordHeader {
/// Creates a new `RecordHeader`. `R` and `rtype` should be compatible.
pub const fn new<R: HasRType<Header = Self>>(
rtype: u16,
publisher_id: u16,
instrument_id: u64,
ts_event: u64,
) -> Self {
Self {
// The v4 `length` counts only the bytes following the header.
length: (std::mem::size_of::<R>() - std::mem::size_of::<Self>()) as u16,
rtype,
publisher_id,
_reserved: [0; 2],
instrument_id,
ts_event,
}
}
/// A header whose `length` covers `record_size` total bytes, for a record whose
/// size comes from its stream's layout rather than from a compiled struct.
///
/// # Errors
/// Returns an error if `record_size` cannot hold the header, or overflows `length`.
pub fn for_record_size(rtype: u16, record_size: usize) -> crate::Result<Self> {
let body = record_size
.checked_sub(std::mem::size_of::<Self>())
.ok_or_else(|| {
crate::Error::encode(format!(
"record size {record_size} is shorter than the header"
))
})?;
Ok(Self {
length: u16::try_from(body).map_err(|_| {
crate::Error::encode(format!("record size {record_size} overflows `length`"))
})?,
rtype,
publisher_id: 0,
_reserved: [0; 2],
instrument_id: 0,
ts_event: crate::UNDEF_TIMESTAMP,
})
}
/// Returns the size of the **entire** record in bytes. Unlike the v1-v3 header, the
/// v4 `length` is a plain byte count of the fields following the header.
pub const fn record_size(&self) -> usize {
std::mem::size_of::<Self>() + self.length as usize
}
/// Tries to convert the raw record type into an enum.
///
/// # Errors
/// This function returns an error if `rtype` does not contain a valid, known
/// [`RType`].
pub fn rtype(&self) -> crate::Result<RType> {
// `RType` is sized for the v1-v3 `rtype` field, so a v4 rtype beyond a byte has
// no variant to convert to.
u8::try_from(self.rtype)
.ok()
.and_then(|rtype| RType::try_from(rtype).ok())
.ok_or_else(|| crate::Error::conversion::<RType>(format!("{:#06X}", self.rtype)))
}
/// Tries to convert the raw `publisher_id` into an enum.
///
/// # Errors
/// This function returns an error if `publisher_id` does not correspond with any
/// known [`Publisher`].
pub fn publisher(&self) -> crate::Result<Publisher> {
Publisher::try_from(self.publisher_id)
.map_err(|_| crate::Error::conversion::<Publisher>(self.publisher_id))
}
}
#[cfg(test)]
mod tests {
use std::{
mem::{align_of, offset_of, size_of},
os::raw::c_char,
};
use rstest::rstest;
use super::*;
use crate::{FlagSet, Record, RecordRef, RecordRefMut};
fn mbo() -> MboMsg {
MboMsg {
hd: RecordHeader::new::<MboMsg>(rtype::MBO, 1, 5482, 1609160400000000000),
order_id: 16,
price: 5500,
size: 3,
flags: FlagSet::empty(),
channel_id: 14,
action: b'B' as c_char,
side: b'A' as c_char,
ts_recv: 1609160400000000100,
ts_in_delta: 22_000,
sequence: 1_002_375,
}
}
#[test]
fn header_layout() {
assert_eq!(size_of::<RecordHeader>(), 24);
assert_eq!(align_of::<RecordHeader>(), 8);
assert_eq!(offset_of!(RecordHeader, rtype), 2);
assert_eq!(offset_of!(RecordHeader, publisher_id), 4);
assert_eq!(offset_of!(RecordHeader, instrument_id), 8);
assert_eq!(offset_of!(RecordHeader, ts_event), 16);
}
// The v4 `length` counts the bytes after the header.
#[test]
fn new_frames_the_whole_record() {
let hd = RecordHeader::new::<MboMsg>(rtype::MBO, 1, 5482, 0);
assert_eq!(
hd.length as usize,
size_of::<MboMsg>() - size_of::<RecordHeader>()
);
assert_eq!(hd.record_size(), size_of::<MboMsg>());
}
#[rstest]
#[case::mbo(rtype::MBO, true)]
#[case::trades(rtype::MBP_0, false)]
#[case::above_a_byte(0x01A0, false)]
fn has_rtype_takes_the_full_width(#[case] rtype: u16, #[case] expected: bool) {
assert_eq!(MboMsg::has_rtype(rtype), expected);
}
#[test]
fn record_ref_round_trips() {
let mbo = mbo();
let rec: RecordRef<'_, RecordHeader> = RecordRef::from(&mbo);
assert_eq!(rec.record_size(), size_of::<MboMsg>());
assert_eq!(rec.as_ref().len(), size_of::<MboMsg>());
assert_eq!(rec.get::<MboMsg>(), Some(&mbo));
}
#[test]
fn record_ref_mut_writes_through() {
let mut mbo = mbo();
let mut rec: RecordRefMut<'_, RecordHeader> = RecordRefMut::from(&mut mbo);
rec.get_mut::<MboMsg>().unwrap().order_id = 42;
assert_eq!(mbo.order_id, 42);
}
#[test]
fn record_buf_round_trips() {
let buf = RecordBuf::<{ MAX_RECORD_LEN }>::from(mbo());
assert_eq!(buf.header().record_size(), size_of::<MboMsg>());
assert_eq!(buf.get::<MboMsg>(), Some(&mbo()));
}
}