123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- #ifndef _LSUP_GRAPH_H
- #define _LSUP_GRAPH_H
- #include "store.h"
- #include "environment.h"
- #include "term.h"
- /** @brief Graph object.
- */
- typedef struct graph_t LSUP_Graph;
- /** @brief Graph iterator.
- *
- * This opaque handle is generated by #LSUP_graph_lookup_txn 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 graph_iter_t LSUP_GraphIterator;
- /** @brief Create an empty graph.
- *
- * @param[in] store Back end store handle. It may be the result of
- * #LSUP_store_new(), or NULL; in the latter case, it defaults to a temporary
- * HTable store that is freed together with the graph.
- *
- * @param[in] uri URI of the new graph. If NULL, a UUID4 URN is generated. The
- * term is copied into the graph and may be freed after this function is
- * called.
- *
- * @param[in] nsm Namespace map to use for an in-memory graph. This is ignored
- * by graphs backed by permanent stores, which handle their own namespace map.
- * If this is NULL, the graph is assigned a global namespace map that lives
- * until #LSUP_done() is called.
- *
- * @return New graph, or NULL on error. Must be freed with #LSUP_graph_free().
- */
- LSUP_Graph *
- LSUP_graph_new (LSUP_Store *store, LSUP_Term *uri, LSUP_NSMap *nsm);
- /** @brief Create a temp graph from stored triples.
- *
- * The new graph is stored in a hash map and is made up of all the triples
- * found in the store with the given context URI. The new graph URI is the
- * same as the given context.
- *
- * @param[in] store Back end store handle. The store must exist.
- *
- * @param[in] uri URI of the graph to retrieve.
- *
- * @param[out] ct If not NULL, it will be populated with the number of triples
- * found.
- *
- * @return New graph handle. It must be freed by the caller. If no matching
- * context URI was found, NULL is returned.
- */
- LSUP_Graph *
- LSUP_graph_get_txn (void *txn, LSUP_Store *store, LSUP_Term *uri, size_t *ct);
- /// Non-transactional version of #LSUP_graph_get_txn().
- #define LSUP_graph_get(...) LSUP_graph_get_txn (NULL, __VA_ARGS__)
- /** @brief Copy triples from a source graph into a destination one.
- *
- * The destination graph is not initialized here, so the copy is cumulative.
- *
- * @param[in] txn Transaction handle. It may be NULL.
- *
- * @param src[in] Source graph.
- *
- * @param dest[in] Destination graph.
- */
- LSUP_rc
- LSUP_graph_copy_contents_txn (
- void *txn, const LSUP_Graph *src, LSUP_Graph *dest);
- /// Non-transactional version of #LSUP_graph_copy_contents_txn.
- #define LSUP_graph_copy_contents(...) \
- LSUP_graph_copy_contents_txn (NULL, __VA_ARGS__)
- /** Perform a boolean operation between two graphs.
- *
- * This method populates an initialized graph with the result of the operation
- * between two other graphs. The resulting graph may be of any store type and
- * may be the result of graphs of different store types.
- *
- * @param[in] txn Transaction handle. It may be NULL.
- *
- * @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. The handle should be initialized via
- * #LSUP_graph_new() or equivalent. Any preexisting contents are not removed.
- * If an unrecoverable error occurs, this graph is freed.
- *
- * @return LSUP_OK on success; <0 on error.
- */
- LSUP_rc
- LSUP_graph_bool_op_txn (
- void *txn, const LSUP_bool_op op,
- const LSUP_Graph *gr1, const LSUP_Graph *gr2, LSUP_Graph *res);
- /// Non-transactional version of #LSUP_graph_bool_op_txn.
- #define LSUP_graph_bool_op(...) LSUP_graph_bool_op_txn (NULL, __VA_ARGS__)
- /** @brief Free a graph.
- */
- void
- LSUP_graph_free (LSUP_Graph *gr);
- /** @brief Compare two graphs.
- *
- * Note that if any of the two graphs has an open transaction, the function
- * is performed in the first graph's transaction.
- *
- * @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);
- /** @brief Underlying graph store handle.
- */
- LSUP_Store *
- LSUP_graph_store (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 graph stores with LSUP_STORE_PERM.
- *
- * @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] txn Transaction handle. It may be NULL. If not NULL, its handle
- * will be bound to the iterator handle for its whole lifa cycle.
- *
- * @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_txn (void *txn, LSUP_Graph *gr);
- /// Non-transactional version of #LSUP_graph_init_txn().
- #define LSUP_graph_add_init(...) LSUP_graph_add_init_txn (NULL, __VA_ARGS__)
- /** @brief Add a single triple to the store.
- *
- * @param[in] it Iterator obtained with #LSUP_graph_add_init_txn().
- *
- * @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_txn().
- *
- * @param[in] it Iterator to finalize.
- */
- void
- LSUP_graph_add_done (LSUP_GraphIterator *it);
- /** @brief Add triples to a graph.
- *
- * @param[in] txn Transaction handle. It may be NULL.
- *
- * @param[in] gr Graph to add triples to.
- *
- * @param[in] trp NULL-terminated array of triple handles to add.
- *
- * @param[out] ct This will be filled with the total number of triples
- * inserted.
- */
- LSUP_rc
- LSUP_graph_add_txn (
- void *txn, LSUP_Graph *gr, LSUP_Triple *const *trp, size_t *ct);
- /// Non-transactional version of #LSUP_graph_add_txn.
- #define LSUP_graph_add(...) LSUP_graph_add_txn (NULL, __VA_ARGS__)
- /** @brief Delete triples by a matching pattern.
- *
- * @param[in] txn Transaction handle. It may be NULL.
- *
- * @param[in] gr[in] Graph to delete triples from.
- *
- * @param[in] s, p, o Matching pattern. Any and all of s, p, o can be NULL.
- *
- * @param[out] ct If not NULL it is populated with the number of triples
- * deleted.
- */
- LSUP_rc
- LSUP_graph_remove_txn (
- void *txn, LSUP_Graph *gr, const LSUP_Term *s, const LSUP_Term *p,
- const LSUP_Term *o, size_t *ct);
- /// Non-transactional version of #LSUP_graph_remove_txn.
- #define LSUP_graph_remove(...) LSUP_graph_remove_txn (NULL, __VA_ARGS__)
- /** @brief Look up triples by a matching pattern and yield an iterator.
- *
- * @param[in] txn Transaction handle. It may be NULL.
- *
- * @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_txn (void *txn, const LSUP_Graph *gr, const LSUP_Term *s,
- const LSUP_Term *p, const LSUP_Term *o, size_t *ct);
- /// Non-transactional version of #LSUP_graph_lookup_txn.
- #define LSUP_graph_lookup(...) LSUP_graph_lookup_txn (NULL, __VA_ARGS__)
- /** @brief Advance a cursor obtained by a lookup and return a matching triple.
- *
- * @param it[in] Iterator handle obtained through #LSUP_graph_lookup_txn.
- *
- * @param spo[out] Triple handle pointer to be populated with the next result.
- * If not NULL, it will allocate a new triple and new terms, and should be
- * freed with LSUP_triple_free().
- *
- * @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 Return the graph related to an iterator.
- */
- const LSUP_Graph *
- LSUP_graph_iter_graph (LSUP_GraphIterator *it);
- /** @brief Free a graph iterator.
- *
- * DO NOT USE with iterators obtained with #LSUP_graph_add_init_txn(). Use
- * #LSUP_graph_add_done() with those.
- *
- * @param[in] it Iterator to finalize.
- */
- void
- LSUP_graph_iter_free (LSUP_GraphIterator *it);
- /** @brief Get term pairs connected to a term in a graph.
- *
- * This returns a #LSUP_LinkMap extracted from a graph for a given term. The
- * map can generate triples using #LSUP_link_map_triples().
- *
- * Depending on the type requested (`LSUP_CONN_*), the term can be leveraged
- * as a subject, predicate, or object.
- *
- * @param[in] gr Graph to extract the connection list from.
- *
- * @param[in] t Term to query for connections.
- *
- * @param[in] type Type of connections to look up.
- *
- * @return Link map for the requested term. It should be freed with
- * #LSUP_conn_list_free().
- */
- LSUP_LinkMap *
- LSUP_graph_connections (
- const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type);
- /** @brief Get a list of terms related to a term pair in a graph.
- *
- * @param[in] gr Graph to extract terms from.
- *
- * @param[in] t1 First term.
- *
- * @param[in] t1_pos Position of the first term in the triples to look up.
- *
- * @param[in] t2 Second term.
- *
- * @param[in] t2_pos Position of the second term in the triples to look up.
- *
- * @return Term set of results.
- */
- LSUP_TermSet *
- LSUP_graph_term_set (
- const LSUP_Graph *gr, LSUP_Term *t1, LSUP_TriplePos t1_pos,
- LSUP_Term *t2, LSUP_TriplePos t2_pos);
- /** @brief Get all unique subjcts, predicates, or objects in a graph.
- *
- * @param[in] gr Graph handle.
- *
- * @param[in] pos Position in the triples of the terms to look for.
- */
- LSUP_TermSet *
- LSUP_graph_unique_terms (const LSUP_Graph *gr, LSUP_TriplePos pos);
- /** @brief Add triples for a term and related connection list to a graph.
- *
- * The connection list can be of inbound, outbound, or edge type; depending on
- * that, triples are added with the given term as the subject, the predicate,
- * or the object.
- *
- * @param[in] it Graph iterator obtained with #LSUP_graph_add_init_txn().
- *
- * @param[in] t Term to be associated with the collection list.
- *
- * @param[in] cl Link map.
- *
- * @return Number of triples parsed on success, or <0 (LSUP_*_ERR) on error.
- */
- size_t
- LSUP_graph_add_link_map (
- LSUP_GraphIterator *it, LSUP_Term *t, LSUP_LinkMap *cl);
- /** @brief Add triples for an anonymous collection to a graph.
- *
- * The `rdf:first`, `rdf:rest`, etc. terms are automatically added and the term
- * for the first item in the list is returned.
- *
- * @param[in] it Graph iterator to use for insertion.
- *
- * @param[in] ts Source term set.
- *
- * @return Blank node representing the first list item.
- */
- LSUP_Term *
- LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts);
- #endif
|