1use 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_i128(&self, row: &mut EncodedValues, index: usize, value: impl Into<i128>) {
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::Int16);
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 i128,
19 value.into(),
20 )
21 }
22 }
23
24 pub fn get_i128(&self, row: &EncodedValues, index: usize) -> i128 {
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::Int16);
28 unsafe { (row.as_ptr().add(field.offset as usize) as *const i128).read_unaligned() }
29 }
30
31 pub fn try_get_i128(&self, row: &EncodedValues, index: usize) -> Option<i128> {
32 if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Int16 {
33 Some(self.get_i128(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_i128() {
48 let schema = Schema::testing(&[Type::Int16]);
49 let mut row = schema.allocate();
50 schema.set_i128(&mut row, 0, 123456789012345678901234567890i128);
51 assert_eq!(schema.get_i128(&row, 0), 123456789012345678901234567890i128);
52 }
53
54 #[test]
55 fn test_try_get_i128() {
56 let schema = Schema::testing(&[Type::Int16]);
57 let mut row = schema.allocate();
58
59 assert_eq!(schema.try_get_i128(&row, 0), None);
60
61 schema.set_i128(&mut row, 0, 123456789012345678901234567890i128);
62 assert_eq!(schema.try_get_i128(&row, 0), Some(123456789012345678901234567890i128));
63 }
64
65 #[test]
66 fn test_extremes() {
67 let schema = Schema::testing(&[Type::Int16]);
68 let mut row = schema.allocate();
69
70 schema.set_i128(&mut row, 0, i128::MAX);
71 assert_eq!(schema.get_i128(&row, 0), i128::MAX);
72
73 let mut row2 = schema.allocate();
74 schema.set_i128(&mut row2, 0, i128::MIN);
75 assert_eq!(schema.get_i128(&row2, 0), i128::MIN);
76
77 let mut row3 = schema.allocate();
78 schema.set_i128(&mut row3, 0, 0i128);
79 assert_eq!(schema.get_i128(&row3, 0), 0i128);
80 }
81
82 #[test]
83 fn test_very_large_values() {
84 let schema = Schema::testing(&[Type::Int16]);
85
86 let test_values = [
87 -170141183460469231731687303715884105728i128, -99999999999999999999999999999999999999i128,
89 -1i128,
90 0i128,
91 1i128,
92 99999999999999999999999999999999999999i128,
93 170141183460469231731687303715884105727i128, ];
95
96 for value in test_values {
97 let mut row = schema.allocate();
98 schema.set_i128(&mut row, 0, value);
99 assert_eq!(schema.get_i128(&row, 0), value);
100 }
101 }
102
103 #[test]
104 fn test_powers_of_ten() {
105 let schema = Schema::testing(&[Type::Int16]);
106
107 let powers = [
108 1i128,
109 10i128,
110 100i128,
111 1_000i128,
112 10_000i128,
113 100_000i128,
114 1_000_000i128,
115 10_000_000i128,
116 100_000_000i128,
117 1_000_000_000i128,
118 10_000_000_000i128,
119 100_000_000_000i128,
120 ];
121
122 for power in powers {
123 let mut row = schema.allocate();
124 schema.set_i128(&mut row, 0, power);
125 assert_eq!(schema.get_i128(&row, 0), power);
126
127 let mut row2 = schema.allocate();
128 schema.set_i128(&mut row2, 0, -power);
129 assert_eq!(schema.get_i128(&row2, 0), -power);
130 }
131 }
132
133 #[test]
134 fn test_mixed_with_other_types() {
135 let schema = Schema::testing(&[Type::Int16, Type::Boolean, Type::Int16]);
136 let mut row = schema.allocate();
137
138 let large_negative = -12345678901234567890123456789012345i128;
139 let large_positive = 98765432109876543210987654321098765i128;
140
141 schema.set_i128(&mut row, 0, large_negative);
142 schema.set_bool(&mut row, 1, true);
143 schema.set_i128(&mut row, 2, large_positive);
144
145 assert_eq!(schema.get_i128(&row, 0), large_negative);
146 assert_eq!(schema.get_bool(&row, 1), true);
147 assert_eq!(schema.get_i128(&row, 2), large_positive);
148 }
149
150 #[test]
151 fn test_undefined_handling() {
152 let schema = Schema::testing(&[Type::Int16, Type::Int16]);
153 let mut row = schema.allocate();
154
155 let value = 170141183460469231731687303715884105727i128; schema.set_i128(&mut row, 0, value);
157
158 assert_eq!(schema.try_get_i128(&row, 0), Some(value));
159 assert_eq!(schema.try_get_i128(&row, 1), None);
160
161 schema.set_undefined(&mut row, 0);
162 assert_eq!(schema.try_get_i128(&row, 0), None);
163 }
164
165 #[test]
166 fn test_try_get_i128_wrong_type() {
167 let schema = Schema::testing(&[Type::Boolean]);
168 let mut row = schema.allocate();
169
170 schema.set_bool(&mut row, 0, true);
171
172 assert_eq!(schema.try_get_i128(&row, 0), None);
173 }
174}