Fix for journal committing.
[bluesky.git] / bluesky / store.c
index 2043806..af25ca3 100644 (file)
@@ -87,6 +87,11 @@ void bluesky_store_free(BlueSkyStore *store)
     g_free(store);
 }
 
+char *bluesky_store_lookup_last(BlueSkyStore *store, const char *prefix)
+{
+    return store->impl->lookup_last(store->handle, prefix);
+}
+
 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
 {
     BlueSkyStoreAsync *async;
@@ -231,10 +236,12 @@ void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
               async, elapsed, latency);
     }
 
-    if (async->op == STORE_OP_GET) {
-        bluesky_stats_add(async->store->stats_get, 1);
-    } else if (async->op == STORE_OP_PUT) {
-        bluesky_stats_add(async->store->stats_put, 1);
+    if (async->data) {
+        if (async->op == STORE_OP_GET) {
+            bluesky_stats_add(async->store->stats_get, async->data->len);
+        } else if (async->op == STORE_OP_PUT) {
+            bluesky_stats_add(async->store->stats_put, async->data->len);
+        }
     }
 }
 
@@ -290,13 +297,15 @@ void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
     g_mutex_unlock(barrier->lock);
 
     g_mutex_lock(async->lock);
-    if (async->barrier == NULL) {
+    if (async->barrier == NULL && async->status != ASYNC_COMPLETE) {
         async->barrier = barrier;
+        g_mutex_unlock(async->lock);
     } else {
-        g_warning("Adding async to more than one barrier!\n");
+        if (async->barrier != NULL)
+            g_warning("Adding async to more than one barrier!\n");
+        g_mutex_unlock(async->lock);
         bluesky_store_async_add_notifier(async, op_complete, barrier);
     }
-    g_mutex_unlock(async->lock);
 }
 
 static void notifier_task(gpointer n, gpointer s)
@@ -490,11 +499,61 @@ static void filestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
 {
 }
 
+static char *filestore_lookup_last(gpointer store, const char *prefix)
+{
+    char *last = NULL;
+    GDir *dir = g_dir_open(".", 0, NULL);
+    if (dir == NULL) {
+        g_warning("Unable to open directory for listing");
+        return NULL;
+    }
+
+    const gchar *file;
+    while ((file = g_dir_read_name(dir)) != NULL) {
+        if (strncmp(file, prefix, strlen(prefix)) == 0) {
+            if (last == NULL || strcmp(file, last) > 0) {
+                g_free(last);
+                last = g_strdup(file);
+            }
+        }
+    }
+    g_dir_close(dir);
+
+    return last;
+}
+
 static BlueSkyStoreImplementation filestore_impl = {
     .create = filestore_create,
     .destroy = filestore_destroy,
     .submit = filestore_submit,
     .cleanup = filestore_cleanup,
+    .lookup_last = filestore_lookup_last,
+};
+
+/* A store implementation which simply discards all data, for testing. */
+static gpointer nullstore_create(const gchar *path)
+{
+    return (gpointer)nullstore_create;
+}
+
+static void nullstore_destroy(gpointer store)
+{
+}
+
+static void nullstore_submit(gpointer s, BlueSkyStoreAsync *async)
+{
+    bluesky_store_async_mark_complete(async);
+}
+
+static void nullstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
+{
+}
+
+static BlueSkyStoreImplementation nullstore_impl = {
+    .create = nullstore_create,
+    .destroy = nullstore_destroy,
+    .submit = nullstore_submit,
+    .cleanup = nullstore_cleanup,
 };
 
 void bluesky_store_init()
@@ -504,4 +563,5 @@ void bluesky_store_init()
                                              bluesky_max_threads, FALSE, NULL);
     bluesky_store_register(&memstore_impl, "mem");
     bluesky_store_register(&filestore_impl, "file");
+    bluesky_store_register(&nullstore_impl, "null");
 }