forked from influxdata/influxdb3_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.rs
384 lines (308 loc) · 11 KB
/
string.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use arrow::array::ArrayDataBuilder;
use arrow::array::StringArray;
use arrow::buffer::Buffer;
use arrow::buffer::NullBuffer;
use num_traits::{AsPrimitive, FromPrimitive, Zero};
use std::fmt::Debug;
use std::ops::Range;
/// A packed string array that stores start and end indexes into
/// a contiguous string slice.
///
/// The type parameter K alters the type used to store the offsets
#[derive(Debug, Clone)]
pub struct PackedStringArray<K> {
/// The start and end offsets of strings stored in storage
offsets: Vec<K>,
/// A contiguous array of string data
storage: String,
}
impl<K: Zero> Default for PackedStringArray<K> {
fn default() -> Self {
Self {
offsets: vec![K::zero()],
storage: String::new(),
}
}
}
impl<K: AsPrimitive<usize> + FromPrimitive + Zero> PackedStringArray<K> {
pub fn new() -> Self {
Self::default()
}
pub fn new_empty(len: usize) -> Self {
Self {
offsets: vec![K::zero(); len + 1],
storage: String::new(),
}
}
pub fn with_capacity(keys: usize, values: usize) -> Self {
let mut offsets = Vec::with_capacity(keys + 1);
offsets.push(K::zero());
Self {
offsets,
storage: String::with_capacity(values),
}
}
/// Append a value
///
/// Returns the index of the appended data
pub fn append(&mut self, data: &str) -> usize {
let id = self.offsets.len() - 1;
let offset = self.storage.len() + data.len();
let offset = K::from_usize(offset).expect("failed to fit into offset type");
self.offsets.push(offset);
self.storage.push_str(data);
id
}
/// Extends this [`PackedStringArray`] by the contents of `other`
pub fn extend_from(&mut self, other: &Self) {
let offset = self.storage.len();
self.storage.push_str(other.storage.as_str());
// Copy offsets skipping the first element as this string start delimiter is already
// provided by the end delimiter of the current offsets array
self.offsets.extend(
other
.offsets
.iter()
.skip(1)
.map(|x| K::from_usize(x.as_() + offset).expect("failed to fit into offset type")),
)
}
/// Extends this [`PackedStringArray`] by `range` elements from `other`
pub fn extend_from_range(&mut self, other: &Self, range: Range<usize>) {
let first_offset: usize = other.offsets[range.start].as_();
let end_offset: usize = other.offsets[range.end].as_();
let insert_offset = self.storage.len();
self.storage
.push_str(&other.storage[first_offset..end_offset]);
self.offsets.extend(
other.offsets[(range.start + 1)..(range.end + 1)]
.iter()
.map(|x| {
K::from_usize(x.as_() - first_offset + insert_offset)
.expect("failed to fit into offset type")
}),
)
}
/// Get the value at a given index
pub fn get(&self, index: usize) -> Option<&str> {
let start_offset = self.offsets.get(index)?.as_();
let end_offset = self.offsets.get(index + 1)?.as_();
Some(&self.storage[start_offset..end_offset])
}
/// Pads with empty strings to reach length
pub fn extend(&mut self, len: usize) {
let offset = K::from_usize(self.storage.len()).expect("failed to fit into offset type");
self.offsets.resize(self.offsets.len() + len, offset);
}
/// Truncates the array to the given length
pub fn truncate(&mut self, len: usize) {
self.offsets.truncate(len + 1);
let last_idx = self.offsets.last().expect("offsets empty");
self.storage.truncate(last_idx.as_());
}
/// Removes all elements from this array
pub fn clear(&mut self) {
self.offsets.truncate(1);
self.storage.clear();
}
pub fn iter(&self) -> PackedStringIterator<'_, K> {
PackedStringIterator {
array: self,
index: 0,
}
}
/// The number of strings in this array
pub fn len(&self) -> usize {
self.offsets.len() - 1
}
pub fn is_empty(&self) -> bool {
self.offsets.len() == 1
}
/// Return the amount of memory in bytes taken up by this array
pub fn size(&self) -> usize {
self.storage.capacity() + self.offsets.capacity() * std::mem::size_of::<K>()
}
pub fn inner(&self) -> (&[K], &str) {
(&self.offsets, &self.storage)
}
pub fn into_inner(self) -> (Vec<K>, String) {
(self.offsets, self.storage)
}
/// Split this [`PackedStringArray`] at `n`, such that `self`` contains the
/// elements `[0, n)` and the returned [`PackedStringArray`] contains
/// elements `[n, len)`.
pub fn split_off(&mut self, n: usize) -> Self {
if n > self.len() {
return Default::default();
}
let offsets = self.offsets.split_off(n + 1);
// Figure out where to split the string storage.
let split_point = self.offsets.last().map(|v| v.as_()).unwrap();
// Split the storage at the split point, such that the first N values
// appear in self.
let storage = self.storage.split_off(split_point);
// The new "offsets" now needs remapping such that the first offset
// starts at 0, so that indexing into the new storage string will hit
// the right start point.
let offsets = std::iter::once(K::zero())
.chain(
offsets
.into_iter()
.map(|v| K::from_usize(v.as_() - split_point).unwrap()),
)
.collect::<Vec<_>>();
Self { offsets, storage }
}
}
impl PackedStringArray<i32> {
/// Convert to an arrow with an optional null bitmask
pub fn to_arrow(&self, nulls: Option<NullBuffer>) -> StringArray {
let len = self.offsets.len() - 1;
let offsets = Buffer::from_slice_ref(&self.offsets);
let values = Buffer::from(self.storage.as_bytes());
let data = ArrayDataBuilder::new(arrow::datatypes::DataType::Utf8)
.len(len)
.add_buffer(offsets)
.add_buffer(values)
.nulls(nulls)
.build()
// TODO consider skipping the validation checks by using
// `new_unchecked`
.expect("Valid array data");
StringArray::from(data)
}
}
#[derive(Debug)]
pub struct PackedStringIterator<'a, K> {
array: &'a PackedStringArray<K>,
index: usize,
}
impl<'a, K: AsPrimitive<usize> + FromPrimitive + Zero> Iterator for PackedStringIterator<'a, K> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
let item = self.array.get(self.index)?;
self.index += 1;
Some(item)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.array.len() - self.index;
(len, Some(len))
}
}
#[cfg(test)]
mod tests {
use crate::string::PackedStringArray;
use proptest::prelude::*;
#[test]
fn test_storage() {
let mut array = PackedStringArray::<i32>::new();
array.append("hello");
array.append("world");
array.append("cupcake");
assert_eq!(array.get(0).unwrap(), "hello");
assert_eq!(array.get(1).unwrap(), "world");
assert_eq!(array.get(2).unwrap(), "cupcake");
assert!(array.get(-1_i32 as usize).is_none());
assert!(array.get(3).is_none());
array.extend(2);
assert_eq!(array.get(3).unwrap(), "");
assert_eq!(array.get(4).unwrap(), "");
assert!(array.get(5).is_none());
}
#[test]
fn test_empty() {
let array = PackedStringArray::<u8>::new_empty(20);
assert_eq!(array.get(12).unwrap(), "");
assert_eq!(array.get(9).unwrap(), "");
assert_eq!(array.get(3).unwrap(), "");
}
#[test]
fn test_truncate() {
let mut array = PackedStringArray::<i32>::new();
array.append("hello");
array.append("world");
array.append("cupcake");
array.truncate(1);
assert_eq!(array.len(), 1);
assert_eq!(array.get(0).unwrap(), "hello");
array.append("world");
assert_eq!(array.len(), 2);
assert_eq!(array.get(0).unwrap(), "hello");
assert_eq!(array.get(1).unwrap(), "world");
}
#[test]
fn test_extend_from() {
let mut a = PackedStringArray::<i32>::new();
a.append("hello");
a.append("world");
a.append("cupcake");
a.append("");
let mut b = PackedStringArray::<i32>::new();
b.append("foo");
b.append("bar");
a.extend_from(&b);
let a_content: Vec<_> = a.iter().collect();
assert_eq!(
a_content,
vec!["hello", "world", "cupcake", "", "foo", "bar"]
);
}
#[test]
fn test_extend_from_range() {
let mut a = PackedStringArray::<i32>::new();
a.append("hello");
a.append("world");
a.append("cupcake");
a.append("");
let mut b = PackedStringArray::<i32>::new();
b.append("foo");
b.append("bar");
b.append("");
b.append("fiz");
a.extend_from_range(&b, 1..3);
assert_eq!(a.len(), 6);
let a_content: Vec<_> = a.iter().collect();
assert_eq!(a_content, vec!["hello", "world", "cupcake", "", "bar", ""]);
// Should be a no-op
a.extend_from_range(&b, 0..0);
let a_content: Vec<_> = a.iter().collect();
assert_eq!(a_content, vec!["hello", "world", "cupcake", "", "bar", ""]);
a.extend_from_range(&b, 0..1);
let a_content: Vec<_> = a.iter().collect();
assert_eq!(
a_content,
vec!["hello", "world", "cupcake", "", "bar", "", "foo"]
);
a.extend_from_range(&b, 1..4);
let a_content: Vec<_> = a.iter().collect();
assert_eq!(
a_content,
vec!["hello", "world", "cupcake", "", "bar", "", "foo", "bar", "", "fiz"]
);
}
proptest! {
#[test]
fn prop_split_off(
a in prop::collection::vec(any::<String>(), 0..20),
b in prop::collection::vec(any::<String>(), 0..20),
) {
let mut p = PackedStringArray::<i32>::new();
// Add all the elements in "a" and "b" to the string array.
for v in a.iter().chain(b.iter()) {
p.append(v);
}
// Split the packed string array at the boundary of "a".
let p2 = p.split_off(a.len());
assert_eq!(p.iter().collect::<Vec<_>>(), a, "parent");
assert_eq!(p2.iter().collect::<Vec<_>>(), b, "child");
}
}
#[test]
fn test_split_off_oob() {
let mut p = PackedStringArray::<i32>::new();
p.append("bananas");
let got = p.split_off(42);
assert_eq!(p.len(), 1);
assert_eq!(got.len(), 0);
}
}