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::traits::RuntimeEnumContext>,
71        field_name: &'static str,
72    ) -> Result<T, crate::error::InternalError>
73    where
74        T: crate::traits::RuntimeValueDecode,
75    {
76        crate::traits::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,
84        GeneratedStructuralMapPayloadSlices, JournalTailStore, PersistedRow, PersistedScalar,
85        PrimaryKeyComponent, PrimaryKeyValue, ScalarSlotValueRef, ScalarValueRef, SlotReader,
86        SlotWriter, StoreRuntimeStorageCapabilities,
87    };
88    pub use crate::error::{ErrorClass, ErrorOrigin, InternalError};
89    pub use crate::traits::{
90        AuthoredFieldProjection, EntityKeyBytes, FieldProjection, KeyValueCodec,
91        PersistedByKindCodec, PersistedFieldMetaCodec, PersistedFieldSlotCodec,
92        PersistedStructuredFieldCodec, PrimaryKeyCodec, PrimaryKeyDecode, PrimaryKeyEncodeError,
93        RuntimeEnumContext, RuntimeEnumSelection, RuntimeValueDecode, RuntimeValueEncode,
94        RuntimeValueKind, RuntimeValueMeta, ScalarRelationTargetKey,
95        ScalarRelationTargetKeyMatchesDeclaredPrimitive, runtime_value_btree_map_from_value,
96        runtime_value_btree_set_from_value, runtime_value_collection_to_value,
97        runtime_value_from_value, runtime_value_from_value_with_enum_context,
98        runtime_value_from_value_with_optional_enum_context, runtime_value_from_vec_into,
99        runtime_value_from_vec_into_btree_map, runtime_value_from_vec_into_btree_set,
100        runtime_value_into, runtime_value_map_collection_to_value, runtime_value_to_value,
101        runtime_value_vec_from_value,
102    };
103    pub use crate::value::{InputValue, InputValueEnum, Value, ValueEnum};
104    pub use ic_memory::{
105        bootstrap_default_memory_manager, ic_memory_declaration, ic_memory_key, ic_memory_range,
106    };
107
108    #[doc(hidden)]
109    #[must_use]
110    pub fn encode_generated_structural_text_payload_bytes(value: &str) -> Vec<u8> {
111        crate::db::encode_generated_structural_text_payload_bytes(value)
112    }
113
114    #[doc(hidden)]
115    #[must_use]
116    pub fn encode_generated_structural_list_payload_bytes(items: &[&[u8]]) -> Vec<u8> {
117        crate::db::encode_generated_structural_list_payload_bytes(items)
118    }
119
120    #[doc(hidden)]
121    #[must_use]
122    pub fn encode_generated_structural_map_payload_bytes(entries: &[(&[u8], &[u8])]) -> Vec<u8> {
123        crate::db::encode_generated_structural_map_payload_bytes(entries)
124    }
125
126    #[doc(hidden)]
127    pub fn decode_generated_structural_text_payload_bytes(
128        raw_bytes: &[u8],
129    ) -> Result<String, crate::error::InternalError> {
130        crate::db::decode_generated_structural_text_payload_bytes(raw_bytes)
131    }
132
133    #[doc(hidden)]
134    pub fn decode_generated_structural_list_payload_bytes(
135        raw_bytes: &[u8],
136    ) -> Result<Vec<&[u8]>, crate::error::InternalError> {
137        crate::db::decode_generated_structural_list_payload_bytes(raw_bytes)
138    }
139
140    #[doc(hidden)]
141    pub fn decode_generated_structural_map_payload_bytes(
142        raw_bytes: &[u8],
143    ) -> Result<crate::db::GeneratedStructuralMapPayloadSlices<'_>, crate::error::InternalError>
144    {
145        crate::db::decode_generated_structural_map_payload_bytes(raw_bytes)
146    }
147
148    #[doc(hidden)]
149    pub fn generated_persisted_structured_payload_decode_failed(
150        detail: impl std::fmt::Display,
151    ) -> crate::error::InternalError {
152        crate::db::generated_persisted_structured_payload_decode_failed(detail)
153    }
154
155    #[doc(hidden)]
156    pub fn encode_non_enum_protocol_value_bytes(
157        value: &crate::value::Value,
158    ) -> Result<Vec<u8>, crate::error::InternalError> {
159        crate::db::encode_non_enum_protocol_value_bytes(value)
160    }
161
162    #[doc(hidden)]
163    pub fn decode_non_enum_protocol_value_bytes(
164        raw_bytes: &[u8],
165    ) -> Result<crate::value::Value, crate::error::InternalError> {
166        crate::db::decode_non_enum_protocol_value_bytes(raw_bytes)
167    }
168
169    #[doc(hidden)]
170    pub fn encode_persisted_structured_many_slot_payload<T>(
171        value: &[T],
172        field_name: &'static str,
173    ) -> Result<Vec<u8>, crate::error::InternalError>
174    where
175        T: crate::traits::PersistedStructuredFieldCodec,
176    {
177        crate::db::encode_persisted_structured_many_slot_payload(value, field_name)
178    }
179
180    #[doc(hidden)]
181    pub fn decode_persisted_structured_many_slot_payload<T>(
182        bytes: &[u8],
183        field_name: &'static str,
184    ) -> Result<Vec<T>, crate::error::InternalError>
185    where
186        T: crate::traits::PersistedStructuredFieldCodec,
187    {
188        crate::db::decode_persisted_structured_many_slot_payload(bytes, field_name)
189    }
190
191    #[doc(hidden)]
192    pub fn encode_persisted_structured_slot_payload<T>(
193        value: &T,
194        field_name: &'static str,
195    ) -> Result<Vec<u8>, crate::error::InternalError>
196    where
197        T: crate::traits::PersistedStructuredFieldCodec,
198    {
199        crate::db::encode_persisted_structured_slot_payload(value, field_name)
200    }
201
202    #[doc(hidden)]
203    pub fn decode_persisted_structured_slot_payload<T>(
204        bytes: &[u8],
205        field_name: &'static str,
206    ) -> Result<T, crate::error::InternalError>
207    where
208        T: crate::traits::PersistedStructuredFieldCodec,
209    {
210        crate::db::decode_persisted_structured_slot_payload(bytes, field_name)
211    }
212
213    #[doc(hidden)]
214    pub fn encode_persisted_option_scalar_slot_payload<T>(
215        value: &Option<T>,
216        field_name: &'static str,
217    ) -> Result<Vec<u8>, crate::error::InternalError>
218    where
219        T: PersistedScalar,
220    {
221        crate::db::encode_persisted_option_scalar_slot_payload(value, field_name)
222    }
223
224    #[doc(hidden)]
225    pub fn decode_persisted_option_scalar_slot_payload<T>(
226        bytes: &[u8],
227        field_name: &'static str,
228    ) -> Result<Option<T>, crate::error::InternalError>
229    where
230        T: PersistedScalar,
231    {
232        crate::db::decode_persisted_option_scalar_slot_payload(bytes, field_name)
233    }
234
235    #[doc(hidden)]
236    pub fn encode_persisted_scalar_slot_payload<T>(
237        value: &T,
238        field_name: &'static str,
239    ) -> Result<Vec<u8>, crate::error::InternalError>
240    where
241        T: PersistedScalar,
242    {
243        crate::db::encode_persisted_scalar_slot_payload(value, field_name)
244    }
245
246    #[doc(hidden)]
247    pub fn decode_persisted_scalar_slot_payload<T>(
248        bytes: &[u8],
249        field_name: &'static str,
250    ) -> Result<T, crate::error::InternalError>
251    where
252        T: PersistedScalar,
253    {
254        crate::db::decode_persisted_scalar_slot_payload(bytes, field_name)
255    }
256
257    #[doc(hidden)]
258    pub fn encode_persisted_slot_payload_by_kind<T>(
259        value: &T,
260        kind: crate::model::field::FieldKind,
261        field_name: &'static str,
262    ) -> Result<Vec<u8>, crate::error::InternalError>
263    where
264        T: crate::traits::PersistedByKindCodec,
265    {
266        crate::db::encode_persisted_slot_payload_by_kind(value, kind, field_name)
267    }
268
269    #[doc(hidden)]
270    pub fn decode_persisted_slot_payload_by_kind<T>(
271        bytes: &[u8],
272        kind: crate::model::field::FieldKind,
273        field_name: &'static str,
274    ) -> Result<T, crate::error::InternalError>
275    where
276        T: crate::traits::PersistedByKindCodec,
277    {
278        crate::db::decode_persisted_slot_payload_by_kind(bytes, kind, field_name)
279    }
280
281    #[doc(hidden)]
282    pub fn decode_persisted_option_slot_payload_by_kind<T>(
283        bytes: &[u8],
284        kind: crate::model::field::FieldKind,
285        field_name: &'static str,
286    ) -> Result<Option<T>, crate::error::InternalError>
287    where
288        T: crate::traits::PersistedByKindCodec,
289    {
290        crate::db::decode_persisted_option_slot_payload_by_kind(bytes, kind, field_name)
291    }
292
293    #[doc(hidden)]
294    pub fn encode_persisted_option_slot_payload_by_meta<T>(
295        value: &Option<T>,
296        field_name: &'static str,
297    ) -> Result<Vec<u8>, crate::error::InternalError>
298    where
299        T: crate::traits::PersistedFieldMetaCodec,
300    {
301        crate::db::encode_persisted_option_slot_payload_by_meta(value, field_name)
302    }
303
304    #[doc(hidden)]
305    pub fn decode_persisted_option_slot_payload_by_meta<T>(
306        bytes: &[u8],
307        field_name: &'static str,
308    ) -> Result<Option<T>, crate::error::InternalError>
309    where
310        T: crate::traits::PersistedFieldMetaCodec,
311    {
312        crate::db::decode_persisted_option_slot_payload_by_meta(bytes, field_name)
313    }
314
315    #[doc(hidden)]
316    pub fn encode_persisted_many_slot_payload_by_meta<T>(
317        value: &[T],
318        field_name: &'static str,
319    ) -> Result<Vec<u8>, crate::error::InternalError>
320    where
321        T: crate::traits::FieldTypeMeta + crate::traits::RuntimeValueEncode,
322    {
323        crate::db::encode_persisted_many_slot_payload_by_meta(value, field_name)
324    }
325
326    #[doc(hidden)]
327    pub fn decode_persisted_many_slot_payload_by_meta<T>(
328        bytes: &[u8],
329        field_name: &'static str,
330    ) -> Result<Vec<T>, crate::error::InternalError>
331    where
332        T: crate::traits::FieldTypeMeta + crate::traits::RuntimeValueDecode,
333    {
334        crate::db::decode_persisted_many_slot_payload_by_meta(bytes, field_name)
335    }
336
337    #[doc(hidden)]
338    pub fn encode_persisted_slot_payload_by_meta<T>(
339        value: &T,
340        field_name: &'static str,
341    ) -> Result<Vec<u8>, crate::error::InternalError>
342    where
343        T: crate::traits::PersistedFieldMetaCodec,
344    {
345        crate::db::encode_persisted_slot_payload_by_meta(value, field_name)
346    }
347
348    #[doc(hidden)]
349    pub fn decode_persisted_slot_payload_by_meta<T>(
350        bytes: &[u8],
351        field_name: &'static str,
352    ) -> Result<T, crate::error::InternalError>
353    where
354        T: crate::traits::PersistedFieldMetaCodec,
355    {
356        crate::db::decode_persisted_slot_payload_by_meta(bytes, field_name)
357    }
358}