store_mdb.c 48 KB

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