test_store.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. * This runs context-aware stores in triple mode. They should behave exactly
  11. * in the same way as non-context stores.
  12. */
  13. static int test_triple_store()
  14. {
  15. if (store->sif->setup_fn)
  16. EXPECT_PASS (store->sif->setup_fn (store->id, true));
  17. log_info ("Testing triple store for %s", store->id);
  18. store->data = store->sif->new_fn (store->id, 0);
  19. ASSERT (store->data != NULL, "Error creating store data back end!");
  20. LSUP_Triple **trp = create_triples();
  21. LSUP_BufferTriple ser_trp[NUM_TRP];
  22. for (int i = 0; i < NUM_TRP; i++) {
  23. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
  24. ser_trp[i] = *tmp;
  25. free (tmp);
  26. }
  27. // Test adding.
  28. void *it = store->sif->add_init_fn (store->data, NULL, NULL);
  29. size_t ins = 0;
  30. for (size_t i = 0; i < NUM_TRP; i++) {
  31. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  32. ASSERT (rc >= 0, "Error inserting triples!");
  33. if (rc == LSUP_OK) ins++;
  34. }
  35. store->sif->add_done_fn (it);
  36. EXPECT_INT_EQ (ins, 8);
  37. EXPECT_INT_EQ (store->sif->size_fn (store->data), ins);
  38. // Test lookups.
  39. LSUP_Buffer *lut[12][3] = {
  40. {NULL, NULL, NULL},
  41. {ser_trp[0].s, NULL, NULL},
  42. {ser_trp[2].s, NULL, NULL},
  43. {NULL, ser_trp[0].p, NULL},
  44. {NULL, ser_trp[0].s, NULL},
  45. {NULL, NULL, ser_trp[6].o},
  46. {ser_trp[4].s, ser_trp[4].p, NULL},
  47. {NULL, ser_trp[7].p, ser_trp[7].o},
  48. {ser_trp[5].s, NULL, ser_trp[5].o},
  49. {ser_trp[5].s, NULL, ser_trp[5].o},
  50. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
  51. {ser_trp[4].s, ser_trp[4].p, ser_trp[6].o},
  52. };
  53. LSUP_Buffer *luc[12] = {
  54. NULL,
  55. NULL,
  56. ser_trp[2].s,
  57. NULL,
  58. NULL,
  59. NULL,
  60. NULL,
  61. NULL,
  62. NULL,
  63. ser_trp[2].s,
  64. NULL,
  65. NULL,
  66. };
  67. size_t results[12] = {
  68. 8,
  69. 5,
  70. // Lookup on nonexisting context is ignored by non-context store.
  71. store->sif->features & LSUP_STORE_CTX ? 0 : 1,
  72. 1, 0, 1,
  73. 2, 1, 2,
  74. store->sif->features & LSUP_STORE_CTX ? 0 : 2,
  75. 1, 0,
  76. };
  77. for (int i = 0; i < 12; i++) {
  78. size_t ct;
  79. log_info ("Testing triple lookup #%d.", i);
  80. void *it = store->sif->lookup_fn (
  81. store->data, lut[i][0], lut[i][1], lut[i][2], luc[i],
  82. NULL, &ct);
  83. ASSERT (it != NULL, "Error creating iterator!");
  84. EXPECT_INT_EQ (ct, results[i]);
  85. store->sif->lu_free_fn (it);
  86. }
  87. for (int i = 0; i < NUM_TRP; i++) {
  88. LSUP_buffer_free (ser_trp[i].s);
  89. LSUP_buffer_free (ser_trp[i].p);
  90. LSUP_buffer_free (ser_trp[i].o);
  91. }
  92. store->sif->free_fn (store->data);
  93. free_triples (trp);
  94. return 0;
  95. }
  96. /** @brief Test quad store.
  97. *
  98. * Insert the same triple set twice with different contexts.
  99. *
  100. * This is skipped for non-context stores.
  101. */
  102. static int test_quad_store()
  103. {
  104. if (!(store->sif->features & LSUP_STORE_CTX)) return 0;
  105. if (store->sif->setup_fn)
  106. EXPECT_PASS (store->sif->setup_fn (store->id, true));
  107. log_info ("Testing quad store for %s", store->id);
  108. store->data = store->sif->new_fn (store->id, 0);
  109. ASSERT (store->data != NULL, "Error creating store data back end!");
  110. LSUP_Triple **trp = create_triples();
  111. LSUP_BufferTriple ser_trp[NUM_TRP];
  112. for (int i = 0; i < NUM_TRP; i++) {
  113. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
  114. ser_trp[i] = *tmp;
  115. free (tmp);
  116. }
  117. void *it;
  118. size_t ins;
  119. // Only triples 0÷5 in default context.
  120. it = store->sif->add_init_fn (store->data, NULL, NULL);
  121. ins = 0;
  122. for (size_t i = 0; i < 6; i++) {
  123. log_info ("Inserting triple #%d in default context.", i);
  124. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  125. EXPECT_PASS (rc);
  126. if (rc == LSUP_OK) ins++;
  127. }
  128. store->sif->add_done_fn (it);
  129. EXPECT_INT_EQ (ins, 6);
  130. LSUP_Buffer *sc1 = LSUP_default_ctx_buf;
  131. LSUP_Term *ctx2 = LSUP_iriref_new("urn:c:2", NULL);
  132. LSUP_Buffer *sc2 = LSUP_term_serialize (ctx2);
  133. // Only triples 4÷9 in context 2 (effectively 4 non-duplicates).
  134. it = store->sif->add_init_fn (store->data, sc2, NULL);
  135. ASSERT (it != NULL, "Error creating iterator!");
  136. ins = 0;
  137. for (size_t i = 4; i < 10; i++) {
  138. log_info ("Inserting triple #%d in context 2.", i);
  139. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  140. ASSERT (rc == LSUP_OK || rc == LSUP_NOACTION, "Error adding triples!");
  141. if (rc == LSUP_OK) ins++;
  142. }
  143. store->sif->add_done_fn (it);
  144. EXPECT_INT_EQ (ins, 4);
  145. // 6 triples in ctx1 + 6 in ctx2 - 2 duplicates
  146. EXPECT_INT_EQ (store->sif->size_fn (store->data), 10);
  147. // This context has no triples.
  148. LSUP_Term *ctx3 = LSUP_iriref_new("urn:c:3", NULL);
  149. LSUP_Buffer *sc3 = LSUP_term_serialize (ctx3);
  150. // Test lookups.
  151. LSUP_Buffer *lut[41][3] = {
  152. // Any context
  153. {NULL, NULL, NULL}, // #0
  154. {ser_trp[0].s, NULL, NULL}, // #1
  155. {NULL, ser_trp[0].p, NULL}, // #2
  156. {NULL, ser_trp[0].s, NULL}, // #3
  157. {NULL, NULL, ser_trp[6].o}, // #4
  158. {ser_trp[4].s, ser_trp[4].p, NULL}, // #5
  159. {NULL, ser_trp[7].p, ser_trp[7].o}, // #6
  160. {ser_trp[5].s, NULL, ser_trp[5].o}, // #7
  161. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #8
  162. {ser_trp[2].s, ser_trp[4].p, ser_trp[5].o}, // #9
  163. // Context 1 (trp[0÷5])
  164. {NULL, NULL, NULL}, // #10
  165. {ser_trp[0].s, NULL, NULL}, // #11
  166. {ser_trp[2].s, NULL, NULL}, // #12
  167. {NULL, ser_trp[0].p, NULL}, // #13
  168. {NULL, ser_trp[6].p, NULL}, // #14
  169. {NULL, NULL, ser_trp[6].o}, // #15
  170. {ser_trp[4].s, ser_trp[4].p, NULL}, // #16
  171. {NULL, ser_trp[7].p, ser_trp[7].o}, // #17
  172. {ser_trp[5].s, NULL, ser_trp[5].o}, // #18
  173. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #19
  174. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #20
  175. // Context 2 (trp[4÷9])
  176. {NULL, NULL, NULL}, // #21
  177. {ser_trp[0].s, NULL, NULL}, // #22
  178. {NULL, ser_trp[0].p, NULL}, // #23
  179. {NULL, ser_trp[0].s, NULL}, // #24
  180. {NULL, NULL, ser_trp[6].o}, // #25
  181. {ser_trp[4].s, ser_trp[4].p, NULL}, // #26
  182. {NULL, ser_trp[7].p, ser_trp[7].o}, // #27
  183. {ser_trp[5].s, NULL, ser_trp[5].o}, // #28
  184. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #29
  185. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #30
  186. // Non-existing context
  187. {NULL, NULL, NULL}, // #31
  188. {ser_trp[0].s, NULL, NULL}, // #32
  189. {NULL, ser_trp[0].p, NULL}, // #33
  190. {NULL, ser_trp[0].s, NULL}, // #34
  191. {NULL, NULL, ser_trp[6].o}, // #35
  192. {ser_trp[4].s, ser_trp[4].p, NULL}, // #36
  193. {NULL, ser_trp[7].p, ser_trp[7].o}, // #37
  194. {ser_trp[5].s, NULL, ser_trp[5].o}, // #38
  195. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #39
  196. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #40
  197. };
  198. LSUP_Buffer *luc[41] = {
  199. // Any context
  200. NULL, // #0
  201. NULL, NULL, NULL, NULL, // #1-#4
  202. NULL, NULL, NULL, // #5-#7
  203. NULL, NULL, // #8-#9
  204. // Context 1 (trp[0÷5])
  205. sc1, // #10
  206. sc1, sc1, sc1, sc1, sc1, // #11-#15
  207. sc1, sc1, sc1, // #16-#18
  208. sc1, sc1, // #19-#20
  209. // Context 2 (trp[4÷9])
  210. sc2, // #21
  211. sc2, sc2, sc2, sc2, // #22-#25
  212. sc2, sc2, sc2, // #26-#28
  213. sc2, sc2, // #29-#30
  214. // Non-existing context
  215. sc3, // #31
  216. sc3, sc3, sc3, sc3, // #32-#35
  217. sc3, sc3, sc3, // #36-#38
  218. sc3, sc3, // #39-#40
  219. };
  220. size_t results[41] = {
  221. // NULL ctx
  222. 8, // #0
  223. 5, 1, 0, 1, // #1-#4
  224. 2, 1, 2, // #5-#7
  225. 1, 0, // #8-#9
  226. // ctx1
  227. 6, // #10
  228. 4, 1, 1, 0, 0, // #11-#15
  229. 1, 0, 1, // #16-#18
  230. 1, 0, // #19-#20
  231. // ctx2
  232. 4, // #21
  233. 3, 0, 0, 1, // #22-#25
  234. 2, 1, 2, // #26-#28
  235. 1, 1, // #29-#30
  236. // ctx3
  237. 0, // #31-#32
  238. 0, 0, 0, 0, // #33-#36
  239. 0, 0, 0, // #37-#39
  240. 0, 0, // #40
  241. };
  242. int ctx_ct[10] = {
  243. // BEGIN ctx1 (triples 0÷5)
  244. 1, 1, 1, 1,
  245. // BEGIN ctx2 (triples 4÷9)
  246. 2, 2,
  247. // END ctx 1
  248. 1, 1, 1, 1,
  249. // END ctx 2
  250. };
  251. for (int i = 0; i < 41; i++) {
  252. size_t ct;
  253. log_info ("Checking triple #%d.", i);
  254. void *it = store->sif->lookup_fn (
  255. store->data, lut[i][0], lut[i][1], lut[i][2], luc[i],
  256. NULL, &ct);
  257. ASSERT (it != NULL, "Lookup error!");
  258. EXPECT_INT_EQ (ct, results[i]);
  259. store->sif->lu_free_fn (it);
  260. }
  261. // Check triple contexts.
  262. for (int i = 0; i < 10; i++) {
  263. void *it = store->sif->lookup_fn (
  264. store->data, ser_trp[i].s, ser_trp[i].p, ser_trp[i].o,
  265. NULL, NULL, NULL);
  266. log_info ("Checking contexts for triple %d.", i);
  267. LSUP_Buffer *ctx_a;
  268. EXPECT_PASS (store->sif->lu_next_fn (it, NULL, &ctx_a));
  269. store->sif->lu_free_fn (it);
  270. ASSERT (ctx_a != NULL, "No contexts found!");
  271. size_t j = 0;
  272. while (ctx_a[j].addr) j++;
  273. free (ctx_a);
  274. EXPECT_INT_EQ (j, ctx_ct[i]);
  275. }
  276. for (int i = 0; i < NUM_TRP; i++) {
  277. LSUP_buffer_free (ser_trp[i].s);
  278. LSUP_buffer_free (ser_trp[i].p);
  279. LSUP_buffer_free (ser_trp[i].o);
  280. }
  281. LSUP_term_free (ctx2);
  282. LSUP_term_free (ctx3);
  283. LSUP_buffer_free (sc2);
  284. LSUP_buffer_free (sc3);
  285. free_triples (trp);
  286. store->sif->free_fn (store->data);
  287. return 0;
  288. }
  289. static int test_txn_commit (void)
  290. {
  291. if (!(store->sif->features & LSUP_STORE_TXN)) return 0;
  292. ASSERT (
  293. store->sif->txn_begin_fn != NULL,
  294. "Transaction begin function is NULL!");
  295. ASSERT (
  296. store->sif->txn_commit_fn != NULL,
  297. "Transaction commit function is NULL!");
  298. ASSERT (
  299. store->sif->txn_abort_fn != NULL,
  300. "Transaction abort function is NULL!");
  301. ASSERT (
  302. store->sif->add_abort_fn != NULL,
  303. "Add abort function is NULL!");
  304. ASSERT (
  305. store->sif->iter_txn_fn != NULL,
  306. "Iterator transaction function is NULL!");
  307. if (store->sif->setup_fn)
  308. EXPECT_PASS (store->sif->setup_fn (store->id, true));
  309. log_info ("Testing transaction control for %s", store->id);
  310. store->data = store->sif->new_fn (store->id, 0);
  311. ASSERT (store->data != NULL, "Error creating store data back end!");
  312. LSUP_Triple **trp = create_triples();
  313. LSUP_BufferTriple ser_trp[NUM_TRP];
  314. for (int i = 0; i < NUM_TRP; i++) {
  315. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
  316. ser_trp[i] = *tmp;
  317. free (tmp);
  318. }
  319. void *it;
  320. // Start adding then commit.
  321. it = store->sif->add_init_fn (store->data, NULL, NULL);
  322. for (size_t i = 0; i < NUM_TRP; i++) {
  323. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  324. ASSERT (rc >= 0, "Error inserting triples!");
  325. }
  326. store->sif->add_abort_fn (it);
  327. EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
  328. // Add within a transaction, commit, then commit parent transaction.
  329. void *txn;
  330. EXPECT_PASS (store->sif->txn_begin_fn (store->data, 0, &txn));
  331. it = store->sif->add_init_fn (store->data, NULL, txn);
  332. for (size_t i = 0; i < NUM_TRP; i++) {
  333. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  334. ASSERT (rc >= 0, "Error inserting triples!");
  335. }
  336. store->sif->add_done_fn (it);
  337. // Triples are added in child txn but parent is still open.
  338. // Size function always calculates outside of all transactions.
  339. EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
  340. size_t ct;
  341. it = store->sif->lookup_fn (
  342. store->data, NULL, NULL, NULL, NULL, txn, &ct);
  343. store->sif->lu_free_fn (it);
  344. // Should show triples added within the parent txn.
  345. EXPECT_INT_EQ (ct, 8);
  346. // commit child txn operations.
  347. EXPECT_PASS (store->sif->txn_commit_fn (txn));
  348. it = store->sif->lookup_fn (
  349. store->data, NULL, NULL, NULL, NULL, NULL, &ct);
  350. store->sif->lu_free_fn (it);
  351. EXPECT_INT_EQ (ct, 8);
  352. EXPECT_INT_EQ (store->sif->size_fn (store->data), 8);
  353. for (int i = 0; i < NUM_TRP; i++) {
  354. LSUP_buffer_free (ser_trp[i].s);
  355. LSUP_buffer_free (ser_trp[i].p);
  356. LSUP_buffer_free (ser_trp[i].o);
  357. }
  358. store->sif->free_fn (store->data);
  359. free_triples (trp);
  360. return 0;
  361. }
  362. static int test_txn_abort (void)
  363. {
  364. if (!(store->sif->features & LSUP_STORE_TXN)) return 0;
  365. if (store->sif->setup_fn)
  366. EXPECT_PASS (store->sif->setup_fn (store->id, true));
  367. log_info ("Testing transaction control for %s", store->id);
  368. store->data = store->sif->new_fn (store->id, 0);
  369. ASSERT (store->data != NULL, "Error creating store data back end!");
  370. LSUP_Triple **trp = create_triples();
  371. LSUP_BufferTriple ser_trp[NUM_TRP];
  372. for (int i = 0; i < NUM_TRP; i++) {
  373. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp[i]);
  374. ser_trp[i] = *tmp;
  375. free (tmp);
  376. }
  377. void *it;
  378. // Start adding then abort.
  379. it = store->sif->add_init_fn (store->data, NULL, NULL);
  380. for (size_t i = 0; i < NUM_TRP; i++) {
  381. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  382. ASSERT (rc >= 0, "Error inserting triples!");
  383. }
  384. store->sif->add_abort_fn (it);
  385. EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
  386. // Add within a transaction, commit, then abort parent transaction.
  387. void *txn;
  388. EXPECT_PASS (store->sif->txn_begin_fn (store->data, 0, &txn));
  389. it = store->sif->add_init_fn (store->data, NULL, txn);
  390. for (size_t i = 0; i < NUM_TRP; i++) {
  391. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  392. ASSERT (rc >= 0, "Error inserting triples!");
  393. }
  394. store->sif->add_done_fn (it);
  395. // Triples are added in child txn but parent is still open.
  396. // Size function always calculates outside of all transactions.
  397. EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
  398. size_t ct;
  399. it = store->sif->lookup_fn (
  400. store->data, NULL, NULL, NULL, NULL, txn, &ct);
  401. store->sif->lu_free_fn (it);
  402. // Should show triples added within the parent txn.
  403. EXPECT_INT_EQ (ct, 8);
  404. // Discard child txn operations.
  405. store->sif->txn_abort_fn (txn);
  406. it = store->sif->lookup_fn (
  407. store->data, NULL, NULL, NULL, NULL, NULL, &ct);
  408. store->sif->lu_free_fn (it);
  409. EXPECT_INT_EQ (ct, 0);
  410. for (int i = 0; i < NUM_TRP; i++) {
  411. LSUP_buffer_free (ser_trp[i].s);
  412. LSUP_buffer_free (ser_trp[i].p);
  413. LSUP_buffer_free (ser_trp[i].o);
  414. }
  415. store->sif->free_fn (store->data);
  416. free_triples (trp);
  417. return 0;
  418. }
  419. int store_tests()
  420. {
  421. #define ENTRY(a, b) \
  422. store->type = LSUP_STORE_##a; \
  423. store->id = STORE_ID_##a; \
  424. store->sif = &b; \
  425. RUN (test_triple_store); \
  426. RUN (test_quad_store); \
  427. RUN (test_txn_commit); \
  428. RUN (test_txn_abort);
  429. BACKEND_TBL
  430. #undef ENTRY
  431. return 0;
  432. }