reifydb_core/value/encoded/
uint.rs1use std::ptr;
5
6use num_bigint::BigUint;
7use num_traits::ToPrimitive;
8use reifydb_type::{Type, Uint};
9
10use crate::value::encoded::{EncodedValues, EncodedValuesLayout};
11
12const MODE_INLINE: u128 = 0x00000000000000000000000000000000;
16const MODE_DYNAMIC: u128 = 0x80000000000000000000000000000000;
17const MODE_MASK: u128 = 0x80000000000000000000000000000000;
18
19const INLINE_VALUE_MASK: u128 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
21
22const DYNAMIC_OFFSET_MASK: u128 = 0x0000000000000000FFFFFFFFFFFFFFFF; const DYNAMIC_LENGTH_MASK: u128 = 0x7FFFFFFFFFFFFFFF0000000000000000; impl EncodedValuesLayout {
27 pub fn set_uint(&self, row: &mut EncodedValues, index: usize, value: &Uint) {
31 let field = &self.fields[index];
32 debug_assert_eq!(field.r#type, Type::Uint);
33
34 let unsigned_value = value.0.to_biguint().unwrap_or(BigUint::from(0u32));
36
37 if let Some(u128_val) = unsigned_value.to_u128() {
39 if u128_val < (1u128 << 127) {
41 let packed = MODE_INLINE | (u128_val & INLINE_VALUE_MASK);
43 unsafe {
44 ptr::write_unaligned(
45 row.make_mut().as_mut_ptr().add(field.offset) as *mut u128,
46 packed.to_le(),
47 );
48 }
49 row.set_valid(index, true);
50 return;
51 }
52 }
53
54 debug_assert!(!row.is_defined(index), "Uint field {} already set", index);
56
57 let bytes = unsigned_value.to_bytes_le();
59
60 let dynamic_offset = self.dynamic_section_size(row);
61 let total_size = bytes.len();
62
63 row.0.extend_from_slice(&bytes);
65
66 let offset_part = (dynamic_offset as u128) & DYNAMIC_OFFSET_MASK;
68 let length_part = ((total_size as u128) << 64) & DYNAMIC_LENGTH_MASK;
69 let packed = MODE_DYNAMIC | offset_part | length_part;
70
71 unsafe {
72 ptr::write_unaligned(
73 row.make_mut().as_mut_ptr().add(field.offset) as *mut u128,
74 packed.to_le(),
75 );
76 }
77 row.set_valid(index, true);
78 }
79
80 pub fn get_uint(&self, row: &EncodedValues, index: usize) -> Uint {
82 let field = &self.fields[index];
83 debug_assert_eq!(field.r#type, Type::Uint);
84
85 let packed = unsafe { (row.as_ptr().add(field.offset) as *const u128).read_unaligned() };
86 let packed = u128::from_le(packed);
87
88 let mode = packed & MODE_MASK;
89
90 if mode == MODE_INLINE {
91 let value = packed & INLINE_VALUE_MASK;
93 let unsigned = BigUint::from(value);
95 Uint::from(num_bigint::BigInt::from(unsigned))
96 } else {
97 let offset = (packed & DYNAMIC_OFFSET_MASK) as usize;
100 let length = ((packed & DYNAMIC_LENGTH_MASK) >> 64) as usize;
101
102 let dynamic_start = self.dynamic_section_start();
103 let data_bytes = &row.as_slice()[dynamic_start + offset..dynamic_start + offset + length];
104
105 let unsigned = BigUint::from_bytes_le(data_bytes);
107 Uint::from(num_bigint::BigInt::from(unsigned))
108 }
109 }
110
111 pub fn try_get_uint(&self, row: &EncodedValues, index: usize) -> Option<Uint> {
113 if row.is_defined(index) {
114 Some(self.get_uint(row, index))
115 } else {
116 None
117 }
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use num_traits::Zero;
124 use reifydb_type::{Type, Uint};
125
126 use crate::value::encoded::EncodedValuesLayout;
127
128 #[test]
129 fn test_u64_inline() {
130 let layout = EncodedValuesLayout::new(&[Type::Uint]);
131 let mut row = layout.allocate();
132
133 let small = Uint::from(42u64);
135 layout.set_uint(&mut row, 0, &small);
136 assert!(row.is_defined(0));
137
138 let retrieved = layout.get_uint(&row, 0);
139 assert_eq!(retrieved, small);
140
141 let mut row2 = layout.allocate();
143 let large = Uint::from(999999999999u64);
144 layout.set_uint(&mut row2, 0, &large);
145 assert_eq!(layout.get_uint(&row2, 0), large);
146 }
147
148 #[test]
149 fn test_u128_boundary() {
150 let layout = EncodedValuesLayout::new(&[Type::Uint]);
151 let mut row = layout.allocate();
152
153 let large = Uint::from(u64::MAX);
155 layout.set_uint(&mut row, 0, &large);
156 assert!(row.is_defined(0));
157
158 let retrieved = layout.get_uint(&row, 0);
159 assert_eq!(retrieved, large);
160
161 let mut row2 = layout.allocate();
163 let max_u127 = Uint::from(u128::MAX >> 1); layout.set_uint(&mut row2, 0, &max_u127);
165 assert_eq!(layout.get_uint(&row2, 0), max_u127);
166 }
167
168 #[test]
169 fn test_dynamic_storage() {
170 let layout = EncodedValuesLayout::new(&[Type::Uint]);
171 let mut row = layout.allocate();
172
173 let huge = Uint::from(
176 num_bigint::BigInt::parse_bytes(
177 b"123456789012345678901234567890123456789012345678901234567890",
178 10,
179 )
180 .unwrap(),
181 );
182
183 layout.set_uint(&mut row, 0, &huge);
184 assert!(row.is_defined(0));
185
186 let retrieved = layout.get_uint(&row, 0);
187 assert_eq!(retrieved, huge);
188 }
189
190 #[test]
191 fn test_zero() {
192 let layout = EncodedValuesLayout::new(&[Type::Uint]);
193 let mut row = layout.allocate();
194
195 let zero = Uint::from(0);
196 layout.set_uint(&mut row, 0, &zero);
197 assert!(row.is_defined(0));
198
199 let retrieved = layout.get_uint(&row, 0);
200 assert!(retrieved.is_zero());
201 }
202
203 #[test]
204 fn test_try_get() {
205 let layout = EncodedValuesLayout::new(&[Type::Uint]);
206 let mut row = layout.allocate();
207
208 assert_eq!(layout.try_get_uint(&row, 0), None);
210
211 let value = Uint::from(12345u64);
213 layout.set_uint(&mut row, 0, &value);
214 assert_eq!(layout.try_get_uint(&row, 0), Some(value));
215 }
216
217 #[test]
218 fn test_clone_on_write() {
219 let layout = EncodedValuesLayout::new(&[Type::Uint]);
220 let row1 = layout.allocate();
221 let mut row2 = row1.clone();
222
223 let value = Uint::from(999999999999999u64);
224 layout.set_uint(&mut row2, 0, &value);
225
226 assert!(!row1.is_defined(0));
227 assert!(row2.is_defined(0));
228 assert_ne!(row1.as_ptr(), row2.as_ptr());
229 assert_eq!(layout.get_uint(&row2, 0), value);
230 }
231
232 #[test]
233 fn test_multiple_fields() {
234 let layout = EncodedValuesLayout::new(&[Type::Boolean, Type::Uint, Type::Utf8, Type::Uint, Type::Int4]);
235 let mut row = layout.allocate();
236
237 layout.set_bool(&mut row, 0, true);
238
239 let small = Uint::from(100u64);
240 layout.set_uint(&mut row, 1, &small);
241
242 layout.set_utf8(&mut row, 2, "test");
243
244 let large = Uint::from(u128::MAX >> 1);
245 layout.set_uint(&mut row, 3, &large);
246
247 layout.set_i32(&mut row, 4, 42);
248
249 assert_eq!(layout.get_bool(&row, 0), true);
250 assert_eq!(layout.get_uint(&row, 1), small);
251 assert_eq!(layout.get_utf8(&row, 2), "test");
252 assert_eq!(layout.get_uint(&row, 3), large);
253 assert_eq!(layout.get_i32(&row, 4), 42);
254 }
255
256 #[test]
257 fn test_negative_input_handling() {
258 let layout = EncodedValuesLayout::new(&[Type::Uint]);
259
260 let mut row1 = layout.allocate();
263 let negative = Uint::from(-42); layout.set_uint(&mut row1, 0, &negative);
265
266 let retrieved = layout.get_uint(&row1, 0);
268 assert_eq!(retrieved, Uint::from(0));
269 }
270}