1use std::ptr;
5
6use reifydb_value::{reifydb_assertions, value::value_type::ValueType};
7
8use crate::encoded::{row::EncodedRow, shape::RowShape};
9
10impl RowShape {
11 pub fn set_u32(&self, row: &mut EncodedRow, index: usize, value: impl Into<u32>) {
12 let field = &self.fields()[index];
13 reifydb_assertions! {
14 assert!(
15 row.len() >= self.total_static_size(),
16 "row/shape size mismatch: row.len()={} < total_static_size()={}",
17 row.len(),
18 self.total_static_size()
19 );
20 assert_eq!(*field.constraint.get_type().inner_type(), ValueType::Uint4);
21 }
22 row.set_valid(index, true);
23 unsafe {
24 ptr::write_unaligned(
25 row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut u32,
26 value.into(),
27 )
28 }
29 }
30
31 pub fn get_u32(&self, row: &EncodedRow, index: usize) -> u32 {
32 let field = &self.fields()[index];
33 reifydb_assertions! {
34 assert!(
35 row.len() >= self.total_static_size(),
36 "row/shape size mismatch: row.len()={} < total_static_size()={}",
37 row.len(),
38 self.total_static_size()
39 );
40 assert_eq!(*field.constraint.get_type().inner_type(), ValueType::Uint4);
41 }
42 unsafe { (row.as_ptr().add(field.offset as usize) as *const u32).read_unaligned() }
43 }
44
45 pub fn try_get_u32(&self, row: &EncodedRow, index: usize) -> Option<u32> {
46 if row.is_defined(index) && self.fields()[index].constraint.get_type() == ValueType::Uint4 {
47 Some(self.get_u32(row, index))
48 } else {
49 None
50 }
51 }
52}
53
54#[cfg(test)]
55pub mod tests {
56 use reifydb_value::value::value_type::ValueType;
57
58 use crate::encoded::shape::RowShape;
59
60 #[test]
61 fn test_set_get_u32() {
62 let shape = RowShape::testing(&[ValueType::Uint4]);
63 let mut row = shape.allocate();
64 shape.set_u32(&mut row, 0, 4294967295u32);
65 assert_eq!(shape.get_u32(&row, 0), 4294967295u32);
66 }
67
68 #[test]
69 fn test_try_get_u32() {
70 let shape = RowShape::testing(&[ValueType::Uint4]);
71 let mut row = shape.allocate();
72
73 assert_eq!(shape.try_get_u32(&row, 0), None);
74
75 shape.set_u32(&mut row, 0, 4294967295u32);
76 assert_eq!(shape.try_get_u32(&row, 0), Some(4294967295u32));
77 }
78
79 #[test]
80 fn test_extremes() {
81 let shape = RowShape::testing(&[ValueType::Uint4]);
82 let mut row = shape.allocate();
83
84 shape.set_u32(&mut row, 0, u32::MAX);
85 assert_eq!(shape.get_u32(&row, 0), u32::MAX);
86
87 let mut row2 = shape.allocate();
88 shape.set_u32(&mut row2, 0, u32::MIN);
89 assert_eq!(shape.get_u32(&row2, 0), u32::MIN);
90
91 let mut row3 = shape.allocate();
92 shape.set_u32(&mut row3, 0, 0u32);
93 assert_eq!(shape.get_u32(&row3, 0), 0u32);
94 }
95
96 #[test]
97 fn test_large_values() {
98 let shape = RowShape::testing(&[ValueType::Uint4]);
99
100 let test_values = [
101 0u32,
102 1u32,
103 1_000_000u32,
104 1_000_000_000u32,
105 2_147_483_647u32, 2_147_483_648u32, 4_000_000_000u32,
108 4_294_967_294u32,
109 4_294_967_295u32, ];
111
112 for value in test_values {
113 let mut row = shape.allocate();
114 shape.set_u32(&mut row, 0, value);
115 assert_eq!(shape.get_u32(&row, 0), value);
116 }
117 }
118
119 #[test]
120 fn test_timestamp_values() {
121 let shape = RowShape::testing(&[ValueType::Uint4]);
122
123 let timestamps = [
125 0u32, 946684800u32, 1640995200u32, 2147483647u32, ];
130
131 for timestamp in timestamps {
132 let mut row = shape.allocate();
133 shape.set_u32(&mut row, 0, timestamp);
134 assert_eq!(shape.get_u32(&row, 0), timestamp);
135 }
136 }
137
138 #[test]
139 fn test_mixed_with_other_types() {
140 let shape = RowShape::testing(&[ValueType::Uint4, ValueType::Float4, ValueType::Uint4]);
141 let mut row = shape.allocate();
142
143 shape.set_u32(&mut row, 0, 3_000_000_000u32);
144 shape.set_f32(&mut row, 1, 3.14f32);
145 shape.set_u32(&mut row, 2, 1_500_000_000u32);
146
147 assert_eq!(shape.get_u32(&row, 0), 3_000_000_000u32);
148 assert_eq!(shape.get_f32(&row, 1), 3.14f32);
149 assert_eq!(shape.get_u32(&row, 2), 1_500_000_000u32);
150 }
151
152 #[test]
153 fn test_undefined_handling() {
154 let shape = RowShape::testing(&[ValueType::Uint4, ValueType::Uint4]);
155 let mut row = shape.allocate();
156
157 shape.set_u32(&mut row, 0, 123456789u32);
158
159 assert_eq!(shape.try_get_u32(&row, 0), Some(123456789));
160 assert_eq!(shape.try_get_u32(&row, 1), None);
161
162 shape.set_none(&mut row, 0);
163 assert_eq!(shape.try_get_u32(&row, 0), None);
164 }
165
166 #[test]
167 fn test_try_get_u32_wrong_type() {
168 let shape = RowShape::testing(&[ValueType::Boolean]);
169 let mut row = shape.allocate();
170
171 shape.set_bool(&mut row, 0, true);
172
173 assert_eq!(shape.try_get_u32(&row, 0), None);
174 }
175}