Expose cache over the FFI.

This exposes an FFI function for each direction of caching, `Forward`, `Reverse` and `Both`. This is to make is as clear as possible to consumers which direction they are caching their attributes in. The original implementation exposed the `CacheDirection` enum over FFI and it made mistakes very easy to make. This is more explicit and therefore less prone to error.
This commit is contained in:
Emily Toop 2018-04-27 11:24:25 +01:00
parent e0cd9b6b20
commit b4b558e196

View file

@ -87,6 +87,7 @@ use std::vec;
pub use mentat::{
Binding,
CacheDirection,
Entid,
FindSpec,
HasSchema,
@ -1026,9 +1027,55 @@ pub unsafe extern "C" fn tx_report_entity_for_temp_id(tx_report: *mut TxReport,
}
}
// TODO: cache
/// Adds an attribute to the cache.
/// `store_cache_attribute_forward` caches values for an attribute keyed by entity
/// (i.e. find values and entities that have this attribute, or find values of attribute for an entity)
///
/// # Safety
///
/// Callers must ensure that the pointer to the `Store` is not dangling and that
/// the C string provided to `attribute` is valid.
#[no_mangle]
pub extern "C" fn store_cache_attribute_forward(store: *mut Store, attribute: *const c_char) -> *mut ExternResult {
let store = unsafe { &mut *store };
let kw = kw_from_string(c_char_to_string(attribute));
Box::into_raw(Box::new(store.cache(&kw, CacheDirection::Forward).into()))
}
// TODO: q_once
/// Adds an attribute to the cache.
/// `store_cache_attribute_reverse` caches entities for an attribute keyed by value.
/// (i.e. find entities that have a particular value for an attribute).
///
/// # Safety
///
/// Callers must ensure that the pointer to the `Store` is not dangling and that
/// the C string provided to `attribute` is valid.
#[no_mangle]
pub extern "C" fn store_cache_attribute_reverse(store: *mut Store, attribute: *const c_char) -> *mut ExternResult {
let store = unsafe { &mut *store };
let kw = kw_from_string(c_char_to_string(attribute));
Box::into_raw(Box::new(store.cache(&kw, CacheDirection::Reverse).into()))
}
/// Adds an attribute to the cache.
/// `store_cache_attribute_bi_directional` caches entity in both available directions, forward and reverse.
///
/// `Forward` caches values for an attribute keyed by entity
/// (i.e. find values and entities that have this attribute, or find values of attribute for an entity)
///
/// `Reverse` caches entities for an attribute keyed by value.
/// (i.e. find entities that have a particular value for an attribute).
///
/// # Safety
///
/// Callers must ensure that the pointer to the `Store` is not dangling and that
/// the C string provided to `attribute` is valid.
#[no_mangle]
pub extern "C" fn store_cache_attribute_bi_directional(store: *mut Store, attribute: *const c_char) -> *mut ExternResult {
let store = unsafe { &mut *store };
let kw = kw_from_string(c_char_to_string(attribute));
Box::into_raw(Box::new(store.cache(&kw, CacheDirection::Both).into()))
}
/// Creates a [QueryBuilder](mentat::QueryBuilder) from the given store to execute the provided query.
///