graph.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 *src, LSUP_Graph *dest);
  29. size_t
  30. LSUP_graph_capacity(LSUP_Graph *gr);
  31. size_t
  32. LSUP_graph_size(LSUP_Graph *gr);
  33. bool
  34. LSUP_graph_contains(const LSUP_Graph *gr, const LSUP_Triple *t);
  35. /**
  36. * Execute a custom function on a graph based on a match pattern.
  37. *
  38. * This function executes an arbitrary callback on a graph, `res`, based on
  39. * triples matched by a pattern on graph `gr`. `res` must be initialized but
  40. * need not be empty. `res` can point to the same object as `gr` if changes
  41. * are to be done in place (e.g. removing triples).
  42. *
  43. * @param[in] gr Graph to perform pattern matching.
  44. * @param[out] res Result graph to apply the callback to.
  45. * @param[in] spo Triple pattern. Each term of the triple members can be either
  46. * a term pointer or NULL. If NULL, the term is unbound.
  47. * @param[in] callback_fn Callback function to apply.
  48. * @param[in] match_cond If true, apply the callback to each triple a match is
  49. * found for. Otherwise, apply to each triple no match is found for.
  50. * @param[in|out] ctx Arbitrary context that may be handled in the callback
  51. * function.
  52. *
  53. * @return LSUP_OK on match, LSUP_NOACTION on no match, <0 on error.
  54. */
  55. int LSUP_graph_match_callback(
  56. LSUP_Graph *gr, LSUP_Graph *res, const LSUP_Triple *spo,
  57. keyset_match_fn_t callback_fn, bool match_cond, void *ctx);
  58. /**
  59. * Add triples to a graph.
  60. */
  61. int
  62. LSUP_graph_add(LSUP_Graph *gr, const LSUP_Triple data[], size_t data_size);
  63. int LSUP_graph_lookup(LSUP_Graph *gr, LSUP_Graph *res, const LSUP_Triple *spo);
  64. /**
  65. * Set-theoretical union (gr1 ∪ gr2).
  66. *
  67. * The resulting Keyset is initialized beforehand and is not compacted.
  68. */
  69. int LSUP_graph_join(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  70. /**
  71. * Set-theoretical complement (gr1 \ gr2).
  72. *
  73. * The resulting Keyset is initialized beforehand and is not compacted.
  74. */
  75. int LSUP_graph_subtract(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  76. /**
  77. * Set-theoretical intersection (gr1 ∩ gr2).
  78. *
  79. * The resulting Keyset is initialized beforehand and is not compacted.
  80. */
  81. int LSUP_graph_intersect(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  82. /**
  83. * Disjunctive union (XOR) (gr1 ⊕ gr2).
  84. *
  85. * The resulting Keyset is initialized beforehand and is not compacted.
  86. */
  87. int LSUP_graph_xor(LSUP_Graph *gr1, LSUP_Graph *gr2, LSUP_Graph *res);
  88. void
  89. LSUP_graph_free(LSUP_Graph *gr);
  90. #endif