Skip to main content

vortex_datafusion/persistent/
reader.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Factory for creating [`VortexReadAt`][vortex::io::VortexReadAt] instances
5//! from [`PartitionedFile`]s.
6
7use std::fmt::Debug;
8use std::sync::Arc;
9
10use datafusion_common::Result as DFResult;
11use datafusion_datasource::PartitionedFile;
12use object_store::ObjectStore;
13use vortex::io::VortexReadAt;
14use vortex::io::object_store::ObjectStoreReadAt;
15use vortex::io::session::RuntimeSessionExt;
16use vortex::session::VortexSession;
17
18/// Factory to create [`VortexReadAt`] instances to read the target file.
19pub trait VortexReaderFactory: Debug + Send + Sync + 'static {
20    /// Create a reader for a target object.
21    fn create_reader(
22        &self,
23        file: &PartitionedFile,
24        session: &VortexSession,
25    ) -> DFResult<Arc<dyn VortexReadAt>>;
26}
27
28/// Default factory, creates [`ObjectStore`] backed readers for files,
29/// works with multiple cloud providers.
30#[derive(Debug)]
31pub struct DefaultVortexReaderFactory {
32    object_store: Arc<dyn ObjectStore>,
33}
34
35impl DefaultVortexReaderFactory {
36    /// Creates new instance
37    pub fn new(object_store: Arc<dyn ObjectStore>) -> Self {
38        Self { object_store }
39    }
40}
41
42impl VortexReaderFactory for DefaultVortexReaderFactory {
43    fn create_reader(
44        &self,
45        file: &PartitionedFile,
46        session: &VortexSession,
47    ) -> DFResult<Arc<dyn VortexReadAt>> {
48        Ok(Arc::new(ObjectStoreReadAt::new(
49            self.object_store.clone(),
50            file.path().as_ref().into(),
51            session.handle(),
52        )) as _)
53    }
54}