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
OrderBookis the publisher (subject) - The
MarketDepthCacheis the subscriber (observer) OrderEventis 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
impl MarketDepthCache
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty market depth cache.
§Examples
use order_book::MarketDepthCache;
let cache = MarketDepthCache::new();Sourcepub fn process_order_event(&self, event: OrderEvent)
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);Sourcepub fn get_aggregated_market_depth(
&self,
) -> (AggregatedDepthMap, AggregatedDepthMap)
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));Sourcepub fn get_quantity_at_level(
&self,
aggregated_level: Decimal,
side: Side,
) -> u64
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 queryside: 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);Sourcepub fn bid_levels_count(&self) -> usize
pub fn bid_levels_count(&self) -> usize
Returns the number of aggregated price levels on the bid side.
Sourcepub fn ask_levels_count(&self) -> usize
pub fn ask_levels_count(&self) -> usize
Returns the number of aggregated price levels on the ask side.