store.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "store.h"
  2. #define ENTRY(a, b) case LSUP_STORE_##a: return &b;
  3. static inline const LSUP_StoreInt *
  4. select_interface (LSUP_StoreType be) {
  5. switch (be) {
  6. BACKEND_TBL
  7. default: return NULL;
  8. }
  9. }
  10. #undef ENTRY
  11. const LSUP_StoreInt *
  12. LSUP_store_int (LSUP_StoreType type) {
  13. switch (type) {
  14. #define ENTRY(a, b) \
  15. case LSUP_STORE_##a: return &b;
  16. BACKEND_TBL
  17. #undef ENTRY
  18. default: return NULL;
  19. }
  20. }
  21. LSUP_Store *
  22. LSUP_store_new (
  23. const LSUP_StoreType store_type, const char *store_id, size_t size)
  24. {
  25. if (UNLIKELY (!LSUP_IS_INIT)) {
  26. log_error (
  27. "Environment is not initialized. Did you call LSUP_init()?");
  28. return NULL;
  29. }
  30. const LSUP_StoreInt *sif = select_interface (store_type);
  31. if (UNLIKELY (!sif)) {
  32. log_error ("Not a valid store type: %d", store_type);
  33. return NULL;
  34. }
  35. LSUP_Store *store;
  36. MALLOC_GUARD (store, NULL);
  37. store->type = store_type;
  38. store->sif = sif;
  39. store->id = store_id ? strdup (store_id) : NULL;
  40. // TODO implement custom default context.
  41. store->data = store->sif->new_fn (store_id, size);
  42. return store;
  43. }
  44. void
  45. LSUP_store_free (LSUP_Store *store)
  46. {
  47. store->sif->free_fn (store->data);
  48. free (store);
  49. }
  50. LSUP_rc
  51. LSUP_store_begin (LSUP_Store *store, int flags, void **txn) {
  52. if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
  53. return store->sif->txn_begin_fn (store->data, flags, txn);
  54. }
  55. LSUP_rc
  56. LSUP_store_commit (LSUP_Store *store, void *txn)
  57. {
  58. if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
  59. return store->sif->txn_commit_fn (txn);
  60. }
  61. void
  62. LSUP_store_abort (LSUP_Store *store, void *txn)
  63. { return store->sif->txn_abort_fn (txn); }