Skip to content

Commit

Permalink
Implement OrderBookDeltas FFI and pyo3 interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Feb 13, 2024
1 parent 753b769 commit 0009f3a
Show file tree
Hide file tree
Showing 12 changed files with 543 additions and 45 deletions.
27 changes: 12 additions & 15 deletions nautilus_core/model/src/data/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use super::delta::OrderBookDelta;
use crate::identifiers::instrument_id::InstrumentId;

/// Represents a grouped batch of `OrderBookDelta` updates for an `OrderBook`.
#[repr(C)]
///
/// This type cannot be `repr(C)` due to the `deltas` vec.
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "python",
Expand All @@ -46,14 +47,14 @@ pub struct OrderBookDeltas {
impl OrderBookDeltas {
#[allow(clippy::too_many_arguments)]
#[must_use]
pub fn new(
instrument_id: InstrumentId,
deltas: Vec<OrderBookDelta>,
flags: u8,
sequence: u64,
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> Self {
pub fn new(instrument_id: InstrumentId, deltas: Vec<OrderBookDelta>) -> Self {
assert!(!deltas.is_empty(), "`deltas` cannot be empty");
// SAFETY: We asserted `deltas` is not empty
let last = deltas.last().unwrap();
let flags = last.flags;
let sequence = last.sequence;
let ts_event = last.ts_event;
let ts_init = last.ts_init;
Self {
instrument_id,
deltas,
Expand All @@ -65,7 +66,7 @@ impl OrderBookDeltas {
}
}

// TODO: Potentially implement later
// TODO: Implement
// impl Serializable for OrderBookDeltas {}

// TODO: Exact format for Debug and Display TBD
Expand Down Expand Up @@ -195,7 +196,7 @@ pub mod stubs {

let deltas = vec![delta0, delta1, delta2, delta3, delta4, delta5, delta6];

OrderBookDeltas::new(instrument_id, deltas, flags, sequence, ts_event, ts_init)
OrderBookDeltas::new(instrument_id, deltas)
}
}

Expand Down Expand Up @@ -310,10 +311,6 @@ mod tests {
let deltas = OrderBookDeltas::new(
instrument_id,
vec![delta0, delta1, delta2, delta3, delta4, delta5, delta6],
flags,
sequence,
ts_event,
ts_init,
);

assert_eq!(deltas.instrument_id, instrument_id);
Expand Down
3 changes: 1 addition & 2 deletions nautilus_core/model/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use self::{

#[repr(C)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[allow(clippy::large_enum_variant)] // TODO: Optimize this (largest variant 1008 vs 136 bytes)
pub enum Data {
Delta(OrderBookDelta),
Expand Down Expand Up @@ -129,5 +128,5 @@ impl From<Bar> for Data {

#[no_mangle]
pub extern "C" fn data_clone(data: &Data) -> Data {
*data // Actually a copy
data.clone()
}
118 changes: 118 additions & 0 deletions nautilus_core/model/src/ffi/data/deltas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::ops::{Deref, DerefMut};

use nautilus_core::{ffi::cvec::CVec, time::UnixNanos};

use crate::{
data::{delta::OrderBookDelta, deltas::OrderBookDeltas},
enums::BookAction,
identifiers::instrument_id::InstrumentId,
};

/// Provides a C compatible Foreign Function Interface (FFI) for an underlying [`OrderBookDeltas`].
///
/// This struct wraps `OrderBookDeltas` in a way that makes it compatible with C function
/// calls, enabling interaction with `OrderBookDeltas` in a C environment.
///
/// It implements the `Deref` trait, allowing instances of `OrderBookDeltas_API` to be
/// dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
/// having to manually access the underlying `OrderBookDeltas` instance.
#[repr(C)]
#[allow(non_camel_case_types)]
pub struct OrderBookDeltas_API(Box<OrderBookDeltas>);

impl Deref for OrderBookDeltas_API {
type Target = OrderBookDeltas;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for OrderBookDeltas_API {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

/// Creates a new `OrderBookDeltas` object from a CVec of `OrderBookDelta`.
///
/// # Safety
/// - The `deltas` must be a valid pointer to a `CVec` containing `OrderBookDelta` objects
/// - This function clones the data pointed to by `deltas` into Rust-managed memory, then forgets the original `Vec` to prevent Rust from auto-deallocating it
/// - The caller is responsible for managing the memory of `deltas` (including its deallocation) to avoid memory leaks
#[no_mangle]
pub extern "C" fn orderbook_deltas_new(
instrument_id: InstrumentId,
deltas: &CVec,
) -> OrderBookDeltas_API {
let CVec { ptr, len, cap } = *deltas;
let deltas: Vec<OrderBookDelta> =
unsafe { Vec::from_raw_parts(ptr as *mut OrderBookDelta, len, cap) };
let cloned_deltas = deltas.clone();
std::mem::forget(deltas); // Prevents Rust from dropping `deltas`
OrderBookDeltas_API(Box::new(OrderBookDeltas::new(instrument_id, cloned_deltas)))
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_drop(deltas: OrderBookDeltas_API) {
drop(deltas); // Memory freed here
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_instrument_id(deltas: &OrderBookDeltas_API) -> InstrumentId {
deltas.instrument_id
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_vec_deltas(deltas: &OrderBookDeltas_API) -> CVec {
deltas.deltas.clone().into()
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_is_snapshot(deltas: &OrderBookDeltas_API) -> u8 {
u8::from(deltas.deltas[0].action == BookAction::Clear)
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_flags(deltas: &OrderBookDeltas_API) -> u8 {
deltas.flags
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_sequence(deltas: &OrderBookDeltas_API) -> u64 {
deltas.sequence
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_ts_event(deltas: &OrderBookDeltas_API) -> UnixNanos {
deltas.ts_event
}

#[no_mangle]
pub extern "C" fn orderbook_deltas_ts_init(deltas: &OrderBookDeltas_API) -> UnixNanos {
deltas.ts_init
}

#[allow(clippy::drop_non_drop)]
#[no_mangle]
pub extern "C" fn orderbook_deltas_vec_drop(v: CVec) {
let CVec { ptr, len, cap } = v;
let deltas: Vec<OrderBookDelta> =
unsafe { Vec::from_raw_parts(ptr as *mut OrderBookDelta, len, cap) };
drop(deltas); // Memory freed here
}
1 change: 1 addition & 0 deletions nautilus_core/model/src/ffi/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

pub mod bar;
pub mod delta;
pub mod deltas;
pub mod depth;
pub mod order;
pub mod quote;
Expand Down
126 changes: 126 additions & 0 deletions nautilus_core/model/src/python/data/deltas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

// use std::{
// collections::{hash_map::DefaultHasher, HashMap},
// hash::{Hash, Hasher},
// };

use nautilus_core::time::UnixNanos;
use pyo3::prelude::*;

use crate::{
data::{delta::OrderBookDelta, deltas::OrderBookDeltas},
identifiers::instrument_id::InstrumentId,
python::PY_MODULE_MODEL,
};

#[pymethods]
impl OrderBookDeltas {
#[new]
fn py_new(instrument_id: InstrumentId, deltas: Vec<OrderBookDelta>) -> Self {
Self::new(instrument_id, deltas)
}

// TODO: Implement
// fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
// match op {
// CompareOp::Eq => self.eq(other).into_py(py),
// CompareOp::Ne => self.ne(other).into_py(py),
// _ => py.NotImplemented(),
// }
// }

// TODO: Implement
// fn __hash__(&self) -> isize {
// let mut h = DefaultHasher::new();
// self.hash(&mut h);
// h.finish() as isize
// }

fn __str__(&self) -> String {
self.to_string()
}

fn __repr__(&self) -> String {
format!("{self:?}")
}

#[getter]
#[pyo3(name = "instrument_id")]
fn py_instrument_id(&self) -> InstrumentId {
self.instrument_id
}

#[getter]
#[pyo3(name = "deltas")]
fn py_deltas(&self) -> Vec<OrderBookDelta> {
// `OrderBookDelta` is `Copy`
self.deltas.clone()
}

#[getter]
#[pyo3(name = "flags")]
fn py_flags(&self) -> u8 {
self.flags
}

#[getter]
#[pyo3(name = "sequence")]
fn py_sequence(&self) -> u64 {
self.sequence
}

#[getter]
#[pyo3(name = "ts_event")]
fn py_ts_event(&self) -> UnixNanos {
self.ts_event
}

#[getter]
#[pyo3(name = "ts_init")]
fn py_ts_init(&self) -> UnixNanos {
self.ts_init
}

#[staticmethod]
#[pyo3(name = "fully_qualified_name")]
fn py_fully_qualified_name() -> String {
format!("{}:{}", PY_MODULE_MODEL, stringify!(OrderBookDeltas))
}

// /// Creates a `PyCapsule` containing a raw pointer to a `Data::Delta` object.
// ///
// /// This function takes the current object (assumed to be of a type that can be represented as
// /// `Data::Delta`), and encapsulates a raw pointer to it within a `PyCapsule`.
// ///
// /// # Safety
// ///
// /// This function is safe as long as the following conditions are met:
// /// - The `Data::Delta` object pointed to by the capsule must remain valid for the lifetime of the capsule.
// /// - The consumer of the capsule must ensure proper handling to avoid dereferencing a dangling pointer.
// ///
// /// # Panics
// ///
// /// The function will panic if the `PyCapsule` creation fails, which can occur if the
// /// `Data::Delta` object cannot be converted into a raw pointer.
// ///
// #[pyo3(name = "as_pycapsule")]
// fn py_as_pycapsule(&self, py: Python<'_>) -> PyObject {
// data_to_pycapsule(py, Data::Delta(*self))
// }

// TODO: Implement `Serializable` and the other methods can be added
}
1 change: 1 addition & 0 deletions nautilus_core/model/src/python/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

pub mod bar;
pub mod delta;
pub mod deltas;
pub mod depth;
pub mod order;
pub mod quote;
Expand Down
Loading

0 comments on commit 0009f3a

Please sign in to comment.