123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #ifndef _LSUP_GRAPH_H
- #define _LSUP_GRAPH_H
- #include "environment.h"
- #include "term.h"
- /*
- * Define backend types.
- *
- * Add new store implementations to this table.
- */
- #define BACKEND_TBL \
- /* #enum pfx #store #iterator */\
- /*ENTRY( HTABLE, htstore_int, htiter_int )*/ \
- ENTRY( MDB, mdbstore_int, mdbiter_int ) \
- ENTRY( MDB_TMP, mdbstore_int, mdbiter_int ) \
- /** @brief Store types. All prefixed with `LSUP_STORE_`.
- */
- typedef enum {
- #define ENTRY(a, b, c) LSUP_STORE_##a,
- BACKEND_TBL
- #undef ENTRY
- } LSUP_store_type;
- /** @brief Graph object.
- */
- typedef struct Graph LSUP_Graph;
- /** @brief Graph iterator.
- *
- * This opaque handle is generated by #LSUP_graph_lookup and is used to iterate
- * over lookup results with #LSUP_graph_iter_next. It must be freed with
- * #LSUP_graph_iter_free when done.
- */
- typedef struct GraphIterator LSUP_GraphIterator;
- /** @brief Create an empty graph.
- *
- * The new graph has zero capacity and a random URN. To change either one, use
- * #LSUP_graph_resize and #LSUP_graph_set_uri, respectively.
- *
- * The graph is assigned a default (volatile) namespace map if it's in memory,
- * hence all graphs share the same namespace map by default. To change this,
- * use #LSUP_graph_set_namespace(). MDB graphs use a persistent namespace map
- * that is common to all the graphs in the same store. This cannot be changed.
- *
- * @param store_type[in] Type of store for the graph. One of the values of
- * #LSUP_store_type.
- *
- * @return LSUP_OK if the graph was created, or < 0 if an error occurred.
- */
- LSUP_Graph *
- LSUP_graph_new_env (
- const LSUP_Env *env, LSUP_Term *uri, const LSUP_store_type store_type);
- /** @brief Create an empty graph with the default environment.
- *
- * This is likely to be used more often than #LSUP_graph_new_env().
- */
- #define LSUP_graph_new(uri, type) \
- LSUP_graph_new_env (LSUP_default_env, uri, type)
- /** @brief Shortcut for #LSUP_graph_new_lookup_env() on default MDB store.
- */
- #define LSUP_graph_new_lookup(s, p, o) \
- LSUP_graph_new_lookup_env (LSUP_default_env, (s), (p), (o))
- /** @brief copy a graph into a new one.
- *
- * The new graph is compacted to the minimum required size.
- *
- * src[in] Graph to be copied.
- *
- * @param uri URI of the destination graph. If NULL, a UUID4 URN is generated.
- *
- * @param gr[out] Pointer to a pointer to the destination graph. It must be
- * freed with #LSUP_graph_free when done.
- *
- * @return LSUP_OK if the graph was copied, or < 0 if an error occurred.
- */
- LSUP_Graph *
- LSUP_graph_copy (const LSUP_Graph *src);
- /** @brief Copy the contents of a graph into a permanent store.
- *
- * It is possible to store a memory graph, a RAMdisk MDB graph, or a
- * permanently stored graph into another environment.
- *
- * The namespace map associated with the graph is stored into the destination
- * as well, except for existing namespaces and prefixes.
- *
- * @param[in] src Graph to store.
- *
- * @param[out] dest Pointer to graph handle for the new stored graph. The new
- * graph will have the same URI as the source. It may be NULL.
- *
- * @param[in] env Environment to copy to. If NULL, it is set to the deafult
- * LSUP store. This makes it possible to copy MDB graphs across different
- * environments. If the source is a MDB graph and the environment is the same
- * as the source, no change occurs.
- *
- * @return LSUP_OK on success; LSUP_NOACTION if the graph is already stored in
- * the same enviroment; <0 on error.
- */
- LSUP_rc
- LSUP_graph_store (
- const LSUP_Graph *src, LSUP_Graph **dest_p, const LSUP_Env *env);
- /** Perform a boolean operation between two graphs.
- *
- * This method yields a new graph as the result of the operation.
- *
- * @param op[in] Operation to perform. One of #LSUP_bool_op.
- *
- * @param gr1[in] First operand.
- *
- * @param gr2[in] Second operand.
- *
- * @param res[out] Result graph. It must be freed with #LSUP_graph_free when
- * done.
- */
- LSUP_Graph *
- LSUP_graph_bool_op(
- const LSUP_bool_op op, const LSUP_Graph *gr1, const LSUP_Graph *gr2);
- /** @brief Free a graph.
- */
- void
- LSUP_graph_free (LSUP_Graph *gr);
- /** @brief Compare two graphs.
- *
- * @param[in] gr1 First operand.
- *
- * @param[in] gr2 Second operand.
- *
- * @return True if the graphs are topologically equal, false otherwise.
- */
- bool
- LSUP_graph_equals (const LSUP_Graph *gr1, const LSUP_Graph *gr2);
- /** @brief Read-only graph URI.
- *
- * To change the graph URI, use #LSUP_graph_set_uri.
- */
- LSUP_Term *
- LSUP_graph_uri (const LSUP_Graph *gr);
- /** Set the URI of a graph.
- *
- * Note that by changing the URI of a graph backed by a context-sensitive store
- * (i.e. LSUP_STORE_MDB*) effectively changes the underlying data set that the
- * graph points to. Triples are looked up in, and added to, the context that
- * the graph URI represents. A non-context graph retains the same triple set
- * when graph URI changes.
- *
- * @param gr[in] Graph handle.
- *
- * @param uri[in] IRI handle. The graph takes ownership of the handle.
- *
- * @return LSUP_OK on success; <0 on error.
- */
- LSUP_rc
- LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri);
- /** @brief Get the namespace map for an in-memory graph.
- *
- * @return Namespace handler for in-memory graphs, NULL for MDB graphs.
- */
- LSUP_NSMap *
- LSUP_graph_namespace (const LSUP_Graph *gr);
- /** @brief Set the namespace map for an in-memory graph.
- *
- * This has no effect on MDB graphs.
- *
- * @param[in] gr Graph to set the namespace map for.
- *
- * @param[in] nsm Namespace handle.
- */
- void
- LSUP_graph_set_namespace (LSUP_Graph *gr, LSUP_NSMap *nsm);
- /** @brief Number of triples in a graph.
- */
- size_t
- LSUP_graph_size (const LSUP_Graph *gr);
- /** @brief Whether a graph contains a triple.
- *
- * @param[in] gr Graph to look up into.
- *
- * @param[in] spo Triple to look up.
- *
- * @return 1 if the triple is found, 0 if not found.
- */
- bool
- LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
- /** @brief Initialize an iterator to add triples.
- *
- * @param[in] gr Graph to add to. It is added to the iterator state.
- *
- * @return Iterator handle. This should be passed to #LSUP_graph_add_iter() and
- * must be freed with #LSUP_graph_add_done().
- */
- LSUP_GraphIterator *
- LSUP_graph_add_init (LSUP_Graph *gr);
- /** @brief Add a single triple to the store.
- *
- * @param[in] it Iterator obtained with #LSUP_graph_add_init().
- *
- * @param[in] spo Triple to add. Caller retains ownership.
- */
- LSUP_rc
- LSUP_graph_add_iter (LSUP_GraphIterator *it, const LSUP_Triple *spo);
- /** @brief Finalize an add iteration loop and free the iterator.
- *
- * DO NOT USE with iterators obtained with other than #LSUP_graph_add_init().
- *
- * @param[in] it Iterator to finalize.
- */
- void
- LSUP_graph_add_done (LSUP_GraphIterator *it);
- /** @brief Add triples to a graph.
- *
- * @param[in] gr Graph to add triples to.
- *
- * @param[in] trp Array of triples to add. The last triple must be NULL.
- *
- * @param[in] strp Array of buffer triples to add. The last one must be NULL.
- *
- * @param[out] inserted This will be filled with the total number of triples
- * inserted.
- */
- LSUP_rc
- LSUP_graph_add (LSUP_Graph *gr, const LSUP_Triple trp[], size_t *inserted);
- /** @brief Delete triples by a matching pattern.
- *
- * @param gr[in] Graph to delete triples from.
- *
- * @param ptn[in] Matching pattern. Any and all of s, p, o can be NULL.
- *
- * @param ct[out] If not NULL it is populated with the number of triples
- * deleted.
- */
- LSUP_rc
- LSUP_graph_remove (
- LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
- const LSUP_Term *o, size_t *ct);
- /** Look up triples by a matching pattern and yield an iterator.
- *
- * @param gr[in] Graph to look up.
- *
- * @param spo[in] Triple to look for. Any and all terms can be NULL, which
- * indicate unbound terms.
- *
- * @param it[out] Pointer to a #LSUP_GraphIterator to be generated. It must be
- * freed with #LSUP_graph_iter_free after use.
- */
- LSUP_GraphIterator *
- LSUP_graph_lookup (const LSUP_Graph *gr, const LSUP_Term *s,
- const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
- /** @brief Advance a cursor obtained by a lookup and return a matching triple.
- *
- * @param it[in] Iterator handle obtained through #LSUP_graph_lookup.
- *
- * @param spo[out] Triple to be populated with the next result. May be NULL
- * (e.g. for counters).
- *
- * @return LSUP_OK if a result was found; LSUP_END if the end of the match list
- * was reached.
- */
- LSUP_rc
- LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo);
- /** @brief Free a graph iterator.
- *
- * DO NOT USE with iterators obtained with #LSUP_graph_add_init(). Use
- * #LSUP_graph_add_done() with those.
- *
- * @param[in] it Iterator to finalize.
- */
- void
- LSUP_graph_iter_free (LSUP_GraphIterator *it);
- #endif
|