graph.h 3.4 KB

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