store.h 4.6 KB

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