Skip to main content

reifydb_codec/encoded/
i64.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use 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_i64(&self, row: &mut EncodedRow, index: usize, value: impl Into<i64>) {
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::Int8);
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 i64,
26				value.into(),
27			)
28		}
29	}
30
31	pub fn get_i64(&self, row: &EncodedRow, index: usize) -> i64 {
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::Int8);
41		}
42		unsafe { (row.as_ptr().add(field.offset as usize) as *const i64).read_unaligned() }
43	}
44
45	pub fn try_get_i64(&self, row: &EncodedRow, index: usize) -> Option<i64> {
46		if row.is_defined(index) && self.fields()[index].constraint.get_type() == ValueType::Int8 {
47			Some(self.get_i64(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_i64() {
62		let shape = RowShape::testing(&[ValueType::Int8]);
63		let mut row = shape.allocate();
64		shape.set_i64(&mut row, 0, -987654321i64);
65		assert_eq!(shape.get_i64(&row, 0), -987654321i64);
66	}
67
68	#[test]
69	fn test_try_get_i64() {
70		let shape = RowShape::testing(&[ValueType::Int8]);
71		let mut row = shape.allocate();
72
73		assert_eq!(shape.try_get_i64(&row, 0), None);
74
75		shape.set_i64(&mut row, 0, -987654321i64);
76		assert_eq!(shape.try_get_i64(&row, 0), Some(-987654321i64));
77	}
78
79	#[test]
80	fn test_extremes() {
81		let shape = RowShape::testing(&[ValueType::Int8]);
82		let mut row = shape.allocate();
83
84		shape.set_i64(&mut row, 0, i64::MAX);
85		assert_eq!(shape.get_i64(&row, 0), i64::MAX);
86
87		let mut row2 = shape.allocate();
88		shape.set_i64(&mut row2, 0, i64::MIN);
89		assert_eq!(shape.get_i64(&row2, 0), i64::MIN);
90
91		let mut row3 = shape.allocate();
92		shape.set_i64(&mut row3, 0, 0i64);
93		assert_eq!(shape.get_i64(&row3, 0), 0i64);
94	}
95
96	#[test]
97	fn test_large_values() {
98		let shape = RowShape::testing(&[ValueType::Int8]);
99
100		let test_values = [
101			-9_223_372_036_854_775_808i64,
102			-1_000_000_000_000_000_000i64,
103			-1i64,
104			0i64,
105			1i64,
106			1_000_000_000_000_000_000i64,
107			9_223_372_036_854_775_807i64,
108		];
109
110		for value in test_values {
111			let mut row = shape.allocate();
112			shape.set_i64(&mut row, 0, value);
113			assert_eq!(shape.get_i64(&row, 0), value);
114		}
115	}
116
117	#[test]
118	fn test_timestamp_values() {
119		let shape = RowShape::testing(&[ValueType::Int8]);
120
121		// Test typical Unix timestamp values
122		let timestamps = [
123			0i64,           // Unix epoch
124			1640995200i64,  // 2022-01-01 00:00:00 SVTC
125			1735689600i64,  // 2025-01-01 00:00:00 SVTC
126			-2147483648i64, // Before Unix epoch
127		];
128
129		for timestamp in timestamps {
130			let mut row = shape.allocate();
131			shape.set_i64(&mut row, 0, timestamp);
132			assert_eq!(shape.get_i64(&row, 0), timestamp);
133		}
134	}
135
136	#[test]
137	fn test_mixed_with_other_types() {
138		let shape = RowShape::testing(&[ValueType::Int8, ValueType::Float8, ValueType::Int8]);
139		let mut row = shape.allocate();
140
141		shape.set_i64(&mut row, 0, -9_000_000_000_000_000i64);
142		shape.set_f64(&mut row, 1, 3.14159265359);
143		shape.set_i64(&mut row, 2, 8_000_000_000_000_000i64);
144
145		assert_eq!(shape.get_i64(&row, 0), -9_000_000_000_000_000i64);
146		assert_eq!(shape.get_f64(&row, 1), 3.14159265359);
147		assert_eq!(shape.get_i64(&row, 2), 8_000_000_000_000_000i64);
148	}
149
150	#[test]
151	fn test_undefined_handling() {
152		let shape = RowShape::testing(&[ValueType::Int8, ValueType::Int8]);
153		let mut row = shape.allocate();
154
155		shape.set_i64(&mut row, 0, 1234567890123456789i64);
156
157		assert_eq!(shape.try_get_i64(&row, 0), Some(1234567890123456789));
158		assert_eq!(shape.try_get_i64(&row, 1), None);
159
160		shape.set_none(&mut row, 0);
161		assert_eq!(shape.try_get_i64(&row, 0), None);
162	}
163
164	#[test]
165	fn test_try_get_i64_wrong_type() {
166		let shape = RowShape::testing(&[ValueType::Boolean]);
167		let mut row = shape.allocate();
168
169		shape.set_bool(&mut row, 0, true);
170
171		assert_eq!(shape.try_get_i64(&row, 0), None);
172	}
173}