reifydb_core/value/encoded/
int.rs1use std::ptr;
5
6use num_bigint::BigInt as StdBigInt;
7use num_traits::ToPrimitive;
8use reifydb_type::{Int, Type};
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_int(&self, row: &mut EncodedValues, index: usize, value: &Int) {
31 let field = &self.fields[index];
32 debug_assert_eq!(field.r#type, Type::Int);
33
34 if let Some(i128_val) = value.0.to_i128() {
36 if i128_val >= -(1i128 << 126) && i128_val < (1i128 << 126) {
38 let packed = MODE_INLINE | ((i128_val as u128) & INLINE_VALUE_MASK);
40 unsafe {
41 ptr::write_unaligned(
42 row.make_mut().as_mut_ptr().add(field.offset) as *mut u128,
43 packed.to_le(),
44 );
45 }
46 row.set_valid(index, true);
47 return;
48 }
49 }
50
51 debug_assert!(!row.is_defined(index), "Int field {} already set", index);
53
54 let bytes = value.0.to_signed_bytes_le();
55 let dynamic_offset = self.dynamic_section_size(row);
56
57 row.0.extend_from_slice(&bytes);
59
60 let offset_part = (dynamic_offset as u128) & DYNAMIC_OFFSET_MASK;
62 let length_part = ((bytes.len() as u128) << 64) & DYNAMIC_LENGTH_MASK;
63 let packed = MODE_DYNAMIC | offset_part | length_part;
64
65 unsafe {
66 ptr::write_unaligned(
67 row.make_mut().as_mut_ptr().add(field.offset) as *mut u128,
68 packed.to_le(),
69 );
70 }
71 row.set_valid(index, true);
72 }
73
74 pub fn get_int(&self, row: &EncodedValues, index: usize) -> Int {
76 let field = &self.fields[index];
77 debug_assert_eq!(field.r#type, Type::Int);
78
79 let packed = unsafe { (row.as_ptr().add(field.offset) as *const u128).read_unaligned() };
80 let packed = u128::from_le(packed);
81
82 let mode = packed & MODE_MASK;
83
84 if mode == MODE_INLINE {
85 let value = (packed & INLINE_VALUE_MASK) as i128;
87 let signed = if value & (1i128 << 126) != 0 {
88 value | (1i128 << 127)
90 } else {
91 value
92 };
93 Int::from(signed)
94 } else {
95 let offset = (packed & DYNAMIC_OFFSET_MASK) as usize;
98 let length = ((packed & DYNAMIC_LENGTH_MASK) >> 64) as usize;
99
100 let dynamic_start = self.dynamic_section_start();
101 let bigint_bytes = &row.as_slice()[dynamic_start + offset..dynamic_start + offset + length];
102
103 Int::from(StdBigInt::from_signed_bytes_le(bigint_bytes))
104 }
105 }
106
107 pub fn try_get_int(&self, row: &EncodedValues, index: usize) -> Option<Int> {
109 if row.is_defined(index) && self.fields[index].r#type == Type::Int {
110 Some(self.get_int(row, index))
111 } else {
112 None
113 }
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use reifydb_type::{Int, Type};
120
121 use crate::value::encoded::EncodedValuesLayout;
122
123 #[test]
124 fn test_i64_inline() {
125 let layout = EncodedValuesLayout::new(&[Type::Int]);
126 let mut row = layout.allocate();
127
128 let small = Int::from(42i64);
130 layout.set_int(&mut row, 0, &small);
131 assert!(row.is_defined(0));
132
133 let retrieved = layout.get_int(&row, 0);
134 assert_eq!(retrieved, small);
135
136 let mut row2 = layout.allocate();
138 let negative = Int::from(-999999i64);
139 layout.set_int(&mut row2, 0, &negative);
140 assert_eq!(layout.get_int(&row2, 0), negative);
141 }
142
143 #[test]
144 fn test_i128_boundary() {
145 let layout = EncodedValuesLayout::new(&[Type::Int]);
146 let mut row = layout.allocate();
147
148 let large = Int::from(i64::MAX);
150 layout.set_int(&mut row, 0, &large);
151 assert!(row.is_defined(0));
152
153 let retrieved = layout.get_int(&row, 0);
154 assert_eq!(retrieved, large);
155
156 let mut row2 = layout.allocate();
158 let max_i128 = Int::from(i128::MAX);
159 layout.set_int(&mut row2, 0, &max_i128);
160 assert_eq!(layout.get_int(&row2, 0), max_i128);
161
162 let mut row3 = layout.allocate();
164 let min_i128 = Int::from(i128::MIN);
165 layout.set_int(&mut row3, 0, &min_i128);
166 assert_eq!(layout.get_int(&row3, 0), min_i128);
167 }
168
169 #[test]
170 fn test_dynamic_storage() {
171 let layout = EncodedValuesLayout::new(&[Type::Int]);
172 let mut row = layout.allocate();
173
174 let huge_str = "999999999999999999999999999999999999999999999999";
176 let huge = Int::from(num_bigint::BigInt::parse_bytes(huge_str.as_bytes(), 10).unwrap());
177
178 layout.set_int(&mut row, 0, &huge);
179 assert!(row.is_defined(0));
180
181 let retrieved = layout.get_int(&row, 0);
182 assert_eq!(retrieved, huge);
183 assert_eq!(retrieved.to_string(), huge_str);
184 }
185
186 #[test]
187 fn test_zero() {
188 let layout = EncodedValuesLayout::new(&[Type::Int]);
189 let mut row = layout.allocate();
190
191 let zero = Int::from(0);
192 layout.set_int(&mut row, 0, &zero);
193 assert!(row.is_defined(0));
194
195 let retrieved = layout.get_int(&row, 0);
196 assert_eq!(retrieved, zero);
197 }
198
199 #[test]
200 fn test_try_get() {
201 let layout = EncodedValuesLayout::new(&[Type::Int]);
202 let mut row = layout.allocate();
203
204 assert_eq!(layout.try_get_int(&row, 0), None);
206
207 let value = Int::from(12345);
209 layout.set_int(&mut row, 0, &value);
210 assert_eq!(layout.try_get_int(&row, 0), Some(value));
211 }
212
213 #[test]
214 fn test_clone_on_write() {
215 let layout = EncodedValuesLayout::new(&[Type::Int]);
216 let row1 = layout.allocate();
217 let mut row2 = row1.clone();
218
219 let value = Int::from(999999999999999i64);
220 layout.set_int(&mut row2, 0, &value);
221
222 assert!(!row1.is_defined(0));
223 assert!(row2.is_defined(0));
224 assert_ne!(row1.as_ptr(), row2.as_ptr());
225 assert_eq!(layout.get_int(&row2, 0), value);
226 }
227
228 #[test]
229 fn test_multiple_fields() {
230 let layout = EncodedValuesLayout::new(&[Type::Int4, Type::Int, Type::Utf8, Type::Int]);
231 let mut row = layout.allocate();
232
233 layout.set_i32(&mut row, 0, 42);
234
235 let small = Int::from(100);
236 layout.set_int(&mut row, 1, &small);
237
238 layout.set_utf8(&mut row, 2, "test");
239
240 let large = Int::from(i128::MAX);
241 layout.set_int(&mut row, 3, &large);
242
243 assert_eq!(layout.get_i32(&row, 0), 42);
244 assert_eq!(layout.get_int(&row, 1), small);
245 assert_eq!(layout.get_utf8(&row, 2), "test");
246 assert_eq!(layout.get_int(&row, 3), large);
247 }
248
249 #[test]
250 fn test_negative_values() {
251 let layout = EncodedValuesLayout::new(&[Type::Int]);
252
253 let mut row1 = layout.allocate();
255 let small_neg = Int::from(-42);
256 layout.set_int(&mut row1, 0, &small_neg);
257 assert_eq!(layout.get_int(&row1, 0), small_neg);
258
259 let mut row2 = layout.allocate();
261 let large_neg = Int::from(i64::MIN);
262 layout.set_int(&mut row2, 0, &large_neg);
263 assert_eq!(layout.get_int(&row2, 0), large_neg);
264
265 let mut row3 = layout.allocate();
267 let huge_neg_str = "-999999999999999999999999999999999999999999999999";
268 let huge_neg = Int::from(
269 -num_bigint::BigInt::parse_bytes(huge_neg_str.trim_start_matches('-').as_bytes(), 10).unwrap(),
270 );
271 layout.set_int(&mut row3, 0, &huge_neg);
272 assert_eq!(layout.get_int(&row3, 0), huge_neg);
273 }
274
275 #[test]
276 fn test_try_get_int_wrong_type() {
277 let layout = EncodedValuesLayout::new(&[Type::Boolean]);
278 let mut row = layout.allocate();
279
280 layout.set_bool(&mut row, 0, true);
281
282 assert_eq!(layout.try_get_int(&row, 0), None);
283 }
284}