store.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /** @file store.h
  2. *
  3. * @brief Generic store dependency.
  4. *
  5. * This code allows to use the store interface with any supported back end
  6. * without any knowledge of the back end implementations. Code using the store
  7. * interface need only include this file.
  8. *
  9. * New store implementations should be added to this file as `#include`s as
  10. * well as entries in the `BACKEND_TBL` macro.
  11. */
  12. #ifndef _LSUP_STORE_H
  13. #define _LSUP_STORE_H
  14. /*
  15. * Add new back end headers here.
  16. */
  17. #include "lsup/store_htable.h"
  18. #include "lsup/store_mdb.h"
  19. /** @defgroup store Backend store module
  20. * @ingroup public
  21. * @{
  22. */
  23. /*
  24. * Define backend types.
  25. *
  26. * Add new store implementations to this table.
  27. */
  28. #define BACKEND_TBL \
  29. /* #enum suffix#store if */\
  30. ENTRY( HTABLE, htstore_int ) \
  31. ENTRY( MDB, mdbstore_int ) \
  32. /// Store types. All prefixed with `LSUP_STORE_`.
  33. typedef enum {
  34. #define ENTRY(a, b) \
  35. LSUP_STORE_##a,
  36. BACKEND_TBL
  37. #undef ENTRY
  38. } LSUP_StoreType;
  39. /** @brief Store structure.
  40. *
  41. * Code using the store interface should create an instance of this structure
  42. * with pointers to the store interface of their choice, and to an opaque
  43. * structure to hold the store state.
  44. *
  45. * Iterator state handles generated by lookup and add functions are kept
  46. * outside of this structure, but manipulated by the interface specified in
  47. * here.
  48. *
  49. * @sa #LSUP_graph_new()
  50. */
  51. typedef struct store_t {
  52. LSUP_StoreType type; ///< Store type.
  53. char * id; /**< Store ID.
  54. *
  55. * NOTE: This is
  56. * NULL for volatile stores.
  57. */
  58. const LSUP_StoreInt * sif; ///< Store interface.
  59. void * data; ///< Store back end data.
  60. } LSUP_Store;
  61. /** @brief Return store interface for a specific type.
  62. */
  63. const LSUP_StoreInt *
  64. LSUP_store_int (LSUP_StoreType type);
  65. /** @brief Return the store type label.
  66. *
  67. * @param type Store type enum.
  68. *
  69. * @return Store type label as `STORE_<type>`.
  70. */
  71. const char *
  72. LSUP_store_type_label (LSUP_StoreType type);
  73. /** @brief Create a new store.
  74. *
  75. * The life cycle of a store may normally outspan the one of one or multiple
  76. * graphs with the same back end, hence it is managed independently.
  77. *
  78. * @param[in] store_type Type of store backing the graph. One of the values of
  79. * #LSUP_StoreType.
  80. *
  81. * @param[in] store_id Identifier for the store. This may be
  82. * interpreted differently by each store implementation. For the MDB store,
  83. * this is the file path where the store is located. It is ignored by volatile
  84. * stores (with LSUP_STORE_PERM feature flag set to false). If a store
  85. * does not exist for the given identifier, a new one is initialized. If this
  86. * parameter is NULL, the default store for the selected type is used.
  87. *
  88. * @param[in] size Initial size of the store. Only used for optimization
  89. * purposes. It may be ignored by some implementations and it is always safe
  90. * to set to 0.
  91. *
  92. * @param clear Flag that is passed to the `init_fn` function of the store
  93. * interface, if present, to clear an existing store with the same ID.
  94. *
  95. * @return Store handle. It must be freed with #LSUP_store_free().
  96. */
  97. LSUP_Store *
  98. LSUP_store_new (
  99. const LSUP_StoreType store_type, const char *store_id, size_t size,
  100. bool clear);
  101. /** @brief Free a store created with #LSUP_store_new().
  102. *
  103. * @param[in] store Store handle.
  104. */
  105. void
  106. LSUP_store_free (LSUP_Store *store);
  107. /** @brief Begin a transaction.
  108. *
  109. * If the store supports it, begin a transaction. Only one transaction may be
  110. * opened at a time.
  111. *
  112. * The transaction must be either committed with #LSUP_store_commit() or
  113. * rolled back with #LSUP_store_abort().
  114. *
  115. * @param[in] store Store handle.
  116. *
  117. * @param[in] flags Unused for now, use 0. TODO
  118. *
  119. * @param[out] txn Address to be populated with the new transaction handle.
  120. *
  121. * @return LSUP_OK on success; LSUP_VALUE_ERR if the store does not
  122. * support transactions; LSUP_TXN_ERR if the store has already an uncommitted
  123. * transaction; <0 on other errors.
  124. */
  125. LSUP_rc
  126. LSUP_store_begin (LSUP_Store *store, int flags, void **txn);
  127. /** @brief Commit a transaction.
  128. *
  129. * If the store supports it, commit an open transaction. In case of
  130. * error, the transaction is left open and it is advisable to roll it back with
  131. * #LSUP_store_abort().
  132. *
  133. * @param[in] store Store handle.
  134. *
  135. * @param[in] txn transaction handle to commit.
  136. *
  137. * @return LSUP_OK if the transaction was committed successfully; LSUP_NOACTION
  138. * if NULL was passed; LSUP_TXN_ERR on error.
  139. */
  140. LSUP_rc
  141. LSUP_store_commit (LSUP_Store *store, void *txn);
  142. /** @brief Abort (roll back) a transaction.
  143. *
  144. * If the store supports it, abort an open transaction and abandon all changes.
  145. *
  146. * @param[in] store Store handle.
  147. *
  148. * @param[in] txn transaction handle to abort.
  149. */
  150. void
  151. LSUP_store_abort (LSUP_Store *store, void *txn);
  152. /// @} END store
  153. #endif /* LSUP_STORE_H */