store_mdb.c 50 KB

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