test_store_mdb.c 10 KB

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