X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=bluesky%2Fstore.c;h=13a080ffbec151c5956ed4310af059c644e2d1f4;hb=c513d64c6a1f7c2ff2bad97db69e2f40ef642167;hp=52cc6b356d5116f5d3d881b07ff36ddab5979c5d;hpb=b7e08dcf6552eb8977ccef56f00e775da8133cf8;p=bluesky.git diff --git a/bluesky/store.c b/bluesky/store.c index 52cc6b3..13a080f 100644 --- a/bluesky/store.c +++ b/bluesky/store.c @@ -14,7 +14,8 @@ /* Interaction with cloud storage. We expose very simple GET/PUT style * interface, which different backends can implement. Available backends - * (will) include Amazon S3 and a simple local store for testing purposes. */ + * (will) include Amazon S3 and a simple local store for testing purposes. + * Operations may be performed asynchronously. */ struct _BlueSkyStore { const BlueSkyStoreImplementation *impl; @@ -53,17 +54,79 @@ void bluesky_store_free(BlueSkyStore *store) g_free(store); } +BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store) +{ + BlueSkyStoreAsync *async; + + async = g_new(BlueSkyStoreAsync, 1); + async->store = store; + async->lock = g_mutex_new(); + async->completion_cond = g_cond_new(); + async->status = ASYNC_NEW; + async->op = STORE_OP_NONE; + async->key = NULL; + async->data = NULL; + async->result = -1; + async->store_private = NULL; + + return async; +} + +/* Block until the given operation has completed. */ +void bluesky_store_async_wait(BlueSkyStoreAsync *async) +{ + g_return_if_fail(async != NULL); + g_mutex_lock(async->lock); + + if (async->status == ASYNC_NEW) { + g_error("bluesky_store_async_wait on a new async object!\n"); + g_mutex_unlock(async->lock); + return; + } + + while (async->status != ASYNC_COMPLETE) { + g_cond_wait(async->completion_cond, async->lock); + } +} + +/* Mark an asynchronous operation as complete. This should only be called by + * the store implementations. The lock must be held when calling this + * function. */ +void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async) +{ + async->status = ASYNC_COMPLETE; + g_cond_broadcast(async->completion_cond); +} + +/* Convenience wrappers that perform a single operation synchronously. */ BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key) { - return store->impl->get(store->handle, key); + BlueSkyStoreAsync *async = bluesky_store_async_new(store); + async->op = STORE_OP_GET; + async->key = g_strdup(key); + store->impl->submit(store->handle, async); + + bluesky_store_async_wait(async); + + BlueSkyRCStr *data = async->data; + bluesky_string_ref(data); + return data; } void bluesky_store_put(BlueSkyStore *store, const gchar *key, BlueSkyRCStr *val) { - store->impl->put(store->handle, key, val); + BlueSkyStoreAsync *async = bluesky_store_async_new(store); + async->op = STORE_OP_PUT; + async->key = g_strdup(key); + bluesky_string_ref(val); + async->data = val; + store->impl->submit(store->handle, async); + + bluesky_store_async_wait(async); } +#if 0 /* Simple in-memory data store for test purposes. */ typedef struct { GMutex *lock; @@ -145,10 +208,11 @@ static BlueSkyStoreImplementation filestore_impl = { .get = filestore_get, .put = filestore_put, }; +#endif void bluesky_store_init() { store_implementations = g_hash_table_new(g_str_hash, g_str_equal); - bluesky_store_register(&memstore_impl, "mem"); - bluesky_store_register(&filestore_impl, "file"); + //bluesky_store_register(&memstore_impl, "mem"); + //bluesky_store_register(&filestore_impl, "file"); }