|
| 1 | +pub mod predicate; |
| 2 | +pub mod slice; |
| 3 | + |
| 4 | +use std::sync::{Arc, Mutex}; |
| 5 | + |
| 6 | +use polars_error::PolarsResult; |
| 7 | + |
| 8 | +use super::MultiFileReaderConfig; |
| 9 | +use super::bridge::BridgeState; |
| 10 | +use crate::async_executor::{self, AbortOnDropHandle, TaskPriority}; |
| 11 | +use crate::async_primitives::connector::{self}; |
| 12 | +use crate::async_primitives::wait_group::WaitToken; |
| 13 | +use crate::morsel::Morsel; |
| 14 | +use crate::nodes::io_sources::multi_file_reader::bridge::spawn_bridge; |
| 15 | + |
| 16 | +pub struct MultiScanTaskInitializer { |
| 17 | + pub(super) config: Arc<MultiFileReaderConfig>, |
| 18 | +} |
| 19 | + |
| 20 | +impl MultiScanTaskInitializer { |
| 21 | + pub fn new(config: Arc<MultiFileReaderConfig>) -> Self { |
| 22 | + Self { config } |
| 23 | + } |
| 24 | + |
| 25 | + #[expect(clippy::type_complexity)] |
| 26 | + pub fn spawn_background_tasks( |
| 27 | + self, |
| 28 | + ) -> ( |
| 29 | + AbortOnDropHandle<PolarsResult<()>>, |
| 30 | + connector::Sender<(connector::Sender<Morsel>, WaitToken)>, |
| 31 | + Arc<Mutex<BridgeState>>, |
| 32 | + ) { |
| 33 | + assert!(self.config.num_pipelines() > 0); |
| 34 | + let verbose = self.config.verbose(); |
| 35 | + |
| 36 | + if verbose { |
| 37 | + eprintln!( |
| 38 | + "[MultiScanTaskInitializer]: spawn_background_tasks(), {} sources, reader name: {}, {:?}", |
| 39 | + self.config.sources.len(), |
| 40 | + self.config.file_reader_builder.reader_name(), |
| 41 | + self.config.file_reader_builder.reader_capabilities(), |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + let bridge_state = Arc::new(Mutex::new(BridgeState::NotYetStarted)); |
| 46 | + |
| 47 | + let (bridge_handle, bridge_recv_port_tx, send_phase_chan_to_bridge) = |
| 48 | + spawn_bridge(bridge_state.clone()); |
| 49 | + |
| 50 | + let verbose = self.config.verbose(); |
| 51 | + |
| 52 | + let background_tasks_handle = AbortOnDropHandle::new(async_executor::spawn( |
| 53 | + TaskPriority::Low, |
| 54 | + async move { |
| 55 | + let (skip_files_mask, predicate) = self.initialize_predicate()?; |
| 56 | + |
| 57 | + if verbose { |
| 58 | + eprintln!( |
| 59 | + "[MultiScanTaskInitializer]: \ |
| 60 | + predicate: {:?}, \ |
| 61 | + skip files mask: {:?}, \ |
| 62 | + predicate to reader: {:?}", |
| 63 | + self.config.predicate.is_some().then_some("<predicate>"), |
| 64 | + skip_files_mask.is_some().then_some("<skip_files>"), |
| 65 | + predicate.is_some().then_some("<predicate>"), |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + #[expect(clippy::never_loop)] |
| 70 | + loop { |
| 71 | + if skip_files_mask |
| 72 | + .as_ref() |
| 73 | + .is_some_and(|x| x.unset_bits() == 0) |
| 74 | + { |
| 75 | + if verbose { |
| 76 | + eprintln!( |
| 77 | + "[MultiScanTaskInitializer]: early return (skip_files_mask / predicate)" |
| 78 | + ) |
| 79 | + } |
| 80 | + } else if self.config.pre_slice.as_ref().is_some_and(|x| x.len() == 0) { |
| 81 | + if cfg!(debug_assertions) { |
| 82 | + panic!("should quit earlier"); |
| 83 | + } |
| 84 | + |
| 85 | + if verbose { |
| 86 | + eprintln!( |
| 87 | + "[MultiScanTaskInitializer]: early return (pre_slice.len == 0)" |
| 88 | + ) |
| 89 | + } |
| 90 | + } else { |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + return Ok(()); |
| 95 | + } |
| 96 | + |
| 97 | + let predicate = predicate.cloned(); |
| 98 | + |
| 99 | + self.init_and_run(bridge_recv_port_tx, skip_files_mask, predicate) |
| 100 | + .await? |
| 101 | + .await?; |
| 102 | + |
| 103 | + bridge_handle.await; |
| 104 | + |
| 105 | + Ok(()) |
| 106 | + }, |
| 107 | + )); |
| 108 | + |
| 109 | + ( |
| 110 | + background_tasks_handle, |
| 111 | + send_phase_chan_to_bridge, |
| 112 | + bridge_state, |
| 113 | + ) |
| 114 | + } |
| 115 | +} |
0 commit comments