pub struct KeyDef { /* private fields */ }
Expand description
An object which describes how to extract a key for a given tarantool index from a tuple. Basically it contains information the parts of the key, specifically location of the parts within the tuple, their types, their nullability and collation.
Can be used to
- compare tuples (
Self::compare
) - extract a key from a tuple (
Self::extract_key
) - check if a tuple has the expected format (
Self::validate_tuple
) - etc.
You can construct one of these from an explicit list of key part definitions
using Self::new
, or automtically from an index’s metadata like so:
let space = Space::find("some_space").unwrap();
let index = space.index("some_index").unwrap();
let meta = index.meta().unwrap();
let key_def: KeyDef = meta.to_key_def();
Implementations§
source§impl KeyDef
impl KeyDef
sourcepub fn new<'a>(
parts: impl IntoIterator<Item = &'a KeyDefPart<'a>>,
) -> Result<Self>
pub fn new<'a>( parts: impl IntoIterator<Item = &'a KeyDefPart<'a>>, ) -> Result<Self>
Create key definition with key fields with passed typed on passed positions. May be used for tuple format creation and/or tuple comparison.
items
- array with key field identifiers and key field types (see FieldType)
sourcepub fn compare(&self, tuple_a: &Tuple, tuple_b: &Tuple) -> Ordering
pub fn compare(&self, tuple_a: &Tuple, tuple_b: &Tuple) -> Ordering
Compare tuples using the key definition.
tuple_a
- first tupletuple_b
- second tuple
Returns:
Ordering::Equal
ifkey_fields(tuple_a) == key_fields(tuple_b)
Ordering::Less
ifkey_fields(tuple_a) < key_fields(tuple_b)
Ordering::Greater
ifkey_fields(tuple_a) > key_fields(tuple_b)
sourcepub fn compare_with_key<K>(&self, tuple: &Tuple, key: &K) -> Orderingwhere
K: ToTupleBuffer + ?Sized,
pub fn compare_with_key<K>(&self, tuple: &Tuple, key: &K) -> Orderingwhere
K: ToTupleBuffer + ?Sized,
Compare tuple with key using the key definition.
tuple
- tuplekey
- key with MessagePack array header
Returns:
Ordering::Equal
ifkey_fields(tuple) == parts(key)
Ordering::Less
ifkey_fields(tuple) < parts(key)
Ordering::Greater
ifkey_fields(tuple) > parts(key)
sourcepub fn validate_tuple(&self, tuple: &Tuple) -> Result<()>
pub fn validate_tuple(&self, tuple: &Tuple) -> Result<()>
Checks if tuple
satisfies the key definition’s format, i.e. do the
tuple’s fields described the self
’s key parts have the same types.
Note that the tuple may still not satisfy the full format of the space,
this function only checks if it contains a part which could be used as
a key of an index.
There’s currently no good way of checking if the tuple satisfies the format of the space other than trying to insert into that space.
This function is used internally by Self::extract_key
to check if
it’s safe to extract the key described by this KeyDef
from a given tuple.
sourcepub fn extract_key(&self, tuple: &Tuple) -> Result<TupleBuffer>
pub fn extract_key(&self, tuple: &Tuple) -> Result<TupleBuffer>
Extracts the key described by this KeyDef
from tuple
.
Returns an error if tuple
doesn’t satisfy this KeyDef
.
sourcepub unsafe fn extract_key_raw<'box_region>(
&self,
tuple: &Tuple,
multikey_idx: i32,
) -> Result<&'box_region [u8]>
pub unsafe fn extract_key_raw<'box_region>( &self, tuple: &Tuple, multikey_idx: i32, ) -> Result<&'box_region [u8]>
Extracts the key described by this KeyDef
from tuple
.
TODO: what is multikey_idx
? Pass a -1
as the default value.
Returns an error in case memory runs out.
§Safety
tuple
must satisfy the key definition’s format.
Use Self::extract_key
if you want the tuple to be validated automatically,
or use Self::validate_tuple
to validate the tuple explicitly.