test_store.c 17 KB

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