reifydb_core/encoded/
dictionary_id.rs1use std::ptr;
5
6use reifydb_type::value::{constraint::Constraint, dictionary::DictionaryEntryId, r#type::Type};
7
8use crate::encoded::{encoded::EncodedValues, schema::Schema};
9
10impl Schema {
11 pub fn set_dictionary_id(&self, row: &mut EncodedValues, index: usize, entry: &DictionaryEntryId) {
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::DictionaryId);
15 row.set_valid(index, true);
16 unsafe {
17 let ptr = row.make_mut().as_mut_ptr().add(field.offset as usize);
18 match entry {
19 DictionaryEntryId::U1(v) => ptr.write_unaligned(*v),
20 DictionaryEntryId::U2(v) => ptr::write_unaligned(ptr as *mut u16, *v),
21 DictionaryEntryId::U4(v) => ptr::write_unaligned(ptr as *mut u32, *v),
22 DictionaryEntryId::U8(v) => ptr::write_unaligned(ptr as *mut u64, *v),
23 DictionaryEntryId::U16(v) => ptr::write_unaligned(ptr as *mut u128, *v),
24 }
25 }
26 }
27
28 pub fn get_dictionary_id(&self, row: &EncodedValues, index: usize) -> DictionaryEntryId {
29 let field = &self.fields()[index];
30 debug_assert!(row.len() >= self.total_static_size());
31 debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::DictionaryId);
32 let id_type = match field.constraint.constraint() {
33 Some(Constraint::Dictionary(_, id_type)) => id_type.clone(),
34 _ => Type::Uint4, };
36 unsafe {
37 let ptr = row.as_ptr().add(field.offset as usize);
38 let raw: u128 = match id_type {
39 Type::Uint1 => ptr.read_unaligned() as u128,
40 Type::Uint2 => (ptr as *const u16).read_unaligned() as u128,
41 Type::Uint4 => (ptr as *const u32).read_unaligned() as u128,
42 Type::Uint8 => (ptr as *const u64).read_unaligned() as u128,
43 Type::Uint16 => (ptr as *const u128).read_unaligned(),
44 _ => (ptr as *const u32).read_unaligned() as u128,
45 };
46 DictionaryEntryId::from_u128(raw, id_type).unwrap()
47 }
48 }
49
50 pub fn try_get_dictionary_id(&self, row: &EncodedValues, index: usize) -> Option<DictionaryEntryId> {
51 if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::DictionaryId {
52 Some(self.get_dictionary_id(row, index))
53 } else {
54 None
55 }
56 }
57}