oxirs-fuseki 0.3.2

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Federated SPARQL query planning: endpoint registry, discovery, query
//! decomposition, join-order optimization and cost estimation.
//!
//! Implementations split out from `federated_query_optimizer` for
//! maintainability.

use crate::{
    error::{FusekiError, FusekiResult},
    federated_query_types::*,
};
use dashmap::DashMap;
use reqwest::{Client, ClientBuilder, StatusCode};
use std::{
    collections::HashMap,
    sync::Arc,
    time::{Duration, Instant},
};
use tokio::sync::RwLock;

impl Default for EndpointRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl EndpointRegistry {
    pub fn new() -> Self {
        Self {
            endpoints: HashMap::new(),
            health_cache: DashMap::new(),
            discovery: Arc::new(EndpointDiscovery::new()),
        }
    }

    /// Register a new endpoint
    pub fn register_endpoint(&mut self, endpoint: EndpointInfo) {
        self.endpoints.insert(endpoint.url.clone(), endpoint);
    }

    /// Accessor for the underlying endpoint map.
    pub fn endpoints(&self) -> &HashMap<String, EndpointInfo> {
        &self.endpoints
    }

    /// Check endpoint health
    pub async fn check_endpoint_health(&self, endpoint: EndpointInfo) -> FusekiResult<()> {
        let client = ClientBuilder::new()
            .timeout(Duration::from_millis(5000))
            .build()
            .map_err(|e| FusekiError::internal(format!("Client error: {e}")))?;

        let start = Instant::now();
        let response = client
            .get(&endpoint.url)
            .header("Accept", "application/sparql-results+json")
            .query(&[("query", "ASK { ?s ?p ?o } LIMIT 1")])
            .send()
            .await;

        let response_time = start.elapsed().as_millis() as u64;

        match response {
            Ok(resp) if resp.status() == StatusCode::OK => {
                self.health_cache.insert(
                    endpoint.url.clone(),
                    HealthStatus {
                        is_healthy: true,
                        last_check: Instant::now(),
                        response_time_ms: response_time,
                        error_count: 0,
                        success_count: 1,
                    },
                );
                Ok(())
            }
            Ok(resp) => Err(FusekiError::bad_request(format!(
                "Endpoint returned status: {}",
                resp.status()
            ))),
            Err(e) => {
                self.health_cache.insert(
                    endpoint.url.clone(),
                    HealthStatus {
                        is_healthy: false,
                        last_check: Instant::now(),
                        response_time_ms: response_time,
                        error_count: 1,
                        success_count: 0,
                    },
                );
                Err(FusekiError::internal(format!("Health check failed: {e}")))
            }
        }
    }

    /// Discover endpoints from catalogs
    pub async fn discover_endpoints(&self) -> FusekiResult<Vec<EndpointInfo>> {
        self.discovery.discover_from_catalogs().await
    }
}

impl Default for EndpointDiscovery {
    fn default() -> Self {
        Self::new()
    }
}

impl EndpointDiscovery {
    pub fn new() -> Self {
        Self {
            client: Client::new(),
            catalogs: vec![
                "https://www.w3.org/wiki/SparqlEndpoints".to_string(),
                "https://lod-cloud.net/endpoints".to_string(),
            ],
        }
    }

    /// Discover endpoints from known catalogs
    pub async fn discover_from_catalogs(&self) -> FusekiResult<Vec<EndpointInfo>> {
        // Placeholder for actual discovery implementation
        // Would parse VOID descriptions, SPARQL Service Descriptions, etc.
        Ok(vec![])
    }
}

impl Default for QueryPlanner {
    fn default() -> Self {
        Self::new()
    }
}

impl QueryPlanner {
    pub fn new() -> Self {
        Self {
            decomposition_rules: Self::create_decomposition_rules(),
            join_optimizer: Arc::new(JoinOrderOptimizer::new()),
            statistics: Arc::new(RwLock::new(FederationStatistics::new())),
        }
    }

    /// Create standard decomposition rules
    fn create_decomposition_rules() -> Vec<DecompositionRule> {
        vec![
            // Triple pattern decomposition
            DecompositionRule {
                name: "TriplePatternDecomposition".to_string(),
                pattern: "triple_pattern".to_string(),
                applicability_check: Box::new(|query| {
                    query.contains("?s") && query.contains("?p") && query.contains("?o")
                }),
                decompose: Box::new(|_query| {
                    // Decompose triple patterns across endpoints
                    vec![]
                }),
            },
            // UNION decomposition
            DecompositionRule {
                name: "UnionDecomposition".to_string(),
                pattern: "union".to_string(),
                applicability_check: Box::new(|query| query.to_uppercase().contains("UNION")),
                decompose: Box::new(|_query| {
                    // Split UNION branches for parallel execution
                    vec![]
                }),
            },
            // OPTIONAL decomposition
            DecompositionRule {
                name: "OptionalDecomposition".to_string(),
                pattern: "optional".to_string(),
                applicability_check: Box::new(|query| query.to_uppercase().contains("OPTIONAL")),
                decompose: Box::new(|_query| {
                    // Handle OPTIONAL patterns
                    vec![]
                }),
            },
        ]
    }

    /// Create execution plan for federated query
    pub async fn create_execution_plan(
        &self,
        query: &str,
        service_patterns: &[ServicePattern],
    ) -> FusekiResult<ExecutionPlan> {
        // Decompose query into fragments
        let fragments = self.decompose_query(query, service_patterns)?;

        // Optimize join order
        let join_plan = self.join_optimizer.optimize_joins(&fragments).await?;

        // Create execution plan
        let required_endpoints: Vec<String> = service_patterns
            .iter()
            .map(|p| p.service_url.clone())
            .collect();

        let execution_steps: Vec<String> = fragments
            .iter()
            .enumerate()
            .map(|(i, f)| {
                format!(
                    "Execute fragment {} ({}) at {:?}",
                    i, f.sparql, f.target_endpoints
                )
            })
            .collect();

        let estimated_cost = fragments.iter().map(|f| f.estimated_cost).sum::<f64>(); // Sum of fragment costs

        Ok(ExecutionPlan {
            query_id: crate::federated_query_executor::new_query_id(),
            fragments: fragments.clone(),
            join_plan,
            timeout_ms: 30000,
            optimization_hints: HashMap::new(),
            execution_steps,
            estimated_cost,
            resource_requirements: ResourceRequirements {
                required_endpoints,
                estimated_memory_mb: fragments.len() as f64 * 5.0,
                estimated_cpu_cores: (fragments.len() as f64 / 2.0).max(1.0),
            },
        })
    }

    /// Decompose query into executable fragments
    fn decompose_query(
        &self,
        query: &str,
        service_patterns: &[ServicePattern],
    ) -> FusekiResult<Vec<QueryFragment>> {
        let mut fragments = Vec::new();

        // Create fragments for each SERVICE pattern
        for (idx, pattern) in service_patterns.iter().enumerate() {
            fragments.push(QueryFragment {
                fragment_id: format!("service_{idx}"),
                sparql: pattern.pattern.clone(),
                target_endpoints: vec![pattern.service_url.clone()],
                dependencies: vec![],
                estimated_cost: 1.0,
                is_optional: pattern.is_optional,
            });
        }

        // Apply decomposition rules
        for rule in &self.decomposition_rules {
            if (rule.applicability_check)(query) {
                let decomposed = (rule.decompose)(query);
                fragments.extend(decomposed);
            }
        }

        Ok(fragments)
    }
}

impl Default for JoinOrderOptimizer {
    fn default() -> Self {
        Self::new()
    }
}

impl JoinOrderOptimizer {
    pub fn new() -> Self {
        Self {
            cost_model: Arc::new(JoinCostModel::new()),
            dp_cache: DashMap::new(),
        }
    }

    /// Optimize join order for fragments
    pub async fn optimize_joins(&self, fragments: &[QueryFragment]) -> FusekiResult<JoinPlan> {
        // Use dynamic programming to find optimal join order
        let cache_key = self.compute_cache_key(fragments);

        if let Some(cached_plan) = self.dp_cache.get(&cache_key) {
            return Ok(cached_plan.clone());
        }

        // Compute optimal plan
        let plan = self.compute_optimal_plan(fragments).await?;

        // Cache the result
        self.dp_cache.insert(cache_key, plan.clone());

        Ok(plan)
    }

    /// Compute cache key for fragments
    fn compute_cache_key(&self, fragments: &[QueryFragment]) -> String {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        for fragment in fragments {
            std::hash::Hash::hash(&fragment.fragment_id, &mut hasher);
        }
        format!("{:x}", std::hash::Hasher::finish(&hasher))
    }

    /// Compute optimal join plan
    async fn compute_optimal_plan(&self, fragments: &[QueryFragment]) -> FusekiResult<JoinPlan> {
        // Simplified join planning
        let mut steps = Vec::new();

        if fragments.len() > 1 {
            // Create pairwise joins
            for i in 0..fragments.len() - 1 {
                steps.push(JoinStep {
                    operation: JoinOperation::HashJoin,
                    left_source: fragments[i].fragment_id.clone(),
                    right_source: fragments[i + 1].fragment_id.clone(),
                    output_destination: format!("join_{i}"),
                });
            }
        }

        Ok(JoinPlan {
            steps,
            estimated_cost: fragments.len() as f64,
            estimated_time_ms: fragments.len() as u64 * 100,
        })
    }
}

impl Default for JoinCostModel {
    fn default() -> Self {
        Self::new()
    }
}

impl JoinCostModel {
    pub fn new() -> Self {
        Self {
            latency_map: DashMap::new(),
            bandwidth_map: DashMap::new(),
        }
    }
}

impl Default for CostEstimator {
    fn default() -> Self {
        Self::new()
    }
}

impl CostEstimator {
    pub fn new() -> Self {
        Self {
            history: Arc::new(RwLock::new(QueryHistory::new())),
            ml_model: None,
            cardinality: Arc::new(CardinalityEstimator::new()),
        }
    }

    /// Estimate cost of execution plan
    pub async fn estimate_cost(&self, plan: &ExecutionPlan) -> FusekiResult<f64> {
        let mut total_cost = 0.0;

        // Estimate fragment costs
        for fragment in &plan.fragments {
            let fragment_cost = self.estimate_fragment_cost(fragment).await?;
            total_cost += fragment_cost;
        }

        // Add join costs
        for step in &plan.join_plan.steps {
            let join_cost = self.estimate_join_cost(step).await?;
            total_cost += join_cost;
        }

        Ok(total_cost)
    }

    /// Estimate cost of a query fragment
    async fn estimate_fragment_cost(&self, fragment: &QueryFragment) -> FusekiResult<f64> {
        // Use historical data if available
        let history = self.history.read().await;
        if let Some(stats) = history.patterns.get(&fragment.fragment_id) {
            return Ok(stats.avg_execution_time);
        }

        // Otherwise use cardinality estimation
        let cardinality = self
            .cardinality
            .estimate_cardinality(&fragment.sparql)
            .await?;
        Ok(cardinality as f64 * 0.001) // 1ms per 1000 results
    }

    /// Estimate cost of a join operation
    async fn estimate_join_cost(&self, step: &JoinStep) -> FusekiResult<f64> {
        match step.operation {
            JoinOperation::HashJoin => Ok(10.0),
            JoinOperation::SortMergeJoin => Ok(20.0),
            JoinOperation::NestedLoopJoin => Ok(100.0),
            JoinOperation::BroadcastJoin => Ok(5.0),
            JoinOperation::IndexJoin => Ok(2.0),
        }
    }
}

impl Default for QueryHistory {
    fn default() -> Self {
        Self::new()
    }
}

impl QueryHistory {
    pub fn new() -> Self {
        Self {
            executions: Vec::new(),
            patterns: HashMap::new(),
        }
    }
}

impl Default for CardinalityEstimator {
    fn default() -> Self {
        Self::new()
    }
}

impl CardinalityEstimator {
    pub fn new() -> Self {
        Self {
            endpoint_stats: DashMap::new(),
            histograms: DashMap::new(),
        }
    }

    /// Estimate result cardinality
    pub async fn estimate_cardinality(&self, query: &str) -> FusekiResult<u64> {
        // Simplified cardinality estimation
        if query.contains("LIMIT") {
            if let Some(limit_pos) = query.find("LIMIT") {
                let limit_str = &query[limit_pos + 5..].trim();

                // Extract the limit value (handle both cases: with space after or at end of query)
                let limit_val = if let Some(space_pos) = limit_str.find(' ') {
                    &limit_str[..space_pos]
                } else {
                    // No space found, use the entire remaining string
                    limit_str
                };

                if let Ok(limit) = limit_val.parse::<u64>() {
                    return Ok(limit);
                }
            }
        }

        // Default estimate
        Ok(1000)
    }
}

impl Default for FederationStatistics {
    fn default() -> Self {
        Self::new()
    }
}

impl FederationStatistics {
    pub fn new() -> Self {
        Self {
            query_stats: HashMap::new(),
            endpoint_stats: HashMap::new(),
        }
    }
}