/
row.rs
1371 lines (1255 loc) · 42.9 KB
/
row.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Materialize, Inc. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.
use std::borrow::Borrow;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::convert::TryInto;
use std::fmt;
use std::mem::{size_of, transmute};
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use ordered_float::OrderedFloat;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use uuid::Uuid;
use crate::adt::array::{
Array, ArrayDimension, ArrayDimensions, InvalidArrayError, MAX_ARRAY_DIMENSIONS,
};
use crate::adt::decimal::Significand;
use crate::adt::interval::Interval;
use crate::Datum;
use fmt::Debug;
/// A packed representation for `Datum`s.
///
/// `Datum` is easy to work with but very space inefficent. A `Datum::Int32(42)` is laid out in memory like this:
///
/// tag: 3
/// padding: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
/// data: 0 0 0 42
/// padding: 0 0 0 0 0 0 0 0 0 0 0 0
///
/// For a total of 32 bytes! The second set of padding is needed in case we were to write a `Datum::Decimal` into this location. The first set of padding is needed to align that hypothetical decimal to a 16 bytes boundary.
///
/// A `Row` stores zero or more `Datum`s without any padding.
/// We avoid the need for the first set of padding by only providing access to the `Datum`s via calls to `ptr::read_unaligned`, which on modern x86 is barely penalized.
/// We avoid the need for the second set of padding by not providing mutable access to the `Datum`. Instead, `Row` is append-only.
///
/// A `Row` can be built from a collection of `Datum`s using `Row::pack`, but it often more efficient to use and re-use a `RowPacker` which can avoid unneccesary allocations.
///
/// ```
/// # use repr::{Row, Datum};
/// let row = Row::pack(&[Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)]);
/// assert_eq!(row.unpack(), vec![Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)])
/// ```
///
/// `Row`s can be unpacked by iterating over them:
///
/// ```
/// # use repr::{Row, Datum};
/// let row = Row::pack(&[Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)]);
/// assert_eq!(row.iter().nth(1).unwrap(), Datum::Int32(1));
/// ```
///
/// If you want random access to the `Datum`s in a `Row`, use `Row::unpack` to create a `Vec<Datum>`
/// ```
/// # use repr::{Row, Datum};
/// let row = Row::pack(&[Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)]);
/// let datums = row.unpack();
/// assert_eq!(datums[1], Datum::Int32(1));
/// ```
///
/// # Performance
///
/// Rows are dynamically sized, but up to a fixed size their data is stored in-line.
/// It is best to re-use a `RowPacker` across multiple `Row` creation calls, as this
/// avoids the allocations involved in `RowPacker::new()`.
#[derive(Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Row {
data: SmallVec<[u8; 16]>,
}
/// These implementations order first by length, and then by slice contents.
/// This allows many comparisons to complete without dereferencing memory.
impl PartialOrd for Row {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.data.len().cmp(&other.data.len()) {
std::cmp::Ordering::Less => Some(std::cmp::Ordering::Less),
std::cmp::Ordering::Greater => Some(std::cmp::Ordering::Greater),
std::cmp::Ordering::Equal => Some(self.data.cmp(&other.data)),
}
}
}
impl Ord for Row {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.data.len().cmp(&other.data.len()) {
std::cmp::Ordering::Less => std::cmp::Ordering::Less,
std::cmp::Ordering::Greater => std::cmp::Ordering::Greater,
std::cmp::Ordering::Equal => self.data.cmp(&other.data),
}
}
}
#[derive(Debug)]
pub struct DatumListIter<'a> {
data: &'a [u8],
offset: usize,
}
#[derive(Debug)]
pub struct DatumDictIter<'a> {
data: &'a [u8],
offset: usize,
prev_key: Option<&'a str>,
}
/// `RowPacker` is used to build a `Row`.
///
/// ```
/// # use repr::{Row, Datum, RowPacker};
/// let mut packer = RowPacker::new();
/// packer.push(Datum::Int32(2));
/// packer.push(Datum::String("two"));
/// let row = packer.finish();
/// ```
#[derive(Debug)]
pub struct RowPacker {
data: Vec<u8>,
}
/// `RowArena` is used to hold on to temporary `Row`s for functions like `eval` that need to create complex `Datum`s but don't have a `Row` to put them in yet.
#[derive(Debug)]
pub struct RowArena {
inner: RefCell<RowArenaInner>,
}
#[derive(Debug)]
struct RowArenaInner {
owned_bytes: Vec<Box<[u8]>>,
owned_rows: Vec<Row>,
}
// DatumList and DatumDict defined here rather than near Datum because we need private access to the unsafe data field
/// A sequence of Datums
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct DatumList<'a> {
/// Points at the serialized datums
data: &'a [u8],
}
impl<'a> Debug for DatumList<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl Ord for DatumList<'_> {
fn cmp(&self, other: &DatumList) -> Ordering {
self.iter().cmp(other.iter())
}
}
impl PartialOrd for DatumList<'_> {
fn partial_cmp(&self, other: &DatumList) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/// A mapping from string keys to Datums
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct DatumDict<'a> {
/// Points at the serialized datums, which should be sorted in key order
data: &'a [u8],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Tag {
Null,
False,
True,
Int32,
Int64,
Float32,
Float64,
Decimal,
Date,
Time,
Timestamp,
TimestampTz,
Interval,
Bytes,
String,
Uuid,
Array,
List,
Dict,
JsonNull,
Dummy,
}
// --------------------------------------------------------------------------------
// reading data
/// Reads a `Copy` value starting at byte `offset`.
///
/// Updates `offset` to point to the first byte after the end of the read region.
///
/// # Safety
///
/// This function is safe if a value of type `T` was previously written at this offset by `push_copy!`.
/// Otherwise it could return invalid values, which is Undefined Behavior.
#[inline(always)]
unsafe fn read_copy<T>(data: &[u8], offset: &mut usize) -> T
where
T: Copy,
{
debug_assert!(data.len() >= *offset + size_of::<T>());
let ptr = data.as_ptr().add(*offset);
*offset += size_of::<T>();
(ptr as *const T).read_unaligned()
}
/// Read a byte slice starting at byte `offset`.
///
/// Updates `offset` to point to the first byte after the end of the read region.
///
/// # Safety
///
/// This function is safe if a `&[u8]` was previously written at this offset by `push_untagged_bytes`.
/// Otherwise it could return invalid values, which is Undefined Behavior.
unsafe fn read_untagged_bytes<'a>(data: &'a [u8], offset: &mut usize) -> &'a [u8] {
let len = read_copy::<usize>(data, offset);
let bytes = &data[*offset..(*offset + len)];
*offset += len;
bytes
}
/// Read a string starting at byte `offset`.
///
/// Updates `offset` to point to the first byte after the end of the read region.
///
/// # Safety
///
/// This function is safe if a `str` was previously written at this offset by `push_untagged_string`.
/// Otherwise it could return invalid values, which is Undefined Behavior.
unsafe fn read_untagged_string<'a>(data: &'a [u8], offset: &mut usize) -> &'a str {
let bytes = read_untagged_bytes(data, offset);
std::str::from_utf8_unchecked(bytes)
}
/// Read a datum starting at byte `offset`.
///
/// Updates `offset` to point to the first byte after the end of the read region.
///
/// # Safety
///
/// This function is safe if a `Datum` was previously written at this offset by `push_datum`.
/// Otherwise it could return invalid values, which is Undefined Behavior.
unsafe fn read_datum<'a>(data: &'a [u8], offset: &mut usize) -> Datum<'a> {
let tag = read_copy::<Tag>(data, offset);
match tag {
Tag::Null => Datum::Null,
Tag::False => Datum::False,
Tag::True => Datum::True,
Tag::Int32 => {
let i = read_copy::<i32>(data, offset);
Datum::Int32(i)
}
Tag::Int64 => {
let i = read_copy::<i64>(data, offset);
Datum::Int64(i)
}
Tag::Float32 => {
let f = read_copy::<f32>(data, offset);
Datum::Float32(OrderedFloat::from(f))
}
Tag::Float64 => {
let f = read_copy::<f64>(data, offset);
Datum::Float64(OrderedFloat::from(f))
}
Tag::Date => {
let d = read_copy::<NaiveDate>(data, offset);
Datum::Date(d)
}
Tag::Time => {
let t = read_copy::<NaiveTime>(data, offset);
Datum::Time(t)
}
Tag::Timestamp => {
let t = read_copy::<NaiveDateTime>(data, offset);
Datum::Timestamp(t)
}
Tag::TimestampTz => {
let t = read_copy::<DateTime<Utc>>(data, offset);
Datum::TimestampTz(t)
}
Tag::Interval => {
let months = read_copy::<i32>(data, offset);
let duration = read_copy::<i128>(data, offset);
Datum::Interval(Interval { months, duration })
}
Tag::Decimal => {
let s = read_copy::<Significand>(data, offset);
Datum::Decimal(s)
}
Tag::Bytes => {
let bytes = read_untagged_bytes(data, offset);
Datum::Bytes(bytes)
}
Tag::String => {
let string = read_untagged_string(data, offset);
Datum::String(string)
}
Tag::Uuid => {
let mut b: uuid::Bytes = [0; 16];
b.copy_from_slice(read_untagged_bytes(data, offset));
Datum::Uuid(Uuid::from_bytes(b))
}
Tag::Array => {
// See the comment in `Row::push_array` for details on the encoding
// of arrays.
let ndims = read_copy::<u8>(data, offset);
let dims_size = usize::from(ndims) * size_of::<usize>() * 2;
let dims = &data[*offset..*offset + dims_size];
*offset += dims_size;
let data = read_untagged_bytes(data, offset);
Datum::Array(Array {
dims: ArrayDimensions { data: dims },
elements: DatumList { data },
})
}
Tag::List => {
let bytes = read_untagged_bytes(data, offset);
Datum::List(DatumList { data: bytes })
}
Tag::Dict => {
let bytes = read_untagged_bytes(data, offset);
Datum::Dict(DatumDict { data: bytes })
}
Tag::JsonNull => Datum::JsonNull,
Tag::Dummy => Datum::Dummy,
}
}
// --------------------------------------------------------------------------------
// writing data
fn assert_is_copy<T: Copy>() {}
// See https://github.com/rust-lang/rust/issues/43408 for why this can't be a function
macro_rules! push_copy {
($data:expr, $t:expr, $T:ty) => {
assert_is_copy::<$T>();
$data.extend_from_slice(&unsafe { transmute::<$T, [u8; size_of::<$T>()]>($t) })
};
}
fn push_untagged_bytes(data: &mut Vec<u8>, bytes: &[u8]) {
push_copy!(data, bytes.len(), usize);
data.extend_from_slice(bytes);
}
fn push_untagged_string(data: &mut Vec<u8>, string: &str) {
push_untagged_bytes(data, string.as_bytes())
}
fn push_datum(data: &mut Vec<u8>, datum: Datum) {
match datum {
Datum::Null => data.push(Tag::Null as u8),
Datum::False => data.push(Tag::False as u8),
Datum::True => data.push(Tag::True as u8),
Datum::Int32(i) => {
data.push(Tag::Int32 as u8);
push_copy!(data, i, i32);
}
Datum::Int64(i) => {
data.push(Tag::Int64 as u8);
push_copy!(data, i, i64);
}
Datum::Float32(f) => {
data.push(Tag::Float32 as u8);
push_copy!(data, f.to_bits(), u32);
}
Datum::Float64(f) => {
data.push(Tag::Float64 as u8);
push_copy!(data, f.to_bits(), u64);
}
Datum::Date(d) => {
data.push(Tag::Date as u8);
push_copy!(data, d, NaiveDate);
}
Datum::Time(t) => {
data.push(Tag::Time as u8);
push_copy!(data, t, NaiveTime);
}
Datum::Timestamp(t) => {
data.push(Tag::Timestamp as u8);
push_copy!(data, t, NaiveDateTime);
}
Datum::TimestampTz(t) => {
data.push(Tag::TimestampTz as u8);
push_copy!(data, t, DateTime<Utc>);
}
Datum::Interval(i) => {
data.push(Tag::Interval as u8);
push_copy!(data, i.months, i32);
push_copy!(data, i.duration, i128);
}
Datum::Decimal(s) => {
data.push(Tag::Decimal as u8);
push_copy!(data, s, Significand);
}
Datum::Bytes(bytes) => {
data.push(Tag::Bytes as u8);
push_untagged_bytes(data, bytes);
}
Datum::String(string) => {
data.push(Tag::String as u8);
push_untagged_string(data, string);
}
Datum::Uuid(u) => {
data.push(Tag::Uuid as u8);
push_untagged_bytes(data, u.as_bytes());
}
Datum::Array(array) => {
// See the comment in `Row::push_array` for details on the encoding
// of arrays.
data.push(Tag::Array as u8);
data.push(array.dims.ndims());
data.extend_from_slice(array.dims.data);
push_untagged_bytes(data, &array.elements.data);
}
Datum::List(list) => {
data.push(Tag::List as u8);
push_untagged_bytes(data, &list.data);
}
Datum::Dict(dict) => {
data.push(Tag::Dict as u8);
push_untagged_bytes(data, &dict.data);
}
Datum::JsonNull => data.push(Tag::JsonNull as u8),
Datum::Dummy => data.push(Tag::Dummy as u8),
}
}
/// Number of bytes required by the datum.
///
/// This is used to optimistically pre-allocate buffers for packing rows.
pub fn datum_size(datum: &Datum) -> usize {
match datum {
Datum::Null => 1,
Datum::False => 1,
Datum::True => 1,
Datum::Int32(_) => 1 + size_of::<i32>(),
Datum::Int64(_) => 1 + size_of::<i64>(),
Datum::Float32(_) => 1 + size_of::<u32>(),
Datum::Float64(_) => 1 + size_of::<u64>(),
Datum::Date(_) => 1 + size_of::<NaiveDate>(),
Datum::Time(_) => 1 + size_of::<NaiveTime>(),
Datum::Timestamp(_) => 1 + size_of::<NaiveDateTime>(),
Datum::TimestampTz(_) => 1 + size_of::<DateTime<Utc>>(),
Datum::Interval(_) => 1 + size_of::<i32>() + size_of::<i128>(),
Datum::Decimal(_) => 1 + size_of::<Significand>(),
Datum::Bytes(bytes) => 1 + size_of::<usize>() + bytes.len(),
Datum::String(string) => 1 + size_of::<usize>() + string.as_bytes().len(),
Datum::Uuid(_) => 1 + size_of::<Uuid>(),
Datum::Array(array) => {
1 + size_of::<u8>() + array.dims.data.len() + array.elements.data.len()
}
Datum::List(list) => 1 + size_of::<usize>() + list.data.len(),
Datum::Dict(dict) => 1 + size_of::<usize>() + dict.data.len(),
Datum::JsonNull => 1,
Datum::Dummy => 1,
}
}
// --------------------------------------------------------------------------------
// public api
impl Row {
/// Take some `Datum`s and pack them into a `Row`.
pub fn pack<'a, I, D>(iter: I) -> Row
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
// make a big buffer up front to avoid resizing
let mut packer = RowPacker::new();
packer.extend(iter);
// drop the excess capacity
packer.finish()
}
/// Like [`Row::pack`], but the provided iterator is allowed to produce an
/// error, in which case the packing operation is aborted and the error
/// returned.
pub fn try_pack<'a, I, D, E>(iter: I) -> Result<Row, E>
where
I: IntoIterator<Item = Result<D, E>>,
D: Borrow<Datum<'a>>,
{
// make a big buffer up front to avoid resizing
let mut packer = RowPacker::new();
packer.try_extend(iter)?;
// drop the excess capacity
Ok(packer.finish())
}
// TODO(justin): find a better place to put this.
pub fn new(data: Vec<u8>) -> Self {
Row { data: data.into() }
}
/// Pack a slice of `Datum`s into a `Row`.
///
/// This method has the advantage over `pack` that it can determine the required
/// allocation before packing the elements, ensuring only one allocation and no
/// redundant copies required.
///
/// TODO: This could also be done for cloneable iterators, though we would need to be
/// very careful to avoid using it when iterators are either expensive or have
/// side effects.
pub fn pack_slice<'a, I, D>(slice: &[Datum<'a>]) -> Row {
let needed = slice.iter().map(|d| datum_size(d)).sum();
let mut packer = RowPacker::with_capacity(needed);
packer.extend(slice.iter());
packer.finish()
}
/// Unpack `self` into a `Vec<Datum>` for efficient random access.
pub fn unpack(&self) -> Vec<Datum> {
// It's usually cheaper to unpack twice to figure out the right length than it is to grow the vec as we go
let len = self.iter().count();
let mut vec = Vec::with_capacity(len);
vec.extend(self.iter());
vec
}
/// Return the first `Datum` in `self`
///
/// Panics if the `Row` is empty.
pub fn unpack_first(&self) -> Datum {
unsafe { read_datum(&self.data, &mut 0) }
}
pub fn iter(&self) -> DatumListIter {
DatumListIter {
data: &self.data,
offset: 0,
}
}
/// For debugging only
pub fn data(&self) -> &[u8] {
&self.data
}
}
impl<'a> IntoIterator for &'a Row {
type Item = Datum<'a>;
type IntoIter = DatumListIter<'a>;
fn into_iter(self) -> DatumListIter<'a> {
self.iter()
}
}
impl fmt::Debug for Row {
/// Debug representation using the internal datums
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Row{")?;
f.debug_list().entries(self.iter()).finish()?;
f.write_str("}")
}
}
impl fmt::Display for Row {
/// Display representation using the internal datums
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("(")?;
for (i, datum) in self.iter().enumerate() {
if i != 0 {
f.write_str(", ")?;
}
write!(f, "{}", datum)?;
}
f.write_str(")")
}
}
impl<'a> DatumList<'a> {
pub fn empty() -> DatumList<'static> {
DatumList { data: &[] }
}
pub fn iter(&self) -> DatumListIter<'a> {
DatumListIter {
data: self.data,
offset: 0,
}
}
/// For debugging only
pub fn data(&self) -> &'a [u8] {
&self.data
}
}
impl<'a> IntoIterator for &'a DatumList<'a> {
type Item = Datum<'a>;
type IntoIter = DatumListIter<'a>;
fn into_iter(self) -> DatumListIter<'a> {
self.iter()
}
}
impl<'a> Iterator for DatumListIter<'a> {
type Item = Datum<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.data.len() {
None
} else {
Some(unsafe { read_datum(self.data, &mut self.offset) })
}
}
}
impl<'a> DatumDict<'a> {
pub fn empty() -> DatumDict<'static> {
DatumDict { data: &[] }
}
pub fn iter(&self) -> DatumDictIter<'a> {
DatumDictIter {
data: self.data,
offset: 0,
prev_key: None,
}
}
/// For debugging only
pub fn data(&self) -> &'a [u8] {
&self.data
}
}
impl<'a> IntoIterator for &'a DatumDict<'a> {
type Item = (&'a str, Datum<'a>);
type IntoIter = DatumDictIter<'a>;
fn into_iter(self) -> DatumDictIter<'a> {
self.iter()
}
}
impl<'a> Iterator for DatumDictIter<'a> {
type Item = (&'a str, Datum<'a>);
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.data.len() {
None
} else {
Some(unsafe {
let key_tag = read_copy::<Tag>(self.data, &mut self.offset);
assert!(
key_tag == Tag::String,
"Dict keys must be strings, got {:?}",
key_tag
);
let key = read_untagged_string(self.data, &mut self.offset);
let val = read_datum(self.data, &mut self.offset);
// if in debug mode, sanity check keys
if cfg!(debug_assertions) {
if let Some(prev_key) = self.prev_key {
debug_assert!(
prev_key < key,
"Dict keys must be unique and given in ascending order: {} came before {}",
prev_key,
key
);
}
self.prev_key = Some(key);
}
(key, val)
})
}
}
}
impl RowPacker {
/// Allocates an empty row packer.
pub fn new() -> Self {
// TODO: Determine if this is the best default choice.
Self::with_capacity(0)
}
/// Allocates an empty row packer with a supplied capacity.
pub fn with_capacity(capacity: usize) -> Self {
RowPacker {
// make a big buffer up front to avoid resizing
data: Vec::with_capacity(capacity),
}
}
/// Push `datum` onto the end of `self`
pub fn push(&mut self, datum: Datum) {
push_datum(&mut self.data, datum)
}
/// Extends `self` with the contents of an iterator.
pub fn extend<'a, I, D>(&mut self, iter: I)
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
for datum in iter {
self.push(*datum.borrow());
}
}
/// Creates a new `Row` from an iterator.
///
/// Unlike `Row::pack`, this re-uses the `RowPacker` to avoid
/// unneccesary allocations.
pub fn pack<'a, I, D>(&mut self, iter: I) -> Row
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
self.data.clear();
for datum in iter {
self.push(*datum.borrow());
}
self.finish_and_reuse()
}
/// Like [`RowPacker::extend`], but the provided iterator is allowed to
/// produce an error.
///
/// `try_extend` is not atomic, so if the iterator produces an error `self`
/// will still be extended with all elements the iterator produces before
/// the error.
pub fn try_extend<'a, I, E, D>(&mut self, iter: I) -> Result<(), E>
where
I: IntoIterator<Item = Result<D, E>>,
D: Borrow<Datum<'a>>,
{
for datum in iter {
self.push(*datum?.borrow());
}
Ok(())
}
/// Appends the datums of an entire `Row`.
pub fn extend_by_row(&mut self, row: &Row) {
self.data.extend(&*row.data);
}
/// Finish packing and return a `Row`.
///
/// This does not re-use the allocation of `RowPacker`, which means this
/// method has relatively few advantages over `finish_and_reuse()`.
pub fn finish(self) -> Row {
Row {
data: SmallVec::from(&self.data[..]),
}
}
/// Finish packing and return a `Row`.
///
/// Unlike [`RowPacker::finish`], this method uses `self.data` to right-size an
/// allocation for the new `Row` copied from `self.data`, rather than
/// move the allocation (and potentially re-alloc to resize it).
/// In principle this can reduce the amount of interaction with the
/// allocator, as opposed to creating new row packers for each row.
pub fn finish_and_reuse(&mut self) -> Row {
let data = SmallVec::from(&self.data[..]);
self.data.clear();
Row { data }
}
/// Pushes a [`DatumList`] that is built from a closure.
///
/// The supplied closure will be invoked once with a `RowPacker` that can
/// be used to populate the list. It is valid to call any method on the
/// [`RowPacker`] except for [`Row::finish_and_reuse`].
///
/// Returns the value returned by the closure, if any.
///
/// ```
/// # use repr::{Row, Datum, RowPacker};
/// let mut packer = RowPacker::new();
/// packer.push_list_with(|packer| {
/// packer.push(Datum::String("age"));
/// packer.push(Datum::Int64(42));
/// });
/// let row = packer.finish();
///
/// assert_eq!(
/// row.unpack_first().unwrap_list().iter().collect::<Vec<_>>(),
/// vec![Datum::String("age"), Datum::Int64(42)],
/// );
/// ```
#[inline]
pub fn push_list_with<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut RowPacker) -> R,
{
self.data.push(Tag::List as u8);
let start = self.data.len();
// write a dummy len, will fix it up later
push_copy!(&mut self.data, 0, usize);
let out = f(self);
let len = self.data.len() - start - size_of::<usize>();
// fix up the len
self.data[start..start + size_of::<usize>()].copy_from_slice(&len.to_le_bytes());
out
}
/// Pushes a [`DatumDict`] that is built from a closure.
///
/// The supplied closure will be invoked once with a `RowPacker` that can be
/// used to populate the dict.
///
/// The closure **must** alternate pushing string keys and arbitary values,
/// otherwise reading the dict will cause a panic.
///
/// The closure **must** push keys in ascending order, otherwise equality
/// checks on the resulting `Row` may be wrong and reading the dict IN DEBUG
/// MODE will cause a panic.
///
/// The closure **must not** call [`RowPacker::finish_and_reuse`].
///
/// # Example
///
/// ```
/// # use repr::{Row, Datum, RowPacker};
/// let mut packer = RowPacker::new();
/// packer.push_dict_with(|packer| {
///
/// // key
/// packer.push(Datum::String("age"));
/// // value
/// packer.push(Datum::Int64(42));
///
/// // key
/// packer.push(Datum::String("name"));
/// // value
/// packer.push(Datum::String("bob"));
/// });
/// let row = packer.finish();
///
/// assert_eq!(
/// row.unpack_first().unwrap_dict().iter().collect::<Vec<_>>(),
/// vec![("age", Datum::Int64(42)), ("name", Datum::String("bob"))]
/// );
/// ```
pub fn push_dict_with<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut RowPacker) -> R,
{
self.data.push(Tag::Dict as u8);
let start = self.data.len();
// write a dummy len, will fix it up later
push_copy!(&mut self.data, 0, usize);
let res = f(self);
let len = self.data.len() - start - size_of::<usize>();
// fix up the len
self.data[start..start + size_of::<usize>()].copy_from_slice(&len.to_le_bytes());
res
}
/// Convenience function to construct an array from an iter of `Datum`s.
///
/// Returns an error if the number of elements in `iter` does not match
/// the cardinality of the array as described by `dims`, or if the
/// number of dimensions exceeds [`MAX_ARRAY_DIMENSIONS`]. If an error
/// occurs, the packer's state will be unchanged.
pub fn push_array<'a, I, D>(
&mut self,
dims: &[ArrayDimension],
iter: I,
) -> Result<(), InvalidArrayError>
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
// Arrays are encoded as follows.
//
// u8 ndims
// usize dim_0 lower bound
// usize dim_0 length
// ...
// usize dim_n lower bound
// usize dim_n length
// usize element data size in bytes
// u8 element data, where elements are encoded in row-major order
if dims.len() > usize::from(MAX_ARRAY_DIMENSIONS) {
return Err(InvalidArrayError::TooManyDimensions(dims.len()));
}
let start = self.data.len();
self.data.push(Tag::Array as u8);
// Write dimension information.
self.data
.push(dims.len().try_into().expect("ndims verified to fit in u8"));
for dim in dims {
push_copy!(&mut self.data, dim.lower_bound, usize);
push_copy!(&mut self.data, dim.length, usize);
}
// Write elements.
let off = self.data.len();
push_copy!(&mut self.data, 0, usize); // dummy length fixed up below
let mut nelements = 0;
for datum in iter {
self.push(*datum.borrow());
nelements += 1;
}
let len = self.data.len() - off - size_of::<usize>();
self.data[off..off + size_of::<usize>()].copy_from_slice(&len.to_le_bytes());
// Check that the number of elements written matches the dimension
// information.
let cardinality = dims.iter().map(|d| d.length).product();
if nelements != cardinality {
self.data.truncate(start);
return Err(InvalidArrayError::WrongCardinality {
actual: nelements,
expected: cardinality,
});
}
Ok(())
}
/// Convenience function to push a `DatumList` from an iter of `Datum`s
///
/// See [`push_dict_with`] if you need to be able to handle errors
pub fn push_list<'a, I, D>(&mut self, iter: I)
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
self.push_list_with(|packer| {
for elem in iter {
packer.push(*elem.borrow())
}
});
}
/// Convenience function to push a `DatumDict` from an iter of `(&str, Datum)` pairs
///
/// See [`try_push_dict_with`] if you need to be able to handle errors
pub fn push_dict<'a, I, D>(&mut self, iter: I)
where
I: IntoIterator<Item = (&'a str, D)>,
D: Borrow<Datum<'a>>,
{
self.push_dict_with(|packer| {
for (k, v) in iter {
packer.push(Datum::String(k));
packer.push(*v.borrow())
}
})
}
/// # Safety
///
/// Truncates the underlying storage to the specified byte position.
///
/// `pos` MUST specify a byte offset that lies on a datum boundary.
/// If `pos` specifies a byte offset that is *within* a datum, the row
/// packer will produce an invalid row, the unpacking of which may
/// trigger undefined behavior!
///
/// To find the byte offset of a datum boundary, inspect the the packer's
/// byte length by calling `packer.data().len()` after pushing the desired
/// number of datums onto the packer.
pub unsafe fn truncate(&mut self, pos: usize) {
self.data.truncate(pos)
}
/// For debugging only
pub fn data(&self) -> &[u8] {
&self.data
}
}
impl RowArena {
pub fn new() -> Self {
RowArena {
inner: RefCell::new(RowArenaInner {
owned_bytes: vec![],
owned_rows: vec![],