store.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "lsup/store.h"
  2. #define ENTRY(a, b) case LSUP_STORE_##a: return &b;
  3. const LSUP_StoreInt *
  4. LSUP_store_int (LSUP_StoreType type) {
  5. switch (type) {
  6. BACKEND_TBL
  7. default: return NULL;
  8. }
  9. }
  10. #undef ENTRY
  11. const char *
  12. LSUP_store_type_label (LSUP_StoreType type)
  13. {
  14. #define ENTRY(a, b) case LSUP_STORE_##a: return "STORE_" #a;
  15. switch (type) {
  16. BACKEND_TBL
  17. default: return "";
  18. }
  19. #undef ENTRY
  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 = LSUP_store_int (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. if (!UNLIKELY (!store)) return;
  48. store->sif->free_fn (store->data);
  49. if (store->id) free (store->id);
  50. free (store);
  51. }
  52. LSUP_rc
  53. LSUP_store_begin (LSUP_Store *store, int flags, void **txn) {
  54. if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
  55. return store->sif->txn_begin_fn (store->data, flags, txn);
  56. }
  57. LSUP_rc
  58. LSUP_store_commit (LSUP_Store *store, void *txn)
  59. {
  60. if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
  61. return store->sif->txn_commit_fn (txn);
  62. }
  63. void
  64. LSUP_store_abort (LSUP_Store *store, void *txn)
  65. { store->sif->txn_abort_fn (txn); }