store_mdb.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. #include "lsup/store_mdb.h"
  2. /**
  3. * Number of DBs defined. See MAIN_TABLE and LOOKUP_TABLE defines below.
  4. */
  5. #define N_DB 12
  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( PFX_NS, "pfx:ns", 0 ) /* Prefix to NS */ \
  99. ENTRY( IDK_ID, "idk:id", 0 ) /* ID key to ID */ \
  100. /**
  101. * Lookup DBs. These are indices and may be destroyed and rebuilt.
  102. */
  103. #define LOOKUP_TABLE \
  104. /* #ID pfx #DB label #Flags */ \
  105. ENTRY( S_PO, "s:po", DUPFIXED_MASK ) /* 1-bound lookup */ \
  106. ENTRY( P_SO, "p:so", DUPFIXED_MASK ) /* 1-bound lookup */ \
  107. ENTRY( O_SP, "o:sp", DUPFIXED_MASK ) /* 1-bound lookup */ \
  108. ENTRY( PO_S, "po:s", DUPFIXED_MASK ) /* 2-bound lookup */ \
  109. ENTRY( SO_P, "so:p", DUPFIXED_MASK ) /* 2-bound lookup */ \
  110. ENTRY( SP_O, "sp:o", DUPFIXED_MASK ) /* 2-bound lookup */ \
  111. ENTRY( C_SPO, "c:spo", DUPFIXED_MASK ) /* Context lookup */ \
  112. ENTRY( NS_PFX, "ns:pfx", DUPSORT_MASK ) /* NS to prefix */ \
  113. /**
  114. * DB labels. They are prefixed with DB_
  115. */
  116. #define ENTRY(a, b, c) static const DbLabel DB_##a = b;
  117. MAIN_TABLE
  118. LOOKUP_TABLE
  119. #undef ENTRY
  120. /*
  121. * Numeric index of each DB. Prefixed with IDX_
  122. *
  123. * These index numbers are referred to in all the arrays defeined below. They
  124. * are independent from the LMDB dbi values which are considered opaque here.
  125. */
  126. typedef enum {
  127. #define ENTRY(a, b, c) IDX_##a,
  128. MAIN_TABLE
  129. LOOKUP_TABLE
  130. #undef ENTRY
  131. } DBIdx;
  132. /**
  133. * DB labels.
  134. */
  135. static const char *db_labels[N_DB] = {
  136. #define ENTRY(a, b, c) DB_##a,
  137. MAIN_TABLE
  138. LOOKUP_TABLE
  139. #undef ENTRY
  140. };
  141. /*
  142. * DB flags. These are aligned with the dbi_labels index.
  143. */
  144. static const unsigned int db_flags[N_DB] = {
  145. #define ENTRY(a, b, c) c,
  146. MAIN_TABLE
  147. LOOKUP_TABLE
  148. #undef ENTRY
  149. };
  150. /*
  151. * 1-bound and 2-bound lookup indices.
  152. *
  153. * N.B. Only the first 6 (1-bound and 2-bound term lookup) are used.
  154. * The others are added just because they belong logically to the lookup table.
  155. */
  156. static DBIdx lookup_indices[9] = {
  157. #define ENTRY(a, b, c) IDX_##a,
  158. LOOKUP_TABLE
  159. #undef ENTRY
  160. };
  161. static const uint8_t lookup_ordering_1bound[3][3] = {
  162. {0, 1, 2}, // s:po
  163. {1, 0, 2}, // p:so
  164. {2, 0, 1}, // o:sp
  165. };
  166. static const uint8_t lookup_ordering_2bound[3][3] = {
  167. {1, 2, 0}, // po:s
  168. {0, 2, 1}, // so:p
  169. {0, 1, 2}, // sp:o
  170. };
  171. /*
  172. * Static prototypes.
  173. */
  174. static int index_triple(
  175. MDBStore *store, StoreOp op, LSUP_TripleKey spok, LSUP_Key ck,
  176. MDB_txn *txn);
  177. static LSUP_rc mdbstore_add_term (void *h, const LSUP_Buffer *sterm, void *th);
  178. inline static LSUP_rc lookup_0bound (MDBIterator *it, size_t *ct);
  179. inline static LSUP_rc lookup_1bound (
  180. uint8_t idx0, MDBIterator *it, size_t *ct);
  181. inline static LSUP_rc lookup_2bound (
  182. uint8_t idx0, uint8_t idx1, MDBIterator *it, size_t *ct);
  183. inline static LSUP_rc lookup_3bound(MDBIterator *it, size_t *ct);
  184. /**
  185. * Store interface.
  186. */
  187. static LSUP_NSMap *
  188. mdbstore_nsm_get (void *h)
  189. {
  190. MDBStore *store = h;
  191. LSUP_NSMap *nsm = LSUP_nsmap_new();
  192. if (UNLIKELY (!nsm)) return NULL;
  193. MDB_txn *txn;
  194. mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
  195. MDB_cursor *cur;
  196. if (mdb_cursor_open (txn, store->dbi[IDX_PFX_NS], &cur) != MDB_SUCCESS) {
  197. mdb_txn_abort (txn);
  198. return NULL;
  199. }
  200. MDB_val ns_v, pfx_v;
  201. if (mdb_cursor_get (cur, &pfx_v, &ns_v, MDB_FIRST) != MDB_SUCCESS)
  202. goto finally;
  203. do {
  204. LSUP_ns_pfx pfx;
  205. char *ns = malloc (ns_v.mv_size);
  206. strncpy (pfx, pfx_v.mv_data, pfx_v.mv_size);
  207. strncpy (ns, ns_v.mv_data, ns_v.mv_size);
  208. LSUP_nsmap_add (nsm, pfx, ns);
  209. free (ns);
  210. } while (mdb_cursor_get (
  211. cur, &pfx_v, &ns_v, MDB_NEXT_NODUP) == MDB_SUCCESS);
  212. finally:
  213. mdb_cursor_close (cur);
  214. mdb_txn_abort (txn);
  215. return nsm;
  216. }
  217. static LSUP_rc
  218. mdbstore_nsm_put (void *h, const LSUP_NSMap *nsm, void *th)
  219. {
  220. MDBStore *store = h;
  221. MDB_txn *txn;
  222. RCCK (mdb_txn_begin (store->env, (MDB_txn *) th, 0, &txn));
  223. LSUP_rc rc = LSUP_NOACTION;
  224. int db_rc;
  225. MDB_cursor *dcur = NULL, *icur = NULL;
  226. if (
  227. mdb_cursor_open (txn, store->dbi[IDX_PFX_NS], &dcur) != MDB_SUCCESS
  228. ||
  229. mdb_cursor_open (txn, store->dbi[IDX_NS_PFX], &icur) != MDB_SUCCESS
  230. ) {
  231. mdb_txn_abort (txn);
  232. return LSUP_DB_ERR;
  233. }
  234. MDB_val pfx_v, ns_v;
  235. const char ***nsm_data = LSUP_nsmap_dump (nsm);
  236. for (size_t i = 0; nsm_data[i] != NULL; i++) {
  237. // At least 1 action. If not OK, it will change during the iteration.
  238. if (i == 0) rc = LSUP_OK;
  239. // On previous error, just clean up the NSM data array.
  240. if (rc < 0) goto loop_end;
  241. pfx_v.mv_data = (void *) nsm_data[i][0];
  242. pfx_v.mv_size = strlen (nsm_data[i][0]) + 1;
  243. ns_v.mv_data = (void *) nsm_data[i][1];
  244. ns_v.mv_size = strlen (nsm_data[i][1]) + 1;
  245. // If either ns or pfx exist, skip.
  246. if (
  247. mdb_cursor_get (dcur, &pfx_v, &ns_v, MDB_SET) != MDB_NOTFOUND
  248. ||
  249. mdb_cursor_get (icur, &ns_v, &pfx_v, MDB_SET) != MDB_NOTFOUND
  250. ) {
  251. rc = LSUP_CONFLICT;
  252. goto loop_end;
  253. }
  254. db_rc = mdb_cursor_put (dcur, &pfx_v, &ns_v, 0);
  255. db_rc |= mdb_cursor_put (icur, &ns_v, &pfx_v, 0);
  256. if (db_rc != MDB_SUCCESS) {
  257. log_error ("DB error: %s", LSUP_strerror (db_rc));
  258. rc = LSUP_DB_ERR;
  259. }
  260. loop_end:
  261. free (nsm_data[i]);
  262. }
  263. free (nsm_data);
  264. if (UNLIKELY (rc != LSUP_OK)) mdb_txn_abort (txn);
  265. else if (UNLIKELY (mdb_txn_commit (txn) != MDB_SUCCESS)) {
  266. mdb_txn_abort (txn);
  267. rc = LSUP_TXN_ERR;
  268. }
  269. return rc;
  270. }
  271. static const char *
  272. mdbstore_path_from_id (const char *id)
  273. {
  274. // Set environment path.
  275. if (!id) id = getenv ("LSUP_MDB_STORE_URN");
  276. if (!id) {
  277. id = LSUP_MDB_STORE_URN;
  278. log_info (
  279. "`LSUP_MDB_STORE_URN' environment variable is not "
  280. "set. The default URN %s has been set as the store ID.", id
  281. );
  282. }
  283. if (strncmp ("file://", id, 7) != 0) {
  284. log_error ("MDB store ID must be in the `file://<abs_path>` format.");
  285. return NULL;
  286. }
  287. return id + 7;
  288. }
  289. /** @brief Create the MDB environment and databases on disk.
  290. *
  291. * This function takes care of creaating the environment path if not existing,
  292. * and checking that it's a writable directory. If the path is not specified
  293. * in the LSUP_MDB_STORE_URN environment variable, a default directory is used.
  294. */
  295. static LSUP_rc
  296. mdbstore_setup (const char *id, bool clear)
  297. {
  298. const char *path = mdbstore_path_from_id (id);
  299. if (!path) return LSUP_VALUE_ERR;
  300. // If the directory exists (unless clear == true), do nothing.
  301. if (clear) rm_r (path);
  302. LSUP_rc rc = mkdir_p (path, ENV_DIR_MODE);
  303. log_info ("Create dir rc: %d", rc);
  304. log_info ("LSUP Create dir rc: %d", rc);
  305. RCCK (rc);
  306. // Open a temporary environment and txn to create the DBs.
  307. MDB_env *env;
  308. RCCK (mdb_env_create (&env));
  309. RCCK (mdb_env_set_maxdbs (env, N_DB));
  310. RCCK (mdb_env_open (env, path, 0, ENV_FILE_MODE));
  311. LOG_DEBUG("Environment opened at %s.", path);
  312. MDB_txn *txn;
  313. RCCK (mdb_txn_begin (env, NULL, 0, &txn));
  314. for (int i = 0; i < N_DB; i++) {
  315. LOG_TRACE("Creating DB %s", db_labels[i]);
  316. MDB_dbi dbi;
  317. RCCK (
  318. mdb_dbi_open (txn, db_labels[i], db_flags[i] | MDB_CREATE, &dbi)
  319. );
  320. }
  321. mdb_txn_commit (txn);
  322. mdb_env_close (env);
  323. return LSUP_OK;
  324. }
  325. /** @brief Open an MDB store.
  326. *
  327. * The store must have been set up with #mdbstore_setup.
  328. *
  329. * Some environment variables affect various store parameters:
  330. *
  331. * - LSUP_MDB_MAPSIZE Long int specifying the size of the memory map. Usually
  332. * it is not necessary to modify this, unless one is operating under memory
  333. * and disk constraints. The default map size is 1Tb.
  334. */
  335. static void *
  336. mdbstore_new (const char *id, size_t _unused)
  337. {
  338. (void) _unused;
  339. const char *path = mdbstore_path_from_id (id);
  340. if (!path) return NULL;
  341. MDBStore *store;
  342. CALLOC_GUARD (store, NULL);
  343. RCNL (mdb_env_create (&store->env));
  344. MDB_txn *txn = NULL;
  345. // Set map size.
  346. size_t mapsize;
  347. char *env_mapsize = getenv ("LSUP_MDB_MAPSIZE");
  348. if (env_mapsize == NULL) mapsize = DEFAULT_MAPSIZE;
  349. else sscanf (env_mapsize, "%lu", &mapsize);
  350. log_info (
  351. "Setting environment map size at %s to %lu Mb.",
  352. path, mapsize / 1024 / 1024);
  353. CHECK (mdb_env_set_mapsize (store->env, mapsize), fail);
  354. CHECK (mdb_env_set_maxdbs (store->env, N_DB), fail);
  355. CHECK (mdb_env_open (store->env, path, 0, ENV_FILE_MODE), fail);
  356. // Assign DB handles to store->dbi.
  357. CHECK (mdb_txn_begin (store->env, NULL, 0, &txn), fail);
  358. for (int i = 0; i < N_DB; i++)
  359. CHECK (mdb_dbi_open (
  360. txn, db_labels[i], db_flags[i], store->dbi + i), fail);
  361. // Bootstrap the permanent store with initial data.
  362. MDB_stat stat;
  363. CHECK (mdb_stat (txn, store->dbi[IDX_PFX_NS], &stat), fail);
  364. if (stat.ms_entries == 0) {
  365. LOG_DEBUG("Loading initial data into %s", path);
  366. // Load initial NS map.
  367. mdbstore_nsm_put (store, LSUP_default_nsm, txn);
  368. // Index default context.
  369. mdbstore_add_term (store, LSUP_default_ctx_buf, txn);
  370. }
  371. store->flags |= LSSTORE_OPEN;
  372. mdb_txn_commit (txn);
  373. txn = NULL;
  374. return store;
  375. fail:
  376. if (txn) mdb_txn_abort (txn);
  377. mdb_env_close (store->env);
  378. return NULL;
  379. }
  380. static void
  381. mdbstore_free (void *h)
  382. {
  383. MDBStore *store = h;
  384. if (store->flags & LSSTORE_OPEN) {
  385. const char *path;
  386. mdb_env_get_path (store->env, &path);
  387. log_info ("Closing MDB env at %s.", path);
  388. mdb_env_close (store->env);
  389. }
  390. free (store);
  391. }
  392. #if 0
  393. static char *
  394. mdbstore_id (const void *h)
  395. {
  396. const MDBStore *store = h;
  397. const char *path;
  398. mdb_env_get_path (store->env, &path);
  399. return strcat ("file://", path);
  400. }
  401. #endif
  402. static LSUP_rc
  403. mdbstore_stat (const MDBStore *store, MDB_stat *stat)
  404. {
  405. if (!(store->flags & LSSTORE_OPEN)) return 0;
  406. MDB_txn *txn;
  407. mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);
  408. if (mdb_stat (txn, store->dbi[IDX_SPO_C], stat) != MDB_SUCCESS)
  409. return LSUP_DB_ERR;
  410. mdb_txn_abort (txn);
  411. return LSUP_OK;
  412. }
  413. static size_t
  414. mdbstore_size (const void *h)
  415. {
  416. const MDBStore *store = h;
  417. // Size is calculated outside of any pending write txn.
  418. MDB_stat stat;
  419. if (mdbstore_stat (store, &stat) != LSUP_OK) return 0;
  420. return stat.ms_entries;
  421. }
  422. static LSUP_rc
  423. mdbstore_txn_begin (void *h, int flags, void **th)
  424. {
  425. MDBStore *store = h;
  426. RCCK (mdb_txn_begin (store->env, NULL, flags, (MDB_txn **) th));
  427. return LSUP_OK;
  428. }
  429. static LSUP_rc
  430. mdbstore_txn_commit (void *th)
  431. {
  432. RCCK (mdb_txn_commit ((MDB_txn *) th));
  433. return LSUP_OK;
  434. }
  435. static void
  436. mdbstore_txn_abort (void *th)
  437. { mdb_txn_abort ((MDB_txn *) th); }
  438. static void *
  439. mdbiter_txn (void *h)
  440. { return ((MDBIterator *) h)->txn; }
  441. /** @brief Begin an add loop.
  442. *
  443. * @sa #store_add_init_fn_t
  444. *
  445. * @param[in] th Previously opened MDB_txn handle, if the add loop shall be
  446. * run within an enclosing transaction. The transaction must be read-write.
  447. * The operation will always open a new transaction that is closed with
  448. * #mdbstore_add_done() or #mdbstore_add_abort(). If this parameter is not
  449. * NULL, the loop transaction will have the passed txn set as its parent.
  450. */
  451. static void *
  452. mdbstore_add_init (void *h, const LSUP_Buffer *sc, void *th)
  453. {
  454. MDBStore *store = h;
  455. /* An iterator is used here. Some members are a bit misused but it does
  456. * its job without having to define a very similar struct.
  457. */
  458. MDBIterator *it;
  459. MALLOC_GUARD (it, NULL);
  460. it->store = store;
  461. it->i = 0;
  462. CHECK (mdb_txn_begin (store->env, (MDB_txn *) th, 0, &it->txn), fail);
  463. if (sc) {
  464. // Store context if it's not the default one.
  465. it->luc = LSUP_buffer_hash (sc);
  466. // Insert t:st for context.
  467. //LOG_DEBUG("Adding context: %s", sc);
  468. it->key.mv_data = &it->luc;
  469. it->key.mv_size = KLEN;
  470. it->data.mv_data = sc->addr;
  471. it->data.mv_size = sc->size;
  472. int db_rc = mdb_put (
  473. it->txn, it->store->dbi[IDX_T_ST],
  474. &it->key, &it->data, MDB_NOOVERWRITE);
  475. if (db_rc != MDB_SUCCESS && db_rc != MDB_KEYEXIST) {
  476. log_error (LSUP_strerror (db_rc));
  477. mdb_txn_abort (it->txn);
  478. return NULL;
  479. }
  480. } else {
  481. LOG_DEBUG("No context passed to iterator, using default.");
  482. it->luc = LSUP_buffer_hash (LSUP_default_ctx_buf);
  483. }
  484. return it;
  485. fail:
  486. free (it);
  487. return NULL;
  488. }
  489. /*
  490. * NOTE: at the moment #mdbstore_remove() or another
  491. * #mdbstore_init() cannot be called between #mdbstore_add_init and
  492. * #mdbstore_add_abort or #mdbstore_add_done. FIXME
  493. *
  494. */
  495. static LSUP_rc
  496. mdbstore_add_iter (void *h, const LSUP_BufferTriple *sspo)
  497. {
  498. if (UNLIKELY (!h)) return LSUP_VALUE_ERR;
  499. MDBIterator *it = h;
  500. int db_rc = LSUP_NOACTION;
  501. LSUP_TripleKey spok = NULL_TRP;
  502. // Add triple terms.
  503. for (int i = 0; i < 3; i++) {
  504. LSUP_Buffer *st = LSUP_btriple_pos (sspo, i);
  505. spok[i] = LSUP_buffer_hash (st);
  506. it->key.mv_data = spok + i;
  507. it->key.mv_size = KLEN;
  508. it->data.mv_data = st->addr;
  509. it->data.mv_size = st->size;
  510. db_rc = mdb_put(
  511. it->txn, it->store->dbi[IDX_T_ST],
  512. &it->key, &it->data, MDB_NOOVERWRITE);
  513. if (db_rc != MDB_SUCCESS && db_rc != MDB_KEYEXIST) {
  514. LOG_RC (db_rc);
  515. return LSUP_DB_ERR;
  516. }
  517. }
  518. LOG_TRACE("Inserting spok: {%lx, %lx, %lx}", spok[0], spok[1], spok[2]);
  519. LOG_TRACE("Into context: %lx", it->luc);
  520. // Insert spo:c.
  521. it->key.mv_data = spok;
  522. it->key.mv_size = TRP_KLEN;
  523. // In triple mode, data is empty (= NULL_KEY).
  524. it->data.mv_data = &it->luc;
  525. it->data.mv_size = it->luc == NULL_KEY ? 0 : KLEN;
  526. db_rc = mdb_put(
  527. it->txn, it->store->dbi[IDX_SPO_C],
  528. &it->key, &it->data, MDB_NODUPDATA);
  529. if (db_rc == MDB_KEYEXIST) return LSUP_NOACTION;
  530. if (db_rc != MDB_SUCCESS) {
  531. log_error (
  532. "MDB error while inserting triple: %s", LSUP_strerror(db_rc));
  533. return LSUP_DB_ERR;
  534. }
  535. // Index.
  536. LSUP_rc rc = index_triple (it->store, OP_ADD, spok, it->luc, it->txn);
  537. if (rc == LSUP_OK) it->i++;
  538. return rc;
  539. }
  540. static LSUP_rc
  541. mdbstore_add_done (void *h)
  542. {
  543. MDBIterator *it = h;
  544. LSUP_rc rc = LSUP_OK;
  545. if (mdb_txn_commit (it->txn) != MDB_SUCCESS) {
  546. mdb_txn_abort (it->txn);
  547. rc = LSUP_TXN_ERR;
  548. }
  549. free (it);
  550. return rc;
  551. }
  552. static void
  553. mdbstore_add_abort (void *h)
  554. {
  555. MDBIterator *it = h;
  556. mdb_txn_abort (it->txn);
  557. free (it);
  558. }
  559. static LSUP_rc
  560. key_to_sterm (
  561. MDBStore *store, MDB_txn *txn, 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 (txn, 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. if (th) it->txn = th;
  598. else if (!it->txn) {
  599. // Start RO transaction if not in a write txn already.
  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 in lookup: %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: %x", it->luc);
  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->store, it->txn, it->spok[0], sspo->s);
  741. key_to_sterm (it->store, it->txn, it->spok[1], sspo->p);
  742. key_to_sterm (it->store, it->txn, 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->store, it->txn, 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. LSUP_Buffer **
  1027. mdbstore_ctx_list (void *h, void *th)
  1028. {
  1029. MDBStore *store = h;
  1030. LSUP_rc db_rc;
  1031. MDB_txn *txn;
  1032. if (th) txn = th;
  1033. else CHECK (mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn), fail);
  1034. MDB_cursor *cur;
  1035. CHECK (mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &cur), fail);
  1036. MDB_val key, data;
  1037. db_rc = mdb_cursor_get (cur, &key, &data, MDB_FIRST);
  1038. LSUP_Buffer **tdata = NULL;
  1039. size_t i = 0;
  1040. while (db_rc == MDB_SUCCESS) {
  1041. tdata = realloc (tdata, (i + 1) * sizeof (*tdata));
  1042. if (UNLIKELY (!tdata)) goto fail;
  1043. tdata[i] = BUF_DUMMY;
  1044. LSUP_Key tkey = *(LSUP_Key*)key.mv_data;
  1045. CHECK (key_to_sterm (store, txn, tkey, tdata[i]), fail);
  1046. db_rc = mdb_cursor_get (cur, &key, &data, MDB_NEXT_NODUP);
  1047. i++;
  1048. }
  1049. tdata = realloc (tdata, i * sizeof (data));
  1050. tdata[i] = NULL; // Sentinel
  1051. mdb_cursor_close (cur);
  1052. if (txn != th && txn != NULL) mdb_txn_abort (txn);
  1053. return tdata;
  1054. fail:
  1055. if (txn != th && txn != NULL) mdb_txn_abort (txn);
  1056. if (tdata) free (tdata);
  1057. return NULL;
  1058. }
  1059. const LSUP_StoreInt mdbstore_int = {
  1060. .name = "MDB Store",
  1061. .features = LSUP_STORE_PERM | LSUP_STORE_CTX | LSUP_STORE_IDX
  1062. | LSUP_STORE_TXN | LSUP_STORE_COW,
  1063. .setup_fn = mdbstore_setup,
  1064. .new_fn = mdbstore_new,
  1065. .free_fn = mdbstore_free,
  1066. .size_fn = mdbstore_size,
  1067. .txn_begin_fn = mdbstore_txn_begin,
  1068. .txn_commit_fn = mdbstore_txn_commit,
  1069. .txn_abort_fn = mdbstore_txn_abort,
  1070. .iter_txn_fn = mdbiter_txn,
  1071. .add_init_fn = mdbstore_add_init,
  1072. .add_iter_fn = mdbstore_add_iter,
  1073. .add_abort_fn = mdbstore_add_abort,
  1074. .add_done_fn = mdbstore_add_done,
  1075. .add_term_fn = mdbstore_add_term,
  1076. .update_ctx_fn = mdbstore_update_ctx,
  1077. .lookup_fn = mdbstore_lookup,
  1078. .lu_next_fn = mdbiter_next,
  1079. .lu_free_fn = mdbiter_free,
  1080. .remove_fn = mdbstore_remove,
  1081. .nsm_put_fn = mdbstore_nsm_put,
  1082. .nsm_get_fn = mdbstore_nsm_get,
  1083. .ctx_list_fn = mdbstore_ctx_list,
  1084. };
  1085. /* * * Static functions. * * */
  1086. /** @brief Index an added or removed triple.
  1087. *
  1088. * @param store[in] MDB store to index.
  1089. * @param op[in] Store operation. One of OP_ADD or OP_REMOVE.
  1090. * @param spok[in] Triple key to index.
  1091. * @param ck[in] Context to index, may be NULL.
  1092. * @param[in] th Transaction handle. This MUST be a valid pointer to an open
  1093. * RW transaction.
  1094. */
  1095. static LSUP_rc
  1096. index_triple(
  1097. MDBStore *store, StoreOp op, LSUP_TripleKey spok, LSUP_Key ck,
  1098. MDB_txn *txn)
  1099. {
  1100. int db_rc;
  1101. LSUP_rc rc = LSUP_NOACTION;
  1102. MDB_val v1, v2;
  1103. LOG_TRACE("Indexing triple: {%lx %lx %lx}", spok[0], spok[1], spok[2]);
  1104. // Index c:spo.
  1105. if (op == OP_REMOVE) {
  1106. LOG_TRACE("Indexing op: REMOVE");
  1107. if (ck != NULL_KEY) {
  1108. MDB_cursor *cur;
  1109. v1.mv_data = &ck;
  1110. v1.mv_size = KLEN;
  1111. v2.mv_data = spok;
  1112. v2.mv_size = TRP_KLEN;
  1113. mdb_cursor_open (txn, store->dbi[IDX_C_SPO], &cur);
  1114. if (mdb_cursor_get (cur, &v1, &v2, MDB_GET_BOTH) == MDB_SUCCESS) {
  1115. db_rc = mdb_cursor_del (cur, 0);
  1116. if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
  1117. rc = LSUP_OK;
  1118. }
  1119. mdb_cursor_close (cur);
  1120. }
  1121. } else if (op == OP_ADD) {
  1122. LOG_TRACE("Indexing op: ADD");
  1123. if (ck != NULL_KEY) {
  1124. v1.mv_data = &ck;
  1125. v1.mv_size = KLEN;
  1126. v2.mv_data = spok;
  1127. v2.mv_size = TRP_KLEN;
  1128. db_rc = mdb_put(
  1129. txn, store->dbi[IDX_C_SPO],
  1130. &v1, &v2, MDB_NODUPDATA);
  1131. if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
  1132. if (db_rc != MDB_KEYEXIST) rc = LSUP_OK;
  1133. }
  1134. } else return LSUP_VALUE_ERR;
  1135. LSUP_DoubleKey dbl_keys[3] = {
  1136. {spok[1], spok[2]}, // po
  1137. {spok[0], spok[2]}, // so
  1138. {spok[0], spok[1]}, // sp
  1139. };
  1140. // Add terms to index.
  1141. v1.mv_size = KLEN;
  1142. v2.mv_size = DBL_KLEN;
  1143. for (int i = 0; i < 3; i++) {
  1144. MDB_dbi db1 = store->dbi[lookup_indices[i]]; // s:po, p:so, o:sp
  1145. MDB_dbi db2 = store->dbi[lookup_indices[i + 3]]; // po:s, so:p, sp:o
  1146. v1.mv_data = spok + i;
  1147. v2.mv_data = dbl_keys[i];
  1148. if (op == OP_REMOVE) {
  1149. MDB_cursor *cur1, *cur2;
  1150. mdb_cursor_open(txn, store->dbi[lookup_indices[i]], &cur1);
  1151. db_rc = mdb_cursor_get (cur1, &v1, &v2, MDB_GET_BOTH);
  1152. if (db_rc == MDB_SUCCESS) mdb_cursor_del (cur1, 0);
  1153. mdb_cursor_close (cur1);
  1154. // Restore pointers invalidated after delete.
  1155. v1.mv_data = spok + i;
  1156. v2.mv_data = dbl_keys[i];
  1157. mdb_cursor_open(txn, store->dbi[lookup_indices[i + 3]], &cur2);
  1158. db_rc = mdb_cursor_get (cur2, &v2, &v1, MDB_GET_BOTH);
  1159. if (db_rc == MDB_SUCCESS) mdb_cursor_del (cur2, 0);
  1160. // TODO error handling.
  1161. rc = LSUP_OK;
  1162. mdb_cursor_close (cur2);
  1163. } else { // OP_ADD is guaranteed.
  1164. // 1-bound index.
  1165. LOG_TRACE("Indexing in %s: ", db_labels[lookup_indices[i]]);
  1166. LOG_TRACE(
  1167. "%lx: %lx %lx", *(size_t*)(v1.mv_data),
  1168. *(size_t*)(v2.mv_data), *(size_t*)(v2.mv_data) + 1);
  1169. db_rc = mdb_put (txn, db1, &v1, &v2, MDB_NODUPDATA);
  1170. if (db_rc == MDB_SUCCESS) rc = LSUP_OK;
  1171. else if (db_rc != MDB_KEYEXIST) return LSUP_DB_ERR;
  1172. // 2-bound index.
  1173. LOG_TRACE("Indexing in %s: ", db_labels[lookup_indices[i + 3]]);
  1174. LOG_TRACE(
  1175. "%lx %lx: %lx", *(size_t*)(v2.mv_data),
  1176. *(size_t*)(v2.mv_data) + 1, *(size_t*)(v1.mv_data));
  1177. db_rc = mdb_put (txn, db2, &v2, &v1, MDB_NODUPDATA);
  1178. if (db_rc == MDB_SUCCESS) rc = LSUP_OK;
  1179. else if (db_rc != MDB_KEYEXIST) return LSUP_DB_ERR;
  1180. }
  1181. }
  1182. return rc;
  1183. }
  1184. /* * * Term-specific iterators. * * */
  1185. /** @brief Advance 0-bound iterator.
  1186. *
  1187. * Cursor: spo:c
  1188. */
  1189. inline static void
  1190. it_next_0bound (MDBIterator *it)
  1191. {
  1192. memcpy (it->spok, it->key.mv_data, sizeof (LSUP_TripleKey));
  1193. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_NEXT);
  1194. }
  1195. /** @brief Advance 1-bound iterator.
  1196. *
  1197. * Uses paged data in a nested loop.
  1198. *
  1199. * Cursor: s:po, p:so, or o:sp.
  1200. */
  1201. inline static void
  1202. it_next_1bound (MDBIterator *it)
  1203. {
  1204. LSUP_DoubleKey *lu_dset = it->data.mv_data;
  1205. it->spok[it->term_order[0]] = it->luk[0];
  1206. it->spok[it->term_order[1]] = lu_dset[it->i][0];
  1207. it->spok[it->term_order[2]] = lu_dset[it->i][1];
  1208. LOG_TRACE(
  1209. "Composed triple: {%lx %lx %lx}",
  1210. it->spok[0], it->spok[1], it->spok[2]);
  1211. // Ensure next block within the same page is not beyond the last.
  1212. if (it->i < it->data.mv_size / DBL_KLEN - 1) {
  1213. it->i ++;
  1214. //LOG_DEBUG("Increasing page cursor to %lu.", it->i);
  1215. //LOG_DEBUG("it->rc: %d", it->rc);
  1216. } else {
  1217. // If the last block in the page is being yielded,
  1218. // move cursor to beginning of next page.
  1219. it->i = 0;
  1220. //LOG_DEBUG("Reset page cursor to %lu.", it->i);
  1221. it->rc = mdb_cursor_get (
  1222. it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
  1223. }
  1224. }
  1225. /** @brief Advance 2-bound iterator.
  1226. *
  1227. * Uses paged data in a nested loop.
  1228. *
  1229. * Cursor: po:s, so:p, or sp:o.
  1230. */
  1231. inline static void
  1232. it_next_2bound (MDBIterator *it)
  1233. {
  1234. LSUP_Key *lu_dset = it->data.mv_data;
  1235. it->spok[it->term_order[0]] = it->luk[0];
  1236. it->spok[it->term_order[1]] = it->luk[1];
  1237. it->spok[it->term_order[2]] = lu_dset[it->i];
  1238. // Ensure next block within the same page is not beyond the last.
  1239. if (it->i < it->data.mv_size / KLEN - 1)
  1240. it->i ++;
  1241. else {
  1242. // If the last block in the page is being yielded,
  1243. // move cursor to beginning of next page.
  1244. it->i = 0;
  1245. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_NEXT_MULTIPLE);
  1246. }
  1247. }
  1248. /** @brief Advance 3-bound iterator.
  1249. *
  1250. * This is a special case of 0÷1 results; either there was one matching triple,
  1251. * which was already set in the first result, or there was none, i.e. it->rc is
  1252. * already MDB_NOTFOUND and this function will not be called.
  1253. */
  1254. inline static void
  1255. it_next_3bound (MDBIterator *it)
  1256. { it->rc = MDB_NOTFOUND; }
  1257. /* * * Term-specific lookups. * * */
  1258. inline static LSUP_rc
  1259. lookup_0bound (MDBIterator *it, size_t *ct)
  1260. {
  1261. LOG_DEBUG("Looking up 0 bound terms.");
  1262. if (ct) {
  1263. if (it->luc != NULL_KEY) {
  1264. // Look up by given context.
  1265. it->rc = mdb_cursor_open (
  1266. it->txn, it->store->dbi[IDX_C_SPO], &it->cur);
  1267. it->key.mv_data = &it->luc;
  1268. it->key.mv_size = KLEN;
  1269. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1270. if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
  1271. mdb_cursor_close (it->cur);
  1272. it->cur = NULL;
  1273. } else {
  1274. // Look up all contexts.
  1275. MDB_stat stat;
  1276. mdb_stat (it->txn, it->store->dbi[IDX_S_PO], &stat);
  1277. *ct = stat.ms_entries;
  1278. }
  1279. LOG_DEBUG("Found %lu triples.", *ct);
  1280. }
  1281. it->rc = mdb_cursor_open (it->txn, it->store->dbi[IDX_SPO_C], &it->cur);
  1282. if (it->rc != MDB_SUCCESS) {
  1283. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1284. return LSUP_DB_ERR;
  1285. }
  1286. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_FIRST);
  1287. /*
  1288. mdb_cursor_close (it->cur);
  1289. it->cur = NULL;
  1290. */
  1291. it->iter_op_fn = it_next_0bound;
  1292. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1293. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1294. return LSUP_DB_ERR;
  1295. }
  1296. return LSUP_OK;
  1297. }
  1298. inline static LSUP_rc
  1299. lookup_1bound (uint8_t idx0, MDBIterator *it, size_t *ct)
  1300. {
  1301. it->term_order = (const uint8_t*)lookup_ordering_1bound[idx0];
  1302. LOG_DEBUG("Looking up 1 bound term: %lx", it->luk[0]);
  1303. mdb_cursor_open (it->txn, it->store->dbi[lookup_indices[idx0]], &it->cur);
  1304. it->key.mv_data = it->luk;
  1305. it->key.mv_size = KLEN;
  1306. if (ct) {
  1307. // If a context is specified, the only way to count triples matching
  1308. // the context is to loop over them.
  1309. if (it->luc != NULL_KEY) {
  1310. LOG_DEBUG("Counting in context: %lx", it->luc);
  1311. MDBIterator *ct_it;
  1312. MALLOC_GUARD (ct_it, LSUP_MEM_ERR);
  1313. /*
  1314. memcpy (ct_it, it, sizeof (*ct_it));
  1315. */
  1316. ct_it->store = it->store;
  1317. ct_it->txn = it->txn;
  1318. ct_it->ctx_cur = it->ctx_cur;
  1319. ct_it->key = it->key;
  1320. ct_it->data = it->data;
  1321. ct_it->ck = NULL;
  1322. ct_it->luk[0] = it->luk[0];
  1323. ct_it->luc = it->luc;
  1324. ct_it->i = 0;
  1325. LSUP_rc rc = lookup_1bound (idx0, ct_it, NULL);
  1326. if (rc < 0) return rc;
  1327. LSUP_rc db_rc;
  1328. while (LSUP_END != (db_rc = mdbiter_next_key (ct_it))) {
  1329. if (UNLIKELY (db_rc < 0)) return db_rc;
  1330. (*ct)++;
  1331. }
  1332. // Free the counter iterator without freeing the shared txn.
  1333. if (ct_it->cur) mdb_cursor_close (ct_it->cur);
  1334. free (ct_it->ck);
  1335. free (ct_it);
  1336. } else {
  1337. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1338. if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
  1339. }
  1340. LOG_DEBUG("Found %lu triples.", *ct);
  1341. }
  1342. it->i = 0;
  1343. it->iter_op_fn = it_next_1bound;
  1344. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1345. if (it->rc == MDB_SUCCESS)
  1346. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
  1347. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1348. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1349. return LSUP_DB_ERR;
  1350. }
  1351. return LSUP_OK;
  1352. }
  1353. inline static LSUP_rc
  1354. lookup_2bound(uint8_t idx0, uint8_t idx1, MDBIterator *it, size_t *ct)
  1355. {
  1356. uint8_t luk1_offset, luk2_offset;
  1357. MDB_dbi dbi = 0;
  1358. // Establish lookup ordering with some awkward offset math.
  1359. for (int i = 0; i < 3; i++) {
  1360. if (
  1361. (
  1362. idx0 == lookup_ordering_2bound[i][0] &&
  1363. idx1 == lookup_ordering_2bound[i][1]
  1364. ) || (
  1365. idx0 == lookup_ordering_2bound[i][1] &&
  1366. idx1 == lookup_ordering_2bound[i][0]
  1367. )
  1368. ) {
  1369. it->term_order = (const uint8_t*)lookup_ordering_2bound[i];
  1370. if (it->term_order[0] == idx0) {
  1371. luk1_offset = 0;
  1372. luk2_offset = 1;
  1373. } else {
  1374. luk1_offset = 1;
  1375. luk2_offset = 0;
  1376. }
  1377. dbi = it->store->dbi[lookup_indices[i + 3]];
  1378. LOG_DEBUG(
  1379. "Looking up 2 bound in %s",
  1380. db_labels[lookup_indices[i + 3]]);
  1381. break;
  1382. }
  1383. }
  1384. if (dbi == 0) {
  1385. log_error (
  1386. "Values %d and %d not found in lookup keys.",
  1387. idx0, idx1);
  1388. return LSUP_VALUE_ERR;
  1389. }
  1390. // Compose term keys in lookup key.
  1391. LSUP_DoubleKey luk;
  1392. luk[luk1_offset] = it->luk[0];
  1393. luk[luk2_offset] = it->luk[1];
  1394. it->key.mv_data = luk;
  1395. it->key.mv_size = DBL_KLEN;
  1396. mdb_cursor_open (it->txn, dbi, &it->cur);
  1397. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1398. if (ct) {
  1399. // If a context is specified, the only way to count triples matching
  1400. // the context is to loop over them.
  1401. if (it->luc != NULL_KEY) {
  1402. MDBIterator *ct_it;
  1403. MALLOC_GUARD (ct_it, LSUP_MEM_ERR);
  1404. ct_it->store = it->store;
  1405. ct_it->txn = it->txn;
  1406. ct_it->ctx_cur = it->ctx_cur;
  1407. ct_it->ck = NULL;
  1408. ct_it->luk[0] = it->luk[0];
  1409. ct_it->luk[1] = it->luk[1];
  1410. ct_it->luc = it->luc;
  1411. ct_it->i = 0;
  1412. lookup_2bound (idx0, idx1, ct_it, NULL);
  1413. while (mdbiter_next_key (ct_it) != LSUP_END) (*ct) ++;
  1414. // Free the counter iterator without freeing the shared txn.
  1415. if (ct_it->cur) mdb_cursor_close (ct_it->cur);
  1416. free (ct_it->ck);
  1417. free (ct_it);
  1418. } else {
  1419. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1420. if (it->rc == MDB_SUCCESS) mdb_cursor_count (it->cur, ct);
  1421. }
  1422. LOG_DEBUG("Found %lu triples.", *ct);
  1423. }
  1424. it->i = 0;
  1425. it->iter_op_fn = it_next_2bound;
  1426. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_SET);
  1427. if (it->rc == MDB_SUCCESS)
  1428. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_MULTIPLE);
  1429. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1430. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1431. return LSUP_DB_ERR;
  1432. }
  1433. return LSUP_OK;
  1434. }
  1435. inline static LSUP_rc
  1436. lookup_3bound (MDBIterator *it, size_t *ct)
  1437. {
  1438. LOG_DEBUG(
  1439. "Looking up 3 bound: {%lx, %lx, %lx}",
  1440. it->luk[0], it->luk[1], it->luk[2]);
  1441. it->key.mv_data = it->luk;
  1442. if (it->luc != NULL_KEY) {
  1443. it->rc = mdb_cursor_open (
  1444. it->txn, it->store->dbi[IDX_SPO_C], &it->cur);
  1445. it->key.mv_size = TRP_KLEN;
  1446. it->data.mv_data = &it->luc;
  1447. it->data.mv_size = KLEN;
  1448. } else {
  1449. it->rc = mdb_cursor_open (it->txn, it->store->dbi[IDX_S_PO], &it->cur);
  1450. it->key.mv_size = KLEN;
  1451. it->data.mv_data = it->luk + 1;
  1452. it->data.mv_size = DBL_KLEN;
  1453. }
  1454. it->rc = mdb_cursor_get (it->cur, &it->key, &it->data, MDB_GET_BOTH);
  1455. if (it->rc != MDB_SUCCESS && it->rc != MDB_NOTFOUND) {
  1456. log_error ("Database error: %s", LSUP_strerror (it->rc));
  1457. return LSUP_DB_ERR;
  1458. }
  1459. mdb_cursor_close (it->cur);
  1460. it->cur = NULL;
  1461. if (ct && it->rc == MDB_SUCCESS) *ct = 1;
  1462. it->iter_op_fn = it_next_3bound;
  1463. memcpy (it->spok, it->luk, sizeof (LSUP_TripleKey));
  1464. return LSUP_OK;
  1465. }