This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
mutable.rs
193 lines (165 loc) · 5.47 KB
/
mutable.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
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
use std::{iter::FromIterator, sync::Arc};
use crate::{
array::{Array, MutableArray, Offset, TryExtend, TryPush},
bitmap::MutableBitmap,
buffer::MutableBuffer,
datatypes::DataType,
error::{ArrowError, Result},
};
use super::BinaryArray;
/// The Arrow's equivalent to `Vec<Option<Vec<u8>>>`.
/// Converting a [`MutableBinaryArray`] into a [`BinaryArray`] is `O(1)`.
/// # Implementation
/// This struct does not allocate a validity until one is required (i.e. push a null to it).
#[derive(Debug)]
pub struct MutableBinaryArray<O: Offset> {
offsets: MutableBuffer<O>,
values: MutableBuffer<u8>,
validity: Option<MutableBitmap>,
}
impl<O: Offset> From<MutableBinaryArray<O>> for BinaryArray<O> {
fn from(other: MutableBinaryArray<O>) -> Self {
BinaryArray::<O>::from_data(
other.offsets.into(),
other.values.into(),
other.validity.map(|x| x.into()),
)
}
}
impl<O: Offset> Default for MutableBinaryArray<O> {
fn default() -> Self {
Self::new()
}
}
impl<O: Offset> MutableBinaryArray<O> {
/// Creates a new empty [`MutableBinaryArray`].
/// # Implementation
/// This allocates a [`MutableBuffer`] of one element
pub fn new() -> Self {
Self::with_capacity(0)
}
/// Creates a new [`MutableBinaryArray`] with capacity for `capacity` values.
/// # Implementation
/// This does not allocate the validity.
pub fn with_capacity(capacity: usize) -> Self {
let mut offsets = MutableBuffer::<O>::with_capacity(capacity + 1);
offsets.push(O::default());
Self {
offsets,
values: MutableBuffer::<u8>::new(),
validity: None,
}
}
/// Reserves `additional` slots.
pub fn reserve(&mut self, additional: usize) {
self.offsets.reserve(additional);
if let Some(x) = self.validity.as_mut() {
x.reserve(additional)
}
}
#[inline]
fn last_offset(&self) -> O {
*self.offsets.last().unwrap()
}
/// Pushes a new element to the array.
/// # Panic
/// This operation panics iff the length of all values (in bytes) exceeds `O` maximum value.
pub fn push<T: AsRef<[u8]>>(&mut self, value: Option<T>) {
self.try_push(value).unwrap()
}
fn try_from_iter<P: AsRef<[u8]>, I: IntoIterator<Item = Option<P>>>(iter: I) -> Result<Self> {
let iterator = iter.into_iter();
let (lower, _) = iterator.size_hint();
let mut primitive = Self::with_capacity(lower);
for item in iterator {
primitive.try_push(item.as_ref())?
}
Ok(primitive)
}
fn init_validity(&mut self) {
self.validity = Some(MutableBitmap::from_trusted_len_iter(
std::iter::repeat(true)
.take(self.len() - 1)
.chain(std::iter::once(false)),
))
}
/// Converts itself into an [`Array`].
pub fn into_arc(self) -> Arc<dyn Array> {
let a: BinaryArray<O> = self.into();
Arc::new(a)
}
}
impl<O: Offset> MutableArray for MutableBinaryArray<O> {
fn len(&self) -> usize {
self.offsets.len() - 1
}
fn validity(&self) -> &Option<MutableBitmap> {
&self.validity
}
fn as_arc(&mut self) -> Arc<dyn Array> {
Arc::new(BinaryArray::from_data(
std::mem::take(&mut self.offsets).into(),
std::mem::take(&mut self.values).into(),
std::mem::take(&mut self.validity).map(|x| x.into()),
))
}
fn data_type(&self) -> &DataType {
if O::is_large() {
&DataType::LargeUtf8
} else {
&DataType::Utf8
}
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
#[inline]
fn push_null(&mut self) {
self.push::<&[u8]>(None)
}
}
impl<O: Offset, P: AsRef<[u8]>> FromIterator<Option<P>> for MutableBinaryArray<O> {
fn from_iter<I: IntoIterator<Item = Option<P>>>(iter: I) -> Self {
Self::try_from_iter(iter).unwrap()
}
}
impl<O: Offset, T: AsRef<[u8]>> Extend<Option<T>> for MutableBinaryArray<O> {
fn extend<I: IntoIterator<Item = Option<T>>>(&mut self, iter: I) {
self.try_extend(iter).unwrap();
}
}
impl<O: Offset, T: AsRef<[u8]>> TryExtend<Option<T>> for MutableBinaryArray<O> {
fn try_extend<I: IntoIterator<Item = Option<T>>>(&mut self, iter: I) -> Result<()> {
let mut iter = iter.into_iter();
self.reserve(iter.size_hint().0);
iter.try_for_each(|x| self.try_push(x))
}
}
impl<O: Offset, T: AsRef<[u8]>> TryPush<Option<T>> for MutableBinaryArray<O> {
fn try_push(&mut self, value: Option<T>) -> Result<()> {
match value {
Some(value) => {
let bytes = value.as_ref();
let size = O::from_usize(self.values.len() + bytes.len())
.ok_or(ArrowError::KeyOverflowError)?;
self.values.extend_from_slice(bytes);
self.offsets.push(size);
match &mut self.validity {
Some(validity) => validity.push(true),
None => {}
}
}
None => {
self.offsets.push(self.last_offset());
match &mut self.validity {
Some(validity) => validity.push(false),
None => self.init_validity(),
}
}
}
Ok(())
}
}