store_interface.h 20 KB

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