test_store_mdb.c 10 KB

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