store_mdb.c 43 KB

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