1use crate::algebra::TriplePattern;
7use crate::optimizer::index_types::IndexType;
8use std::collections::HashMap;
9
10#[derive(Debug, Clone)]
12pub struct OptimizedBGP {
13 pub patterns: Vec<TriplePattern>,
15 pub estimated_cost: f64,
17 pub selectivity_info: SelectivityInfo,
19 pub index_plan: IndexUsagePlan,
21}
22
23#[derive(Debug, Clone)]
25pub struct SelectivityInfo {
26 pub pattern_selectivity: Vec<PatternSelectivity>,
28 pub join_selectivity: HashMap<(usize, usize), f64>,
30 pub overall_selectivity: f64,
32}
33
34#[derive(Debug, Clone)]
36pub struct PatternSelectivity {
37 pub pattern: TriplePattern,
39 pub selectivity: f64,
41 pub cardinality: usize,
43 pub factors: SelectivityFactors,
45}
46
47#[derive(Debug, Clone)]
49pub struct SelectivityFactors {
50 pub subject_selectivity: f64,
52 pub predicate_selectivity: f64,
54 pub object_selectivity: f64,
56 pub type_selectivity: f64,
58 pub literal_selectivity: f64,
60 pub index_factor: f64,
62 pub distribution_factor: f64,
64}
65
66#[derive(Debug, Clone)]
68pub struct IndexUsagePlan {
69 pub pattern_indexes: Vec<IndexAssignment>,
71 pub join_indexes: Vec<JoinIndexOpportunity>,
73 pub index_intersections: Vec<IndexIntersection>,
75 pub bloom_filter_candidates: Vec<BloomFilterCandidate>,
77 pub recommended_indices: Vec<IndexType>,
79 pub access_patterns: Vec<String>,
81 pub estimated_cost_reduction: f64,
83}
84
85#[derive(Debug, Clone)]
87pub struct IndexAssignment {
88 pub pattern_idx: usize,
90 pub index_type: IndexType,
92 pub scan_cost: f64,
94}
95
96#[derive(Debug, Clone)]
98pub struct JoinIndexOpportunity {
99 pub left_pattern_idx: usize,
101 pub right_pattern_idx: usize,
103 pub join_var: String,
105 pub speedup_factor: f64,
107}
108
109#[derive(Debug, Clone)]
111pub struct IndexIntersection {
112 pub pattern_idx: usize,
114 pub primary_index: IndexType,
116 pub secondary_indexes: Vec<IndexType>,
118 pub selectivity_improvement: f64,
120 pub intersection_algorithm: IntersectionAlgorithm,
122}
123
124#[derive(Debug, Clone)]
126pub enum IntersectionType {
127 VariableJoin,
129 ValueIntersection,
131 SpatialIntersection,
133 TemporalIntersection,
135}
136
137#[derive(Debug, Clone)]
139pub enum IntersectionAlgorithm {
140 Bitmap,
142 Hash,
144 SkipList,
146}
147
148#[derive(Debug, Clone)]
150pub struct BloomFilterCandidate {
151 pub pattern_idx: usize,
153 pub filter_position: TermPosition,
155 pub false_positive_rate: f64,
157 pub memory_footprint: usize,
159 pub performance_gain: f64,
161}
162
163#[derive(Debug, Clone)]
165pub struct AdaptiveIndexSelector {
166 #[allow(dead_code)]
168 pub(crate) pattern_frequency: HashMap<String, usize>,
169 #[allow(dead_code)]
171 pub(crate) index_effectiveness: HashMap<IndexType, f64>,
172 #[allow(dead_code)]
174 pub(crate) workload_characteristics: WorkloadCharacteristics,
175}
176
177#[derive(Debug, Clone, Default)]
179pub struct WorkloadCharacteristics {
180 pub avg_query_complexity: f64,
182 pub predicate_diversity: f64,
184 pub join_frequency: HashMap<String, usize>,
186 pub temporal_access_patterns: HashMap<String, Vec<std::time::Instant>>,
188}
189
190#[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#[derive(Debug, Clone)]
210pub enum SpillOperation {
211 IntermediateResults,
213 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}