Hook John's kvstore up to the BlueSky implementation.
[bluesky.git] / bluesky / store-kv.cc
diff --git a/bluesky/store-kv.cc b/bluesky/store-kv.cc
new file mode 100644 (file)
index 0000000..7a6ac71
--- /dev/null
@@ -0,0 +1,86 @@
+/* Blue Sky: File Systems in the Cloud
+ *
+ * Copyright (C) 2009  The Regents of the University of California
+ * Written by Michael Vrable <mvrable@cs.ucsd.edu>
+ *
+ * TODO: Licensing
+ */
+
+/* Interface to John McCullough's simple key/value store. */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <glib.h>
+#include <string.h>
+
+#include "bluesky-private.h"
+#include "kvservice.h"
+#include "kvclient.h"
+
+using namespace boost;
+using namespace kvstore;
+using namespace std;
+
+static gpointer kvstore_new()
+{
+    KeyValueClient *client = new KeyValueClient("127.0.0.1", "9090");
+    return client;
+}
+
+static void kvstore_destroy(gpointer store)
+{
+    KeyValueClient *client = (KeyValueClient *)store;
+    delete client;
+}
+
+static void kvstore_submit(gpointer store, BlueSkyStoreAsync *async)
+{
+    KeyValueClient *client = (KeyValueClient *)store;
+
+    g_return_if_fail(async->status == ASYNC_NEW);
+    g_return_if_fail(async->op != STORE_OP_NONE);
+
+    switch (async->op) {
+    case STORE_OP_GET:
+    {
+        string value;
+        if (client->Get(async->key, &value)) {
+            async->data = bluesky_string_new(g_memdup(value.c_str(),
+                                                    value.length()),
+                                            value.length());
+            async->result = 0;
+        }
+        break;
+    }
+
+    case STORE_OP_PUT:
+    {
+        string value(async->data->data, async->data->len);
+        client->Put(async->key, value);
+        break;
+    }
+
+    default:
+        g_warning("Uknown operation type for MemStore: %d\n", async->op);
+        return;
+    }
+
+    bluesky_store_async_mark_complete(async);
+}
+
+static void kvstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
+{
+    KeyValueClient *client = (KeyValueClient *)store;
+}
+
+static BlueSkyStoreImplementation store_impl = {
+    kvstore_new,
+    kvstore_destroy,
+    kvstore_submit,
+    kvstore_cleanup,
+};
+
+extern "C" void bluesky_store_init_kv(void)
+{
+    bluesky_store_register(&store_impl, "kv");
+}