test_store.c 16 KB

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