Macro schemadb::define_schema
source · macro_rules! define_schema { ($schema_type: ident, $key_type: ty, $value_type: ty, $cf_name: expr) => { ... }; }
Expand description
Macro for defining a SchemaDB schema.
define_schema!
allows a schema to be defined in the following syntax:
use anyhow::Result;
use schemadb::{
define_schema,
schema::{KeyCodec, SeekKeyCodec, ValueCodec},
};
// Define key type and value type for a schema with derived traits (Clone, Debug, Eq, PartialEq)
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Key;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Value;
// Implement KeyCodec/ValueCodec traits for key and value types
impl KeyCodec<ExampleSchema> for Key {
fn encode_key(&self) -> Result<Vec<u8>> {
Ok(vec![])
}
fn decode_key(data: &[u8]) -> Result<Self> {
Ok(Key)
}
}
impl ValueCodec<ExampleSchema> for Value {
fn encode_value(&self) -> Result<Vec<u8>> {
Ok(vec![])
}
fn decode_value(data: &[u8]) -> Result<Self> {
Ok(Value)
}
}
// And finally define a schema type and associate it with key and value types, as well as the
// column family name, by generating code that implements the `Schema` trait for the type.
define_schema!(ExampleSchema, Key, Value, "exmaple_cf_name");
// SeekKeyCodec is automatically implemented for KeyCodec,
// so you can seek an iterator with the Key type:
// iter.seek(&Key);
// Or if seek-by-prefix is desired, you can implement your own SeekKey
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct PrefixSeekKey;
impl SeekKeyCodec<ExampleSchema> for PrefixSeekKey {
fn encode_seek_key(&self) -> Result<Vec<u8>> {
Ok(vec![])
}
}
// and seek like this:
// iter.seek(&PrefixSeekKey);