pub struct Schema { /* private fields */ }Expand description
Document schema definition for Anda DB.
A Schema describes:
- which fields a document may contain (by name and type),
- their stable on-disk indexes (
FieldEntry::idx), and - a monotonic
versionused to coordinate schema migrations.
Every schema implicitly contains the reserved _id field of type
FieldType::U64 with idx == 0 (see Schema::ID_KEY). It is added
automatically by SchemaBuilder::new.
Schema is Serialize / Deserialize and round-trips through both
JSON and CBOR. Deserialization re-validates every invariant (_id
presence, unique field names and indexes, valid field name characters,
idx <= u16::MAX) so it is safe to load schemas coming from untrusted
storage.
Implementations§
Source§impl Schema
impl Schema
Sourcepub const ID_KEY: &'static str = "_id"
pub const ID_KEY: &'static str = "_id"
The key name for the ID field. it is a special u64 field used as an internal unique identifier in a collection. It is always present in the schema with idx 0.
Sourcepub fn with_version(&mut self, version: u64) -> &mut Schema
pub fn with_version(&mut self, version: u64) -> &mut Schema
Sets the schema version.
Sourcepub fn needs_upgrade(&self, other: &Schema) -> bool
pub fn needs_upgrade(&self, other: &Schema) -> bool
Returns true if self has a higher version than other,
indicating that a schema migration is needed.
Sourcepub fn upgrade_with(&mut self, old: &Schema) -> Result<(), SchemaError>
pub fn upgrade_with(&mut self, old: &Schema) -> Result<(), SchemaError>
Upgrades this schema using field indexes from an older persisted schema.
The new schema (self) is typically built from program code where field indexes are assigned sequentially. The old schema comes from persistent storage with fixed indexes. This method ensures index consistency by:
- For fields present in both schemas: inherits the
idxfrom the old schema, and verifies the field type has not changed (type changes are not allowed). - For new fields (in self but not in old): assigns fresh indexes starting
from
max(old indexes) + 1, ensuring no conflicts with any old index. - For removed fields (in old but not in self): their indexes are simply not reused, preventing data corruption.
§Arguments
old- The old schema loaded from persistent storage.
§Errors
- If
self.versionis not greater thanold.version. - If a field that exists in both schemas has a different type.
Sourcepub fn builder() -> SchemaBuilder
pub fn builder() -> SchemaBuilder
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of fields in the schema. This includes the “_id” field and any other fields defined in the schema.
§Returns
The number of fields.
Sourcepub fn get_field(&self, name: &str) -> Option<&FieldEntry>
pub fn get_field(&self, name: &str) -> Option<&FieldEntry>
Sourcepub fn get_field_or_err(&self, name: &str) -> Result<&FieldEntry, SchemaError>
pub fn get_field_or_err(&self, name: &str) -> Result<&FieldEntry, SchemaError>
Sourcepub fn iter(&self) -> impl Iterator<Item = &FieldEntry>
pub fn iter(&self) -> impl Iterator<Item = &FieldEntry>
Returns an iterator over all fields in the schema.
§Returns
An iterator yielding references to FieldEntry.
Sourcepub fn validate(
&self,
values: &BTreeMap<usize, FieldValue>,
) -> Result<(), SchemaError>
pub fn validate( &self, values: &BTreeMap<usize, FieldValue>, ) -> Result<(), SchemaError>
Validates a set of field values against this schema.
§Arguments
values- The field values to validate.
§Returns
Ok(()) if validation succeeds, Err(SchemaError) otherwise.
§Errors
Returns an error if:
- A field index in values doesn’t exist in the schema
- A required field is missing
- A field value doesn’t match the field type
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Schema
impl<'de> Deserialize<'de> for Schema
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Schema, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Schema, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Serialize for Schema
impl Serialize for Schema
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Eq for Schema
impl StructuralPartialEq for Schema
Auto Trait Implementations§
impl Freeze for Schema
impl RefUnwindSafe for Schema
impl Send for Schema
impl Sync for Schema
impl Unpin for Schema
impl UnsafeUnpin for Schema
impl UnwindSafe for Schema
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more