Type Definition mokuroku::ByteMapper[][src]

pub type ByteMapper = Box<dyn Fn(&[u8], &[u8], &str, &Emitter<'_>) -> Result<(), Error> + Send + Sync>;
Expand description

Responsible for emitting index key/value pairs for any given data record.

Arguments: data record key, data record value, view name, emitter

The application implements a function of this type and passes it to the database constructor. When the library is building an index, this function will be called with the key and value for every record in the default column family. The mapping function is expected to invoke the Emitter to generate the index key and value pairs. It is up to the mapper to recognize the key and/or value of the data record, perform the appropriate deserialization, and then invoke the provided Emitter with index values.

In this example, the LenVal and Asset types are implementations of Document, making it a trivial matter to deserialize the database record and emit index keys. This mapper uses the key prefix as a means of knowing which Document implementation to use.

fn mapper(key: &[u8], value: &[u8], view: &str, emitter: &Emitter) -> Result<(), Error> {
    if &key[..3] == b"lv/".as_ref() {
        let doc = LenVal::from_bytes(key, value)?;
        doc.map(view, emitter)?;
    } else if &key[..3] == b"as/".as_ref() {
        let doc = Asset::from_bytes(key, value)?;
        doc.map(view, emitter)?;
    }
    Ok(())
}