store.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "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. LSUP_Store *
  12. LSUP_store_new (
  13. const LSUP_StoreType store_type, const char *store_id, size_t size)
  14. {
  15. if (UNLIKELY (!LSUP_IS_INIT)) {
  16. log_error (
  17. "Environment is not initialized. Did you call LSUP_init()?");
  18. return NULL;
  19. }
  20. const LSUP_StoreInt *sif = LSUP_store_int (store_type);
  21. if (UNLIKELY (!sif)) {
  22. log_error ("Not a valid store type: %d", store_type);
  23. return NULL;
  24. }
  25. LSUP_Store *store;
  26. MALLOC_GUARD (store, NULL);
  27. store->type = store_type;
  28. store->sif = sif;
  29. store->id = store_id ? strdup (store_id) : NULL;
  30. // TODO implement custom default context.
  31. store->data = store->sif->new_fn (store_id, size);
  32. return store;
  33. }
  34. void
  35. LSUP_store_free (LSUP_Store *store)
  36. {
  37. store->sif->free_fn (store->data);
  38. if (store->id) free (store->id);
  39. free (store);
  40. }
  41. LSUP_rc
  42. LSUP_store_begin (LSUP_Store *store, int flags, void **txn) {
  43. if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
  44. return store->sif->txn_begin_fn (store->data, flags, txn);
  45. }
  46. LSUP_rc
  47. LSUP_store_commit (LSUP_Store *store, void *txn)
  48. {
  49. if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
  50. return store->sif->txn_commit_fn (txn);
  51. }
  52. void
  53. LSUP_store_abort (LSUP_Store *store, void *txn)
  54. { return store->sif->txn_abort_fn (txn); }