Skip to main content

icydb_core/
lib.rs

1//! Module: lib
2//! Responsibility: crate root for the IcyDB core runtime surface.
3//! Does not own: canister-facing facade APIs from the public `icydb` crate.
4//! Boundary: exposes the engine subsystems used by schema, query, executor, and storage layers.
5
6//! Core runtime for IcyDB: entity traits, values, executors, visitors, and
7//! the ergonomics exported via the `prelude`.
8#![warn(unreachable_pub)]
9// The no-default test target intentionally type-checks shared test/helper
10// surfaces whose consuming tests live behind SQL, SQL-explain, or diagnostics
11// features. Keep production and all-features dead-code linting strict.
12#![cfg_attr(all(test, not(feature = "sql")), allow(dead_code))]
13
14extern crate self as icydb;
15
16#[macro_use]
17pub(crate) mod scalar_registry;
18
19// public exports are one module level down
20pub mod db;
21pub mod entity;
22pub mod error;
23pub mod metrics;
24pub mod model;
25pub(crate) mod runtime;
26pub mod sanitize;
27pub mod traits;
28pub mod types;
29pub mod validate;
30pub mod value;
31pub mod visitor;
32
33// testing
34#[cfg(test)]
35pub(crate) mod testing;
36
37///
38/// CONSTANTS
39///
40
41/// Maximum number of indexed fields allowed on an entity.
42///
43/// This limit keeps hashed index keys within bounded, storable sizes and
44/// simplifies sizing tests in the stores.
45pub const MAX_INDEX_FIELDS: usize = 4;
46
47///
48/// Prelude
49///
50/// Prelude contains only domain vocabulary.
51/// No errors, executors, stores, serializers, or helpers are re-exported here.
52///
53
54pub mod prelude {
55    pub use crate::{
56        entity::EntityKind,
57        model::{entity::EntityModel, index::IndexModel},
58        traits::Path,
59        value::{InputValue, OutputValue},
60    };
61}
62
63// Macro/runtime wiring surface used by generated code in local core tests.
64// This mirrors the facade crate's hidden generated-code boundary so derive
65// output can target one stable path regardless of which workspace crate owns
66// the test harness.
67#[doc(hidden)]
68pub mod __macro {
69    #[doc(hidden)]
70    pub fn decode_generated_runtime_field_value<T>(
71        value: &crate::value::Value,
72        context: Option<&dyn crate::value::RuntimeEnumContext>,
73        field_name: &'static str,
74    ) -> Result<T, crate::error::InternalError>
75    where
76        T: crate::value::RuntimeValueDecode,
77    {
78        crate::value::runtime_value_from_value_with_optional_enum_context(value, context)
79            .ok_or_else(|| {
80                crate::error::InternalError::persisted_row_field_decode_failed(field_name, ())
81            })
82    }
83
84    pub use crate::db::{
85        CompositePrimaryKeyValue, CompositePrimaryKeyValueError, EntityKey, EntityKeyBytes,
86        EntityKeyBytesError, GeneratedStructuralMapPayloadSlices, JournalTailStore, KeyValueCodec,
87        PersistedByKindCodec, PersistedRow, PersistedScalar, PersistedStructuralValueCodec,
88        PrimaryKeyComponent, PrimaryKeyDecode, PrimaryKeyEncode, PrimaryKeyEncodeError,
89        PrimaryKeyValue, ScalarRelationTargetKey, ScalarRelationTargetKeyMatchesDeclaredPrimitive,
90        ScalarSlotValueRef, ScalarValueRef, SlotReader, StoreRuntimeStorageCapabilities,
91        validate_entity_key_bytes_buffer,
92    };
93    pub use crate::entity::{
94        EntityCreateInput, EntityCreateMaterialization, EntityCreateType, EntityDeclaration,
95        EntityKind, EntityPlacement, EntityValue,
96    };
97    pub use crate::error::{ErrorClass, ErrorOrigin, InternalError};
98    pub use crate::traits::{
99        AuthoredFieldProjection, CanisterKind, FieldProjection, FieldTypeMeta, Inner, Path,
100        StoreKind,
101    };
102    pub use crate::types::NumericValue;
103    pub use crate::value::{
104        InputValue, InputValueEnum, RuntimeEnumContext, RuntimeEnumSelection, RuntimeValueDecode,
105        RuntimeValueEncode, RuntimeValueKind, RuntimeValueMeta, Value, ValueEnum,
106        runtime_value_btree_map_from_value, runtime_value_btree_set_from_value,
107        runtime_value_collection_to_value, runtime_value_from_value,
108        runtime_value_from_value_with_enum_context,
109        runtime_value_from_value_with_optional_enum_context, runtime_value_from_vec_into,
110        runtime_value_from_vec_into_btree_map, runtime_value_from_vec_into_btree_set,
111        runtime_value_into, runtime_value_map_collection_to_value, runtime_value_to_value,
112        runtime_value_vec_from_value,
113    };
114    pub use ic_memory::{
115        bootstrap_default_memory_manager, ic_memory_declaration, ic_memory_key, ic_memory_range,
116    };
117    pub use serde::Deserialize;
118    pub use std::{
119        clone::Clone,
120        cmp::{Eq, Ord, PartialEq, PartialOrd},
121        convert::From,
122        default::Default,
123        fmt::{Debug, Display},
124        hash::Hash,
125        iter::Sum,
126        marker::Copy,
127        ops::{
128            Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign,
129        },
130    };
131
132    #[doc(hidden)]
133    #[must_use]
134    pub fn encode_generated_structural_text_payload_bytes(value: &str) -> Vec<u8> {
135        crate::db::encode_generated_structural_text_payload_bytes(value)
136    }
137
138    #[doc(hidden)]
139    #[must_use]
140    pub fn encode_generated_structural_list_payload_bytes(items: &[&[u8]]) -> Vec<u8> {
141        crate::db::encode_generated_structural_list_payload_bytes(items)
142    }
143
144    #[doc(hidden)]
145    #[must_use]
146    pub fn encode_generated_structural_map_payload_bytes(entries: &[(&[u8], &[u8])]) -> Vec<u8> {
147        crate::db::encode_generated_structural_map_payload_bytes(entries)
148    }
149
150    #[doc(hidden)]
151    pub fn decode_generated_structural_text_payload_bytes(
152        raw_bytes: &[u8],
153    ) -> Result<String, crate::error::InternalError> {
154        crate::db::decode_generated_structural_text_payload_bytes(raw_bytes)
155    }
156
157    #[doc(hidden)]
158    pub fn decode_generated_structural_list_payload_bytes(
159        raw_bytes: &[u8],
160    ) -> Result<Vec<&[u8]>, crate::error::InternalError> {
161        crate::db::decode_generated_structural_list_payload_bytes(raw_bytes)
162    }
163
164    #[doc(hidden)]
165    pub fn decode_generated_structural_map_payload_bytes(
166        raw_bytes: &[u8],
167    ) -> Result<crate::db::GeneratedStructuralMapPayloadSlices<'_>, crate::error::InternalError>
168    {
169        crate::db::decode_generated_structural_map_payload_bytes(raw_bytes)
170    }
171
172    #[doc(hidden)]
173    pub fn generated_persisted_structured_payload_decode_failed(
174        detail: impl std::fmt::Display,
175    ) -> crate::error::InternalError {
176        crate::db::generated_persisted_structured_payload_decode_failed(detail)
177    }
178
179    #[doc(hidden)]
180    pub fn encode_non_enum_protocol_value_bytes(
181        value: &crate::value::Value,
182    ) -> Result<Vec<u8>, crate::error::InternalError> {
183        crate::db::encode_non_enum_protocol_value_bytes(value)
184    }
185
186    #[doc(hidden)]
187    pub fn decode_non_enum_protocol_value_bytes(
188        raw_bytes: &[u8],
189    ) -> Result<crate::value::Value, crate::error::InternalError> {
190        crate::db::decode_non_enum_protocol_value_bytes(raw_bytes)
191    }
192
193    #[doc(hidden)]
194    pub fn encode_persisted_structured_many_slot_payload<T>(
195        value: &[T],
196        field_name: &'static str,
197    ) -> Result<Vec<u8>, crate::error::InternalError>
198    where
199        T: PersistedStructuralValueCodec,
200    {
201        crate::db::encode_persisted_structured_many_slot_payload(value, field_name)
202    }
203
204    #[doc(hidden)]
205    pub fn decode_persisted_structured_many_slot_payload<T>(
206        bytes: &[u8],
207        field_name: &'static str,
208    ) -> Result<Vec<T>, crate::error::InternalError>
209    where
210        T: PersistedStructuralValueCodec,
211    {
212        crate::db::decode_persisted_structured_many_slot_payload(bytes, field_name)
213    }
214
215    #[doc(hidden)]
216    pub fn encode_persisted_structured_slot_payload<T>(
217        value: &T,
218        field_name: &'static str,
219    ) -> Result<Vec<u8>, crate::error::InternalError>
220    where
221        T: PersistedStructuralValueCodec,
222    {
223        crate::db::encode_persisted_structured_slot_payload(value, field_name)
224    }
225
226    #[doc(hidden)]
227    pub fn decode_persisted_structured_slot_payload<T>(
228        bytes: &[u8],
229        field_name: &'static str,
230    ) -> Result<T, crate::error::InternalError>
231    where
232        T: PersistedStructuralValueCodec,
233    {
234        crate::db::decode_persisted_structured_slot_payload(bytes, field_name)
235    }
236
237    #[doc(hidden)]
238    pub fn encode_persisted_option_scalar_slot_payload<T>(
239        value: &Option<T>,
240        field_name: &'static str,
241    ) -> Result<Vec<u8>, crate::error::InternalError>
242    where
243        T: PersistedScalar,
244    {
245        crate::db::encode_persisted_option_scalar_slot_payload(value, field_name)
246    }
247
248    #[doc(hidden)]
249    pub fn decode_persisted_option_scalar_slot_payload<T>(
250        bytes: &[u8],
251        field_name: &'static str,
252    ) -> Result<Option<T>, crate::error::InternalError>
253    where
254        T: PersistedScalar,
255    {
256        crate::db::decode_persisted_option_scalar_slot_payload(bytes, field_name)
257    }
258
259    #[doc(hidden)]
260    pub fn encode_persisted_scalar_slot_payload<T>(
261        value: &T,
262        field_name: &'static str,
263    ) -> Result<Vec<u8>, crate::error::InternalError>
264    where
265        T: PersistedScalar,
266    {
267        crate::db::encode_persisted_scalar_slot_payload(value, field_name)
268    }
269
270    #[doc(hidden)]
271    pub fn decode_persisted_scalar_slot_payload<T>(
272        bytes: &[u8],
273        field_name: &'static str,
274    ) -> Result<T, crate::error::InternalError>
275    where
276        T: PersistedScalar,
277    {
278        crate::db::decode_persisted_scalar_slot_payload(bytes, field_name)
279    }
280
281    #[doc(hidden)]
282    pub fn encode_persisted_slot_payload_by_kind<T>(
283        value: &T,
284        kind: crate::model::field::FieldKind,
285        field_name: &'static str,
286    ) -> Result<Vec<u8>, crate::error::InternalError>
287    where
288        T: PersistedByKindCodec,
289    {
290        crate::db::encode_persisted_slot_payload_by_kind(value, kind, field_name)
291    }
292
293    #[doc(hidden)]
294    pub fn decode_persisted_slot_payload_by_kind<T>(
295        bytes: &[u8],
296        kind: crate::model::field::FieldKind,
297        field_name: &'static str,
298    ) -> Result<T, crate::error::InternalError>
299    where
300        T: PersistedByKindCodec,
301    {
302        crate::db::decode_persisted_slot_payload_by_kind(bytes, kind, field_name)
303    }
304
305    #[doc(hidden)]
306    pub fn decode_persisted_option_slot_payload_by_kind<T>(
307        bytes: &[u8],
308        kind: crate::model::field::FieldKind,
309        field_name: &'static str,
310    ) -> Result<Option<T>, crate::error::InternalError>
311    where
312        T: PersistedByKindCodec,
313    {
314        crate::db::decode_persisted_option_slot_payload_by_kind(bytes, kind, field_name)
315    }
316}