Minor cleanup.
[bluesky.git] / bluesky / store.c
index 9e0fca1..b60bd05 100644 (file)
 #include <glib.h>
 #include <string.h>
 
-#include "bluesky.h"
+#include "bluesky-private.h"
 
 /* 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,70 +54,115 @@ void bluesky_store_free(BlueSkyStore *store)
     g_free(store);
 }
 
-BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
+BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
 {
-    return store->impl->get(store->handle, key);
-}
+    BlueSkyStoreAsync *async;
 
-void bluesky_store_put(BlueSkyStore *store,
-                       const gchar *key, BlueSkyRCStr *val)
-{
-    store->impl->put(store->handle, key, val);
+    async = g_new(BlueSkyStoreAsync, 1);
+    async->store = store;
+    async->lock = g_mutex_new();
+    async->completion_cond = g_cond_new();
+    async->refcount = 1;
+    async->status = ASYNC_NEW;
+    async->op = STORE_OP_NONE;
+    async->key = NULL;
+    async->data = NULL;
+    async->result = -1;
+    async->store_private = NULL;
+
+    return async;
 }
 
-/* Create and return a new reference-counted string.  The reference count is
- * initially one.  The newly-returned string takes ownership of the memory
- * pointed at by data, and will call g_free on it when the reference count
- * drops to zero. */
-BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len)
+void bluesky_store_async_ref(BlueSkyStoreAsync *async)
 {
-    BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
-    string->data = data;
-    string->len = len;
-    g_atomic_int_set(&string->refcount, 1);
-    return string;
+    if (async == NULL)
+        return;
+
+    g_atomic_int_inc(&async->refcount);
 }
 
-void bluesky_string_ref(BlueSkyRCStr *string)
+void bluesky_store_async_unref(BlueSkyStoreAsync *async)
 {
-    if (string == NULL)
+    if (async == NULL)
         return;
 
-    g_atomic_int_inc(&string->refcount);
+    if (g_atomic_int_dec_and_test(&async->refcount)) {
+        async->store->impl->cleanup(async->store->handle, async);
+        g_mutex_free(async->lock);
+        g_cond_free(async->completion_cond);
+        g_free(async->key);
+        bluesky_string_unref(async->data);
+        g_free(async);
+        g_print("Freed async\n");
+    }
 }
 
-void bluesky_string_unref(BlueSkyRCStr *string)
+/* Block until the given operation has completed. */
+void bluesky_store_async_wait(BlueSkyStoreAsync *async)
 {
-    if (string == NULL)
+    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;
+    }
 
-    if (g_atomic_int_dec_and_test(&string->refcount)) {
-        g_free(string->data);
-        g_free(string);
+    while (async->status != ASYNC_COMPLETE) {
+        g_cond_wait(async->completion_cond, async->lock);
     }
+
+    g_mutex_unlock(async->lock);
 }
 
-/* Duplicate and return a new reference-counted string, containing a copy of
- * the original data, with a reference count of 1.  As an optimization, if the
- * passed-in string already has a reference count of 1, the original is
- * returned.   Can be used to make a mutable copy of a shared string.  For this
- * to truly be safe, it is probably needed that there be some type of lock
- * protecting access to the string. */
-BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
+/* 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)
 {
-    if (string == NULL)
-        return NULL;
+    async->status = ASYNC_COMPLETE;
+    g_cond_broadcast(async->completion_cond);
+}
 
-    if (g_atomic_int_dec_and_test(&string->refcount)) {
-        /* There are no other shared copies, so return this one. */
-        g_atomic_int_inc(&string->refcount);
-        return string;
-    } else {
-        return bluesky_string_new(g_memdup(string->data, string->len),
-                                  string->len);
-    }
+void bluesky_store_async_submit(BlueSkyStoreAsync *async)
+{
+    BlueSkyStore *store = async->store;
+
+    store->impl->submit(store->handle, async);
+}
+
+/* Convenience wrappers that perform a single operation synchronously. */
+BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
+{
+    BlueSkyStoreAsync *async = bluesky_store_async_new(store);
+    async->op = STORE_OP_GET;
+    async->key = g_strdup(key);
+    bluesky_store_async_submit(async);
+
+    bluesky_store_async_wait(async);
+
+    BlueSkyRCStr *data = async->data;
+    bluesky_string_ref(data);
+    bluesky_store_async_unref(async);
+    return data;
+}
+
+void bluesky_store_put(BlueSkyStore *store,
+                       const gchar *key, BlueSkyRCStr *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;
+    bluesky_store_async_submit(async);
+
+    bluesky_store_async_wait(async);
+    bluesky_store_async_unref(async);
 }
 
+#if 0
 /* Simple in-memory data store for test purposes. */
 typedef struct {
     GMutex *lock;
@@ -198,10 +244,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");
 }