|
| 1 | +use crate::core::Event; |
| 2 | +use crate::parsing::janusql_parser::WindowDefinition; |
| 3 | +use crate::storage::segmented_storage::StreamingSegmentedStorage; |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +/// Operator for processing historical data with a sliding window. |
| 7 | +/// It iterates over the storage and yields events for each window. |
| 8 | +pub struct HistoricalSlidingWindowOperator { |
| 9 | + storage: Arc<StreamingSegmentedStorage>, |
| 10 | + window_def: WindowDefinition, |
| 11 | + current_start: u64, |
| 12 | + end_bound: u64, |
| 13 | +} |
| 14 | + |
| 15 | +impl HistoricalSlidingWindowOperator { |
| 16 | + /// Creates a new HistoricalSlidingWindowOperator. |
| 17 | + /// |
| 18 | + /// # Arguments |
| 19 | + /// |
| 20 | + /// * `storage` - The storage backend to query. |
| 21 | + /// * `window_def` - The window definition (width, slide, offset, etc.). |
| 22 | + pub fn new(storage: Arc<StreamingSegmentedStorage>, window_def: WindowDefinition) -> Self { |
| 23 | + let now = std::time::SystemTime::now() |
| 24 | + .duration_since(std::time::UNIX_EPOCH) |
| 25 | + .unwrap() |
| 26 | + .as_millis() as u64; |
| 27 | + |
| 28 | + // Offset is mandatory for HistoricalSliding windows as per the parser and requirements. |
| 29 | + // We subtract it from the query_start to "go back" in time. |
| 30 | + let offset = window_def.offset.expect("Offset must be defined for HistoricalSlidingWindow"); |
| 31 | + let start_time = now.saturating_sub(offset); |
| 32 | + |
| 33 | + HistoricalSlidingWindowOperator { |
| 34 | + storage, |
| 35 | + window_def, |
| 36 | + current_start: start_time, |
| 37 | + end_bound: now, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl Iterator for HistoricalSlidingWindowOperator { |
| 43 | + type Item = Vec<Event>; |
| 44 | + |
| 45 | + fn next(&mut self) -> Option<Self::Item> { |
| 46 | + // Calculate the window bounds |
| 47 | + let window_start = self.current_start; |
| 48 | + let window_end = (window_start + self.window_def.width).min(self.end_bound); |
| 49 | + |
| 50 | + // Check if we have exceeded the query range |
| 51 | + // We stop if the window start goes beyond the end bound. |
| 52 | + // (Alternative: stop if window_end > end_bound, depending on strict containment requirements) |
| 53 | + if window_start > self.end_bound { |
| 54 | + return None; |
| 55 | + } |
| 56 | + |
| 57 | + // Query the storage for events in this window |
| 58 | + // Note: query() is inclusive, so we might need to adjust if we want [start, end) |
| 59 | + // For now, we assume the storage query semantics match what we want or we accept inclusive. |
| 60 | + // Usually windows are [start, end). |
| 61 | + let events_result = self.storage.query(window_start, window_end); |
| 62 | + |
| 63 | + match events_result { |
| 64 | + Ok(events) => { |
| 65 | + // Advance the window |
| 66 | + self.current_start += self.window_def.slide; |
| 67 | + Some(events) |
| 68 | + } |
| 69 | + Err(e) => { |
| 70 | + eprintln!("Error querying storage for window: {}", e); |
| 71 | + None |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use super::*; |
| 80 | + use crate::parsing::janusql_parser::WindowType; |
| 81 | + use crate::storage::util::StreamingConfig; |
| 82 | + use std::fs; |
| 83 | + |
| 84 | + fn create_test_config(path: &str) -> StreamingConfig { |
| 85 | + StreamingConfig { |
| 86 | + segment_base_path: path.to_string(), |
| 87 | + max_batch_events: 10, |
| 88 | + max_batch_bytes: 1024, |
| 89 | + max_batch_age_seconds: 1, |
| 90 | + sparse_interval: 2, |
| 91 | + entries_per_index_block: 2, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_historical_sliding_window() { |
| 97 | + let test_dir = "/tmp/janus_test_sliding_window"; |
| 98 | + let _ = fs::remove_dir_all(test_dir); // Clean up before test |
| 99 | + |
| 100 | + let config = create_test_config(test_dir); |
| 101 | + let storage = Arc::new(StreamingSegmentedStorage::new(config).unwrap()); |
| 102 | + |
| 103 | + let now = std::time::SystemTime::now() |
| 104 | + .duration_since(std::time::UNIX_EPOCH) |
| 105 | + .unwrap() |
| 106 | + .as_millis() as u64; |
| 107 | + |
| 108 | + // Write events in the past: now-500, now-400, now-300, now-200, now-100, now |
| 109 | + for i in 0..6 { |
| 110 | + let ts = now - (500 - (i * 100)); |
| 111 | + storage.write_rdf(ts, "s", "p", "o", "g").unwrap(); |
| 112 | + } |
| 113 | + |
| 114 | + // Define Window: Width 200, Slide 100, Offset 500 (Start at now - 500) |
| 115 | + let window_def = WindowDefinition { |
| 116 | + window_name: "w1".to_string(), |
| 117 | + stream_name: "s1".to_string(), |
| 118 | + width: 200, |
| 119 | + slide: 100, |
| 120 | + offset: Some(500), |
| 121 | + start: None, |
| 122 | + end: None, |
| 123 | + window_type: WindowType::HistoricalSliding, |
| 124 | + }; |
| 125 | + |
| 126 | + let mut operator = HistoricalSlidingWindowOperator::new(storage.clone(), window_def); |
| 127 | + |
| 128 | + // Window 1: [now-500, now-300] -> Events at now-500, now-400, now-300 |
| 129 | + // Note: query is inclusive. |
| 130 | + let w1 = operator.next().unwrap(); |
| 131 | + assert_eq!(w1.len(), 3); |
| 132 | + assert_eq!(w1[0].timestamp, now - 500); |
| 133 | + assert_eq!(w1[2].timestamp, now - 300); |
| 134 | + |
| 135 | + // Window 2: [now-400, now-200] -> Events at now-400, now-300, now-200 |
| 136 | + let w2 = operator.next().unwrap(); |
| 137 | + assert_eq!(w2.len(), 3); |
| 138 | + assert_eq!(w2[0].timestamp, now - 400); |
| 139 | + assert_eq!(w2[2].timestamp, now - 200); |
| 140 | + |
| 141 | + // Window 3: [now-300, now-100] -> Events at now-300, now-200, now-100 |
| 142 | + let w3 = operator.next().unwrap(); |
| 143 | + assert_eq!(w3.len(), 3); |
| 144 | + assert_eq!(w3[0].timestamp, now - 300); |
| 145 | + assert_eq!(w3[2].timestamp, now - 100); |
| 146 | + |
| 147 | + let _ = fs::remove_dir_all(test_dir); |
| 148 | + } |
| 149 | +} |
0 commit comments