triple.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifndef _LSUP_TRIPLE_H
  2. #define _LSUP_TRIPLE_H
  3. #include "term.h"
  4. typedef struct LSUP_Triple {
  5. LSUP_Term *s;
  6. LSUP_Term *p;
  7. LSUP_Term *o;
  8. } LSUP_Triple;
  9. typedef struct LSUP_SerTriple {
  10. LSUP_Buffer *s;
  11. LSUP_Buffer *p;
  12. LSUP_Buffer *o;
  13. } LSUP_SerTriple;
  14. typedef enum {
  15. TRP_POS_S = 0,
  16. TRP_POS_P = 1,
  17. TRP_POS_O = 2,
  18. } LSUP_TriplePos;
  19. LSUP_Triple *
  20. LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  21. #define TRP_DUMMY LSUP_triple_new (NULL, NULL, NULL)
  22. LSUP_SerTriple *
  23. LSUP_striple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  24. #define STRP_DUMMY LSUP_striple_new (NULL, NULL, NULL)
  25. LSUP_Triple *
  26. LSUP_triple_new_from_striple (const LSUP_SerTriple *sspo);
  27. LSUP_SerTriple *
  28. LSUP_striple_new_from_triple (const LSUP_Triple *spo);
  29. /** @brief Initialize internal term pointers in a heap-allocated triple.
  30. *
  31. * NOTE: the term structures are not copied. If the triple is freed with
  32. * #LSUP_triple_free(), the originally provided terms are freed too.
  33. *
  34. * @param spo[in] Triple pointer to initialize.
  35. */
  36. LSUP_rc
  37. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  38. /** @brief Initialize internal term pointers in a heap-allocated buffer triple.
  39. *
  40. * The triple must be freed with #LSUP_striple_free().
  41. *
  42. * @param sspo[in] Serialized triple pointer to initialize.
  43. */
  44. LSUP_rc
  45. LSUP_striple_init (
  46. LSUP_SerTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  47. /** @brief Serialize a RDF triple into a buffer triple.
  48. *
  49. * The internal structure must be freed with #LSUP_striple_done after use.
  50. *
  51. * @param spo[in] Triple to be serialized.
  52. * @param sspo[out] Buffer triple handle. It must point to an already allocated
  53. * structure.
  54. *
  55. * @return LSUP_OK or an error code resulting from #LSUP_term_serialize.
  56. */
  57. LSUP_rc
  58. LSUP_triple_serialize (const LSUP_Triple *spo, LSUP_SerTriple *sspo);
  59. /** @brief Deserialize a buffer triple into a RDF triple.
  60. *
  61. * The internal structure must be freed with #LSUP_triple_done after use.
  62. *
  63. * @param sspo[in] Buffer triple to be serialized.
  64. * @param spo[out] RDF triple handle. It must point to an already allocated
  65. * structure.
  66. *
  67. * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize.
  68. */
  69. LSUP_rc
  70. LSUP_triple_deserialize (const LSUP_SerTriple *sspo, LSUP_Triple *spo);
  71. /** @brief Free the internal pointers of a triple.
  72. *
  73. * @param spo[in] Triple to be freed.
  74. */
  75. void
  76. LSUP_triple_done (LSUP_Triple *spo);
  77. /** @brief Free the internal pointers of a buffer triple.
  78. *
  79. * @param sspo[in] Buffer triple to be freed.
  80. */
  81. void
  82. LSUP_striple_done (LSUP_SerTriple *sspo);
  83. /** @brief Free a triple and all its internal pointers.
  84. *
  85. * NOTE: If the term pointers are not to be freed (e.g. they are owned by a
  86. * back end), use a simple free(spo) instead of this.
  87. *
  88. * @param spo[in] Triple to be freed.
  89. */
  90. void
  91. LSUP_triple_free (LSUP_Triple *spo);
  92. /** @brief Free a buffer triple and all its internal pointers.
  93. *
  94. * NOTE: If the buffer pointers are not to be freed (e.g. they are owned by a
  95. * back end), use a simple free(sspo) instead of this.
  96. *
  97. * @param sspo[in] Buffer triple to be freed.
  98. */
  99. void
  100. LSUP_striple_free (LSUP_SerTriple *sspo);
  101. /** @brief Free a buffer triple and its buffer handles but not the buffer data.
  102. *
  103. * This is useful when freeing a "dummy" triple whose buffers are owned by the
  104. * caller but the data the terms point to are owned by the store.
  105. *
  106. */
  107. void
  108. LSUP_striple_free_shallow (LSUP_SerTriple *sspo);
  109. #define _FN_BODY \
  110. if (n == TRP_POS_S) return trp->s; \
  111. if (n == TRP_POS_P) return trp->p; \
  112. if (n == TRP_POS_O) return trp->o; \
  113. return NULL;
  114. /** @brief Get triple by term position.
  115. *
  116. * Useful for looping over all terms.
  117. *
  118. * @param trp[in] Triple pointer.
  119. *
  120. * @param n[in] A number between 0÷2.
  121. *
  122. * @return Corresponding triple term or NULL if n is out of range.
  123. */
  124. inline LSUP_Term *
  125. LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
  126. { _FN_BODY }
  127. /** @brief Get serialized triple by term position.
  128. *
  129. * Useful for looping over all terms.
  130. *
  131. * @param trp[in] Serialized triple pointer.
  132. *
  133. * @param n[in] A number between 0÷2.
  134. *
  135. * @return Corresponding serialized term or NULL if n is out of range.
  136. */
  137. inline LSUP_Buffer *
  138. LSUP_striple_pos (const LSUP_SerTriple *trp, LSUP_TriplePos n)
  139. { _FN_BODY }
  140. #undef _FN_BODY
  141. /** @brief Hash a buffer triple.
  142. *
  143. * TODO This doesn't handle blank nodes correctly.
  144. */
  145. inline LSUP_Key
  146. LSUP_striple_hash (const LSUP_SerTriple *strp)
  147. {
  148. return XXH64 (
  149. strp->s->addr, strp->s->size,
  150. XXH64 (
  151. strp->p->addr, strp->p->size,
  152. XXH64 (strp->o->addr, strp->o->size, HASH_SEED)
  153. )
  154. );
  155. }
  156. /** @brief Hash a triple.
  157. *
  158. * TODO This doesn't handle blank nodes correctly.
  159. */
  160. inline LSUP_Key
  161. LSUP_triple_hash (const LSUP_Triple *trp)
  162. {
  163. LSUP_SerTriple *strp = LSUP_striple_new_from_triple (trp);
  164. LSUP_Key hash = LSUP_striple_hash (strp);
  165. LSUP_striple_free (strp);
  166. return hash;
  167. }
  168. #endif