store_mdb.c 46 KB

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