store_mdb.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. #include "store_mdb.h"
  2. /**
  3. * Number of DBs defined. See MAIN_TABLE and LOOKUP_TABLE defines below.
  4. */
  5. #define N_DB 13
  6. /**
  7. * Memory map size.
  8. */
  9. #if (defined DEBUG || defined TESTING)
  10. #define DEFAULT_MAPSIZE 1<<24 // 16Mb (limit for Valgrind)
  11. #elif !(defined __LP64__ || defined __LLP64__) || \
  12. defined _WIN32 && !defined _WIN64
  13. #define DEFAULT_MAPSIZE 1<<31 // 2Gb (limit for 32-bit systems)
  14. #else
  15. #define DEFAULT_MAPSIZE 1UL<<40 // 1Tb
  16. #endif
  17. #define ENV_DIR_MODE 0750
  18. #define ENV_FILE_MODE 0640
  19. /*
  20. * Data types.
  21. */
  22. typedef char DbLabel[8];
  23. typedef struct mdbstore_iter_t MDBIterator;
  24. /// Store state flags.
  25. typedef enum {
  26. LSSTORE_OPEN = 1<<0, ///< Env is open.
  27. } StoreFlags;
  28. /// Iterator state flags.
  29. typedef enum {
  30. ITER_OPEN_TXN = 1<<0, /**< A transaction is open.
  31. *
  32. * The iterator has begun a new
  33. * transaction on initialization
  34. * which needs to be closed. If
  35. * false, the iterator is using an
  36. * existing transaction which will
  37. * not be closed with
  38. * #mdbiter_free().
  39. */
  40. } IterFlags;
  41. typedef enum {
  42. OP_ADD,
  43. OP_REMOVE,
  44. } StoreOp;
  45. typedef struct mdbstore_t {
  46. MDB_env * env; ///< Environment handle.
  47. MDB_dbi dbi[N_DB]; ///< DB handles. Refer to DbIdx enum.
  48. StoreFlags flags; ///< Store state flags.
  49. } MDBStore;
  50. /** @brief Iterator operation.
  51. *
  52. * Function executed for each iteration of a #MDBIterator. It assumes that a
  53. * result triple has already been found and is ready to be composed and
  54. * yielded.
  55. *
  56. * Upon call, the rc value of the iterator structure is set to the MDB_* rc
  57. * value for the next result. It is up to the caller to evaluate this value
  58. * and decide whether to call the function again.
  59. */
  60. typedef void (*iter_op_fn_t)(MDBIterator *it);
  61. /// Triple iterator.
  62. typedef struct mdbstore_iter_t {
  63. MDBStore * store; ///< MDB store handle.
  64. IterFlags flags; ///< Iterator flags.
  65. MDB_txn * txn; ///< MDB transaction.
  66. MDB_cursor * cur; ///< MDB cursor.
  67. MDB_cursor * ctx_cur; ///< MDB c:spo index cursor.
  68. MDB_val key; ///< Internal data handler.
  69. MDB_val data; ///< Internal data handler.
  70. LSUP_TripleKey spok; ///< Triple to be populated with match.
  71. LSUP_Key * ck; /**< Context array.
  72. *
  73. * This shall be populated for each
  74. * matching triple if requested. */
  75. iter_op_fn_t iter_op_fn; ///< Function used to look up next match.
  76. const uint8_t * term_order; ///< Term order used in 1-2bound look-ups.
  77. LSUP_Key luk[3]; ///< 0÷3 lookup keys.
  78. LSUP_Key luc; ///< Ctx key to filter by. May be NULL_KEY.
  79. size_t i; ///< Internal counter for paged lookups.
  80. size_t ct; ///< Current count of records inserted or
  81. ///< results found.
  82. int rc; ///< MDB_* return code for the next result.
  83. } MDBIterator;
  84. /*
  85. * Static variables.
  86. */
  87. #define DUPSORT_MASK MDB_DUPSORT
  88. #define DUPFIXED_MASK MDB_DUPSORT | MDB_DUPFIXED
  89. /**
  90. * Main DBs. These are the master information containers.
  91. *
  92. * The number of entries must match the N_DB constant defined above.
  93. */
  94. #define MAIN_TABLE \
  95. /* #ID pfx #DB label #Flags */ \
  96. ENTRY( T_ST, "t:st", 0 ) /* Key to ser. term */ \
  97. ENTRY( SPO_C, "spo:c", DUPFIXED_MASK ) /* Triple to context */ \
  98. ENTRY( C_, "c:", 0 ) /* Track empty ctx */ \
  99. ENTRY( PFX_NS, "pfx:ns", 0 ) /* Prefix to NS */ \
  100. ENTRY( IDK_ID, "idk:id", 0 ) /* ID key to ID */ \
  101. /**
  102. * Lookup DBs. These are indices and may be destroyed and rebuilt.
  103. */
  104. #define LOOKUP_TABLE \
  105. /* #ID pfx #DB label #Flags */ \
  106. ENTRY( S_PO, "s:po", DUPFIXED_MASK ) /* 1-bound lookup */ \
  107. ENTRY( P_SO, "p:so", DUPFIXED_MASK ) /* 1-bound lookup */ \
  108. ENTRY( O_SP, "o:sp", DUPFIXED_MASK ) /* 1-bound lookup */ \
  109. ENTRY( PO_S, "po:s", DUPFIXED_MASK ) /* 2-bound lookup */ \
  110. ENTRY( SO_P, "so:p", DUPFIXED_MASK ) /* 2-bound lookup */ \
  111. ENTRY( SP_O, "sp:o", DUPFIXED_MASK ) /* 2-bound lookup */ \
  112. ENTRY( C_SPO, "c:spo", DUPFIXED_MASK ) /* Context lookup */ \
  113. ENTRY( NS_PFX, "ns:pfx", DUPSORT_MASK ) /* NS to prefix */ \
  114. /**
  115. * DB labels. They are prefixed with DB_
  116. */
  117. #define ENTRY(a, b, c) static const DbLabel DB_##a = b;
  118. MAIN_TABLE
  119. LOOKUP_TABLE
  120. #undef ENTRY
  121. /*
  122. * Numeric index of each DB. Prefixed with IDX_
  123. *
  124. * These index numbers are referred to in all the arrays defeined below. They
  125. * are independent from the LMDB dbi values which are considered opaque here.
  126. */
  127. typedef enum {
  128. #define ENTRY(a, b, c) IDX_##a,
  129. MAIN_TABLE
  130. LOOKUP_TABLE
  131. #undef ENTRY
  132. } DBIdx;
  133. /**
  134. * DB labels.
  135. */
  136. static const char *db_labels[N_DB] = {
  137. #define ENTRY(a, b, c) DB_##a,
  138. MAIN_TABLE
  139. LOOKUP_TABLE
  140. #undef ENTRY
  141. };
  142. /*
  143. * DB flags. These are aligned with the dbi_labels index.
  144. */
  145. static const unsigned int db_flags[N_DB] = {
  146. #define ENTRY(a, b, c) c,
  147. MAIN_TABLE
  148. LOOKUP_TABLE
  149. #undef ENTRY
  150. };
  151. /*
  152. * 1-bound and 2-bound lookup indices.
  153. *
  154. * N.B. Only the first 6 (1-bound and 2-bound term lookup) are used.
  155. * The others are added just because they belong logically to the lookup table.
  156. */
  157. static DBIdx lookup_indices[9] = {
  158. #define ENTRY(a, b, c) IDX_##a,
  159. LOOKUP_TABLE
  160. #undef ENTRY
  161. };
  162. static const uint8_t lookup_ordering_1bound[3][3] = {
  163. {0, 1, 2}, // s:po
  164. {1, 0, 2}, // p:so
  165. {2, 0, 1}, // o:sp
  166. };
  167. static const uint8_t lookup_ordering_2bound[3][3] = {
  168. {1, 2, 0}, // po:s
  169. {0, 2, 1}, // so:p
  170. {0, 1, 2}, // sp:o
  171. };
  172. /*
  173. * Static prototypes.
  174. */
  175. static int index_triple(
  176. MDBStore *store, StoreOp op, LSUP_TripleKey spok, LSUP_Key ck,
  177. MDB_txn *txn);
  178. static LSUP_rc mdbstore_add_term (void *h, const LSUP_Buffer *sterm, void *th);
  179. inline static LSUP_rc lookup_0bound (MDBIterator *it, size_t *ct);
  180. inline static LSUP_rc lookup_1bound (
  181. uint8_t idx0, MDBIterator *it, size_t *ct);
  182. inline static LSUP_rc lookup_2bound (
  183. uint8_t idx0, uint8_t idx1, MDBIterator *it, size_t *ct);
  184. inline static LSUP_rc lookup_3bound(MDBIterator *it, size_t *ct);
  185. /**
  186. * Store interface.
  187. */
  188. static LSUP_NSMap *
  189. mdbstore_nsm_get (void *h)
  190. {
  191. MDBStore *store = h;
  192. LSUP_NSMap *nsm = LSUP_nsmap_new();
  193. if (UNLIKELY (!nsm)) return NULL;
  194. MDB_txn *txn;
  195. mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
  196. MDB_cursor *cur;
  197. if (mdb_cursor_open (txn, store->dbi[IDX_PFX_NS], &cur) != MDB_SUCCESS) {
  198. mdb_txn_abort (txn);
  199. return NULL;
  200. }
  201. MDB_val ns_v, pfx_v;
  202. if (mdb_cursor_get (cur, &pfx_v, &ns_v, MDB_FIRST) != MDB_SUCCESS)
  203. goto finally;
  204. do {
  205. ns_pfx pfx;
  206. char *ns = malloc (ns_v.mv_size);
  207. strncpy (pfx, pfx_v.mv_data, pfx_v.mv_size);
  208. strncpy (ns, ns_v.mv_data, ns_v.mv_size);
  209. LSUP_nsmap_add (nsm, pfx, ns);
  210. free (ns);
  211. } while (mdb_cursor_get (
  212. cur, &pfx_v, &ns_v, MDB_NEXT_NODUP) == MDB_SUCCESS);
  213. finally:
  214. mdb_cursor_close (cur);
  215. mdb_txn_abort (txn);
  216. return nsm;
  217. }
  218. static LSUP_rc
  219. mdbstore_nsm_put (void *h, const LSUP_NSMap *nsm, void *th)
  220. {
  221. MDBStore *store = h;
  222. MDB_txn *txn;
  223. RCCK (mdb_txn_begin (store->env, (MDB_txn *) th, 0, &txn));
  224. LSUP_rc rc = LSUP_NOACTION;
  225. int db_rc;
  226. MDB_cursor *dcur = NULL, *icur = NULL;
  227. if (
  228. mdb_cursor_open (txn, store->dbi[IDX_PFX_NS], &dcur) != MDB_SUCCESS
  229. ||
  230. mdb_cursor_open (txn, store->dbi[IDX_NS_PFX], &icur) != MDB_SUCCESS
  231. ) {
  232. mdb_txn_abort (txn);
  233. return LSUP_DB_ERR;
  234. }
  235. MDB_val pfx_v, ns_v;
  236. const char ***nsm_data = LSUP_nsmap_dump (nsm);
  237. for (size_t i = 0; nsm_data[i] != NULL; i++) {
  238. // At least 1 action. If not OK, it will change during the iteration.
  239. if (i == 0) rc = LSUP_OK;
  240. // On previous error, just clean up the NSM data array.
  241. if (rc < 0) goto loop_end;
  242. pfx_v.mv_data = (void *) nsm_data[i][0];
  243. pfx_v.mv_size = strlen (nsm_data[i][0]) + 1;
  244. ns_v.mv_data = (void *) nsm_data[i][1];
  245. ns_v.mv_size = strlen (nsm_data[i][1]) + 1;
  246. // If either ns or pfx exist, skip.
  247. if (
  248. mdb_cursor_get (dcur, &pfx_v, &ns_v, MDB_SET) != MDB_NOTFOUND
  249. ||
  250. mdb_cursor_get (icur, &ns_v, &pfx_v, MDB_SET) != MDB_NOTFOUND
  251. ) {
  252. rc = LSUP_CONFLICT;
  253. goto loop_end;
  254. }
  255. db_rc = mdb_cursor_put (dcur, &pfx_v, &ns_v, 0);
  256. db_rc |= mdb_cursor_put (icur, &ns_v, &pfx_v, 0);
  257. if (db_rc != MDB_SUCCESS) {
  258. log_error ("DB error: %s", LSUP_strerror (db_rc));
  259. rc = LSUP_DB_ERR;
  260. }
  261. loop_end:
  262. free (nsm_data[i]);
  263. }
  264. free (nsm_data);
  265. if (UNLIKELY (rc != LSUP_OK)) mdb_txn_abort (txn);
  266. else if (UNLIKELY (mdb_txn_commit (txn) != MDB_SUCCESS)) {
  267. mdb_txn_abort (txn);
  268. rc = LSUP_TXN_ERR;
  269. }
  270. return rc;
  271. }
  272. static const char *
  273. mdbstore_path_from_id (const char *id)
  274. {
  275. // Set environment path.
  276. if (!id) id = getenv ("LSUP_MDB_STORE_URN");
  277. if (!id) {
  278. id = LSUP_MDB_STORE_URN;
  279. log_info (
  280. "`LSUP_MDB_STORE_URN' environment variable is not "
  281. "set. The default URN %s has been set as the store ID.", id
  282. );
  283. }
  284. if (strncmp ("file://", id, 7) != 0) {
  285. log_error ("MDB store ID must be in the `file://<abs_path>` format.");
  286. return NULL;
  287. }
  288. return id + 7;
  289. }
  290. /** @brief Create the MDB environment and databases on disk.
  291. *
  292. * This function takes care of creaating the environment path if not existing,
  293. * and checking that it's a writable directory. If the path is not specified
  294. * in the LSUP_MDB_STORE_URN environment variable, a default directory is used.
  295. */
  296. static LSUP_rc
  297. mdbstore_setup (const char *id, bool clear)
  298. {
  299. const char *path = mdbstore_path_from_id (id);
  300. if (!path) return LSUP_VALUE_ERR;
  301. // If the directory exists (unless clear == true), do nothing.
  302. if (clear) rm_r (path);
  303. LSUP_rc rc = mkdir_p (path, ENV_DIR_MODE);
  304. log_info ("Create dir rc: %d", rc);
  305. log_info ("LSUP Create dir rc: %d", rc);
  306. RCCK (rc);
  307. // Open a temporary environment and txn to create the DBs.
  308. MDB_env *env;
  309. RCCK (mdb_env_create (&env));
  310. RCCK (mdb_env_set_maxdbs (env, N_DB));
  311. RCCK (mdb_env_open (env, path, 0, ENV_FILE_MODE));
  312. LOG_DEBUG("Environment opened at %s.", path);
  313. MDB_txn *txn;
  314. RCCK (mdb_txn_begin (env, NULL, 0, &txn));
  315. for (int i = 0; i < N_DB; i++) {
  316. LOG_TRACE("Creating DB %s", db_labels[i]);
  317. MDB_dbi dbi;
  318. RCCK (
  319. mdb_dbi_open (txn, db_labels[i], db_flags[i] | MDB_CREATE, &dbi)
  320. );
  321. }
  322. mdb_txn_commit (txn);
  323. mdb_env_close (env);
  324. return LSUP_OK;
  325. }
  326. /** @brief Open an MDB store.
  327. *
  328. * The store must have been set up with #mdbstore_setup.
  329. *
  330. * Some environment variables affect various store parameters:
  331. *
  332. * - LSUP_MDB_MAPSIZE Long int specifying the size of the memory map. Usually
  333. * it is not necessary to modify this, unless one is operating under memory
  334. * and disk constraints. The default map size is 1Tb.
  335. */
  336. static void *
  337. mdbstore_new (const char *id, size_t _unused)
  338. {
  339. (void) _unused;
  340. const char *path = mdbstore_path_from_id (id);
  341. if (!path) return NULL;
  342. MDBStore *store;
  343. CALLOC_GUARD (store, NULL);
  344. RCNL (mdb_env_create (&store->env));
  345. MDB_txn *txn = NULL;
  346. // Set map size.
  347. size_t mapsize;
  348. char *env_mapsize = getenv ("LSUP_MDB_MAPSIZE");
  349. if (env_mapsize == NULL) mapsize = DEFAULT_MAPSIZE;
  350. else sscanf (env_mapsize, "%lu", &mapsize);
  351. log_info (
  352. "Setting environment map size at %s to %lu Mb.",
  353. path, mapsize / 1024 / 1024);
  354. CHECK (mdb_env_set_mapsize (store->env, mapsize), fail);
  355. CHECK (mdb_env_set_maxdbs (store->env, N_DB), fail);
  356. CHECK (mdb_env_open (store->env, path, 0, ENV_FILE_MODE), fail);
  357. // Assign DB handles to store->dbi.
  358. CHECK (mdb_txn_begin (store->env, NULL, 0, &txn), fail);
  359. for (int i = 0; i < N_DB; i++)
  360. CHECK (mdb_dbi_open (
  361. txn, db_labels[i], db_flags[i], store->dbi + i), fail);
  362. // Bootstrap the permanent store with initial data.
  363. MDB_stat stat;
  364. CHECK (mdb_stat (txn, store->dbi[IDX_PFX_NS], &stat), fail);
  365. if (stat.ms_entries == 0) {
  366. LOG_DEBUG("Loading initial data into %s", path);
  367. // Load initial NS map.
  368. mdbstore_nsm_put (store, LSUP_default_nsm, txn);
  369. // Index default context.
  370. mdbstore_add_term (store, LSUP_default_ctx_buf, txn);
  371. }
  372. store->flags |= LSSTORE_OPEN;
  373. mdb_txn_commit (txn);
  374. txn = NULL;
  375. return store;
  376. fail:
  377. if (txn) mdb_txn_abort (txn);
  378. mdb_env_close (store->env);
  379. return NULL;
  380. }
  381. static void
  382. mdbstore_free (void *h)
  383. {
  384. MDBStore *store = h;
  385. if (store->flags & LSSTORE_OPEN) {
  386. const char *path;
  387. mdb_env_get_path (store->env, &path);
  388. log_info ("Closing MDB env at %s.", path);
  389. mdb_env_close (store->env);
  390. }
  391. free (store);
  392. }
  393. #if 0
  394. static char *
  395. mdbstore_id (const void *h)
  396. {
  397. const MDBStore *store = h;
  398. const char *path;
  399. mdb_env_get_path (store->env, &path);
  400. return strcat ("file://", path);
  401. }
  402. #endif
  403. static LSUP_rc
  404. mdbstore_stat (const MDBStore *store, MDB_stat *stat)
  405. {
  406. if (!(store->flags & LSSTORE_OPEN)) return 0;
  407. MDB_txn *txn;
  408. mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
  409. if (mdb_stat (txn, store->dbi[IDX_SPO_C], stat) != MDB_SUCCESS)
  410. return LSUP_DB_ERR;
  411. mdb_txn_abort (txn);
  412. return LSUP_OK;
  413. }
  414. static size_t
  415. mdbstore_size (const void *h)
  416. {
  417. const MDBStore *store = h;
  418. // Size is calculated outside of any pending write txn.
  419. MDB_stat stat;
  420. if (mdbstore_stat (store, &stat) != LSUP_OK) return 0;
  421. return stat.ms_entries;
  422. }
  423. static LSUP_rc
  424. mdbstore_txn_begin (void *h, int flags, void **th)
  425. {
  426. MDBStore *store = h;
  427. RCCK (mdb_txn_begin (store->env, NULL, flags, (MDB_txn **) th));
  428. return LSUP_OK;
  429. }
  430. static LSUP_rc
  431. mdbstore_txn_commit (void *th)
  432. {
  433. RCCK (mdb_txn_commit ((MDB_txn *) th));
  434. return LSUP_OK;
  435. }
  436. static void
  437. mdbstore_txn_abort (void *th)
  438. { mdb_txn_abort ((MDB_txn *) th); }
  439. static void *
  440. mdbiter_txn (void *h)
  441. { return ((MDBIterator *) h)->txn; }
  442. /** @brief Begin an add loop.
  443. *
  444. * @sa #store_add_init_fn_t
  445. *
  446. * @param[in] th Previously opened MDB_txn handle, if the add loop shall be
  447. * run within an enclosing transaction. The transaction must be read-write.
  448. * The operation will always open a new transaction that is closed with
  449. * #mdbstore_add_done() or #mdbstore_add_abort(). If this parameter is not
  450. * NULL, the loop transaction will have the passed txn set as its parent.
  451. */
  452. static void *
  453. mdbstore_add_init (void *h, const LSUP_Buffer *sc, void *th)
  454. {
  455. MDBStore *store = h;
  456. /* An iterator is used here. Some members are a bit misused but it does
  457. * its job without having to define a very similar struct.
  458. */
  459. MDBIterator *it;
  460. MALLOC_GUARD (it, NULL);
  461. it->store = store;
  462. it->i = 0;
  463. CHECK (mdb_txn_begin (store->env, (MDB_txn *) th, 0, &it->txn), fail);
  464. if (sc) {
  465. // Store context if it's not the default one.
  466. it->luc = LSUP_buffer_hash (sc);
  467. // Insert t:st for context.
  468. //LOG_DEBUG("Adding context: %s", sc);
  469. it->key.mv_data = &it->luc;
  470. it->key.mv_size = KLEN;
  471. it->data.mv_data = sc->addr;
  472. it->data.mv_size = sc->size;
  473. int db_rc = mdb_put (
  474. it->txn, it->store->dbi[IDX_T_ST],
  475. &it->key, &it->data, MDB_NOOVERWRITE);
  476. if (db_rc != MDB_SUCCESS && db_rc != MDB_KEYEXIST) {
  477. log_error (LSUP_strerror (db_rc));
  478. mdb_txn_abort (it->txn);
  479. return NULL;
  480. }
  481. } else {
  482. LOG_DEBUG("No context passed to iterator, using default.");
  483. it->luc = LSUP_buffer_hash (LSUP_default_ctx_buf);
  484. }
  485. return it;
  486. fail:
  487. free (it);
  488. return NULL;
  489. }
  490. /*
  491. * NOTE: at the moment #mdbstore_remove() or another
  492. * #mdbstore_init() cannot be called between #mdbstore_add_init and
  493. * #mdbstore_add_abort or #mdbstore_add_done. FIXME
  494. *
  495. */
  496. static LSUP_rc
  497. mdbstore_add_iter (void *h, const LSUP_BufferTriple *sspo)
  498. {
  499. if (UNLIKELY (!h)) return LSUP_VALUE_ERR;
  500. MDBIterator *it = h;
  501. int db_rc = LSUP_NOACTION;
  502. LSUP_TripleKey spok = NULL_TRP;
  503. // Add triple terms.
  504. for (int i = 0; i < 3; i++) {
  505. LSUP_Buffer *st = LSUP_btriple_pos (sspo, i);
  506. spok[i] = LSUP_buffer_hash (st);
  507. it->key.mv_data = spok + i;
  508. it->key.mv_size = KLEN;
  509. it->data.mv_data = st->addr;
  510. it->data.mv_size = st->size;
  511. db_rc = mdb_put(
  512. it->txn, it->store->dbi[IDX_T_ST],
  513. &it->key, &it->data, MDB_NOOVERWRITE);
  514. if (db_rc != MDB_SUCCESS && db_rc != MDB_KEYEXIST) {
  515. LOG_RC (db_rc);
  516. return LSUP_DB_ERR;
  517. }
  518. }
  519. LOG_TRACE("Inserting spok: {%lx, %lx, %lx}", spok[0], spok[1], spok[2]);
  520. LOG_TRACE("Into context: %lx", it->luc);
  521. // Insert spo:c.
  522. it->key.mv_data = spok;
  523. it->key.mv_size = TRP_KLEN;
  524. // In triple mode, data is empty (= NULL_KEY).
  525. it->data.mv_data = &it->luc;
  526. it->data.mv_size = it->luc == NULL_KEY ? 0 : KLEN;
  527. db_rc = mdb_put(
  528. it->txn, it->store->dbi[IDX_SPO_C],
  529. &it->key, &it->data, MDB_NODUPDATA);
  530. if (db_rc == MDB_KEYEXIST) return LSUP_NOACTION;
  531. if (db_rc != MDB_SUCCESS) {
  532. log_error (
  533. "MDB error while inserting triple: %s", LSUP_strerror(db_rc));
  534. return LSUP_DB_ERR;
  535. }
  536. // Index.
  537. LSUP_rc rc = index_triple (it->store, OP_ADD, spok, it->luc, it->txn);
  538. if (rc == LSUP_OK) it->i++;
  539. return rc;
  540. }
  541. static LSUP_rc
  542. mdbstore_add_done (void *h)
  543. {
  544. MDBIterator *it = h;
  545. LSUP_rc rc = LSUP_OK;
  546. if (mdb_txn_commit (it->txn) != MDB_SUCCESS) {
  547. mdb_txn_abort (it->txn);
  548. rc = LSUP_TXN_ERR;
  549. }
  550. free (it);
  551. return rc;
  552. }
  553. static void
  554. mdbstore_add_abort (void *h)
  555. {
  556. MDBIterator *it = h;
  557. mdb_txn_abort (it->txn);
  558. free (it);
  559. }
  560. static LSUP_rc
  561. key_to_sterm (MDBIterator *it, const LSUP_Key key, LSUP_Buffer *sterm)
  562. {
  563. LSUP_rc rc = LSUP_NORESULT;
  564. int db_rc;
  565. MDB_val key_v, data_v;
  566. key_v.mv_data = (void*)&key;
  567. key_v.mv_size = KLEN;
  568. db_rc = mdb_get (it->txn, it->store->dbi[IDX_T_ST], &key_v, &data_v);
  569. sterm->flags |= LSUP_BUF_BORROWED;
  570. if (db_rc == MDB_SUCCESS) {
  571. sterm->addr = data_v.mv_data;
  572. sterm->size = data_v.mv_size;
  573. rc = LSUP_OK;
  574. } else if (db_rc == MDB_NOTFOUND) {
  575. sterm->addr = NULL;
  576. sterm->size = 0;
  577. } else rc = LSUP_DB_ERR;
  578. return rc;
  579. }
  580. static void *
  581. mdbstore_lookup (
  582. void *h, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
  583. const LSUP_Buffer *so, const LSUP_Buffer *sc, void *th, size_t *ct)
  584. {
  585. LSUP_TripleKey spok = {
  586. LSUP_buffer_hash (ss),
  587. LSUP_buffer_hash (sp),
  588. LSUP_buffer_hash (so),
  589. };
  590. MDBIterator *it;
  591. CALLOC_GUARD (it, NULL);
  592. it->store = h;
  593. it->luc = LSUP_buffer_hash (sc);
  594. LOG_DEBUG("Lookup context: %lx", it->luc);
  595. if (ct) *ct = 0;
  596. uint8_t idx0, idx1;
  597. // Start RO transaction if not in a write txn already.
  598. if (th) it->txn = th;
  599. else {
  600. it->rc = mdb_txn_begin (it->store->env, NULL, MDB_RDONLY, &it->txn);
  601. if (it->rc != MDB_SUCCESS) {
  602. log_error ("Database error: %s", LSUP_strerror (it->rc));
  603. return NULL;
  604. }
  605. LOG_TRACE ("Opening new MDB transaction @%p", it->txn);
  606. it->flags |= ITER_OPEN_TXN;
  607. }
  608. // Context index loop.
  609. if (UNLIKELY (mdb_cursor_open (
  610. it->txn, it->store->dbi[IDX_SPO_C], &it->ctx_cur) != MDB_SUCCESS))
  611. return NULL;
  612. /*
  613. * Lookup decision tree.
  614. */
  615. // s p o (all terms bound)
  616. if (spok[0] != NULL_KEY && spok[1] != NULL_KEY && spok[2] != NULL_KEY) {
  617. it->luk[0] = spok[0];
  618. it->luk[1] = spok[1];
  619. it->luk[2] = spok[2];
  620. PRCNL (lookup_3bound (it, ct));
  621. } else if (spok[0] != NULL_KEY) {
  622. it->luk[0] = spok[0];
  623. idx0 = 0;
  624. // s p ?
  625. if (spok[1] != NULL_KEY) {
  626. it->luk[1] = spok[1];
  627. idx1 = 1;
  628. PRCNL (lookup_2bound (idx0, idx1, it, ct));
  629. // s ? o
  630. } else if (spok[2] != NULL_KEY) {
  631. it->luk[1] = spok[2];
  632. idx1 = 2;
  633. PRCNL (lookup_2bound (idx0, idx1, it, ct));
  634. // s ? ?
  635. } else PRCNL (lookup_1bound (idx0, it, ct));
  636. } else if (spok[1] != NULL_KEY) {
  637. it->luk[0] = spok[1];
  638. idx0 = 1;
  639. // ? p o
  640. if (spok[2] != NULL_KEY) {
  641. it->luk[1] = spok[2];
  642. idx1 = 2;
  643. PRCNL (lookup_2bound (idx0, idx1, it, ct));
  644. // ? p ?
  645. } else PRCNL (lookup_1bound (idx0, it, ct));
  646. // ? ? o
  647. } else if (spok[2] != NULL_KEY) {
  648. it->luk[0] = spok[2];
  649. idx0 = 2;
  650. PRCNL (lookup_1bound (idx0, it, ct));
  651. // ? ? ? (all terms unbound)
  652. } else PRCNL (lookup_0bound (it, ct));
  653. return it;
  654. }
  655. /** @brief Get next iterator key.
  656. *
  657. * ckset is filled with an array of contexts that the triple appears
  658. * in, if not NULL.
  659. */
  660. static LSUP_rc
  661. mdbiter_next_key (MDBIterator *it)
  662. {
  663. if (UNLIKELY (!it)) return LSUP_VALUE_ERR;
  664. // Only advance if the previous it->rc wasn't already at the end.
  665. if (it->rc == MDB_NOTFOUND) return LSUP_END;
  666. if (UNLIKELY (it->rc != MDB_SUCCESS)) {
  667. log_error ("Database error: %s", LSUP_strerror (it->rc));
  668. return LSUP_DB_ERR;
  669. }
  670. LSUP_rc rc;
  671. /* Retrieve current value and advance cursor to the next result.
  672. * it->rc is set to the result of the next iteration.
  673. */
  674. it->iter_op_fn (it);
  675. LOG_TRACE(
  676. "Found spok: {%lx, %lx, %lx}",
  677. it->spok[0], it->spok[1], it->spok[2]);
  678. MDB_val key, data;
  679. int db_rc;
  680. key.mv_size = TRP_KLEN;
  681. data.mv_data = &it->luc;
  682. data.mv_size = KLEN;
  683. if (it->luc) {
  684. rc = LSUP_NORESULT; // Intermediary value, will never be returned.
  685. while (rc == LSUP_NORESULT) {
  686. //LOG_DEBUG("begin ctx loop.");
  687. // If ctx is specified, look if the matching triple is associated
  688. // with it. If not, move on to the next triple.
  689. // The loop normally exits when a triple with matching ctx is found
  690. // (LSUP_OK), if there are no more triples (LSUP_END), or if there
  691. // is an error (LSUP_DB_ERR).
  692. key.mv_data = it->spok;
  693. db_rc = mdb_cursor_get (it->ctx_cur, &key, &data, MDB_GET_BOTH);
  694. if (db_rc == MDB_SUCCESS) {
  695. rc = LSUP_OK;
  696. LOG_TRACE("Triple found for context.");
  697. } else if (db_rc == MDB_NOTFOUND) {
  698. LOG_TRACE("No triples found for context.");
  699. if (it->rc == MDB_NOTFOUND) rc = LSUP_END;
  700. else it->iter_op_fn (it);
  701. } else {
  702. log_error ("Database error: %s", LSUP_strerror (db_rc));
  703. rc = LSUP_DB_ERR;
  704. }
  705. }
  706. } else rc = LSUP_OK;
  707. // Get all contexts for a triple.
  708. key.mv_data = it->spok;
  709. db_rc = mdb_cursor_get (it->ctx_cur, &key, &data, MDB_SET_KEY);
  710. if (db_rc != MDB_SUCCESS) {
  711. log_error ("No context found for triple!");
  712. return LSUP_DB_ERR;
  713. }
  714. size_t ct;
  715. db_rc = mdb_cursor_count (it->ctx_cur, &ct);
  716. if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
  717. // 1 spare for sentinel. Always allocated even on zero matches.
  718. LSUP_Key *tmp_ck = realloc (it->ck, sizeof (*it->ck) * (ct + 1));
  719. if (!tmp_ck) return LSUP_MEM_ERR;
  720. it->ck = tmp_ck;
  721. size_t i = 0;
  722. do {
  723. //LOG_TRACE("Copying to slot #%lu @%p", i, it->ck + i);
  724. memcpy (it->ck + i++, data.mv_data, sizeof (*it->ck));
  725. } while (
  726. mdb_cursor_get (it->ctx_cur, &key, &data, MDB_NEXT_DUP)
  727. == MDB_SUCCESS);
  728. //LOG_TRACE("setting sentinel @%p", it->ck + i);
  729. it->ck[i] = NULL_KEY;
  730. return rc;
  731. }
  732. static LSUP_rc
  733. mdbiter_next (
  734. void *h, LSUP_BufferTriple *sspo, LSUP_Buffer **ctx_p)
  735. {
  736. MDBIterator *it = h;
  737. LSUP_rc rc = mdbiter_next_key (it);
  738. if (rc == LSUP_OK) {
  739. if (sspo) {
  740. key_to_sterm (it, it->spok[0], sspo->s);
  741. key_to_sterm (it, it->spok[1], sspo->p);
  742. key_to_sterm (it, it->spok[2], sspo->o);
  743. // TODO error handling.
  744. }
  745. // Contexts for current triple.
  746. if (ctx_p) {
  747. // Preallocate.
  748. size_t i = 0;
  749. while (it->ck[i++]); // Include sentinel in count.
  750. LSUP_Buffer *ctx;
  751. LOG_TRACE("Allocating %lu context buffers.", i);
  752. ctx = malloc(i * sizeof (*ctx));
  753. if (!ctx) return LSUP_MEM_ERR;
  754. for (i = 0; it->ck[i]; i++)
  755. key_to_sterm (it, it->ck[i], ctx + i);
  756. memset (ctx + i, 0, sizeof (*ctx)); // Sentinel
  757. // TODO error handling.
  758. *ctx_p = ctx;
  759. }
  760. }
  761. return rc;
  762. }
  763. static void
  764. mdbiter_free (void *h)
  765. {
  766. MDBIterator *it = h;
  767. if (!it) return;
  768. if (it->cur) mdb_cursor_close (it->cur);
  769. if (it->ctx_cur) mdb_cursor_close (it->ctx_cur);
  770. if (it->flags & ITER_OPEN_TXN) mdb_txn_abort (it->txn);
  771. free (it->ck);
  772. free (it);
  773. }
  774. static LSUP_rc
  775. mdbstore_update_ctx (
  776. void *h, const LSUP_Buffer *old_c, const LSUP_Buffer *new_c, void *th)
  777. {
  778. LSUP_rc rc = LSUP_NOACTION;
  779. MDBStore *store = h;
  780. unsigned char *trp_data = NULL;
  781. LSUP_Key
  782. old_ck = LSUP_buffer_hash (old_c),
  783. new_ck = LSUP_buffer_hash (new_c);
  784. // lu_key, lu_data look up all triples with old context in c:spo, and
  785. // replace old c with new c.
  786. MDB_txn
  787. *p_txn = th,
  788. *txn;
  789. CHECK (
  790. rc = mdb_txn_begin (store->env, p_txn, 0, &txn),
  791. finally);
  792. MDB_cursor *i_cur, *d_cur;
  793. CHECK (
  794. rc = mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &i_cur),
  795. close_txn);
  796. // TODO error handling.
  797. MDB_val key, data;
  798. // Return error if the graph URI already exists.
  799. key.mv_data = &new_ck;
  800. key.mv_size = KLEN;
  801. rc = mdb_cursor_get (i_cur, &key, &data, MDB_FIRST_DUP);
  802. if (rc == MDB_SUCCESS) {
  803. log_error (
  804. "Context key %lu already exists. Not replacing old graph.",
  805. new_ck);
  806. rc = LSUP_CONFLICT;
  807. goto close_i;
  808. }
  809. // Add new context term.
  810. CHECK (rc = mdbstore_add_term (store, new_c, txn), close_i);
  811. key.mv_data = &old_ck;
  812. // Count triples in cursor.
  813. rc = mdb_cursor_get (i_cur, &key, &data, MDB_SET);
  814. if (rc == MDB_NOTFOUND) {
  815. log_info ("No triples found associated with old context.");
  816. rc = LSUP_NOACTION;
  817. goto close_i;
  818. }
  819. if (rc != MDB_SUCCESS) {
  820. rc = LSUP_DB_ERR;
  821. goto close_i;
  822. }
  823. // From here on, it can only be LSUP_OK or error.
  824. rc = LSUP_OK;
  825. size_t trp_ct;
  826. CHECK (rc = mdb_cursor_count (i_cur, &trp_ct), close_i);
  827. trp_data = malloc (trp_ct * TRP_KLEN);
  828. if (UNLIKELY (!trp_data)) {
  829. rc = LSUP_MEM_ERR;
  830. goto close_i;
  831. }
  832. // Copy triple data as one block to temp buffer so that entries can be
  833. // deleted while cursors are active.
  834. rc = mdb_cursor_get (i_cur, &key, &data, MDB_GET_MULTIPLE);
  835. if (rc != MDB_SUCCESS) {
  836. rc = rc == MDB_NOTFOUND ? LSUP_NOACTION : LSUP_DB_ERR;
  837. goto close_i;
  838. }
  839. size_t loc_cur = 0;
  840. do {
  841. memcpy (trp_data + loc_cur, data.mv_data, data.mv_size);
  842. loc_cur += data.mv_size;
  843. } while (mdb_cursor_get (
  844. i_cur, &key, &data, MDB_NEXT_MULTIPLE) == MDB_SUCCESS);
  845. // Zap c:spo entries in one go.
  846. key.mv_data = &old_ck;
  847. key.mv_size = KLEN;
  848. data.mv_size = TRP_KLEN;
  849. CHECK (rc = mdb_cursor_get (i_cur, &key, NULL, MDB_SET), close_i);
  850. CHECK (rc = mdb_cursor_del (i_cur, MDB_NODUPDATA), close_i);
  851. // Re-ad c:spo data individually.
  852. key.mv_data = &new_ck;
  853. for (size_t i = 0; i < trp_ct; i++) {
  854. data.mv_data = trp_data + i * data.mv_size;
  855. CHECK (
  856. rc = mdb_cursor_put (i_cur, &key, &data, MDB_APPENDDUP),
  857. close_i);
  858. }
  859. // Re-add c:spo data in bulk from buffer with new context.
  860. // FIXME this is not working. Replaced by the for loop above.
  861. /*
  862. MDB_val data_block[] = {
  863. { .mv_data = &new_ck, .mv_size = TRP_KLEN },
  864. { .mv_data = NULL, .mv_size = trp_ct },
  865. };
  866. db_rc = mdb_cursor_put (i_cur, &key, data_block, MDB_MULTIPLE);
  867. */
  868. // Main table.
  869. // Replace spo:c values one by one.
  870. CHECK (rc = mdb_cursor_open (txn, store->dbi[IDX_SPO_C], &d_cur), close_i);
  871. key.mv_size = TRP_KLEN;
  872. data.mv_size = KLEN;
  873. for (size_t i = 0; i < trp_ct; i++) {
  874. key.mv_data = trp_data + i * key.mv_size;
  875. data.mv_data = &old_ck;
  876. CHECK (
  877. rc = mdb_cursor_get (d_cur, &key, &data, MDB_GET_BOTH),
  878. close_d);
  879. CHECK (rc = mdb_cursor_del (d_cur, 0), close_d);
  880. data.mv_data = &new_ck;
  881. CHECK (
  882. rc = mdb_cursor_put (d_cur, &key, &data, MDB_NOOVERWRITE),
  883. close_d);
  884. }
  885. close_d:
  886. mdb_cursor_close (d_cur);
  887. close_i:
  888. mdb_cursor_close (i_cur);
  889. close_txn:
  890. if (rc == LSUP_OK) {
  891. RCCK (mdb_txn_commit (txn));
  892. } else mdb_txn_abort (txn);
  893. if (trp_data) free (trp_data);
  894. finally:
  895. return rc;
  896. }
  897. static LSUP_rc
  898. mdbstore_remove (
  899. void *h, const LSUP_Buffer *ss, const LSUP_Buffer *sp,
  900. const LSUP_Buffer *so, const LSUP_Buffer *sc, void *th, size_t *ct)
  901. {
  902. MDBStore *store = h;
  903. LSUP_rc
  904. rc = LSUP_NOACTION,
  905. db_rc = 0;
  906. LSUP_Key ck = NULL_KEY;
  907. if (sc == NULL) sc = LSUP_default_ctx_buf;
  908. ck = LSUP_buffer_hash (sc);
  909. MDB_txn *txn;
  910. mdb_txn_begin (store->env, (MDB_txn *) th, 0, &txn);
  911. MDB_cursor *dcur, *icur;
  912. mdb_cursor_open (txn, store->dbi[IDX_SPO_C], &dcur);
  913. mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &icur);
  914. MDB_val spok_v, ck_v;
  915. spok_v.mv_size = TRP_KLEN;
  916. ck_v.mv_size = KLEN;
  917. ck_v.mv_data = &ck;
  918. // The lookup operates within the current (bottom) write transaction.
  919. MDBIterator *it = mdbstore_lookup (store, ss, sp, so, sc, txn, ct);
  920. if (UNLIKELY (!it)) return LSUP_DB_ERR;
  921. if (ct) LOG_DEBUG("Found %lu triples to remove.", *ct);
  922. while (mdbiter_next_key (it) == LSUP_OK) {
  923. spok_v.mv_data = it->spok;
  924. db_rc = mdb_cursor_get (dcur, &spok_v, &ck_v, MDB_GET_BOTH);
  925. if (db_rc == MDB_NOTFOUND) continue;
  926. if (UNLIKELY (db_rc != MDB_SUCCESS)) goto fail;
  927. LOG_TRACE(
  928. "Removing {%lx, %lx, %lx}",
  929. it->spok[0], it->spok[1], it->spok[2]);
  930. // Delete spo:c entry.
  931. db_rc = mdb_cursor_del (dcur, 0);
  932. if (UNLIKELY (db_rc != MDB_SUCCESS)) goto fail;
  933. // Restore ck address after each delete.
  934. spok_v.mv_data = it->spok;
  935. ck_v.mv_data = &ck;
  936. // Delete c:spo entry.
  937. db_rc = mdb_cursor_get (icur, &ck_v, &spok_v, MDB_GET_BOTH);
  938. if (db_rc == MDB_NOTFOUND) continue;
  939. if (UNLIKELY (db_rc != MDB_SUCCESS)) goto fail;
  940. db_rc = mdb_cursor_del (icur, 0);
  941. if (UNLIKELY (db_rc != MDB_SUCCESS)) goto fail;
  942. spok_v.mv_data = it->spok;
  943. ck_v.mv_data = &ck;
  944. // If there are no more contexts associated with this triple,
  945. // remove from indices.
  946. db_rc = mdb_cursor_get (dcur, &spok_v, NULL, MDB_SET);
  947. if (db_rc == MDB_SUCCESS) continue;
  948. if (UNLIKELY (db_rc != MDB_NOTFOUND)) goto fail;
  949. rc = index_triple (store, OP_REMOVE, it->spok, ck, txn);
  950. }
  951. mdbiter_free (it);
  952. if (UNLIKELY (mdb_txn_commit (txn) != MDB_SUCCESS)) {
  953. rc = LSUP_TXN_ERR;
  954. goto fail;
  955. }
  956. return rc;
  957. fail:
  958. mdb_txn_abort (txn);
  959. log_error ("Database error: %s", LSUP_strerror (db_rc));
  960. return rc == LSUP_TXN_ERR ? rc : LSUP_DB_ERR;
  961. }
  962. #if 0
  963. static int
  964. mdbstore_tkey_exists (MDBStore *store, LSUP_Key tkey)
  965. {
  966. int db_rc, rc;
  967. MDB_val key, data;
  968. key.mv_data = &tkey;
  969. key.mv_size = KLEN;
  970. MDB_txn *txn = NULL;
  971. mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
  972. MDB_cursor *cur = NULL;
  973. mdb_cursor_open (txn, store->dbi[IDX_T_ST], &cur);
  974. db_rc = mdb_cursor_get (cur, &key, &data, MDB_SET);
  975. if (db_rc == MDB_SUCCESS) rc = 1;
  976. else if (db_rc == MDB_NOTFOUND) rc = 0;
  977. else {
  978. log_error ("DB error: %s", LSUP_strerror (db_rc));
  979. rc = LSUP_DB_ERR;
  980. }
  981. if (cur) mdb_cursor_close (cur);
  982. if (txn) mdb_txn_abort (txn);
  983. return rc;
  984. }
  985. #endif
  986. /** @brief Add a term to the store.
  987. *
  988. * @param[in] h #MDBStore handle.
  989. *
  990. * @param[in] sterm Serialized term to store.
  991. *
  992. * @param[in] th transaction handle. It may be NULL, in which case a new
  993. * transaction is opened and closed for the operation.
  994. *
  995. * @return LSUP_OK on success; <0 on error.
  996. */
  997. static LSUP_rc
  998. mdbstore_add_term (void *h, const LSUP_Buffer *sterm, void *th)
  999. {
  1000. //LOG_TRACE("Adding term to MDB store: %s", sterm->addr);
  1001. MDBStore *store = h;
  1002. int db_rc;
  1003. MDB_val key, data;
  1004. MDB_txn *txn;
  1005. // If an active transaction was passed, use it, otherwise open and
  1006. // close a new one.
  1007. bool borrowed_txn = (th != NULL);
  1008. if (borrowed_txn) txn = th;
  1009. else RCCK (mdb_txn_begin (store->env, NULL, 0, &txn));
  1010. MDB_cursor *cur;
  1011. CHECK (mdb_cursor_open (txn, store->dbi[IDX_T_ST], &cur), fail);
  1012. LSUP_Key k = LSUP_buffer_hash (sterm);
  1013. key.mv_data = &k;
  1014. key.mv_size = sizeof (k);
  1015. data.mv_data = sterm->addr;
  1016. data.mv_size = sterm->size;
  1017. db_rc = mdb_cursor_put (cur, &key, &data, MDB_NOOVERWRITE);
  1018. if (db_rc != MDB_KEYEXIST) CHECK (db_rc, fail);
  1019. if (!borrowed_txn) CHECK (db_rc = mdb_txn_commit (txn), fail);
  1020. return LSUP_OK;
  1021. fail:
  1022. if (!borrowed_txn) mdb_txn_abort (txn);
  1023. LOG_TRACE("Aborted txn for adding term.");
  1024. return LSUP_DB_ERR;
  1025. }
  1026. const LSUP_StoreInt mdbstore_int = {
  1027. .name = "MDB Store",
  1028. .features = LSUP_STORE_PERM | LSUP_STORE_CTX | LSUP_STORE_IDX
  1029. | LSUP_STORE_TXN | LSUP_STORE_COW,
  1030. .setup_fn = mdbstore_setup,
  1031. .new_fn = mdbstore_new,
  1032. .free_fn = mdbstore_free,
  1033. .size_fn = mdbstore_size,
  1034. .txn_begin_fn = mdbstore_txn_begin,
  1035. .txn_commit_fn = mdbstore_txn_commit,
  1036. .txn_abort_fn = mdbstore_txn_abort,
  1037. .iter_txn_fn = mdbiter_txn,
  1038. .add_init_fn = mdbstore_add_init,
  1039. .add_iter_fn = mdbstore_add_iter,
  1040. .add_abort_fn = mdbstore_add_abort,
  1041. .add_done_fn = mdbstore_add_done,
  1042. .add_term_fn = mdbstore_add_term,
  1043. .update_ctx_fn = mdbstore_update_ctx,
  1044. .lookup_fn = mdbstore_lookup,
  1045. .lu_next_fn = mdbiter_next,
  1046. .lu_free_fn = mdbiter_free,
  1047. .remove_fn = mdbstore_remove,
  1048. .nsm_put_fn = mdbstore_nsm_put,
  1049. .nsm_get_fn = mdbstore_nsm_get,
  1050. };
  1051. /* * * Static functions. * * */
  1052. /** @brief Index an added or removed triple.
  1053. *
  1054. * @param store[in] MDB store to index.
  1055. * @param op[in] Store operation. One of OP_ADD or OP_REMOVE.
  1056. * @param spok[in] Triple key to index.
  1057. * @param ck[in] Context to index, may be NULL.
  1058. * @param[in] th Transaction handle. This MUST be a valid pointer to an open
  1059. * RW transaction.
  1060. */
  1061. static LSUP_rc
  1062. index_triple(
  1063. MDBStore *store, StoreOp op, LSUP_TripleKey spok, LSUP_Key ck,
  1064. MDB_txn *txn)
  1065. {
  1066. int db_rc;
  1067. LSUP_rc rc = LSUP_NOACTION;
  1068. MDB_val v1, v2;
  1069. LOG_TRACE("Indexing triple: {%lx %lx %lx}", spok[0], spok[1], spok[2]);
  1070. // Index c:spo.
  1071. if (op == OP_REMOVE) {
  1072. LOG_TRACE("Indexing op: REMOVE");
  1073. if (ck != NULL_KEY) {
  1074. MDB_cursor *cur;
  1075. v1.mv_data = &ck;
  1076. v1.mv_size = KLEN;
  1077. v2.mv_data = spok;
  1078. v2.mv_size = TRP_KLEN;
  1079. mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &cur);
  1080. if (mdb_cursor_get (cur, &v1, &v2, MDB_GET_BOTH) == MDB_SUCCESS) {
  1081. db_rc = mdb_cursor_del (cur, 0);
  1082. if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
  1083. rc = LSUP_OK;
  1084. }
  1085. mdb_cursor_close (cur);
  1086. }
  1087. } else if (op == OP_ADD) {
  1088. LOG_TRACE("Indexing op: ADD");
  1089. if (ck != NULL_KEY) {
  1090. v1.mv_data = &ck;
  1091. v1.mv_size = KLEN;
  1092. v2.mv_data = spok;
  1093. v2.mv_size = TRP_KLEN;
  1094. db_rc = mdb_put(
  1095. txn, store->dbi[IDX_C_SPO],
  1096. &v1, &v2, MDB_NODUPDATA);
  1097. if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
  1098. if (db_rc != MDB_KEYEXIST) rc = LSUP_OK;
  1099. }
  1100. } else return LSUP_VALUE_ERR;
  1101. LSUP_DoubleKey dbl_keys[3] = {
  1102. {spok[1], spok[2]}, // po
  1103. {spok[0], spok[2]}, // so
  1104. {spok[0], spok[1]}, // sp
  1105. };
  1106. // Add terms to index.
  1107. v1.mv_size = KLEN;
  1108. v2.mv_size = DBL_KLEN;
  1109. for (int i = 0; i < 3; i++) {
  1110. MDB_dbi db1 = store->dbi[lookup_indices[i]]; // s:po, p:so, o:sp
  1111. MDB_dbi db2 = store->dbi[lookup_indices[i + 3]]; // po:s, so:p, sp:o
  1112. v1.mv_data = spok + i;
  1113. v2.mv_data = dbl_keys[i];
  1114. if (op == OP_REMOVE) {
  1115. MDB_cursor *cur1, *cur2;
  1116. mdb_cursor_open(txn, store->dbi[lookup_indices[i]], &cur1);
  1117. db_rc = mdb_cursor_get (cur1, &v1, &v2, MDB_GET_BOTH);
  1118. if (db_rc == MDB_SUCCESS) mdb_cursor_del (cur1, 0);
  1119. mdb_cursor_close (cur1);
  1120. // Restore pointers invalidated after delete.
  1121. v1.mv_data = spok + i;
  1122. v2.mv_data = dbl_keys[i];
  1123. mdb_cursor_open(txn, store->dbi[lookup_indices[i + 3]], &cur2);
  1124. db_rc = mdb_cursor_get (cur2, &v2, &v1, MDB_GET_BOTH);
  1125. if (db_rc == MDB_SUCCESS) mdb_cursor_del (cur2, 0);
  1126. // TODO error handling.
  1127. rc = LSUP_OK;
  1128. mdb_cursor_close (cur2);
  1129. } else { // OP_ADD is guaranteed.
  1130. // 1-bound index.
  1131. LOG_TRACE("Indexing in %s: ", db_labels[lookup_indices[i]]);
  1132. LOG_TRACE(
  1133. "%lx: %lx %lx", *(size_t*)(v1.mv_data),
  1134. *(size_t*)(v2.mv_data), *(size_t*)(v2.mv_data) + 1);
  1135. db_rc = mdb_put (txn, db1, &v1, &v2, MDB_NODUPDATA);
  1136. if (db_rc == MDB_SUCCESS) rc = LSUP_OK;
  1137. else if (db_rc != MDB_KEYEXIST) return LSUP_DB_ERR;
  1138. // 2-bound index.
  1139. LOG_TRACE("Indexing in %s: ", db_labels[lookup_indices[i + 3]]);
  1140. LOG_TRACE(
  1141. "%lx %lx: %lx", *(size_t*)(v2.mv_data),
  1142. *(size_t*)(v2.mv_data) + 1, *(size_t*)(v1.mv_data));
  1143. db_rc = mdb_put (txn, db2, &v2, &v1, MDB_NODUPDATA);
  1144. if (db_rc == MDB_SUCCESS) rc = LSUP_OK;
  1145. else if (db_rc != MDB_KEYEXIST) return LSUP_DB_ERR;
  1146. }
  1147. }
  1148. return rc;
  1149. }
  1150. /* * * Term-specific iterators. * * */
  1151. /** @brief Advance 0-bound iterator.
  1152. *
  1153. * Cursor: spo:c
  1154. */
  1155. inline static void
  1156. it_next_0bound (MDBIterator *it)
  1157. {
  1158. memcpy (it->spok, it->key.mv_data, sizeof (LSUP_TripleKey));
  1159. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_NEXT);
  1160. }
  1161. /** @brief Advance 1-bound iterator.
  1162. *
  1163. * Uses paged data in a nested loop.
  1164. *
  1165. * Cursor: s:po, p:so, or o:sp.
  1166. */
  1167. inline static void
  1168. it_next_1bound (MDBIterator *it)
  1169. {
  1170. LSUP_DoubleKey *lu_dset = it->data.mv_data;
  1171. it->spok[it->term_order[0]] = it->luk[0];
  1172. it->spok[it->term_order[1]] = lu_dset[it->i][0];
  1173. it->spok[it->term_order[2]] = lu_dset[it->i][1];
  1174. LOG_TRACE(
  1175. "Composed triple: {%lx %lx %lx}",
  1176. it->spok[0], it->spok[1], it->spok[2]);
  1177. // Ensure next block within the same page is not beyond the last.
  1178. if (it->i < it->data.mv_size / DBL_KLEN - 1) {
  1179. it->i ++;
  1180. //LOG_DEBUG("Increasing page cursor to %lu.", it->i);
  1181. //LOG_DEBUG("it->rc: %d", it->rc);
  1182. } else {
  1183. // If the last block in the page is being yielded,
  1184. // move cursor to beginning of next page.
  1185. it->i = 0;
  1186. //LOG_DEBUG("Reset page cursor to %lu.", it->i);
  1187. it->rc = mdb_cursor_get (
  1188. it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
  1189. }
  1190. }
  1191. /** @brief Advance 2-bound iterator.
  1192. *
  1193. * Uses paged data in a nested loop.
  1194. *
  1195. * Cursor: po:s, so:p, or sp:o.
  1196. */
  1197. inline static void
  1198. it_next_2bound (MDBIterator *it)
  1199. {
  1200. LSUP_Key *lu_dset = it->data.mv_data;
  1201. it->spok[it->term_order[0]] = it->luk[0];
  1202. it->spok[it->term_order[1]] = it->luk[1];
  1203. it->spok[it->term_order[2]] = lu_dset[it->i];
  1204. // Ensure next block within the same page is not beyond the last.
  1205. if (it->i < it->data.mv_size / KLEN - 1)
  1206. it->i ++;
  1207. else {
  1208. // If the last block in the page is being yielded,
  1209. // move cursor to beginning of next page.
  1210. it->i = 0;
  1211. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
  1212. }
  1213. }
  1214. /** @brief Advance 3-bound iterator.
  1215. *
  1216. * This is a special case of 0÷1 results; either there was one matching triple,
  1217. * which was already set in the first result, or there was none, i.e. it->rc is
  1218. * already MDB_NOTFOUND and this function will not be called.
  1219. */
  1220. inline static void
  1221. it_next_3bound (MDBIterator *it)
  1222. { it->rc = MDB_NOTFOUND; }
  1223. /* * * Term-specific lookups. * * */
  1224. inline static LSUP_rc
  1225. lookup_0bound (MDBIterator *it, size_t *ct)
  1226. {
  1227. LOG_DEBUG("Looking up 0 bound terms.");
  1228. if (ct) {
  1229. if (it->luc != NULL_KEY) {
  1230. // Look up by given context.
  1231. it->rc = mdb_cursor_open (
  1232. it->txn, it->store->dbi[IDX_C_SPO], &it->cur);
  1233. it->key.mv_data = &it->luc;
  1234. it->key.mv_size = KLEN;
  1235. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1236. if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
  1237. mdb_cursor_close (it->cur);
  1238. it->cur = NULL;
  1239. } else {
  1240. // Look up all contexts.
  1241. MDB_stat stat;
  1242. mdb_stat (it->txn, it->store->dbi[IDX_S_PO], &stat);
  1243. *ct = stat.ms_entries;
  1244. }
  1245. LOG_DEBUG("Found %lu triples.", *ct);
  1246. }
  1247. it->rc = mdb_cursor_open (it->txn, it->store->dbi[IDX_SPO_C], &it->cur);
  1248. if (it->rc != MDB_SUCCESS) {
  1249. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1250. return LSUP_DB_ERR;
  1251. }
  1252. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_FIRST);
  1253. /*
  1254. mdb_cursor_close (it->cur);
  1255. it->cur = NULL;
  1256. */
  1257. it->iter_op_fn = it_next_0bound;
  1258. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1259. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1260. return LSUP_DB_ERR;
  1261. }
  1262. return LSUP_OK;
  1263. }
  1264. inline static LSUP_rc
  1265. lookup_1bound (uint8_t idx0, MDBIterator *it, size_t *ct)
  1266. {
  1267. it->term_order = (const uint8_t*)lookup_ordering_1bound[idx0];
  1268. LOG_DEBUG("Looking up 1 bound term: %lx", it->luk[0]);
  1269. mdb_cursor_open (it->txn, it->store->dbi[lookup_indices[idx0]], &it->cur);
  1270. it->key.mv_data = it->luk;
  1271. it->key.mv_size = KLEN;
  1272. if (ct) {
  1273. // If a context is specified, the only way to count triples matching
  1274. // the context is to loop over them.
  1275. if (it->luc != NULL_KEY) {
  1276. LOG_DEBUG("Counting in context: %lx", it->luc);
  1277. MDBIterator *ct_it;
  1278. MALLOC_GUARD (ct_it, LSUP_MEM_ERR);
  1279. /*
  1280. memcpy (ct_it, it, sizeof (*ct_it));
  1281. */
  1282. ct_it->store = it->store;
  1283. ct_it->txn = it->txn;
  1284. ct_it->ctx_cur = it->ctx_cur;
  1285. ct_it->key = it->key;
  1286. ct_it->data = it->data;
  1287. ct_it->ck = NULL;
  1288. ct_it->luk[0] = it->luk[0];
  1289. ct_it->luc = it->luc;
  1290. ct_it->i = 0;
  1291. LSUP_rc rc = lookup_1bound (idx0, ct_it, NULL);
  1292. if (rc < 0) return rc;
  1293. LSUP_rc db_rc;
  1294. while (LSUP_END != (db_rc = mdbiter_next_key (ct_it))) {
  1295. if (UNLIKELY (db_rc < 0)) return db_rc;
  1296. (*ct)++;
  1297. }
  1298. // Free the counter iterator without freeing the shared txn.
  1299. if (ct_it->cur) mdb_cursor_close (ct_it->cur);
  1300. free (ct_it->ck);
  1301. free (ct_it);
  1302. } else {
  1303. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1304. if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
  1305. }
  1306. LOG_DEBUG("Found %lu triples.", *ct);
  1307. }
  1308. it->i = 0;
  1309. it->iter_op_fn = it_next_1bound;
  1310. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1311. if (it->rc == MDB_SUCCESS)
  1312. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
  1313. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1314. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1315. return LSUP_DB_ERR;
  1316. }
  1317. return LSUP_OK;
  1318. }
  1319. inline static LSUP_rc
  1320. lookup_2bound(uint8_t idx0, uint8_t idx1, MDBIterator *it, size_t *ct)
  1321. {
  1322. uint8_t luk1_offset, luk2_offset;
  1323. MDB_dbi dbi = 0;
  1324. // Establish lookup ordering with some awkward offset math.
  1325. for (int i = 0; i < 3; i++) {
  1326. if (
  1327. (
  1328. idx0 == lookup_ordering_2bound[i][0] &&
  1329. idx1 == lookup_ordering_2bound[i][1]
  1330. ) || (
  1331. idx0 == lookup_ordering_2bound[i][1] &&
  1332. idx1 == lookup_ordering_2bound[i][0]
  1333. )
  1334. ) {
  1335. it->term_order = (const uint8_t*)lookup_ordering_2bound[i];
  1336. if (it->term_order[0] == idx0) {
  1337. luk1_offset = 0;
  1338. luk2_offset = 1;
  1339. } else {
  1340. luk1_offset = 1;
  1341. luk2_offset = 0;
  1342. }
  1343. dbi = it->store->dbi[lookup_indices[i + 3]];
  1344. LOG_DEBUG(
  1345. "Looking up 2 bound in %s",
  1346. db_labels[lookup_indices[i + 3]]);
  1347. break;
  1348. }
  1349. }
  1350. if (dbi == 0) {
  1351. log_error (
  1352. "Values %d and %d not found in lookup keys.",
  1353. idx0, idx1);
  1354. return LSUP_VALUE_ERR;
  1355. }
  1356. // Compose term keys in lookup key.
  1357. LSUP_DoubleKey luk;
  1358. luk[luk1_offset] = it->luk[0];
  1359. luk[luk2_offset] = it->luk[1];
  1360. it->key.mv_data = luk;
  1361. it->key.mv_size = DBL_KLEN;
  1362. mdb_cursor_open (it->txn, dbi, &it->cur);
  1363. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1364. if (ct) {
  1365. // If a context is specified, the only way to count triples matching
  1366. // the context is to loop over them.
  1367. if (it->luc != NULL_KEY) {
  1368. MDBIterator *ct_it;
  1369. MALLOC_GUARD (ct_it, LSUP_MEM_ERR);
  1370. ct_it->store = it->store;
  1371. ct_it->txn = it->txn;
  1372. ct_it->ctx_cur = it->ctx_cur;
  1373. ct_it->ck = NULL;
  1374. ct_it->luk[0] = it->luk[0];
  1375. ct_it->luk[1] = it->luk[1];
  1376. ct_it->luc = it->luc;
  1377. ct_it->i = 0;
  1378. lookup_2bound (idx0, idx1, ct_it, NULL);
  1379. while (mdbiter_next_key (ct_it) != LSUP_END) (*ct) ++;
  1380. // Free the counter iterator without freeing the shared txn.
  1381. if (ct_it->cur) mdb_cursor_close (ct_it->cur);
  1382. free (ct_it->ck);
  1383. free (ct_it);
  1384. } else {
  1385. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1386. if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
  1387. }
  1388. LOG_DEBUG("Found %lu triples.", *ct);
  1389. }
  1390. it->i = 0;
  1391. it->iter_op_fn = it_next_2bound;
  1392. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1393. if (it->rc == MDB_SUCCESS)
  1394. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
  1395. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1396. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1397. return LSUP_DB_ERR;
  1398. }
  1399. return LSUP_OK;
  1400. }
  1401. inline static LSUP_rc
  1402. lookup_3bound (MDBIterator *it, size_t *ct)
  1403. {
  1404. LOG_DEBUG(
  1405. "Looking up 3 bound: {%lx, %lx, %lx}",
  1406. it->luk[0], it->luk[1], it->luk[2]);
  1407. it->key.mv_data = it->luk;
  1408. if (it->luc != NULL_KEY) {
  1409. it->rc = mdb_cursor_open (
  1410. it->txn, it->store->dbi[IDX_SPO_C], &it->cur);
  1411. it->key.mv_size = TRP_KLEN;
  1412. it->data.mv_data = &it->luc;
  1413. it->data.mv_size = KLEN;
  1414. } else {
  1415. it->rc = mdb_cursor_open (it->txn, it->store->dbi[IDX_S_PO], &it->cur);
  1416. it->key.mv_size = KLEN;
  1417. it->data.mv_data = it->luk + 1;
  1418. it->data.mv_size = DBL_KLEN;
  1419. }
  1420. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_BOTH);
  1421. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1422. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1423. return LSUP_DB_ERR;
  1424. }
  1425. mdb_cursor_close (it->cur);
  1426. it->cur = NULL;
  1427. if (ct && it->rc == MDB_SUCCESS) *ct = 1;
  1428. it->iter_op_fn = it_next_3bound;
  1429. memcpy (it->spok, it->luk, sizeof (LSUP_TripleKey));
  1430. return LSUP_OK;
  1431. }