Skip to main content

reifydb_codec/primitive/
dictionary_id.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::ptr;
5
6use reifydb_value::{
7	reifydb_assertions,
8	value::{constraint::Constraint, dictionary::DictionaryEntryId, value_type::ValueType},
9};
10
11use crate::row::{bytes::RowBuilder, shape::RowShape};
12
13impl RowShape {
14	pub fn set_dictionary_id(&self, row: &mut impl RowBuilder, index: usize, entry: &DictionaryEntryId) {
15		let field = &self.fields()[index];
16		reifydb_assertions! {
17			assert!(
18				row.len() >= self.total_static_size(),
19				"row/shape size mismatch: row.len()={} < total_static_size()={}",
20				row.len(),
21				self.total_static_size()
22			);
23			assert_eq!(*field.constraint.get_type().inner_type(), ValueType::DictionaryId);
24		}
25		self.set_valid(row, index, true);
26		// SAFETY: row.len() >= total_static_size() puts the slot at field.offset inside the uniquely-owned
27		// buffer and it is at least as wide as the widest arm; write_unaligned needs no alignment.
28		unsafe {
29			let ptr = row.as_mut_slice().as_mut_ptr().add(field.offset as usize);
30			match entry {
31				DictionaryEntryId::U1(v) => ptr.write_unaligned(*v),
32				DictionaryEntryId::U2(v) => ptr::write_unaligned(ptr as *mut u16, *v),
33				DictionaryEntryId::U4(v) => ptr::write_unaligned(ptr as *mut u32, *v),
34				DictionaryEntryId::U8(v) => ptr::write_unaligned(ptr as *mut u64, *v),
35				DictionaryEntryId::U16(v) => ptr::write_unaligned(ptr as *mut u128, *v),
36			}
37		}
38	}
39
40	pub fn get_dictionary_id(&self, row: &[u8], index: usize) -> DictionaryEntryId {
41		let field = &self.fields()[index];
42		reifydb_assertions! {
43			assert!(
44				row.len() >= self.total_static_size(),
45				"row/shape size mismatch: row.len()={} < total_static_size()={}",
46				row.len(),
47				self.total_static_size()
48			);
49			assert_eq!(*field.constraint.get_type().inner_type(), ValueType::DictionaryId);
50		}
51		let id_type = match field.constraint.constraint() {
52			Some(Constraint::Dictionary(_, id_type)) => id_type.clone(),
53			_ => ValueType::Uint4,
54		};
55		// SAFETY: row.len() >= total_static_size() puts the slot at field.offset inside the row and it is at
56		// least as wide as the id type read; the unsigned targets have no invalid bit patterns.
57		unsafe {
58			let ptr = row.as_ptr().add(field.offset as usize);
59			let raw: u128 = match id_type {
60				ValueType::Uint1 => ptr.read_unaligned() as u128,
61				ValueType::Uint2 => (ptr as *const u16).read_unaligned() as u128,
62				ValueType::Uint4 => (ptr as *const u32).read_unaligned() as u128,
63				ValueType::Uint8 => (ptr as *const u64).read_unaligned() as u128,
64				ValueType::Uint16 => (ptr as *const u128).read_unaligned(),
65				_ => (ptr as *const u32).read_unaligned() as u128,
66			};
67			DictionaryEntryId::from_u128(raw, id_type).unwrap()
68		}
69	}
70
71	pub fn try_get_dictionary_id(&self, row: &[u8], index: usize) -> Option<DictionaryEntryId> {
72		if self.is_defined(row, index) && self.fields()[index].constraint.get_type() == ValueType::DictionaryId
73		{
74			Some(self.get_dictionary_id(row, index))
75		} else {
76			None
77		}
78	}
79}