Add reference counting to asynchronous storage operations.
[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->refcount = 1;
66     async->status = ASYNC_NEW;
67     async->op = STORE_OP_NONE;
68     async->key = NULL;
69     async->data = NULL;
70     async->result = -1;
71     async->store_private = NULL;
72
73     return async;
74 }
75
76 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
77 {
78     if (async == NULL)
79         return;
80
81     g_atomic_int_inc(&async->refcount);
82 }
83
84 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
85 {
86     if (async == NULL)
87         return;
88
89     if (g_atomic_int_dec_and_test(&async->refcount)) {
90         async->store->impl->cleanup(async->store->handle, async);
91         g_mutex_free(async->lock);
92         g_cond_free(async->completion_cond);
93         g_free(async->key);
94         bluesky_string_unref(async->data);
95         g_free(async);
96         g_print("Freed async\n");
97     }
98 }
99
100 /* Block until the given operation has completed. */
101 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
102 {
103     g_return_if_fail(async != NULL);
104     g_mutex_lock(async->lock);
105
106     if (async->status == ASYNC_NEW) {
107         g_error("bluesky_store_async_wait on a new async object!\n");
108         g_mutex_unlock(async->lock);
109         return;
110     }
111
112     while (async->status != ASYNC_COMPLETE) {
113         g_cond_wait(async->completion_cond, async->lock);
114     }
115
116     g_mutex_unlock(async->lock);
117 }
118
119 /* Mark an asynchronous operation as complete.  This should only be called by
120  * the store implementations.  The lock must be held when calling this
121  * function. */
122 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
123 {
124     async->status = ASYNC_COMPLETE;
125     g_cond_broadcast(async->completion_cond);
126 }
127
128 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
129 {
130     BlueSkyStore *store = async->store;
131
132     store->impl->submit(store->handle, async);
133 }
134
135 /* Convenience wrappers that perform a single operation synchronously. */
136 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
137 {
138     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
139     async->op = STORE_OP_GET;
140     async->key = g_strdup(key);
141     store->impl->submit(store->handle, async);
142
143     bluesky_store_async_wait(async);
144
145     BlueSkyRCStr *data = async->data;
146     bluesky_string_ref(data);
147     bluesky_store_async_unref(async);
148     return data;
149 }
150
151 void bluesky_store_put(BlueSkyStore *store,
152                        const gchar *key, BlueSkyRCStr *val)
153 {
154     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
155     async->op = STORE_OP_PUT;
156     async->key = g_strdup(key);
157     bluesky_string_ref(val);
158     async->data = val;
159     store->impl->submit(store->handle, async);
160
161     bluesky_store_async_wait(async);
162     bluesky_store_async_unref(async);
163 }
164
165 #if 0
166 /* Simple in-memory data store for test purposes. */
167 typedef struct {
168     GMutex *lock;
169
170     /* TODO: A hashtable isn't optimal for list queries... */
171     GHashTable *store;
172 } MemStore;
173
174 static gpointer memstore_create()
175 {
176     MemStore *store = g_new(MemStore, 1);
177     store->lock = g_mutex_new();
178     store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
179                                          g_free,
180                                          (GDestroyNotify)bluesky_string_unref);
181
182     return (gpointer)store;
183 }
184
185 static void memstore_destroy(gpointer store)
186 {
187     /* TODO */
188 }
189
190 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
191 {
192     MemStore *store = (MemStore *)st;
193     BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
194     if (s != NULL)
195         bluesky_string_ref(s);
196     return s;
197 }
198
199 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
200 {
201     MemStore *store = (MemStore *)s;
202     bluesky_string_ref(val);
203     g_hash_table_insert(store->store, g_strdup(key), val);
204 }
205
206 static BlueSkyStoreImplementation memstore_impl = {
207     .create = memstore_create,
208     .destroy = memstore_destroy,
209     .get = memstore_get,
210     .put = memstore_put,
211 };
212
213 /* Store implementation which writes data as files to disk. */
214 static gpointer filestore_create()
215 {
216     return GINT_TO_POINTER(1);
217 }
218
219 static void filestore_destroy()
220 {
221 }
222
223 static BlueSkyRCStr *filestore_get(gpointer s, const gchar *key)
224 {
225     gchar *contents = NULL;
226     gsize length;
227     GError *error = NULL;
228
229     g_file_get_contents(key, &contents, &length, &error);
230     if (contents == NULL)
231         return NULL;
232
233     return bluesky_string_new(contents, length);
234 }
235
236 static void filestore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
237 {
238     g_file_set_contents(key, val->data, val->len, NULL);
239 }
240
241 static BlueSkyStoreImplementation filestore_impl = {
242     .create = filestore_create,
243     .destroy = filestore_destroy,
244     .get = filestore_get,
245     .put = filestore_put,
246 };
247 #endif
248
249 void bluesky_store_init()
250 {
251     store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
252     //bluesky_store_register(&memstore_impl, "mem");
253     //bluesky_store_register(&filestore_impl, "file");
254 }