Skip to main content

reifydb_core/encoded/
int.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::ptr;
5
6use num_bigint::BigInt as StdBigInt;
7use num_traits::ToPrimitive;
8use reifydb_type::value::{int::Int, r#type::Type};
9
10use crate::encoded::{encoded::EncodedValues, schema::Schema};
11
12/// Int storage modes using MSB of i128 as indicator
13/// MSB = 0: Value stored inline in lower 127 bits
14/// MSB = 1: Dynamic storage, lower 127 bits contain offset+length
15const MODE_INLINE: u128 = 0x00000000000000000000000000000000;
16const MODE_DYNAMIC: u128 = 0x80000000000000000000000000000000;
17const MODE_MASK: u128 = 0x80000000000000000000000000000000;
18
19/// Bit masks for inline mode (127 bits for value)
20const INLINE_VALUE_MASK: u128 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
21
22/// Bit masks for dynamic mode (lower 127 bits contain offset+length)
23const DYNAMIC_OFFSET_MASK: u128 = 0x0000000000000000FFFFFFFFFFFFFFFF; // 64 bits for offset
24const DYNAMIC_LENGTH_MASK: u128 = 0x7FFFFFFFFFFFFFFF0000000000000000; // 63 bits for length
25
26impl Schema {
27	/// Set a Int value with 2-tier storage optimization
28	/// - Values fitting in 127 bits: stored inline with MSB=0
29	/// - Large values: stored in dynamic section with MSB=1
30	pub fn set_int(&self, row: &mut EncodedValues, index: usize, value: &Int) {
31		let field = &self.fields()[index];
32		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int);
33
34		// Try i128 inline storage first (fits in 127 bits)
35		if let Some(i128_val) = value.0.to_i128() {
36			// Check if value fits in 127 bits (MSB must be 0)
37			if i128_val >= -(1i128 << 126) && i128_val < (1i128 << 126) {
38				// Mode 0: Store inline in lower 127 bits
39				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 usize) as *mut u128,
43						packed.to_le(),
44					);
45				}
46				row.set_valid(index, true);
47				return;
48			}
49		}
50
51		// Mode 1: Dynamic storage for arbitrary precision
52		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		// Append to dynamic section
58		row.0.extend_from_slice(&bytes);
59
60		// Pack offset and length in lower 127 bits, set MSB=1
61		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 usize) as *mut u128,
68				packed.to_le(),
69			);
70		}
71		row.set_valid(index, true);
72	}
73
74	/// Get a Int value, detecting storage mode from MSB
75	pub fn get_int(&self, row: &EncodedValues, index: usize) -> Int {
76		let field = &self.fields()[index];
77		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int);
78
79		let packed = unsafe { (row.as_ptr().add(field.offset as usize) 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			// Extract 127-bit value and sign-extend to i128
86			let value = (packed & INLINE_VALUE_MASK) as i128;
87			let signed = if value & (1i128 << 126) != 0 {
88				// Sign bit is set, extend with 1s
89				value | (1i128 << 127)
90			} else {
91				value
92			};
93			Int::from(signed)
94		} else {
95			// MODE_DYNAMIC: Extract offset and length for dynamic
96			// storage
97			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	/// Try to get a Int value, returning None if undefined
108	pub fn try_get_int(&self, row: &EncodedValues, index: usize) -> Option<Int> {
109		if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Int {
110			Some(self.get_int(row, index))
111		} else {
112			None
113		}
114	}
115}
116
117#[cfg(test)]
118pub mod tests {
119	use num_bigint::BigInt;
120	use reifydb_type::value::{int::Int, r#type::Type};
121
122	use crate::encoded::schema::Schema;
123
124	#[test]
125	fn test_i64_inline() {
126		let schema = Schema::testing(&[Type::Int]);
127		let mut row = schema.allocate();
128
129		// Test small positive value
130		let small = Int::from(42i64);
131		schema.set_int(&mut row, 0, &small);
132		assert!(row.is_defined(0));
133
134		let retrieved = schema.get_int(&row, 0);
135		assert_eq!(retrieved, small);
136
137		// Test small negative value
138		let mut row2 = schema.allocate();
139		let negative = Int::from(-999999i64);
140		schema.set_int(&mut row2, 0, &negative);
141		assert_eq!(schema.get_int(&row2, 0), negative);
142	}
143
144	#[test]
145	fn test_i128_boundary() {
146		let schema = Schema::testing(&[Type::Int]);
147		let mut row = schema.allocate();
148
149		// Value that doesn't fit in 62 bits but fits in i128
150		let large = Int::from(i64::MAX);
151		schema.set_int(&mut row, 0, &large);
152		assert!(row.is_defined(0));
153
154		let retrieved = schema.get_int(&row, 0);
155		assert_eq!(retrieved, large);
156
157		// Test i128::MAX
158		let mut row2 = schema.allocate();
159		let max_i128 = Int::from(i128::MAX);
160		schema.set_int(&mut row2, 0, &max_i128);
161		assert_eq!(schema.get_int(&row2, 0), max_i128);
162
163		// Test i128::MIN
164		let mut row3 = schema.allocate();
165		let min_i128 = Int::from(i128::MIN);
166		schema.set_int(&mut row3, 0, &min_i128);
167		assert_eq!(schema.get_int(&row3, 0), min_i128);
168	}
169
170	#[test]
171	fn test_dynamic_storage() {
172		let schema = Schema::testing(&[Type::Int]);
173		let mut row = schema.allocate();
174
175		// Create a value larger than i128 can hold
176		let huge_str = "999999999999999999999999999999999999999999999999";
177		let huge = Int::from(BigInt::parse_bytes(huge_str.as_bytes(), 10).unwrap());
178
179		schema.set_int(&mut row, 0, &huge);
180		assert!(row.is_defined(0));
181
182		let retrieved = schema.get_int(&row, 0);
183		assert_eq!(retrieved, huge);
184		assert_eq!(retrieved.to_string(), huge_str);
185	}
186
187	#[test]
188	fn test_zero() {
189		let schema = Schema::testing(&[Type::Int]);
190		let mut row = schema.allocate();
191
192		let zero = Int::from(0);
193		schema.set_int(&mut row, 0, &zero);
194		assert!(row.is_defined(0));
195
196		let retrieved = schema.get_int(&row, 0);
197		assert_eq!(retrieved, zero);
198	}
199
200	#[test]
201	fn test_try_get() {
202		let schema = Schema::testing(&[Type::Int]);
203		let mut row = schema.allocate();
204
205		// Undefined initially
206		assert_eq!(schema.try_get_int(&row, 0), None);
207
208		// Set value
209		let value = Int::from(12345);
210		schema.set_int(&mut row, 0, &value);
211		assert_eq!(schema.try_get_int(&row, 0), Some(value));
212	}
213
214	#[test]
215	fn test_clone_on_write() {
216		let schema = Schema::testing(&[Type::Int]);
217		let row1 = schema.allocate();
218		let mut row2 = row1.clone();
219
220		let value = Int::from(999999999999999i64);
221		schema.set_int(&mut row2, 0, &value);
222
223		assert!(!row1.is_defined(0));
224		assert!(row2.is_defined(0));
225		assert_ne!(row1.as_ptr(), row2.as_ptr());
226		assert_eq!(schema.get_int(&row2, 0), value);
227	}
228
229	#[test]
230	fn test_multiple_fields() {
231		let schema = Schema::testing(&[Type::Int4, Type::Int, Type::Utf8, Type::Int]);
232		let mut row = schema.allocate();
233
234		schema.set_i32(&mut row, 0, 42);
235
236		let small = Int::from(100);
237		schema.set_int(&mut row, 1, &small);
238
239		schema.set_utf8(&mut row, 2, "test");
240
241		let large = Int::from(i128::MAX);
242		schema.set_int(&mut row, 3, &large);
243
244		assert_eq!(schema.get_i32(&row, 0), 42);
245		assert_eq!(schema.get_int(&row, 1), small);
246		assert_eq!(schema.get_utf8(&row, 2), "test");
247		assert_eq!(schema.get_int(&row, 3), large);
248	}
249
250	#[test]
251	fn test_negative_values() {
252		let schema = Schema::testing(&[Type::Int]);
253
254		// Small negative (i64 inline)
255		let mut row1 = schema.allocate();
256		let small_neg = Int::from(-42);
257		schema.set_int(&mut row1, 0, &small_neg);
258		assert_eq!(schema.get_int(&row1, 0), small_neg);
259
260		// Large negative (i128 overflow)
261		let mut row2 = schema.allocate();
262		let large_neg = Int::from(i64::MIN);
263		schema.set_int(&mut row2, 0, &large_neg);
264		assert_eq!(schema.get_int(&row2, 0), large_neg);
265
266		// Huge negative (dynamic)
267		let mut row3 = schema.allocate();
268		let huge_neg_str = "-999999999999999999999999999999999999999999999999";
269		let huge_neg =
270			Int::from(-BigInt::parse_bytes(huge_neg_str.trim_start_matches('-').as_bytes(), 10).unwrap());
271		schema.set_int(&mut row3, 0, &huge_neg);
272		assert_eq!(schema.get_int(&row3, 0), huge_neg);
273	}
274
275	#[test]
276	fn test_try_get_int_wrong_type() {
277		let schema = Schema::testing(&[Type::Boolean]);
278		let mut row = schema.allocate();
279
280		schema.set_bool(&mut row, 0, true);
281
282		assert_eq!(schema.try_get_int(&row, 0), None);
283	}
284}