Rework the storage interface so that operations are asynchronous.
[bluesky.git] / bluesky / store.c
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 #include <stdint.h>
10 #include <glib.h>
11 #include <string.h>
12
13 #include "bluesky-private.h"
14
15 /* Interaction with cloud storage.  We expose very simple GET/PUT style
16  * interface, which different backends can implement.  Available backends
17  * (will) include Amazon S3 and a simple local store for testing purposes.
18  * Operations may be performed asynchronously. */
19
20 struct _BlueSkyStore {
21     const BlueSkyStoreImplementation *impl;
22     gpointer handle;
23 };
24
25 GHashTable *store_implementations;
26
27 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
28                             const gchar *name)
29 {
30     g_hash_table_insert(store_implementations, g_strdup(name), (gpointer)impl);
31 }
32
33 BlueSkyStore *bluesky_store_new(const gchar *type)
34 {
35     const BlueSkyStoreImplementation *impl;
36
37     impl = g_hash_table_lookup(store_implementations, type);
38     if (impl == NULL)
39         return NULL;
40
41     gpointer handle = impl->create();
42     if (handle == NULL)
43         return NULL;
44
45     BlueSkyStore *store = g_new(BlueSkyStore, 1);
46     store->impl = impl;
47     store->handle = handle;
48     return store;
49 }
50
51 void bluesky_store_free(BlueSkyStore *store)
52 {
53     store->impl->destroy(store->handle);
54     g_free(store);
55 }
56
57 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
58 {
59     BlueSkyStoreAsync *async;
60
61     async = g_new(BlueSkyStoreAsync, 1);
62     async->store = store;
63     async->lock = g_mutex_new();
64     async->completion_cond = g_cond_new();
65     async->status = ASYNC_NEW;
66     async->op = STORE_OP_NONE;
67     async->key = NULL;
68     async->data = NULL;
69     async->result = -1;
70     async->store_private = NULL;
71
72     return async;
73 }
74
75 /* Block until the given operation has completed. */
76 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
77 {
78     g_return_if_fail(async != NULL);
79     g_mutex_lock(async->lock);
80
81     if (async->status == ASYNC_NEW) {
82         g_error("bluesky_store_async_wait on a new async object!\n");
83         g_mutex_unlock(async->lock);
84         return;
85     }
86
87     while (async->status != ASYNC_COMPLETE) {
88         g_cond_wait(async->completion_cond, async->lock);
89     }
90 }
91
92 /* Mark an asynchronous operation as complete.  This should only be called by
93  * the store implementations.  The lock must be held when calling this
94  * function. */
95 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
96 {
97     async->status = ASYNC_COMPLETE;
98     g_cond_broadcast(async->completion_cond);
99 }
100
101 /* Convenience wrappers that perform a single operation synchronously. */
102 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
103 {
104     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
105     async->op = STORE_OP_GET;
106     async->key = g_strdup(key);
107     store->impl->submit(store->handle, async);
108
109     bluesky_store_async_wait(async);
110
111     BlueSkyRCStr *data = async->data;
112     bluesky_string_ref(data);
113     return data;
114 }
115
116 void bluesky_store_put(BlueSkyStore *store,
117                        const gchar *key, BlueSkyRCStr *val)
118 {
119     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
120     async->op = STORE_OP_PUT;
121     async->key = g_strdup(key);
122     bluesky_string_ref(val);
123     async->data = val;
124     store->impl->submit(store->handle, async);
125
126     bluesky_store_async_wait(async);
127 }
128
129 #if 0
130 /* Simple in-memory data store for test purposes. */
131 typedef struct {
132     GMutex *lock;
133
134     /* TODO: A hashtable isn't optimal for list queries... */
135     GHashTable *store;
136 } MemStore;
137
138 static gpointer memstore_create()
139 {
140     MemStore *store = g_new(MemStore, 1);
141     store->lock = g_mutex_new();
142     store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
143                                          g_free,
144                                          (GDestroyNotify)bluesky_string_unref);
145
146     return (gpointer)store;
147 }
148
149 static void memstore_destroy(gpointer store)
150 {
151     /* TODO */
152 }
153
154 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
155 {
156     MemStore *store = (MemStore *)st;
157     BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
158     if (s != NULL)
159         bluesky_string_ref(s);
160     return s;
161 }
162
163 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
164 {
165     MemStore *store = (MemStore *)s;
166     bluesky_string_ref(val);
167     g_hash_table_insert(store->store, g_strdup(key), val);
168 }
169
170 static BlueSkyStoreImplementation memstore_impl = {
171     .create = memstore_create,
172     .destroy = memstore_destroy,
173     .get = memstore_get,
174     .put = memstore_put,
175 };
176
177 /* Store implementation which writes data as files to disk. */
178 static gpointer filestore_create()
179 {
180     return GINT_TO_POINTER(1);
181 }
182
183 static void filestore_destroy()
184 {
185 }
186
187 static BlueSkyRCStr *filestore_get(gpointer s, const gchar *key)
188 {
189     gchar *contents = NULL;
190     gsize length;
191     GError *error = NULL;
192
193     g_file_get_contents(key, &contents, &length, &error);
194     if (contents == NULL)
195         return NULL;
196
197     return bluesky_string_new(contents, length);
198 }
199
200 static void filestore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
201 {
202     g_file_set_contents(key, val->data, val->len, NULL);
203 }
204
205 static BlueSkyStoreImplementation filestore_impl = {
206     .create = filestore_create,
207     .destroy = filestore_destroy,
208     .get = filestore_get,
209     .put = filestore_put,
210 };
211 #endif
212
213 void bluesky_store_init()
214 {
215     store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
216     //bluesky_store_register(&memstore_impl, "mem");
217     //bluesky_store_register(&filestore_impl, "file");
218 }