Skip to main content

Router

Struct Router 

Source
pub struct Router<C: ContextProvider = DefaultContextProvider> { /* private fields */ }
Expand description

The main routing engine for SPARQL federation

Implementations§

Source§

impl<C: ContextProvider> Router<C>

Source

pub fn set_online_training(&mut self, _enabled: bool)

Enable or disable online training (no-op without ml feature).

Source

pub fn is_online_training_enabled(&self) -> bool

Whether online training is enabled (always false without ml feature).

Source

pub fn query_log(&self) -> &QueryLog

Get a reference to the query log

Source

pub fn query_log_mut(&mut self) -> &mut QueryLog

Get a mutable reference to the query log

Source

pub fn source_stats(&self, source_id: &str) -> Option<&SourceLogStats>

Return historical stats for a specific source from the query log.

Source

pub fn ranked_sources_from_log(&self) -> Vec<(String, f32)>

Return all sources ranked by their log-derived routing score.

Source

pub fn best_source_from_log(&self) -> Option<String>

Return the best source according to the query log, if any.

Source

pub fn query_log_len(&self) -> usize

Return the total number of entries in the query log.

Source

pub fn route_and_log(&mut self, query: &Query) -> Result<SourceRanking>

Route a query to the best sources, record in the query log, and apply RL feedback.

Feature vectors are captured when ml is enabled and online training is active, stored in the query log for later use by Router::learn_from_outcome.

After execution, call Router::learn_from_outcome with the actual result to complete the feedback loop.

§Errors

Returns an error if no sources are available or routing fails.

Source

pub fn learn_from_outcome( &mut self, query_id: u64, source_id: &str, success: bool, latency_ms: u32, result_count: u32, ) -> Result<()>

Record the outcome of a routed query and update statistics + RL policy.

When online training is enabled and the model supports it, this also updates the ML model weights using the feature vector stored at routing time.

query_id should match the predicate_hash() of the query used in route_and_log.

Source

pub fn update_source_stats( &mut self, source_id: &str, latency_ms: u32, success: bool, result_count: u32, ) -> Result<()>

Update source statistics after query execution

Source

pub fn mark_unavailable(&mut self, source_id: &str) -> Result<()>

Mark a source as unavailable

Source

pub fn mark_available(&mut self, source_id: &str) -> Result<()>

Mark a source as available

Source

pub const fn config(&self) -> &RouterConfig

Get the router configuration

Source

pub fn config_mut(&mut self) -> &mut RouterConfig

Get a mutable reference to the router configuration

Source

pub fn set_circuit_breaker_config(&mut self, cfg: CircuitBreakerConfig)

Replace the circuit breaker configuration.

Useful for disabling (failure_threshold = 0) or tuning the breaker at runtime without rebuilding the whole router config.

Source

pub fn save_state(&self) -> Result<Vec<u8>>

Serialize the router’s full learnable state to bytes (v2 format).

Captures all registered sources, the trained ML model (if any), RL policy state (if any), the query log, and the current router configuration. Pass the resulting bytes to Router::load_state to restore the router after a process restart.

§Errors

Returns an error if JSON serialization of any component fails.

Source

pub fn load_state(&mut self, bytes: &[u8]) -> Result<()>

Restore the router from bytes produced by Router::save_state.

Replaces sources, model, RL policy, query log, and (when present in the snapshot) the router configuration. V1 snapshots that lack a config field leave the current configuration unchanged.

§Errors

Returns OxiRouterError::IncompatibleModel on magic/version mismatch and OxiRouterError::ModelError if a component cannot be deserialized.

Source§

impl<C: ContextProvider> Router<C>

Source

pub fn route(&self, query: &Query) -> Result<SourceRanking>

Route a query to the best sources

§Errors

Returns an error if no sources are available or routing fails

Source

pub fn explain(&self, query: &Query) -> Result<Vec<RoutingExplanation>>

Return per-feature scoring breakdowns for every non-tripped source.

Unlike Router::route this method does not truncate to max_sources — it returns one RoutingExplanation per eligible source so callers can inspect the full picture.

§Errors

Returns OxiRouterError::NoSources when no sources are registered.

Source§

impl Router<DefaultContextProvider>

Source

pub fn new() -> Self

Create a new router with default context provider

Source

pub fn with_config_file<P: AsRef<Path>>(path: P) -> Result<Self>

Create a router by reading a JSON config file from disk.

Deserializes the file as a RouterConfig and calls Router::with_config with the result.

§Errors

Returns OxiRouterError::InvalidSource if the file cannot be read or if its contents are not valid RouterConfig JSON.

Source§

impl<C: ContextProvider> Router<C>

Source

pub fn with_context_provider(context_provider: C) -> Self

Create a new router with a custom context provider

Source

pub fn with_config(config: RouterConfig, context_provider: C) -> Self

Create a new router with custom configuration

Source

pub fn add_source(&mut self, source: DataSource)

Add a data source

Source

pub fn remove_source(&mut self, id: &str) -> Option<DataSource>

Remove a data source

Source

pub fn get_source(&self, id: &str) -> Option<&DataSource>

Get a data source by ID

Source

pub fn get_source_mut(&mut self, id: &str) -> Option<&mut DataSource>

Get a mutable reference to a data source

Source

pub fn sources(&self) -> impl Iterator<Item = &DataSource>

Get all registered sources

Source

pub fn source_count(&self) -> usize

Get the number of registered sources

Trait Implementations§

Source§

impl Default for Router<DefaultContextProvider>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<C> Freeze for Router<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Router<C>
where C: RefUnwindSafe,

§

impl<C> Send for Router<C>

§

impl<C> Sync for Router<C>

§

impl<C> Unpin for Router<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Router<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for Router<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.