#ifndef _LSUP_GRAPH_H #define _LSUP_GRAPH_H #include "triple.h" typedef enum LSUP_store_type { LSUP_STORE_MEM, LSUP_STORE_MDB } LSUP_store_type; typedef struct Graph LSUP_Graph; /** * Post-lookup callback type. * * src is the graph that yielded a match. Its index ponts at the matched triple * key and is accessible via `keyset_peek(ks)`. * * dest is an optional keyset that may be acted upon. It may be NULL. * * cur is the cursor pointing to the matching record in the source. * * ctx is an optional arbitrary pointer to additional data that may be used * by the callback. */ typedef int (*keyset_match_fn_t)( LSUP_Graph *src, LSUP_Graph *dest, const LSUP_TripleKey *spok, void *ctx); int LSUP_graph_init( LSUP_Graph *gr, size_t capacity, char *uri_str, LSUP_store_type store_type); LSUP_Graph * LSUP_graph_new(size_t capacity, char *uri_str, LSUP_store_type store_type); // TODO Make src const; invert operands. int LSUP_graph_copy(LSUP_Graph *dest, LSUP_Graph *src); int LSUP_graph_resize(LSUP_Graph *gr, size_t size); size_t LSUP_graph_capacity(const LSUP_Graph *gr); size_t LSUP_graph_size(const LSUP_Graph *gr); LSUP_Term * LSUP_graph_uri(const LSUP_Graph *gr); bool LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *t); /** * Execute a custom function on a graph based on a match pattern. * * This function executes an arbitrary callback on a graph, `res`, based on * triples matched by a pattern on graph `gr`. `res` must be initialized but * need not be empty. `res` can point to the same object as `gr` if changes * are to be done in place (e.g. removing triples). * * @param[in] gr Graph to perform pattern matching. * @param[out] res Result graph to apply the callback to. * @param[in] spo Triple pattern. Each term of the triple members can be either * a term pointer or NULL. If NULL, the term is unbound. * @param[in] callback_fn Callback function to apply. * @param[in] match_cond If true, apply the callback to each triple a match is * found for. Otherwise, apply to each triple no match is found for. * @param[in|out] ctx Arbitrary context that may be handled in the callback * function. * * @return LSUP_OK on match, LSUP_NOACTION on no match, <0 on error. */ int LSUP_graph_match_callback( LSUP_Graph *gr, LSUP_Graph *res, const LSUP_Triple *spo, keyset_match_fn_t callback_fn, bool match_cond, void *ctx); /** * Add triples to a graph. */ int LSUP_graph_add(LSUP_Graph *gr, const LSUP_Triple data[], size_t data_size); int LSUP_graph_lookup(LSUP_Graph *gr, LSUP_Graph *res, const LSUP_Triple *spo); /** * Set-theoretical union (gr1 ∪ gr2). * * The resulting Keyset is initialized beforehand and is not compacted. */ int LSUP_graph_join(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res); /** * Set-theoretical complement (gr1 \ gr2). * * The resulting Keyset is initialized beforehand and is not compacted. */ int LSUP_graph_subtract(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res); /** * Set-theoretical intersection (gr1 ∩ gr2). * * The resulting Keyset is initialized beforehand and is not compacted. */ int LSUP_graph_intersect(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res); /** * Disjunctive union (XOR) (gr1 ⊕ gr2). * * The resulting Keyset is initialized beforehand and is not compacted. */ int LSUP_graph_xor(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res); void LSUP_graph_free(LSUP_Graph *gr); #endif