graph.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef _LSUP_GRAPH_H
  2. #define _LSUP_GRAPH_H
  3. #include "triple.h"
  4. typedef enum LSUP_store_type {
  5. LSUP_STORE_MEM,
  6. LSUP_STORE_MDB
  7. } LSUP_store_type;
  8. typedef struct Graph LSUP_Graph;
  9. /**
  10. * Post-lookup callback type.
  11. *
  12. * src is the graph that yielded a match. Its index ponts at the matched triple
  13. * key and is accessible via `keyset_peek(ks)`.
  14. *
  15. * dest is an optional keyset that may be acted upon. It may be NULL.
  16. *
  17. * ctx is an optional arbitrary pointer to additional data that may be used
  18. * by the callback.
  19. */
  20. typedef int (*keyset_match_fn_t)(LSUP_Graph *src, LSUP_Graph *dest, void *ctx);
  21. int
  22. LSUP_graph_init(
  23. LSUP_Graph *gr, size_t capacity, char *uri_str,
  24. LSUP_store_type store_type);
  25. LSUP_Graph *
  26. LSUP_graph_new(size_t capacity, char *uri_str, LSUP_store_type store_type);
  27. int
  28. LSUP_graph_copy(LSUP_Graph *dest, LSUP_Graph *src);
  29. size_t
  30. LSUP_graph_capacity(LSUP_Graph *gr);
  31. size_t
  32. LSUP_graph_size(LSUP_Graph *gr);
  33. char *
  34. LSUP_graph_uri(LSUP_Graph *gr);
  35. bool
  36. LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *t);
  37. /**
  38. * Execute a custom function on a graph based on a match pattern.
  39. *
  40. * This function executes an arbitrary callback on a graph, `res`, based on
  41. * triples matched by a pattern on graph `gr`. `res` must be initialized but
  42. * need not be empty. `res` can point to the same object as `gr` if changes
  43. * are to be done in place (e.g. removing triples).
  44. *
  45. * @param[in] gr Graph to perform pattern matching.
  46. * @param[out] res Result graph to apply the callback to.
  47. * @param[in] spo Triple pattern. Each term of the triple members can be either
  48. * a term pointer or NULL. If NULL, the term is unbound.
  49. * @param[in] callback_fn Callback function to apply.
  50. * @param[in] match_cond If true, apply the callback to each triple a match is
  51. * found for. Otherwise, apply to each triple no match is found for.
  52. * @param[in|out] ctx Arbitrary context that may be handled in the callback
  53. * function.
  54. *
  55. * @return LSUP_OK on match, LSUP_NOACTION on no match, <0 on error.
  56. */
  57. int LSUP_graph_match_callback(
  58. LSUP_Graph *gr, LSUP_Graph *res, const LSUP_Triple *spo,
  59. keyset_match_fn_t callback_fn, bool match_cond, void *ctx);
  60. /**
  61. * Add triples to a graph.
  62. */
  63. int
  64. LSUP_graph_add(LSUP_Graph *gr, const LSUP_Triple data[], size_t data_size);
  65. int LSUP_graph_lookup(LSUP_Graph *gr, LSUP_Graph *res, const LSUP_Triple *spo);
  66. /**
  67. * Set-theoretical union (gr1 ∪ gr2).
  68. *
  69. * The resulting Keyset is initialized beforehand and is not compacted.
  70. */
  71. int LSUP_graph_join(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  72. /**
  73. * Set-theoretical complement (gr1 \ gr2).
  74. *
  75. * The resulting Keyset is initialized beforehand and is not compacted.
  76. */
  77. int LSUP_graph_subtract(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  78. /**
  79. * Set-theoretical intersection (gr1 ∩ gr2).
  80. *
  81. * The resulting Keyset is initialized beforehand and is not compacted.
  82. */
  83. int LSUP_graph_intersect(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  84. /**
  85. * Disjunctive union (XOR) (gr1 ⊕ gr2).
  86. *
  87. * The resulting Keyset is initialized beforehand and is not compacted.
  88. */
  89. int LSUP_graph_xor(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  90. void
  91. LSUP_graph_free(LSUP_Graph *gr);
  92. #endif