|
| 1 | +use std::io::Read; |
| 2 | + |
| 3 | +use futures_util::stream; |
| 4 | +use vortex_array::stream::{ArrayStream, ArrayStreamAdapter}; |
| 5 | +use vortex_array::ContextRef; |
| 6 | +use vortex_dtype::DType; |
| 7 | +use vortex_error::VortexResult; |
| 8 | +use vortex_io::VortexReadAt; |
| 9 | +use vortex_layout::scanner::{Poll, Scan}; |
| 10 | +use vortex_layout::{LayoutData, RowMask}; |
| 11 | + |
| 12 | +use crate::v2::footer::Segment; |
| 13 | +use crate::v2::segments::SegmentCache; |
| 14 | + |
| 15 | +pub struct VortexFile<R> { |
| 16 | + pub(crate) read: R, |
| 17 | + pub(crate) ctx: ContextRef, |
| 18 | + pub(crate) layout: LayoutData, |
| 19 | + pub(crate) segments: Vec<Segment>, |
| 20 | + pub(crate) segment_cache: SegmentCache, |
| 21 | +} |
| 22 | + |
| 23 | +/// Async implementation of Vortex File. |
| 24 | +impl<R: VortexReadAt> VortexFile<R> { |
| 25 | + /// Returns the number of rows in the file. |
| 26 | + pub fn row_count(&self) -> u64 { |
| 27 | + self.layout.row_count() |
| 28 | + } |
| 29 | + |
| 30 | + /// Returns the DType of the file. |
| 31 | + pub fn dtype(&self) -> &DType { |
| 32 | + self.layout.dtype() |
| 33 | + } |
| 34 | + |
| 35 | + /// Performs a scan operation over the file. |
| 36 | + pub fn scan(&self, scan: Scan) -> VortexResult<impl ArrayStream + '_> { |
| 37 | + let layout_scan = self.layout.new_scan(scan, self.ctx.clone())?; |
| 38 | + let scan_dtype = layout_scan.dtype().clone(); |
| 39 | + |
| 40 | + // TODO(ngates): we could query the layout for splits and then process them in parallel. |
| 41 | + // For now, we just scan the entire layout with one mask. |
| 42 | + // Note that to implement this we would use stream::try_unfold |
| 43 | + let row_mask = RowMask::new_valid_between(0, layout_scan.layout().row_count()); |
| 44 | + let mut scanner = layout_scan.create_scanner(row_mask)?; |
| 45 | + |
| 46 | + let stream = stream::once(async move { |
| 47 | + loop { |
| 48 | + match scanner.poll(&self.segment_cache)? { |
| 49 | + Poll::Some(array) => return Ok(array), |
| 50 | + Poll::NeedMore(segment_ids) => { |
| 51 | + for segment_id in segment_ids { |
| 52 | + let segment = &self.segments[*segment_id as usize]; |
| 53 | + let bytes = self |
| 54 | + .read |
| 55 | + .read_byte_range(segment.offset, segment.length as u64) |
| 56 | + .await?; |
| 57 | + self.segment_cache.set(segment_id, bytes); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + Ok(ArrayStreamAdapter::new(scan_dtype, stream)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Sync implementation of Vortex File. |
| 69 | +impl<R: Read> VortexFile<R> {} |
0 commit comments