graph.h 3.4 KB

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