reifydb_core/encoded/
i64.rs1use std::ptr;
5
6use reifydb_type::value::r#type::Type;
7
8use crate::encoded::{encoded::EncodedValues, schema::Schema};
9
10impl Schema {
11 pub fn set_i64(&self, row: &mut EncodedValues, index: usize, value: impl Into<i64>) {
12 let field = &self.fields()[index];
13 debug_assert!(row.len() >= self.total_static_size());
14 debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int8);
15 row.set_valid(index, true);
16 unsafe {
17 ptr::write_unaligned(
18 row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut i64,
19 value.into(),
20 )
21 }
22 }
23
24 pub fn get_i64(&self, row: &EncodedValues, index: usize) -> i64 {
25 let field = &self.fields()[index];
26 debug_assert!(row.len() >= self.total_static_size());
27 debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int8);
28 unsafe { (row.as_ptr().add(field.offset as usize) as *const i64).read_unaligned() }
29 }
30
31 pub fn try_get_i64(&self, row: &EncodedValues, index: usize) -> Option<i64> {
32 if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Int8 {
33 Some(self.get_i64(row, index))
34 } else {
35 None
36 }
37 }
38}
39
40#[cfg(test)]
41pub mod tests {
42 use reifydb_type::value::r#type::Type;
43
44 use crate::encoded::schema::Schema;
45
46 #[test]
47 fn test_set_get_i64() {
48 let schema = Schema::testing(&[Type::Int8]);
49 let mut row = schema.allocate();
50 schema.set_i64(&mut row, 0, -987654321i64);
51 assert_eq!(schema.get_i64(&row, 0), -987654321i64);
52 }
53
54 #[test]
55 fn test_try_get_i64() {
56 let schema = Schema::testing(&[Type::Int8]);
57 let mut row = schema.allocate();
58
59 assert_eq!(schema.try_get_i64(&row, 0), None);
60
61 schema.set_i64(&mut row, 0, -987654321i64);
62 assert_eq!(schema.try_get_i64(&row, 0), Some(-987654321i64));
63 }
64
65 #[test]
66 fn test_extremes() {
67 let schema = Schema::testing(&[Type::Int8]);
68 let mut row = schema.allocate();
69
70 schema.set_i64(&mut row, 0, i64::MAX);
71 assert_eq!(schema.get_i64(&row, 0), i64::MAX);
72
73 let mut row2 = schema.allocate();
74 schema.set_i64(&mut row2, 0, i64::MIN);
75 assert_eq!(schema.get_i64(&row2, 0), i64::MIN);
76
77 let mut row3 = schema.allocate();
78 schema.set_i64(&mut row3, 0, 0i64);
79 assert_eq!(schema.get_i64(&row3, 0), 0i64);
80 }
81
82 #[test]
83 fn test_large_values() {
84 let schema = Schema::testing(&[Type::Int8]);
85
86 let test_values = [
87 -9_223_372_036_854_775_808i64,
88 -1_000_000_000_000_000_000i64,
89 -1i64,
90 0i64,
91 1i64,
92 1_000_000_000_000_000_000i64,
93 9_223_372_036_854_775_807i64,
94 ];
95
96 for value in test_values {
97 let mut row = schema.allocate();
98 schema.set_i64(&mut row, 0, value);
99 assert_eq!(schema.get_i64(&row, 0), value);
100 }
101 }
102
103 #[test]
104 fn test_timestamp_values() {
105 let schema = Schema::testing(&[Type::Int8]);
106
107 let timestamps = [
109 0i64, 1640995200i64, 1735689600i64, -2147483648i64, ];
114
115 for timestamp in timestamps {
116 let mut row = schema.allocate();
117 schema.set_i64(&mut row, 0, timestamp);
118 assert_eq!(schema.get_i64(&row, 0), timestamp);
119 }
120 }
121
122 #[test]
123 fn test_mixed_with_other_types() {
124 let schema = Schema::testing(&[Type::Int8, Type::Float8, Type::Int8]);
125 let mut row = schema.allocate();
126
127 schema.set_i64(&mut row, 0, -9_000_000_000_000_000i64);
128 schema.set_f64(&mut row, 1, 3.14159265359);
129 schema.set_i64(&mut row, 2, 8_000_000_000_000_000i64);
130
131 assert_eq!(schema.get_i64(&row, 0), -9_000_000_000_000_000i64);
132 assert_eq!(schema.get_f64(&row, 1), 3.14159265359);
133 assert_eq!(schema.get_i64(&row, 2), 8_000_000_000_000_000i64);
134 }
135
136 #[test]
137 fn test_undefined_handling() {
138 let schema = Schema::testing(&[Type::Int8, Type::Int8]);
139 let mut row = schema.allocate();
140
141 schema.set_i64(&mut row, 0, 1234567890123456789i64);
142
143 assert_eq!(schema.try_get_i64(&row, 0), Some(1234567890123456789));
144 assert_eq!(schema.try_get_i64(&row, 1), None);
145
146 schema.set_none(&mut row, 0);
147 assert_eq!(schema.try_get_i64(&row, 0), None);
148 }
149
150 #[test]
151 fn test_try_get_i64_wrong_type() {
152 let schema = Schema::testing(&[Type::Boolean]);
153 let mut row = schema.allocate();
154
155 schema.set_bool(&mut row, 0, true);
156
157 assert_eq!(schema.try_get_i64(&row, 0), None);
158 }
159}