Hook John's kvstore up to the BlueSky implementation.
[bluesky.git] / bluesky / store-kv.cc
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 /* Interface to John McCullough's simple key/value store. */
10
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <glib.h>
14 #include <string.h>
15
16 #include "bluesky-private.h"
17 #include "kvservice.h"
18 #include "kvclient.h"
19
20 using namespace boost;
21 using namespace kvstore;
22 using namespace std;
23
24 static gpointer kvstore_new()
25 {
26     KeyValueClient *client = new KeyValueClient("127.0.0.1", "9090");
27     return client;
28 }
29
30 static void kvstore_destroy(gpointer store)
31 {
32     KeyValueClient *client = (KeyValueClient *)store;
33     delete client;
34 }
35
36 static void kvstore_submit(gpointer store, BlueSkyStoreAsync *async)
37 {
38     KeyValueClient *client = (KeyValueClient *)store;
39
40     g_return_if_fail(async->status == ASYNC_NEW);
41     g_return_if_fail(async->op != STORE_OP_NONE);
42
43     switch (async->op) {
44     case STORE_OP_GET:
45     {
46         string value;
47         if (client->Get(async->key, &value)) {
48             async->data = bluesky_string_new(g_memdup(value.c_str(),
49                                                     value.length()),
50                                             value.length());
51             async->result = 0;
52         }
53         break;
54     }
55
56     case STORE_OP_PUT:
57     {
58         string value(async->data->data, async->data->len);
59         client->Put(async->key, value);
60         break;
61     }
62
63     default:
64         g_warning("Uknown operation type for MemStore: %d\n", async->op);
65         return;
66     }
67
68     bluesky_store_async_mark_complete(async);
69 }
70
71 static void kvstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
72 {
73     KeyValueClient *client = (KeyValueClient *)store;
74 }
75
76 static BlueSkyStoreImplementation store_impl = {
77     kvstore_new,
78     kvstore_destroy,
79     kvstore_submit,
80     kvstore_cleanup,
81 };
82
83 extern "C" void bluesky_store_init_kv(void)
84 {
85     bluesky_store_register(&store_impl, "kv");
86 }