store_interface.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /** @file store_interface.h
  2. *
  3. * @brief Common store back end interfaces.
  4. *
  5. * Code using the store interface should include NOT this header, but rahter
  6. * `store.h`.
  7. *
  8. * This header is included by all back end implementations, which are in
  9. * their turn included by `store.h`.
  10. *
  11. * The basic interfaces for store and store iterator implementations are
  12. * defined here. New store implementations should include this header and
  13. * implement three basic elements:
  14. *
  15. * - A structure representing the store back end. This structure will be
  16. * opaque to all downstream code and its layout is entirely up to the
  17. * implementer.
  18. *
  19. * - A structure representing a store iterator state, also opaque.
  20. *
  21. * - The LSUP_StoreInt interface with all the functions defined in the
  22. * interface necessary to interact with the store.
  23. *
  24. * See the `store_htable.{c,h}` and `store_mdb.{c,h}` files for examples of
  25. * fully functioning implementations.
  26. *
  27. * The #LSUP_StoreInt structure defines a store interface for raw buffer
  28. * triples. Nothing in the store functions' signatures should hint at RDF
  29. * triples—they should accept and produce exclusively raw byte buffers
  30. * (#LSUP_Buffer). A store interface may have any of the `LSUP_STORE_*` faeture
  31. * flags which should be reflected in the way its members are implemented.
  32. */
  33. #ifndef _LSUP_STORE_INTERFACE_H
  34. #define _LSUP_STORE_INTERFACE_H
  35. #include "environment.h"
  36. /*
  37. * Store feature flags.
  38. *
  39. * NOTE: LSUP_STORE_PERM need only be set by an implementation based on whether
  40. * its path is on a default temporary dir (e.g. LSUP_MDB_RAMDISK_PATH). If this
  41. * flag is not set, it means the data will be cleared before the next execution
  42. * of the program. However, its being set does not guarantee the persistence of
  43. * the medium (i.e. a "permanent" store may have been created ad hoc on a
  44. * tempfs).
  45. */
  46. typedef enum {
  47. LSUP_STORE_PERM = 1<<0, ///< Store is on a permanent support.
  48. LSUP_STORE_CTX = 1<<1, ///< Store supports contexts (quads).
  49. LSUP_STORE_IDX = 1<<2, ///< Store is fully SPO(C)-indexed.
  50. LSUP_STORE_TXN = 1<<3, ///< Supports transaction handling.
  51. LSUP_STORE_COW = 1<<4, ///< Copy on write. @sa #iter_next_fn_t()
  52. } LSUP_StoreFeature;
  53. /*
  54. * Store function types.
  55. */
  56. /** @brief Prototype: create any environment necessary for the store to work.
  57. *
  58. * This function should be idempotent on separate calls to the same `id`,
  59. * unless the `clear` option is set to `true`.
  60. *
  61. * @param[in,out] id Identifier to use for the store. This should be
  62. * a URI that uniquely identifies a back end for the implementation using it,
  63. * e.g. a SQL connection string, file path for an embedded store, the URL of a
  64. * REST API endpoint, etc. It may also be NULL, in which case it will be set to
  65. * the default identifier set by the implementation. It can be retrieved from
  66. * an existing store via #store_id_fn_t .
  67. *
  68. * @param[in] clear Whether to remove an existing environment with the same ID.
  69. */
  70. typedef LSUP_rc (*store_setup_fn_t)(const char *id, bool clear);
  71. /** @brief Prototype: create a new store.
  72. *
  73. * @param[in] id Identifier for the new store. How this is interpreted, and
  74. * whether it is even used, depends on the implementation, which should
  75. * provide documentation on how to pass and interpret this parameter.
  76. *
  77. * @param[in] size Initial size for the store. It may be 0. Only meaningful
  78. * for stores that may preallocate memory, such as #HTStore.
  79. *
  80. * @return New store handle.
  81. */
  82. typedef void * (*store_new_fn_t)(const char *id, size_t size);
  83. /** @brief Prototype: free store handle.
  84. *
  85. * @param[in] store Store handle.
  86. *
  87. */
  88. typedef void (*store_free_fn_t)(void *store);
  89. /** @brief Prototype: get the store ID.
  90. *
  91. * @param[in] store Store handle.
  92. *
  93. * @return store ID string. This is a copy and should be freed after use.
  94. */
  95. typedef char * (*store_id_fn_t)(const void *store);
  96. /** @brief Prototype: get store size.
  97. *
  98. * @param[in] store The store to calculate size of.
  99. *
  100. * @return Number of stored SPO triples (across all contexts if supported).
  101. */
  102. typedef size_t (*store_size_fn_t)(const void *store);
  103. #if 0
  104. /** @brief Print stats about a store.
  105. *
  106. * TODO
  107. *
  108. * @param[in] store The store to get stats for.
  109. */
  110. typedef LSUP_rc (*store_stat_fn_t)(void *store, void *stat);
  111. #endif
  112. /** @brief Begin a transaction.
  113. *
  114. * Only for LSUP_STORE_TXN stores.
  115. *
  116. * The transaction handle is managed by the store implementation and can be any
  117. * data type.
  118. *
  119. * @param[in] store Store handle.
  120. *
  121. * @param[in] flags Transaction flags. These vary with each implementation.
  122. *
  123. * @param[out] txn Will point to the new open transaction on success, or to
  124. * undefined content on failure.
  125. *
  126. * @return LSUP_OK if the transaction started successfully, <0 on error.
  127. */
  128. typedef LSUP_rc (*store_txn_begin_fn_t)(void *store, int flags, void **txn);
  129. /** @brief Commit a transaction.
  130. *
  131. * Only for LSUP_STORE_TXN stores.
  132. *
  133. * @param[in] store Store handle.
  134. *
  135. * @param[in] txn Transaction handle generated by #store_txn_begin_fn_t.
  136. *
  137. * @return LSUP_OK if the transaction was committed successfully, <0 on error.
  138. */
  139. typedef LSUP_rc (*store_txn_commit_fn_t)(void *store);
  140. /** @brief Abort a transaction.
  141. *
  142. * Only for LSUP_STORE_TXN stores.
  143. *
  144. * @param[in] store Store handle.
  145. *
  146. * @param[in] txn Transaction handle generated by #store_txn_begin_fn_t.
  147. */
  148. typedef void (*store_txn_abort_fn_t)(void *store);
  149. /** @brief Initialize bulk triple load.
  150. *
  151. * This is the first step of a bulk load. It is best used when the data at hand
  152. * need to be pre-processed, which can be done in the same loop as the next
  153. * step to keep memory usage low.
  154. *
  155. * @param store[in] The store to add to.
  156. *
  157. * @param sc[in] Context as a serialized term. If this is NULL, and the
  158. * default context is not NULL, triples will be added to the default context
  159. * for the store, If the default context for the store is NULL, regardless of
  160. * the value of sc, triples will be added with no context. Only meaningful
  161. * for stores with the LSUP_STORE_CTX feature.
  162. *
  163. * @param[in] udata User data. Consult individual store implementations for
  164. * how this is interpreted.
  165. *
  166. * @return Iterator handle to be passed to the following load steps.
  167. */
  168. typedef void * (*store_add_init_fn_t)(
  169. void *store, const LSUP_Buffer *sc, void *udata);
  170. /** @brief Add one triple into the store.
  171. *
  172. * This must be called after #add_init_fn, using the iterator
  173. * yielded by that function. It may be called multiple times and must be
  174. * followed by #add_done_fn or #add_abort_fn (if supported).
  175. *
  176. * @param it[in] Iterator obtained by #store_add_init_fn_t.
  177. * The following members are of interest:
  178. * it->i stores the total number of records inserted.
  179. *
  180. * @param sspo[in] Serialized triple to be added.
  181. *
  182. * @return LSUP_OK if the triple was inserted; LSUP_NOACTION if the triple
  183. * already existed; LSUP_DB_ERR if an MDB error occurred.
  184. */
  185. typedef LSUP_rc (*store_add_iter_fn_t)(
  186. void *it, const LSUP_BufferTriple * sspo);
  187. /** @brief Abort an add loop and free iterator.
  188. *
  189. * Usually called on an irrecoverable error from #add_iter_fn. None of the
  190. * successful inserts in the same loop is retained.
  191. *
  192. * @param it[in] Iterator obtained by #store_add_init_fn_t.
  193. */
  194. typedef void (*store_add_abort_fn_t)(void *it);
  195. /*
  196. * Iterator function types.
  197. */
  198. /** @brief Get iterator active transaction handle.
  199. *
  200. * This function is used to get an active transaction during an iteration loop
  201. * in order to perform an action using the store state within that loop. Some
  202. * stores (e.g. MDB) only support one R/W open transaction per thread, so this
  203. * is also the only way to perform anything else than iterating or committing
  204. * while a loop is open.
  205. *
  206. * @param[in] it Iterator handle to get the transaction from.
  207. *
  208. * @return Transaction handle. DO NOT close this transaction directly.
  209. */
  210. typedef void * (*iter_txn_fn_t)(void *it);
  211. /** @brief Finalize an add loop and free iterator.
  212. *
  213. * This must be called after #add_iter_fn.
  214. *
  215. * @param it[in] Iterator obtained by #LSUP_mdbstore_add_init.
  216. */
  217. typedef LSUP_rc (*store_add_done_fn_t)(void *it);
  218. /** @brief Add a single term to the store.
  219. *
  220. * @param[in] store Store handle.
  221. *
  222. * @param[in] sterm Serialized term to store.
  223. */
  224. typedef LSUP_rc (*store_add_term_fn_t)(
  225. void *store, const LSUP_Buffer *sterm, void *udata);
  226. /** @brief Prototype: look up triples by pattern matching.
  227. *
  228. * This function may return a count of matches and/or an iterator of results as
  229. * serialized triples.
  230. *
  231. * For stores with #LSUP_STORE_TXN, this opens a read-only transaction. The
  232. * transaction handle is held in the iterator structure and is closed when the
  233. * iterator is freed with #iter_free_fn_t().
  234. *
  235. * Any and all of the terms may be NULL, which indicates an unbound query
  236. * term. Stores witout context support will always ignore sc.
  237. *
  238. * @param[in] store The store to be queried.
  239. *
  240. * @param[in] ss Serialized s term.
  241. *
  242. * @param[in] sp Serialized p term.
  243. *
  244. * @param[in] so Serialized o term.
  245. *
  246. * @param[in] sc Serialized context to limit search to. It may be NULL, in
  247. * which case search is done in all contexts. Note that triples inserted
  248. * without context are assigned the *default* context for the store.
  249. *
  250. * @param[out] ct If not NULL, this will be populated with the number of
  251. * entries found. It is very inexpensive to set for lookups without context,
  252. * much less so for 1-bound and 2-bound context lookups, in which cases it
  253. * should be set only if needed.
  254. *
  255. * @param[in] udata User data. Consult individual store implementations for
  256. * how this is interpreted.
  257. *
  258. * @return Iterator handle that will be populated with a result iterator. This
  259. * is always created even if no matches are found and must be freed with
  260. * #LSUP_mdbiter_free() after use. If matches are found, the iterator points to
  261. * the first result which can be retrieved with #iter_next_fn().
  262. */
  263. typedef void * (*store_lookup_fn_t)(
  264. void *store,
  265. const LSUP_Buffer *ss, const LSUP_Buffer *sp, const LSUP_Buffer *so,
  266. const LSUP_Buffer *sc, void *udata, size_t *ct);
  267. /** @brief Prototype: check for existence of a triple (T/F).
  268. *
  269. * @param[in] Store to be queried.
  270. *
  271. * @param[in] spo Triple to look up. All members must not be NULL.
  272. *
  273. * @param[in] c Optional context to look into. It may be NULL. It is
  274. * disregarded by stores without the LSUP_STORE_CTX feature.
  275. *
  276. * @return Whether the triple exist in the store (/context).
  277. */
  278. typedef bool (*store_trp_exist_fn_t)(
  279. void *store, const LSUP_BufferTriple *sspo, const LSUP_Buffer *sc);
  280. /** @brief Prototype: delete triples by pattern matching.
  281. *
  282. * The ss, sp, so, sc terms act as a matching pattern as documented in
  283. * @sa #store_lookup_fn. if not NULL, ct yields the number of triples actually
  284. * deleted.
  285. *
  286. * @param[in] udata User data. Consult individual store implementations for
  287. * how this is interpreted.
  288. */
  289. typedef LSUP_rc (*store_remove_fn_t)(
  290. void *store,
  291. const LSUP_Buffer *ss, const LSUP_Buffer *sp, const LSUP_Buffer *so,
  292. const LSUP_Buffer *sc, void *udata, size_t *ct);
  293. /** @brief Put an in-memory namespace map into a permanent back end.
  294. *
  295. * This is only available in stores with the LSUP_STORE_PERM feature.
  296. *
  297. * Existing prefixes and namespaces are not updated. Thus, if the following are
  298. * already stored:
  299. *
  300. * ns1: <urn:ns:a#>
  301. * ns2: <urn:ns:b#>
  302. *
  303. * Neither of the following will be inserted:
  304. *
  305. * ns3: <urn:ns:a#>
  306. * ns2: <urn:ns:c#>
  307. *
  308. * @param[in] store MDB store to update.
  309. *
  310. * @param[out] nsm Namespace map handle to store.
  311. *
  312. * @param[in] udata User-defined data. Consult individual implementations for
  313. * details.
  314. *
  315. * @return LSUP_OK if all terms were updated; LSUP_CONFLICT if one or more
  316. * namespaces or terms were not updated because they already existed; <0 if
  317. * an error occurred.
  318. */
  319. typedef LSUP_rc (*store_nsm_put_fn_t)(
  320. void *store, const LSUP_NSMap * nsm, void *udata);
  321. /** @brief Get the store's namespace prefix map.
  322. *
  323. * @param[in] store MDB store to query.
  324. *
  325. * @return NS map or NULL on error.
  326. */
  327. typedef LSUP_NSMap * (*store_nsm_get_fn_t)(void *store);
  328. /** @brief Prototype: yield the matching triples and advance the iterator.
  329. *
  330. * NOTE: Iterators keep transactions open. Don't hold on to them longer than
  331. * necessary.
  332. *
  333. * NOTE: If the store interface has the LSUP_STORE_COW feature, the memory
  334. * buffer referenced by the #LSUP_Buffer handle is owned by the database. It
  335. * must not be written to or freed. To modify the data or use them beyond the
  336. * caller's scope, this memory must be copied. Note that the #LSUP_Buffer
  337. * handle must still be freed (with a plain `free()`), but not the underlying
  338. * data buffer, since only the latter is owned by the back end. For stores
  339. * without the LSUP_STORE_COW, data are copied on retrieval and the resulting
  340. * buffers can be freed with #LSUP_buffer_free() or analogous methods.
  341. *
  342. * @param[in] it Opaque iterator handle obtained with the store's #lookup_fn.
  343. *
  344. * @param[out] sspo #LSUP_BufferTriple to be populated with three serialized
  345. * terms if found. It may be NULL, in which case it is not populated.
  346. *
  347. * @param[out] ctx If not NULL, it is populated with a NULL-terminated array of
  348. * LSUP_Buffer structs, one for each context associated with the matching
  349. * triple. These contexts are the same regardless of the context filter used
  350. * in the lookup. The array is freed with a simple #free(). This parameter
  351. * is ignored by implementations without the LSUP_STORE_CTX feature.
  352. *
  353. * To iterate over the context array, use this loop:
  354. *
  355. * size_t i = 0;
  356. * while (ctx[i].addr)
  357. * do_something(ctx + (i++));
  358. *
  359. * @return LSUP_OK if results were found; LSUP_END if no (more) results were
  360. * found; LSUP_DB_ERR if a backend error occurred.
  361. */
  362. typedef LSUP_rc (*iter_next_fn_t)(
  363. void *it, LSUP_BufferTriple *sspo, LSUP_Buffer **ctx);
  364. /** @brief Prototype: free an iterator allocated by a lookup.
  365. *
  366. * @param it[in] Iterator pointer. It will be set to NULL after freeing.
  367. */
  368. typedef void (*iter_free_fn_t)(void * it);
  369. /*
  370. * Iterface type definitions.
  371. */
  372. /** @brief Store interface.
  373. *
  374. * New store implementations should define a static structure with the relevant
  375. * members filled in. Some members are only relevant to certain types of stores
  376. * and may be set to NULL.
  377. *
  378. * #setup_fn may be optionally defined and MUST cause an idempotent action,
  379. * unless the `clear` argument is set to `true`. Callers should check if this
  380. * member is NULL and if it is not, call it at the beginning of the
  381. * interaction with the store.
  382. *
  383. * Transaction control members are only applicable to stores with the
  384. * #LSUP_STORE_TXN feature.
  385. */
  386. typedef struct store_if_t {
  387. // Basic properties.
  388. char name[16]; ///< Store type name.
  389. LSUP_StoreFeature features; ///< Feature flags.
  390. // Allocation, setup and deallocation.
  391. store_setup_fn_t setup_fn; ///< Called before #store_new_fn_t.
  392. store_new_fn_t new_fn; ///< Create a new store instance.
  393. store_free_fn_t free_fn; ///< Free the store.
  394. // Metadata.
  395. store_size_fn_t size_fn; ///< Number of triples in the store.
  396. store_id_fn_t id_fn; ///< Get store ID.
  397. // Transaction control.
  398. store_txn_begin_fn_t txn_begin_fn; ///< Begin transaction.
  399. store_txn_commit_fn_t txn_commit_fn; ///< Commit transaction.
  400. store_txn_abort_fn_t txn_abort_fn; ///< Abort transaction.
  401. iter_txn_fn_t iter_txn_fn; ///< Get iterator's transaction.
  402. // Addition.
  403. store_add_init_fn_t add_init_fn; ///< Initialize add iteration.
  404. store_add_iter_fn_t add_iter_fn; ///< Add one triple.
  405. store_add_abort_fn_t add_abort_fn; /**< Abort (roll back) the add process.
  406. *
  407. * Only available in
  408. * stores with #LSUP_STORE_TXN
  409. * feature. Optional.
  410. */
  411. store_add_done_fn_t add_done_fn; ///< Complete the add process.
  412. store_add_term_fn_t add_term_fn; /**< Add (index) a term to the store.
  413. *
  414. * Only available in stores with
  415. * #LSUP_STORE_IDX feature. Optional.
  416. */
  417. // Look up.
  418. store_lookup_fn_t lookup_fn; ///< Look up triples by pattern. 
  419. //store_trp_exist_fn_t exist_fn; ///< Check if a triple exists.
  420. iter_next_fn_t lu_next_fn; ///< Advance the lookup iterator.
  421. iter_free_fn_t lu_free_fn; ///< Free the lookup iterator.
  422. // Removal.
  423. store_remove_fn_t remove_fn; ///< Remove triples by pattern.
  424. // Namespace prefix mapping.
  425. store_nsm_put_fn_t nsm_put_fn; /**< Add a ns/pfx pair to the map.
  426. *
  427. * Only available (and mandatory)
  428. * in stores with the
  429. * #LSUP_STORE_IDX feature.
  430. */
  431. store_nsm_get_fn_t nsm_get_fn; /**< Get a namespace from the map.
  432. *
  433. * Only available (and mandatory)
  434. * in stores with the
  435. * #LSUP_STORE_IDX feature.
  436. */
  437. } LSUP_StoreInt;
  438. /*
  439. * Template for a new store and iterator implementation.
  440. * These should be placed in the .c file where the interface functions are
  441. * defined, and declared as `extern` in the related .h file.
  442. const LSUP_StoreInt my_store_int = {
  443. .name = "My Store", // Truncated to 15 chars.
  444. .features = LSUP_STORE_PERM | LSUP_STORE_IDX,
  445. .setup_fn = my_setup_fn,
  446. .new_fn = my_new_fn,
  447. .free_fn = my_free_fn,
  448. .size_fn = my_size_fn,
  449. .id_fn = my_id_fn,
  450. .txn_begin_fn = my_txn_begin_fn,
  451. .txn_commit_fn = my_txn_commit_fn,
  452. .txn_abort_fn = my_txn_abort_fn,
  453. .add_init_fn = my_init_fn,
  454. .add_iter_fn = my_iter_fn,
  455. .add_abort_fn = my_add_abort_fn,
  456. .add_done_fn = my_add_done_fn,
  457. .add_term_fn = my_add_term_fn,
  458. .lookup_fn = my_lookup_fn,
  459. .lu_next_fn = my_iter_next_fn,
  460. .lu_free_fn = my_iter_free_fn,
  461. .remove_fn = my_remove_fn,
  462. .nsm_put_fn = my_nsm_put_fn,
  463. .nsm_get_fn = my_nsm_get_fn,
  464. };
  465. */
  466. #endif /* _LSUP_STORE_INTERFACE_H */