From dddcd1c17f3c57399a08441533bde8f8e34e1ca2 Mon Sep 17 00:00:00 2001 From: Emily Toop Date: Fri, 27 Apr 2018 11:24:25 +0100 Subject: [PATCH] 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. --- ffi/src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index 4102d1f2..1795db5d 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -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. ///