MarketDepthCache

Struct MarketDepthCache 

Source
pub struct MarketDepthCache { /* private fields */ }
Expand description

An external cache service that maintains aggregated market depth.

This structure is completely decoupled from the core OrderBook and operates on events published by the order book. It maintains its own state and locks, allowing for maximum concurrency:

  • Readers can query market depth without blocking order insertion
  • Order insertion doesn’t need to wait for depth aggregation
  • The cache can be updated asynchronously after the core book is modified

§Architecture

This follows the Observer Pattern:

  • The OrderBook is the publisher (subject)
  • The MarketDepthCache is the subscriber (observer)
  • OrderEvent is the message passed between them

§Thread Safety

The bid and ask depth maps are protected by separate RwLocks, allowing concurrent reads and serialized writes. This structure can be safely shared across threads using Arc<MarketDepthCache>.

Implementations§

Source§

impl MarketDepthCache

Source

pub fn new() -> Self

Creates a new empty market depth cache.

§Examples
use order_book::MarketDepthCache;

let cache = MarketDepthCache::new();
Source

pub fn process_order_event(&self, event: OrderEvent)

Processes an order event and updates the aggregated market depth.

This method is called after an order is inserted into the order book. It aggregates the order price to its level and updates the cached quantity.

The operation is $O(\log{N})$ where $N$ is the number of aggregated price levels. The lock is held only for the duration of the BTreeMap update.

§Arguments
  • event: The order event to process
§Examples
use order_book::{OrderBook, MarketDepthCache, Order, Side};
use rust_decimal::Decimal;

let mut order_book = OrderBook::new();
let cache = MarketDepthCache::new();

let order = Order::new(100.50, 100, Side::Bid);

let event = order_book.insert_order(order);
cache.process_order_event(event);
Source

pub fn get_aggregated_market_depth( &self, ) -> (AggregatedDepthMap, AggregatedDepthMap)

Retrieves a snapshot of the current aggregated market depth.

This method clones the current depth maps to provide a consistent snapshot. Multiple readers can call this method concurrently without blocking each other or blocking order insertion.

The operation is $O(N)$ where $N$ is the number of aggregated price levels, due to the BTreeMap clone.

§Returns

A tuple of (bid_depth, ask_depth) where each is an AggregatedDepthMap mapping aggregated price levels to total quantities.

§Examples
use order_book::{OrderBook, MarketDepthCache, Order, Side};
use rust_decimal::Decimal;

let mut order_book = OrderBook::new();
let cache = MarketDepthCache::new();

let order = Order::new(100.50, 100, Side::Bid);

let event = order_book.insert_order(order);
cache.process_order_event(event);

let (bid_depth, ask_depth) = cache.get_aggregated_market_depth();
assert_eq!(bid_depth.get(&Decimal::new(100, 0)), Some(&100));
Source

pub fn get_quantity_at_level( &self, aggregated_level: Decimal, side: Side, ) -> u64

Returns the total quantity at a specific aggregated price level.

§Arguments
  • aggregated_level: The aggregated price level to query
  • side: The side (bid or ask) to query
§Returns

The total quantity at that level, or 0 if no orders exist

§Examples
use order_book::{OrderBook, MarketDepthCache, Order, Side};
use rust_decimal::Decimal;

let mut order_book = OrderBook::new();
let cache = MarketDepthCache::new();

let order = Order::new(100.50, 100, Side::Bid);

let event = order_book.insert_order(order);
cache.process_order_event(event);

let quantity = cache.get_quantity_at_level(Decimal::new(100, 0), Side::Bid);
assert_eq!(quantity, 100);
Source

pub fn bid_levels_count(&self) -> usize

Returns the number of aggregated price levels on the bid side.

Source

pub fn ask_levels_count(&self) -> usize

Returns the number of aggregated price levels on the ask side.

Source

pub fn clear(&self)

Clears all cached market depth data.

This is useful for testing or resetting the cache state.

Trait Implementations§

Source§

impl Debug for MarketDepthCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MarketDepthCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.