Skip to main content

oxirs_arq/
bgp_optimizer_types.rs

1//! Type definitions for BGP optimization
2//!
3//! This module contains all the data structures used in BGP optimization,
4//! including selectivity information, index planning, and optimization results.
5
6use crate::algebra::TriplePattern;
7use crate::optimizer::index_types::IndexType;
8use std::collections::HashMap;
9
10/// BGP optimization result
11#[derive(Debug, Clone)]
12pub struct OptimizedBGP {
13    /// Reordered triple patterns
14    pub patterns: Vec<TriplePattern>,
15    /// Estimated cost
16    pub estimated_cost: f64,
17    /// Selectivity information
18    pub selectivity_info: SelectivityInfo,
19    /// Recommended index usage
20    pub index_plan: IndexUsagePlan,
21}
22
23/// Selectivity information for BGP
24#[derive(Debug, Clone)]
25pub struct SelectivityInfo {
26    /// Pattern selectivities
27    pub pattern_selectivity: Vec<PatternSelectivity>,
28    /// Join selectivities
29    pub join_selectivity: HashMap<(usize, usize), f64>,
30    /// Overall BGP selectivity
31    pub overall_selectivity: f64,
32}
33
34/// Selectivity information for a single pattern
35#[derive(Debug, Clone)]
36pub struct PatternSelectivity {
37    /// Triple pattern
38    pub pattern: TriplePattern,
39    /// Estimated selectivity (0.0 to 1.0)
40    pub selectivity: f64,
41    /// Estimated cardinality
42    pub cardinality: usize,
43    /// Contributing factors
44    pub factors: SelectivityFactors,
45}
46
47/// Factors contributing to selectivity
48#[derive(Debug, Clone)]
49pub struct SelectivityFactors {
50    /// Subject selectivity
51    pub subject_selectivity: f64,
52    /// Predicate selectivity
53    pub predicate_selectivity: f64,
54    /// Object selectivity
55    pub object_selectivity: f64,
56    /// Type selectivity
57    pub type_selectivity: f64,
58    /// Literal selectivity
59    pub literal_selectivity: f64,
60    /// Index availability factor
61    pub index_factor: f64,
62    /// Data distribution factor
63    pub distribution_factor: f64,
64}
65
66/// Index usage plan for BGP execution
67#[derive(Debug, Clone)]
68pub struct IndexUsagePlan {
69    /// Index assignments per pattern
70    pub pattern_indexes: Vec<IndexAssignment>,
71    /// Join index opportunities
72    pub join_indexes: Vec<JoinIndexOpportunity>,
73    /// Multi-index intersection opportunities
74    pub index_intersections: Vec<IndexIntersection>,
75    /// Bloom filter recommendations
76    pub bloom_filter_candidates: Vec<BloomFilterCandidate>,
77    /// Recommended indices
78    pub recommended_indices: Vec<IndexType>,
79    /// Access patterns
80    pub access_patterns: Vec<String>,
81    /// Estimated cost reduction
82    pub estimated_cost_reduction: f64,
83}
84
85/// Index assignment for a pattern
86#[derive(Debug, Clone)]
87pub struct IndexAssignment {
88    /// Pattern index
89    pub pattern_idx: usize,
90    /// Recommended index type
91    pub index_type: IndexType,
92    /// Expected scan cost
93    pub scan_cost: f64,
94}
95
96/// Join index opportunity
97#[derive(Debug, Clone)]
98pub struct JoinIndexOpportunity {
99    /// Left pattern index
100    pub left_pattern_idx: usize,
101    /// Right pattern index
102    pub right_pattern_idx: usize,
103    /// Join variable
104    pub join_var: String,
105    /// Potential speedup factor
106    pub speedup_factor: f64,
107}
108
109/// Multi-index intersection for complex queries
110#[derive(Debug, Clone)]
111pub struct IndexIntersection {
112    /// Pattern index for intersection
113    pub pattern_idx: usize,
114    /// Primary index type
115    pub primary_index: IndexType,
116    /// Secondary indexes for intersection
117    pub secondary_indexes: Vec<IndexType>,
118    /// Expected benefit from intersection
119    pub selectivity_improvement: f64,
120    /// Intersection algorithm to use
121    pub intersection_algorithm: IntersectionAlgorithm,
122}
123
124/// Types of index intersections
125#[derive(Debug, Clone)]
126pub enum IntersectionType {
127    /// Variable-based join intersection
128    VariableJoin,
129    /// Value-based intersection
130    ValueIntersection,
131    /// Spatial intersection
132    SpatialIntersection,
133    /// Temporal intersection
134    TemporalIntersection,
135}
136
137/// Intersection algorithm types
138#[derive(Debug, Clone)]
139pub enum IntersectionAlgorithm {
140    /// Bitmap intersection for dense results
141    Bitmap,
142    /// Hash-based intersection for sparse results
143    Hash,
144    /// Skip-list intersection for ordered indexes
145    SkipList,
146}
147
148/// Bloom filter candidate for negative lookups
149#[derive(Debug, Clone)]
150pub struct BloomFilterCandidate {
151    /// Pattern index
152    pub pattern_idx: usize,
153    /// Filter position (Subject, Predicate, Object)
154    pub filter_position: TermPosition,
155    /// Expected false positive rate
156    pub false_positive_rate: f64,
157    /// Memory footprint estimate (bytes)
158    pub memory_footprint: usize,
159    /// Expected performance gain
160    pub performance_gain: f64,
161}
162
163/// Adaptive index selector for dynamic optimization
164#[derive(Debug, Clone)]
165pub struct AdaptiveIndexSelector {
166    /// Query pattern frequency
167    #[allow(dead_code)]
168    pub(crate) pattern_frequency: HashMap<String, usize>,
169    /// Index effectiveness history
170    #[allow(dead_code)]
171    pub(crate) index_effectiveness: HashMap<IndexType, f64>,
172    /// Workload characteristics
173    #[allow(dead_code)]
174    pub(crate) workload_characteristics: WorkloadCharacteristics,
175}
176
177/// Workload characteristics for adaptive optimization
178#[derive(Debug, Clone, Default)]
179pub struct WorkloadCharacteristics {
180    /// Average query complexity (number of patterns)
181    pub avg_query_complexity: f64,
182    /// Predicate diversity
183    pub predicate_diversity: f64,
184    /// Join frequency patterns
185    pub join_frequency: HashMap<String, usize>,
186    /// Temporal query patterns
187    pub temporal_access_patterns: HashMap<String, Vec<std::time::Instant>>,
188}
189
190/// Term position enumeration
191#[derive(Debug, Clone, Copy)]
192pub enum TermPosition {
193    Subject,
194    Predicate,
195    Object,
196}
197
198impl TermPosition {
199    pub fn to_string(&self) -> &'static str {
200        match self {
201            TermPosition::Subject => "subject",
202            TermPosition::Predicate => "predicate",
203            TermPosition::Object => "object",
204        }
205    }
206}
207
208/// Operations that can be spilled to disk
209#[derive(Debug, Clone)]
210pub enum SpillOperation {
211    /// Intermediate result sets
212    IntermediateResults,
213    /// Hash table for joins
214    HashTable,
215}
216
217impl Default for AdaptiveIndexSelector {
218    fn default() -> Self {
219        Self::new()
220    }
221}
222
223impl AdaptiveIndexSelector {
224    pub fn new() -> Self {
225        Self {
226            pattern_frequency: HashMap::new(),
227            index_effectiveness: HashMap::new(),
228            workload_characteristics: WorkloadCharacteristics::default(),
229        }
230    }
231}