reifydb_core/encoded/
i32.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_i32(&self, row: &mut EncodedValues, index: usize, value: impl Into<i32>) {
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::Int4);
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 i32,
19 value.into(),
20 )
21 }
22 }
23
24 pub fn get_i32(&self, row: &EncodedValues, index: usize) -> i32 {
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::Int4);
28 unsafe { (row.as_ptr().add(field.offset as usize) as *const i32).read_unaligned() }
29 }
30
31 pub fn try_get_i32(&self, row: &EncodedValues, index: usize) -> Option<i32> {
32 if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Int4 {
33 Some(self.get_i32(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_i32() {
48 let schema = Schema::testing(&[Type::Int4]);
49 let mut row = schema.allocate();
50 schema.set_i32(&mut row, 0, 56789i32);
51 assert_eq!(schema.get_i32(&row, 0), 56789i32);
52 }
53
54 #[test]
55 fn test_try_get_i32() {
56 let schema = Schema::testing(&[Type::Int4]);
57 let mut row = schema.allocate();
58
59 assert_eq!(schema.try_get_i32(&row, 0), None);
60
61 schema.set_i32(&mut row, 0, 56789i32);
62 assert_eq!(schema.try_get_i32(&row, 0), Some(56789i32));
63 }
64
65 #[test]
66 fn test_extremes() {
67 let schema = Schema::testing(&[Type::Int4]);
68 let mut row = schema.allocate();
69
70 schema.set_i32(&mut row, 0, i32::MAX);
71 assert_eq!(schema.get_i32(&row, 0), i32::MAX);
72
73 let mut row2 = schema.allocate();
74 schema.set_i32(&mut row2, 0, i32::MIN);
75 assert_eq!(schema.get_i32(&row2, 0), i32::MIN);
76
77 let mut row3 = schema.allocate();
78 schema.set_i32(&mut row3, 0, 0i32);
79 assert_eq!(schema.get_i32(&row3, 0), 0i32);
80 }
81
82 #[test]
83 fn test_large_values() {
84 let schema = Schema::testing(&[Type::Int4]);
85
86 let test_values =
87 [-2_147_483_648i32, -1_000_000_000i32, -1i32, 0i32, 1i32, 1_000_000_000i32, 2_147_483_647i32];
88
89 for value in test_values {
90 let mut row = schema.allocate();
91 schema.set_i32(&mut row, 0, value);
92 assert_eq!(schema.get_i32(&row, 0), value);
93 }
94 }
95
96 #[test]
97 fn test_mixed_with_other_types() {
98 let schema = Schema::testing(&[Type::Int4, Type::Boolean, Type::Int4, Type::Float4]);
99 let mut row = schema.allocate();
100
101 schema.set_i32(&mut row, 0, -1_000_000i32);
102 schema.set_bool(&mut row, 1, true);
103 schema.set_i32(&mut row, 2, 2_000_000i32);
104 schema.set_f32(&mut row, 3, 3.14f32);
105
106 assert_eq!(schema.get_i32(&row, 0), -1_000_000i32);
107 assert_eq!(schema.get_bool(&row, 1), true);
108 assert_eq!(schema.get_i32(&row, 2), 2_000_000i32);
109 assert_eq!(schema.get_f32(&row, 3), 3.14f32);
110 }
111
112 #[test]
113 fn test_undefined_handling() {
114 let schema = Schema::testing(&[Type::Int4, Type::Int4]);
115 let mut row = schema.allocate();
116
117 schema.set_i32(&mut row, 0, 12345);
118
119 assert_eq!(schema.try_get_i32(&row, 0), Some(12345));
120 assert_eq!(schema.try_get_i32(&row, 1), None);
121
122 schema.set_none(&mut row, 0);
123 assert_eq!(schema.try_get_i32(&row, 0), None);
124 }
125
126 #[test]
127 fn test_try_get_i32_wrong_type() {
128 let schema = Schema::testing(&[Type::Boolean]);
129 let mut row = schema.allocate();
130
131 schema.set_bool(&mut row, 0, true);
132
133 assert_eq!(schema.try_get_i32(&row, 0), None);
134 }
135}