test_store_mdb.c 10 KB

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