store.h 4.6 KB

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