vortex_layout/layouts/flat/
reader.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::sync::Arc;

use vortex_array::ContextRef;
use vortex_error::{vortex_err, vortex_panic, VortexResult};
use vortex_scan::AsyncEvaluator;

use crate::layouts::flat::FlatLayout;
use crate::reader::LayoutReader;
use crate::segments::{AsyncSegmentReader, SegmentId};
use crate::{LayoutData, LayoutEncoding};

pub struct FlatReader {
    layout: LayoutData,
    ctx: ContextRef,
    segments: Arc<dyn AsyncSegmentReader>,
    // The segment ID of the array in this FlatLayout.
    // NOTE(ngates): we don't cache the ArrayData here since the cache lives for as long as the
    //  reader does, which means likely holding a strong reference to the array for much longer
    //  than necessary, and potentially causing a memory leak.
    segment_id: SegmentId,
}

impl FlatReader {
    pub(crate) fn try_new(
        layout: LayoutData,
        ctx: ContextRef,
        segments: Arc<dyn AsyncSegmentReader>,
    ) -> VortexResult<Self> {
        if layout.encoding().id() != FlatLayout.id() {
            vortex_panic!("Mismatched layout ID")
        }

        let segment_id = layout
            .segment_id(0)
            .ok_or_else(|| vortex_err!("FlatLayout missing SegmentID"))?;

        Ok(Self {
            layout,
            ctx,
            segments,
            segment_id,
        })
    }

    pub(crate) fn ctx(&self) -> ContextRef {
        self.ctx.clone()
    }

    pub(crate) fn segments(&self) -> &dyn AsyncSegmentReader {
        self.segments.as_ref()
    }

    pub(crate) fn segment_id(&self) -> SegmentId {
        self.segment_id
    }
}

impl LayoutReader for FlatReader {
    fn layout(&self) -> &LayoutData {
        &self.layout
    }

    fn evaluator(&self) -> &dyn AsyncEvaluator {
        self
    }
}