vortex_array/arrays/varbinview/resolved/mod.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! A [`VarBinViewArray`] with its data buffers resolved for per-row access.
5
6use crate::arrays::VarBinViewArray;
7use crate::arrays::varbinview::BinaryView;
8
9/// A canonical [`VarBinViewArray`] with its data buffers resolved to slices, so reading a row is
10/// a slice index rather than a buffer-handle lookup.
11pub struct ResolvedViews<'a> {
12 views: &'a [BinaryView],
13 buffers: Vec<&'a [u8]>,
14}
15
16impl<'a> ResolvedViews<'a> {
17 /// Resolve the data buffers of `array`.
18 pub(super) fn new(array: &'a VarBinViewArray) -> Self {
19 Self {
20 views: array.views(),
21 buffers: (0..array.data_buffers().len())
22 .map(|idx| array.buffer(idx).as_slice())
23 .collect(),
24 }
25 }
26
27 /// The array's views, one per row.
28 #[inline]
29 pub fn views(&self) -> &'a [BinaryView] {
30 self.views
31 }
32
33 /// The data buffers, indexed by a view's `buffer_index`.
34 #[inline]
35 pub fn buffers(&self) -> &[&'a [u8]] {
36 &self.buffers
37 }
38
39 /// The number of rows.
40 #[inline]
41 pub fn len(&self) -> usize {
42 self.views.len()
43 }
44
45 /// Whether the array has no rows.
46 #[inline]
47 pub fn is_empty(&self) -> bool {
48 self.views.is_empty()
49 }
50
51 /// The view at `index` without a bounds check.
52 ///
53 /// # Safety
54 ///
55 /// `index` must be less than [`len`](Self::len).
56 #[inline]
57 pub unsafe fn view_unchecked(&self, index: usize) -> &'a BinaryView {
58 // SAFETY: caller guarantees index < len.
59 unsafe { self.views.get_unchecked(index) }
60 }
61
62 /// The bytes of the value at `index`.
63 ///
64 /// # Panics
65 ///
66 /// Panics if `index` is out of bounds.
67 #[inline]
68 pub fn bytes(&self, index: usize) -> &'a [u8] {
69 self.view_bytes(&self.views[index])
70 }
71
72 /// The bytes of `view`, which must belong to this array.
73 #[inline]
74 pub fn view_bytes(&self, view: &'a BinaryView) -> &'a [u8] {
75 view.bytes(&self.buffers)
76 }
77}
78
79#[cfg(test)]
80mod tests;