pub struct Router<C: ContextProvider = DefaultContextProvider> { /* private fields */ }Expand description
The main routing engine for SPARQL federation
Implementations§
Source§impl<C: ContextProvider> Router<C>
impl<C: ContextProvider> Router<C>
Sourcepub fn set_online_training(&mut self, _enabled: bool)
pub fn set_online_training(&mut self, _enabled: bool)
Enable or disable online training (no-op without ml feature).
Sourcepub fn is_online_training_enabled(&self) -> bool
pub fn is_online_training_enabled(&self) -> bool
Whether online training is enabled (always false without ml feature).
Sourcepub fn query_log_mut(&mut self) -> &mut QueryLog
pub fn query_log_mut(&mut self) -> &mut QueryLog
Get a mutable reference to the query log
Sourcepub fn source_stats(&self, source_id: &str) -> Option<&SourceLogStats>
pub fn source_stats(&self, source_id: &str) -> Option<&SourceLogStats>
Return historical stats for a specific source from the query log.
Sourcepub fn ranked_sources_from_log(&self) -> Vec<(String, f32)>
pub fn ranked_sources_from_log(&self) -> Vec<(String, f32)>
Return all sources ranked by their log-derived routing score.
Sourcepub fn best_source_from_log(&self) -> Option<String>
pub fn best_source_from_log(&self) -> Option<String>
Return the best source according to the query log, if any.
Sourcepub fn query_log_len(&self) -> usize
pub fn query_log_len(&self) -> usize
Return the total number of entries in the query log.
Sourcepub fn route_and_log(&mut self, query: &Query) -> Result<SourceRanking>
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.
Sourcepub fn learn_from_outcome(
&mut self,
query_id: u64,
source_id: &str,
success: bool,
latency_ms: u32,
result_count: u32,
) -> Result<()>
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.
Sourcepub fn update_source_stats(
&mut self,
source_id: &str,
latency_ms: u32,
success: bool,
result_count: u32,
) -> Result<()>
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
Mark a source as unavailable
Sourcepub fn mark_available(&mut self, source_id: &str) -> Result<()>
pub fn mark_available(&mut self, source_id: &str) -> Result<()>
Mark a source as available
Sourcepub const fn config(&self) -> &RouterConfig
pub const fn config(&self) -> &RouterConfig
Get the router configuration
Sourcepub fn config_mut(&mut self) -> &mut RouterConfig
pub fn config_mut(&mut self) -> &mut RouterConfig
Get a mutable reference to the router configuration
Sourcepub fn set_circuit_breaker_config(&mut self, cfg: CircuitBreakerConfig)
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.
Sourcepub fn save_state(&self) -> Result<Vec<u8>>
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.
Sourcepub fn load_state(&mut self, bytes: &[u8]) -> Result<()>
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>
impl<C: ContextProvider> Router<C>
Sourcepub fn route(&self, query: &Query) -> Result<SourceRanking>
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
Sourcepub fn explain(&self, query: &Query) -> Result<Vec<RoutingExplanation>>
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>
impl Router<DefaultContextProvider>
Sourcepub fn with_config_file<P: AsRef<Path>>(path: P) -> Result<Self>
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>
impl<C: ContextProvider> Router<C>
Sourcepub fn with_context_provider(context_provider: C) -> Self
pub fn with_context_provider(context_provider: C) -> Self
Create a new router with a custom context provider
Sourcepub fn with_config(config: RouterConfig, context_provider: C) -> Self
pub fn with_config(config: RouterConfig, context_provider: C) -> Self
Create a new router with custom configuration
Sourcepub fn add_source(&mut self, source: DataSource)
pub fn add_source(&mut self, source: DataSource)
Add a data source
Sourcepub fn remove_source(&mut self, id: &str) -> Option<DataSource>
pub fn remove_source(&mut self, id: &str) -> Option<DataSource>
Remove a data source
Sourcepub fn get_source(&self, id: &str) -> Option<&DataSource>
pub fn get_source(&self, id: &str) -> Option<&DataSource>
Get a data source by ID
Sourcepub fn get_source_mut(&mut self, id: &str) -> Option<&mut DataSource>
pub fn get_source_mut(&mut self, id: &str) -> Option<&mut DataSource>
Get a mutable reference to a data source
Sourcepub fn sources(&self) -> impl Iterator<Item = &DataSource>
pub fn sources(&self) -> impl Iterator<Item = &DataSource>
Get all registered sources
Sourcepub fn source_count(&self) -> usize
pub fn source_count(&self) -> usize
Get the number of registered sources