#include "store.h" #define ENTRY(a, b) case LSUP_STORE_##a: return &b; static inline const LSUP_StoreInt * select_interface (LSUP_StoreType be) { switch (be) { BACKEND_TBL default: return NULL; } } #undef ENTRY const LSUP_StoreInt * LSUP_store_int (LSUP_StoreType type) { switch (type) { #define ENTRY(a, b) \ case LSUP_STORE_##a: return &b; BACKEND_TBL #undef ENTRY default: return NULL; } } LSUP_Store * LSUP_store_new ( const LSUP_StoreType store_type, const char *store_id, size_t size) { if (UNLIKELY (!LSUP_IS_INIT)) { log_error ( "Environment is not initialized. Did you call LSUP_init()?"); return NULL; } const LSUP_StoreInt *sif = select_interface (store_type); if (UNLIKELY (!sif)) { log_error ("Not a valid store type: %d", store_type); return NULL; } LSUP_Store *store; MALLOC_GUARD (store, NULL); store->type = store_type; store->sif = sif; store->id = store_id ? strdup (store_id) : NULL; // TODO implement custom default context. store->data = store->sif->new_fn (store_id, size); return store; } void LSUP_store_free (LSUP_Store *store) { store->sif->free_fn (store->data); free (store); } LSUP_rc LSUP_store_begin (LSUP_Store *store, int flags, void **txn) { if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR; return store->sif->txn_begin_fn (store->data, flags, txn); } LSUP_rc LSUP_store_commit (LSUP_Store *store, void *txn) { if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR; return store->sif->txn_commit_fn (txn); } void LSUP_store_abort (LSUP_Store *store, void *txn) { return store->sif->txn_abort_fn (txn); }