test_store_mdb.c 9.8 KB

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