#include "lsup/store.h" #define ENTRY(a, b) case LSUP_STORE_##a: return &b; const LSUP_StoreInt * LSUP_store_int (LSUP_StoreType type) { switch (type) { BACKEND_TBL default: return NULL; } } #undef ENTRY const char * LSUP_store_type_label (LSUP_StoreType type) { #define ENTRY(a, b) case LSUP_STORE_##a: return "STORE_" #a; switch (type) { BACKEND_TBL default: return ""; } #undef ENTRY } 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 = LSUP_store_int (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) { if (!UNLIKELY (!store)) return; store->sif->free_fn (store->data); if (store->id) free (store->id); 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) { store->sif->txn_abort_fn (txn); }