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 error;
22pub mod metrics;
23pub mod model;
24pub(crate) mod runtime;
25pub mod sanitize;
26pub mod traits;
27pub mod types;
28pub mod validate;
29pub mod value;
30pub mod visitor;
31
32// testing
33#[cfg(test)]
34pub(crate) mod testing;
35
36///
37/// CONSTANTS
38///
39
40/// Maximum number of indexed fields allowed on an entity.
41///
42/// This limit keeps hashed index keys within bounded, storable sizes and
43/// simplifies sizing tests in the stores.
44pub const MAX_INDEX_FIELDS: usize = 4;
45
46///
47/// Prelude
48///
49/// Prelude contains only domain vocabulary.
50/// No errors, executors, stores, serializers, or helpers are re-exported here.
51///
52
53pub mod prelude {
54    pub use crate::{
55        model::{entity::EntityModel, index::IndexModel},
56        traits::{EntityKind, Path},
57        value::{InputValue, OutputValue},
58    };
59}
60
61// Macro/runtime wiring surface used by generated code in local core tests.
62// This mirrors the facade crate's hidden generated-code boundary so derive
63// output can target one stable path regardless of which workspace crate owns
64// the test harness.
65#[doc(hidden)]
66pub mod __macro {
67    #[doc(hidden)]
68    pub fn decode_generated_runtime_field_value<T>(
69        value: &crate::value::Value,
70        context: Option<&dyn crate::value::RuntimeEnumContext>,
71        field_name: &'static str,
72    ) -> Result<T, crate::error::InternalError>
73    where
74        T: crate::value::RuntimeValueDecode,
75    {
76        crate::value::runtime_value_from_value_with_optional_enum_context(value, context)
77            .ok_or_else(|| {
78                crate::error::InternalError::persisted_row_field_decode_failed(field_name, ())
79            })
80    }
81
82    pub use crate::db::{
83        CompositePrimaryKeyValue, CompositePrimaryKeyValueError, EntityKeyBytes,
84        EntityKeyBytesError, GeneratedStructuralMapPayloadSlices, JournalTailStore, KeyValueCodec,
85        PersistedRow, PersistedScalar, PrimaryKeyComponent, PrimaryKeyDecode, PrimaryKeyEncode,
86        PrimaryKeyEncodeError, PrimaryKeyValue, ScalarRelationTargetKey,
87        ScalarRelationTargetKeyMatchesDeclaredPrimitive, ScalarSlotValueRef, ScalarValueRef,
88        SlotReader, StoreRuntimeStorageCapabilities, validate_entity_key_bytes_buffer,
89    };
90    pub use crate::error::{ErrorClass, ErrorOrigin, InternalError};
91    pub use crate::traits::{
92        AuthoredFieldProjection, FieldProjection, PersistedByKindCodec,
93        PersistedStructuredFieldCodec,
94    };
95    pub use crate::value::{
96        InputValue, InputValueEnum, RuntimeEnumContext, RuntimeEnumSelection, RuntimeValueDecode,
97        RuntimeValueEncode, RuntimeValueKind, RuntimeValueMeta, Value, ValueEnum,
98        runtime_value_btree_map_from_value, runtime_value_btree_set_from_value,
99        runtime_value_collection_to_value, runtime_value_from_value,
100        runtime_value_from_value_with_enum_context,
101        runtime_value_from_value_with_optional_enum_context, runtime_value_from_vec_into,
102        runtime_value_from_vec_into_btree_map, runtime_value_from_vec_into_btree_set,
103        runtime_value_into, runtime_value_map_collection_to_value, runtime_value_to_value,
104        runtime_value_vec_from_value,
105    };
106    pub use ic_memory::{
107        bootstrap_default_memory_manager, ic_memory_declaration, ic_memory_key, ic_memory_range,
108    };
109
110    #[doc(hidden)]
111    #[must_use]
112    pub fn encode_generated_structural_text_payload_bytes(value: &str) -> Vec<u8> {
113        crate::db::encode_generated_structural_text_payload_bytes(value)
114    }
115
116    #[doc(hidden)]
117    #[must_use]
118    pub fn encode_generated_structural_list_payload_bytes(items: &[&[u8]]) -> Vec<u8> {
119        crate::db::encode_generated_structural_list_payload_bytes(items)
120    }
121
122    #[doc(hidden)]
123    #[must_use]
124    pub fn encode_generated_structural_map_payload_bytes(entries: &[(&[u8], &[u8])]) -> Vec<u8> {
125        crate::db::encode_generated_structural_map_payload_bytes(entries)
126    }
127
128    #[doc(hidden)]
129    pub fn decode_generated_structural_text_payload_bytes(
130        raw_bytes: &[u8],
131    ) -> Result<String, crate::error::InternalError> {
132        crate::db::decode_generated_structural_text_payload_bytes(raw_bytes)
133    }
134
135    #[doc(hidden)]
136    pub fn decode_generated_structural_list_payload_bytes(
137        raw_bytes: &[u8],
138    ) -> Result<Vec<&[u8]>, crate::error::InternalError> {
139        crate::db::decode_generated_structural_list_payload_bytes(raw_bytes)
140    }
141
142    #[doc(hidden)]
143    pub fn decode_generated_structural_map_payload_bytes(
144        raw_bytes: &[u8],
145    ) -> Result<crate::db::GeneratedStructuralMapPayloadSlices<'_>, crate::error::InternalError>
146    {
147        crate::db::decode_generated_structural_map_payload_bytes(raw_bytes)
148    }
149
150    #[doc(hidden)]
151    pub fn generated_persisted_structured_payload_decode_failed(
152        detail: impl std::fmt::Display,
153    ) -> crate::error::InternalError {
154        crate::db::generated_persisted_structured_payload_decode_failed(detail)
155    }
156
157    #[doc(hidden)]
158    pub fn encode_non_enum_protocol_value_bytes(
159        value: &crate::value::Value,
160    ) -> Result<Vec<u8>, crate::error::InternalError> {
161        crate::db::encode_non_enum_protocol_value_bytes(value)
162    }
163
164    #[doc(hidden)]
165    pub fn decode_non_enum_protocol_value_bytes(
166        raw_bytes: &[u8],
167    ) -> Result<crate::value::Value, crate::error::InternalError> {
168        crate::db::decode_non_enum_protocol_value_bytes(raw_bytes)
169    }
170
171    #[doc(hidden)]
172    pub fn encode_persisted_structured_many_slot_payload<T>(
173        value: &[T],
174        field_name: &'static str,
175    ) -> Result<Vec<u8>, crate::error::InternalError>
176    where
177        T: crate::traits::PersistedStructuredFieldCodec,
178    {
179        crate::db::encode_persisted_structured_many_slot_payload(value, field_name)
180    }
181
182    #[doc(hidden)]
183    pub fn decode_persisted_structured_many_slot_payload<T>(
184        bytes: &[u8],
185        field_name: &'static str,
186    ) -> Result<Vec<T>, crate::error::InternalError>
187    where
188        T: crate::traits::PersistedStructuredFieldCodec,
189    {
190        crate::db::decode_persisted_structured_many_slot_payload(bytes, field_name)
191    }
192
193    #[doc(hidden)]
194    pub fn encode_persisted_structured_slot_payload<T>(
195        value: &T,
196        field_name: &'static str,
197    ) -> Result<Vec<u8>, crate::error::InternalError>
198    where
199        T: crate::traits::PersistedStructuredFieldCodec,
200    {
201        crate::db::encode_persisted_structured_slot_payload(value, field_name)
202    }
203
204    #[doc(hidden)]
205    pub fn decode_persisted_structured_slot_payload<T>(
206        bytes: &[u8],
207        field_name: &'static str,
208    ) -> Result<T, crate::error::InternalError>
209    where
210        T: crate::traits::PersistedStructuredFieldCodec,
211    {
212        crate::db::decode_persisted_structured_slot_payload(bytes, field_name)
213    }
214
215    #[doc(hidden)]
216    pub fn encode_persisted_option_scalar_slot_payload<T>(
217        value: &Option<T>,
218        field_name: &'static str,
219    ) -> Result<Vec<u8>, crate::error::InternalError>
220    where
221        T: PersistedScalar,
222    {
223        crate::db::encode_persisted_option_scalar_slot_payload(value, field_name)
224    }
225
226    #[doc(hidden)]
227    pub fn decode_persisted_option_scalar_slot_payload<T>(
228        bytes: &[u8],
229        field_name: &'static str,
230    ) -> Result<Option<T>, crate::error::InternalError>
231    where
232        T: PersistedScalar,
233    {
234        crate::db::decode_persisted_option_scalar_slot_payload(bytes, field_name)
235    }
236
237    #[doc(hidden)]
238    pub fn encode_persisted_scalar_slot_payload<T>(
239        value: &T,
240        field_name: &'static str,
241    ) -> Result<Vec<u8>, crate::error::InternalError>
242    where
243        T: PersistedScalar,
244    {
245        crate::db::encode_persisted_scalar_slot_payload(value, field_name)
246    }
247
248    #[doc(hidden)]
249    pub fn decode_persisted_scalar_slot_payload<T>(
250        bytes: &[u8],
251        field_name: &'static str,
252    ) -> Result<T, crate::error::InternalError>
253    where
254        T: PersistedScalar,
255    {
256        crate::db::decode_persisted_scalar_slot_payload(bytes, field_name)
257    }
258
259    #[doc(hidden)]
260    pub fn encode_persisted_slot_payload_by_kind<T>(
261        value: &T,
262        kind: crate::model::field::FieldKind,
263        field_name: &'static str,
264    ) -> Result<Vec<u8>, crate::error::InternalError>
265    where
266        T: crate::traits::PersistedByKindCodec,
267    {
268        crate::db::encode_persisted_slot_payload_by_kind(value, kind, field_name)
269    }
270
271    #[doc(hidden)]
272    pub fn decode_persisted_slot_payload_by_kind<T>(
273        bytes: &[u8],
274        kind: crate::model::field::FieldKind,
275        field_name: &'static str,
276    ) -> Result<T, crate::error::InternalError>
277    where
278        T: crate::traits::PersistedByKindCodec,
279    {
280        crate::db::decode_persisted_slot_payload_by_kind(bytes, kind, field_name)
281    }
282
283    #[doc(hidden)]
284    pub fn decode_persisted_option_slot_payload_by_kind<T>(
285        bytes: &[u8],
286        kind: crate::model::field::FieldKind,
287        field_name: &'static str,
288    ) -> Result<Option<T>, crate::error::InternalError>
289    where
290        T: crate::traits::PersistedByKindCodec,
291    {
292        crate::db::decode_persisted_option_slot_payload_by_kind(bytes, kind, field_name)
293    }
294}