test_store_mdb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "test.h"
  2. #include "store_mdb.h"
  3. #include "assets/triples.h"
  4. static char *path = "/tmp/testdb";
  5. /** @brief Test triple store.
  6. */
  7. static int test_triple_store()
  8. {
  9. EXPECT_PASS (LSUP_mdbstore_setup (path, true));
  10. LSUP_MDBStore *store = LSUP_mdbstore_new (path, NULL); // triple store.
  11. ASSERT (store != NULL, "Error initializing store!");
  12. LSUP_Triple *trp = create_triples();
  13. LSUP_BufferTriple ser_trp[NUM_TRP];
  14. for (int i = 0; i < NUM_TRP; i++) {
  15. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
  16. ser_trp[i] = *tmp;
  17. free (tmp);
  18. }
  19. // Test adding.
  20. size_t ins;
  21. EXPECT_PASS (LSUP_mdbstore_add (store, NULL, ser_trp, NUM_TRP, &ins));
  22. EXPECT_INT_EQ (ins, 8);
  23. EXPECT_INT_EQ (LSUP_mdbstore_size (store), 8);
  24. // Test lookups.
  25. LSUP_Buffer *lut[12][3] = {
  26. {NULL, NULL, NULL},
  27. {ser_trp[0].s, NULL, NULL},
  28. {ser_trp[2].s, NULL, NULL},
  29. {NULL, ser_trp[0].p, NULL},
  30. {NULL, ser_trp[0].s, NULL},
  31. {NULL, NULL, ser_trp[6].o},
  32. {ser_trp[4].s, ser_trp[4].p, NULL},
  33. {NULL, ser_trp[7].p, ser_trp[7].o},
  34. {ser_trp[5].s, NULL, ser_trp[5].o},
  35. {ser_trp[5].s, NULL, ser_trp[5].o},
  36. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
  37. {ser_trp[4].s, ser_trp[4].p, ser_trp[6].o},
  38. };
  39. LSUP_Buffer *luc[12] = {
  40. NULL,
  41. NULL,
  42. ser_trp[2].s,
  43. NULL,
  44. NULL,
  45. NULL,
  46. NULL,
  47. NULL,
  48. NULL,
  49. ser_trp[2].s,
  50. NULL,
  51. NULL,
  52. };
  53. size_t results[12] = {
  54. 8,
  55. 5, 1, 1, 0, 1,
  56. 2, 1, 2, 2,
  57. 1, 0,
  58. };
  59. for (int i = 0; i < 12; i++) {
  60. size_t ct;
  61. log_info ("Testing triple lookup #%d.", i);
  62. LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
  63. store, lut[i][0], lut[i][1], lut[i][2], luc[i], &ct);
  64. ASSERT (it != NULL, "Error creating iterator!");
  65. EXPECT_INT_EQ (ct, results[i]);
  66. LSUP_mdbiter_free (it);
  67. }
  68. for (int i = 0; i < NUM_TRP; i++) {
  69. LSUP_buffer_free (ser_trp[i].s);
  70. LSUP_buffer_free (ser_trp[i].p);
  71. LSUP_buffer_free (ser_trp[i].o);
  72. }
  73. LSUP_mdbstore_free (store);
  74. free_triples (trp);
  75. return 0;
  76. }
  77. /** @brief Test quad store.
  78. *
  79. * Insert the same triple set twice with different contexts.
  80. */
  81. static int test_quad_store()
  82. {
  83. EXPECT_PASS (LSUP_mdbstore_setup (path, true));
  84. LSUP_Term *ctx1 = LSUP_iriref_new("urn:c:1", NULL);
  85. LSUP_Buffer *sc1 = LSUP_term_serialize (ctx1);
  86. LSUP_MDBStore *store = LSUP_mdbstore_new (path, sc1); // quad store.
  87. ASSERT (store != NULL, "Error initializing store!");
  88. LSUP_Triple *trp = create_triples();
  89. LSUP_BufferTriple ser_trp[NUM_TRP];
  90. for (int i = 0; i < NUM_TRP; i++) {
  91. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
  92. ser_trp[i] = *tmp;
  93. free (tmp);
  94. }
  95. // Only triples 0÷5 in default context.
  96. size_t ct;
  97. EXPECT_PASS (LSUP_mdbstore_add (store, NULL, ser_trp, 6, &ct));
  98. EXPECT_INT_EQ (ct, 6);
  99. LSUP_Term *ctx2 = LSUP_iriref_new("urn:c:2", NULL);
  100. LSUP_Buffer *sc2 = LSUP_term_serialize (ctx2);
  101. // Only triples 4÷9 in context 2 (effectively 4 non-duplicates).
  102. EXPECT_PASS (LSUP_mdbstore_add (store, sc2, ser_trp + 4, 6, &ct));
  103. EXPECT_INT_EQ (ct, 4);
  104. // 6 triples in ctx1 + 6 in ctx2 - 2 duplicates
  105. EXPECT_INT_EQ (LSUP_mdbstore_size (store), 10);
  106. // This context has no triples.
  107. LSUP_Term *ctx3 = LSUP_iriref_new("urn:c:3", NULL);
  108. LSUP_Buffer *sc3 = LSUP_term_serialize (ctx3);
  109. // Test lookups.
  110. LSUP_Buffer *lut[41][3] = {
  111. // Any context
  112. {NULL, NULL, NULL}, // #0
  113. {ser_trp[0].s, NULL, NULL}, // #1
  114. {NULL, ser_trp[0].p, NULL}, // #2
  115. {NULL, ser_trp[0].s, NULL}, // #3
  116. {NULL, NULL, ser_trp[6].o}, // #4
  117. {ser_trp[4].s, ser_trp[4].p, NULL}, // #5
  118. {NULL, ser_trp[7].p, ser_trp[7].o}, // #6
  119. {ser_trp[5].s, NULL, ser_trp[5].o}, // #7
  120. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #8
  121. {ser_trp[2].s, ser_trp[4].p, ser_trp[5].o}, // #9
  122. // Context 1 (trp[0÷5])
  123. {NULL, NULL, NULL}, // #10
  124. {ser_trp[0].s, NULL, NULL}, // #11
  125. {ser_trp[2].s, NULL, NULL}, // #12
  126. {NULL, ser_trp[0].p, NULL}, // #13
  127. {NULL, ser_trp[6].p, NULL}, // #14
  128. {NULL, NULL, ser_trp[6].o}, // #15
  129. {ser_trp[4].s, ser_trp[4].p, NULL}, // #16
  130. {NULL, ser_trp[7].p, ser_trp[7].o}, // #17
  131. {ser_trp[5].s, NULL, ser_trp[5].o}, // #18
  132. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #19
  133. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #20
  134. // Context 2 (trp[4÷9])
  135. {NULL, NULL, NULL}, // #21
  136. {ser_trp[0].s, NULL, NULL}, // #22
  137. {NULL, ser_trp[0].p, NULL}, // #23
  138. {NULL, ser_trp[0].s, NULL}, // #24
  139. {NULL, NULL, ser_trp[6].o}, // #25
  140. {ser_trp[4].s, ser_trp[4].p, NULL}, // #26
  141. {NULL, ser_trp[7].p, ser_trp[7].o}, // #27
  142. {ser_trp[5].s, NULL, ser_trp[5].o}, // #28
  143. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #29
  144. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #30
  145. // Non-existing context
  146. {NULL, NULL, NULL}, // #31
  147. {ser_trp[0].s, NULL, NULL}, // #32
  148. {NULL, ser_trp[0].p, NULL}, // #33
  149. {NULL, ser_trp[0].s, NULL}, // #34
  150. {NULL, NULL, ser_trp[6].o}, // #35
  151. {ser_trp[4].s, ser_trp[4].p, NULL}, // #36
  152. {NULL, ser_trp[7].p, ser_trp[7].o}, // #37
  153. {ser_trp[5].s, NULL, ser_trp[5].o}, // #38
  154. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #39
  155. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #40
  156. };
  157. LSUP_Buffer *luc[41] = {
  158. // Any context
  159. NULL, // #0
  160. NULL, NULL, NULL, NULL, // #1-#4
  161. NULL, NULL, NULL, // #5-#7
  162. NULL, NULL, // #8-#9
  163. // Context 1 (trp[0÷5])
  164. sc1, // #10
  165. sc1, sc1, sc1, sc1, sc1, // #11-#15
  166. sc1, sc1, sc1, // #16-#18
  167. sc1, sc1, // #19-#20
  168. // Context 2 (trp[4÷9])
  169. sc2, // #21
  170. sc2, sc2, sc2, sc2, // #22-#25
  171. sc2, sc2, sc2, // #26-#28
  172. sc2, sc2, // #29-#30
  173. // Non-existing context
  174. sc3, // #31
  175. sc3, sc3, sc3, sc3, // #32-#35
  176. sc3, sc3, sc3, // #36-#38
  177. sc3, sc3, // #39-#40
  178. };
  179. size_t results[41] = {
  180. // NULL ctx
  181. 8, // #0
  182. 5, 1, 0, 1, // #1-#4
  183. 2, 1, 2, // #5-#7
  184. 1, 0, // #8-#9
  185. // ctx1
  186. 6, // #10
  187. 4, 1, 1, 0, 0, // #11-#15
  188. 1, 0, 1, // #16-#18
  189. 1, 0, // #19-#20
  190. // ctx2
  191. 4, // #21
  192. 3, 0, 0, 1, // #22-#25
  193. 2, 1, 2, // #26-#28
  194. 1, 1, // #29-#30
  195. // ctx3
  196. 0, // #31-#32
  197. 0, 0, 0, 0, // #33-#36
  198. 0, 0, 0, // #37-#39
  199. 0, 0, // #40
  200. };
  201. int ctx_ct[10] = {
  202. 2,
  203. 2, 1, 0, 1,
  204. 2, 1, 2,
  205. 2, 0,
  206. };
  207. for (int i = 0; i < 41; i++) {
  208. size_t ct;
  209. log_info ("Checking triple #%d.", i);
  210. LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
  211. store, lut[i][0], lut[i][1], lut[i][2], luc[i], &ct);
  212. ASSERT (it != NULL, "Lookup error!");
  213. EXPECT_INT_EQ (ct, results[i]);
  214. LSUP_mdbiter_free (it);
  215. }
  216. // Check triple contexts.
  217. for (int i = 0; i < 10; i++) {
  218. log_info ("Checking contexts for triple %d.", i);
  219. LSUP_Buffer **ctx_a = LSUP_mdbstore_lookup_contexts (
  220. store, lut[i][0], lut[i][1], lut[i][2]);
  221. ASSERT (ctx_a != NULL, "Value is NULL!");
  222. int j = 0;
  223. while (ctx_a[j] != NULL)
  224. free (ctx_a[j++]); // Buffer data are memory-mapped. Not freeing.
  225. free (ctx_a);
  226. EXPECT_INT_EQ (j, ctx_ct[i]);
  227. }
  228. for (int i = 0; i < NUM_TRP; i++) {
  229. LSUP_buffer_free (ser_trp[i].s);
  230. LSUP_buffer_free (ser_trp[i].p);
  231. LSUP_buffer_free (ser_trp[i].o);
  232. }
  233. LSUP_term_free (ctx1);
  234. LSUP_term_free (ctx2);
  235. LSUP_term_free (ctx3);
  236. LSUP_buffer_free (sc1);
  237. LSUP_buffer_free (sc2);
  238. LSUP_buffer_free (sc3);
  239. free_triples (trp);
  240. LSUP_mdbstore_free (store);
  241. return 0;
  242. }
  243. int store_mdb_tests()
  244. {
  245. RUN (test_triple_store);
  246. RUN (test_quad_store);
  247. return 0;
  248. }