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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use std::fmt::Debug;
use std::sync::{Arc, RwLock};

use flatbuffers::root_unchecked;
use once_cell::sync::OnceCell;
use vortex_array::aliases::hash_map::HashMap;
use vortex_buffer::Buffer;
use vortex_dtype::field::Field;
use vortex_dtype::flatbuffers::{extract_field, project_and_deserialize, resolve_field};
use vortex_dtype::{DType, FieldNames};
use vortex_error::{vortex_bail, vortex_err, vortex_panic, VortexResult};
use vortex_flatbuffers::dtype as fbd;

use crate::read::projection::Projection;
use crate::read::{LayoutPartId, MessageId};

#[derive(Default, Debug)]
pub struct LayoutMessageCache {
    cache: HashMap<MessageId, Buffer>,
}

impl LayoutMessageCache {
    pub fn new() -> Self {
        Self {
            cache: HashMap::new(),
        }
    }

    pub fn get(&self, path: &[LayoutPartId]) -> Option<Buffer> {
        self.cache.get(path).cloned()
    }

    pub fn remove(&mut self, path: &[LayoutPartId]) -> Option<Buffer> {
        self.cache.remove(path)
    }

    pub fn set(&mut self, path: MessageId, value: Buffer) {
        self.cache.insert(path, value);
    }
}

#[derive(Debug)]
enum SerializedDTypeField {
    Projection(Projection),
    Field(Field),
}

impl SerializedDTypeField {
    pub fn project(&self, fields: &[Field]) -> VortexResult<Self> {
        match self {
            SerializedDTypeField::Projection(p) => {
                Ok(SerializedDTypeField::Projection(p.project(fields)?))
            }
            SerializedDTypeField::Field(f) => {
                if fields.len() > 1 && &fields[0] != f {
                    vortex_bail!("Can't project field {f} into {fields:?}")
                }
                Ok(SerializedDTypeField::Field(f.clone()))
            }
        }
    }

    pub fn field(&self, field: &Field) -> VortexResult<Self> {
        match self {
            SerializedDTypeField::Projection(p) => {
                match p {
                    Projection::All => {}
                    Projection::Flat(fields) => {
                        if !fields.iter().any(|pf| pf == field) {
                            vortex_bail!("Can't project {fields:?} into {field}")
                        }
                    }
                }
                Ok(SerializedDTypeField::Field(field.clone()))
            }
            SerializedDTypeField::Field(f) => {
                if f != field {
                    vortex_bail!("Can't extract field from field")
                }
                Ok(SerializedDTypeField::Field(field.clone()))
            }
        }
    }
}

#[derive(Debug)]
enum LazyDTypeState {
    DType(DType),
    Serialized(Buffer, OnceCell<DType>, SerializedDTypeField),
    Unknown,
}

#[derive(Debug)]
pub struct LazyDType {
    inner: LazyDTypeState,
}

impl LazyDType {
    /// Create a LazilyDeserializedDType from a flatbuffer schema bytes
    /// i.e., these bytes need to be deserializable as message::Schema
    ///
    /// # Safety
    /// This function is unsafe because it trusts the caller to pass in a valid flatbuffer
    /// representing a message::Schema.
    pub unsafe fn from_schema_bytes(dtype_bytes: Buffer) -> Self {
        Self {
            inner: LazyDTypeState::Serialized(
                dtype_bytes,
                OnceCell::new(),
                SerializedDTypeField::Projection(Projection::All),
            ),
        }
    }

    pub fn from_dtype(dtype: DType) -> Self {
        Self {
            inner: LazyDTypeState::DType(dtype),
        }
    }

    pub fn unknown() -> Self {
        Self {
            inner: LazyDTypeState::Unknown,
        }
    }

    /// Restrict the underlying dtype to selected fields
    pub fn project(&self, fields: &[Field]) -> VortexResult<Arc<Self>> {
        match &self.inner {
            LazyDTypeState::DType(dtype) => {
                let DType::Struct(sdt, n) = dtype else {
                    vortex_bail!("Not a struct dtype")
                };
                Ok(Arc::new(LazyDType::from_dtype(DType::Struct(
                    sdt.project(fields)?,
                    *n,
                ))))
            }
            LazyDTypeState::Serialized(b, _, current_projection) => Ok(Arc::new(Self {
                inner: LazyDTypeState::Serialized(
                    b.clone(),
                    OnceCell::new(),
                    current_projection.project(fields)?,
                ),
            })),
            LazyDTypeState::Unknown => vortex_bail!("Unknown dtype"),
        }
    }

    /// Extract single field out of this dtype
    pub fn field(&self, field: &Field) -> VortexResult<Arc<Self>> {
        match &self.inner {
            LazyDTypeState::DType(dtype) => {
                let DType::Struct(sdt, _) = dtype else {
                    vortex_bail!("Not a struct dtype")
                };
                Ok(Arc::new(LazyDType::from_dtype(
                    sdt.field_info(field)?.dtype.clone(),
                )))
            }
            LazyDTypeState::Serialized(b, _, current_projection) => Ok(Arc::new(Self {
                inner: LazyDTypeState::Serialized(
                    b.clone(),
                    OnceCell::new(),
                    current_projection.field(field)?,
                ),
            })),
            LazyDTypeState::Unknown => vortex_bail!("Unknown dtype"),
        }
    }

    /// Extract field names from the underlying dtype if there are any
    pub fn names(&self) -> VortexResult<FieldNames> {
        match &self.inner {
            LazyDTypeState::DType(dtype) => {
                let DType::Struct(sdt, _) = dtype else {
                    vortex_bail!("Not a struct dtype")
                };
                Ok(sdt.names().clone())
            }
            LazyDTypeState::Serialized(b, _, proj) => field_names(b, proj),
            LazyDTypeState::Unknown => vortex_bail!("Unknown dtype"),
        }
    }

    /// Get vortex dtype out of serialized bytes
    pub fn value(&self) -> VortexResult<&DType> {
        match &self.inner {
            LazyDTypeState::DType(dtype) => Ok(dtype),
            LazyDTypeState::Serialized(bytes, cache, proj) => {
                cache.get_or_try_init(|| project_dtype_bytes(bytes, proj))
            }
            LazyDTypeState::Unknown => vortex_bail!("Unknown dtype"),
        }
    }

    /// Convert all name based references to index based to create globally addressable filter
    pub(crate) fn resolve_field(&self, field: &Field) -> VortexResult<usize> {
        match &self.inner {
            LazyDTypeState::DType(dtype) => {
                let DType::Struct(sdt, _) = dtype else {
                    vortex_bail!("Trying to resolve fields in non struct dtype")
                };
                match field {
                    Field::Name(n) => sdt
                        .names()
                        .iter()
                        .position(|name| name == n)
                        .ok_or_else(|| vortex_err!("Can't find {n} in the type")),
                    Field::Index(i) => Ok(*i),
                }
            }
            LazyDTypeState::Serialized(b, ..) => resolve_field(fb_struct(b.as_ref())?, field),
            LazyDTypeState::Unknown => vortex_bail!("Unknown dtype"),
        }
    }
}

fn field_names(bytes: &[u8], dtype_field: &SerializedDTypeField) -> VortexResult<FieldNames> {
    let struct_field = fb_struct(bytes)?;
    let names = struct_field
        .names()
        .ok_or_else(|| vortex_err!("Not a struct dtype"))?;
    match dtype_field {
        SerializedDTypeField::Projection(projection) => match projection {
            Projection::All => Ok(names.iter().map(Arc::from).collect()),
            Projection::Flat(fields) => fields
                .iter()
                .map(|f| resolve_field(struct_field, f))
                .map(|idx| idx.map(|i| Arc::from(names.get(i))))
                .collect(),
        },
        SerializedDTypeField::Field(f) => Ok(Arc::new([Arc::from(
            names.get(resolve_field(struct_field, f)?),
        )])),
    }
}

fn project_dtype_bytes(bytes: &[u8], dtype_field: &SerializedDTypeField) -> VortexResult<DType> {
    let fb_dtype = fb_dtype(bytes);
    match dtype_field {
        SerializedDTypeField::Projection(projection) => match projection {
            Projection::All => DType::try_from(fb_dtype),
            Projection::Flat(p) => project_and_deserialize(fb_dtype, p),
        },
        SerializedDTypeField::Field(f) => extract_field(fb_dtype, f),
    }
}

fn fb_struct(bytes: &[u8]) -> VortexResult<fbd::Struct_> {
    fb_dtype(bytes)
        .type__as_struct_()
        .ok_or_else(|| vortex_err!("The top-level type should be a struct"))
}

fn fb_dtype(bytes: &[u8]) -> fbd::DType {
    unsafe { root_unchecked::<fbd::DType>(bytes) }
}

#[derive(Debug, Clone)]
pub struct RelativeLayoutCache {
    root: Arc<RwLock<LayoutMessageCache>>,
    dtype: Arc<LazyDType>,
    path: MessageId,
}

impl RelativeLayoutCache {
    pub fn new(root: Arc<RwLock<LayoutMessageCache>>, dtype: Arc<LazyDType>) -> Self {
        Self {
            root,
            dtype,
            path: Vec::new(),
        }
    }

    pub fn relative(&self, id: LayoutPartId, dtype: Arc<LazyDType>) -> Self {
        let mut new_path = Vec::with_capacity(self.path.len() + 1);
        new_path.clone_from(&self.path);
        new_path.push(id);
        Self {
            root: self.root.clone(),
            path: new_path,
            dtype,
        }
    }

    pub fn unknown_dtype(&self, id: LayoutPartId) -> Self {
        self.relative(id, Arc::new(LazyDType::unknown()))
    }

    pub fn get(&self, path: &[LayoutPartId]) -> Option<Buffer> {
        self.root
            .read()
            .unwrap_or_else(|poison| {
                vortex_panic!(
                    "Failed to read from layout cache at path {:?} with error {}",
                    path,
                    poison
                );
            })
            .get(&self.absolute_id(path))
    }

    pub fn remove(&mut self, path: &[LayoutPartId]) -> Option<Buffer> {
        self.root
            .write()
            .unwrap_or_else(|poison| {
                vortex_panic!(
                    "Failed to write to layout cache at path {:?} with error {}",
                    path,
                    poison
                )
            })
            .remove(&self.absolute_id(path))
    }

    pub fn dtype(&self) -> &Arc<LazyDType> {
        &self.dtype
    }

    pub fn absolute_id(&self, path: &[LayoutPartId]) -> MessageId {
        let mut lookup_key = Vec::with_capacity(self.path.len() + path.len());
        lookup_key.clone_from(&self.path);
        lookup_key.extend_from_slice(path);
        lookup_key
    }
}