1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
13 #include "bluesky-private.h"
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. */
20 struct _BlueSkyStore {
21 const BlueSkyStoreImplementation *impl;
25 GHashTable *store_implementations;
27 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
30 g_hash_table_insert(store_implementations, g_strdup(name), (gpointer)impl);
33 BlueSkyStore *bluesky_store_new(const gchar *type)
35 const BlueSkyStoreImplementation *impl;
37 impl = g_hash_table_lookup(store_implementations, type);
41 gpointer handle = impl->create();
45 BlueSkyStore *store = g_new(BlueSkyStore, 1);
47 store->handle = handle;
51 void bluesky_store_free(BlueSkyStore *store)
53 store->impl->destroy(store->handle);
57 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
59 BlueSkyStoreAsync *async;
61 async = g_new(BlueSkyStoreAsync, 1);
63 async->lock = g_mutex_new();
64 async->completion_cond = g_cond_new();
66 async->status = ASYNC_NEW;
67 async->op = STORE_OP_NONE;
71 async->store_private = NULL;
76 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
81 g_atomic_int_inc(&async->refcount);
84 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
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);
94 bluesky_string_unref(async->data);
96 g_print("Freed async\n");
100 /* Block until the given operation has completed. */
101 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
103 g_return_if_fail(async != NULL);
104 g_mutex_lock(async->lock);
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);
112 while (async->status != ASYNC_COMPLETE) {
113 g_cond_wait(async->completion_cond, async->lock);
116 g_mutex_unlock(async->lock);
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
122 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
124 async->status = ASYNC_COMPLETE;
125 g_cond_broadcast(async->completion_cond);
128 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
130 BlueSkyStore *store = async->store;
132 store->impl->submit(store->handle, async);
135 /* Convenience wrappers that perform a single operation synchronously. */
136 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
138 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
139 async->op = STORE_OP_GET;
140 async->key = g_strdup(key);
141 bluesky_store_async_submit(async);
143 bluesky_store_async_wait(async);
145 BlueSkyRCStr *data = async->data;
146 bluesky_string_ref(data);
147 bluesky_store_async_unref(async);
151 void bluesky_store_put(BlueSkyStore *store,
152 const gchar *key, BlueSkyRCStr *val)
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);
159 bluesky_store_async_submit(async);
161 bluesky_store_async_wait(async);
162 bluesky_store_async_unref(async);
166 /* Simple in-memory data store for test purposes. */
170 /* TODO: A hashtable isn't optimal for list queries... */
174 static gpointer memstore_create()
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,
180 (GDestroyNotify)bluesky_string_unref);
182 return (gpointer)store;
185 static void memstore_destroy(gpointer store)
190 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
192 MemStore *store = (MemStore *)st;
193 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
195 bluesky_string_ref(s);
199 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
201 MemStore *store = (MemStore *)s;
202 bluesky_string_ref(val);
203 g_hash_table_insert(store->store, g_strdup(key), val);
206 static BlueSkyStoreImplementation memstore_impl = {
207 .create = memstore_create,
208 .destroy = memstore_destroy,
213 /* Store implementation which writes data as files to disk. */
214 static gpointer filestore_create()
216 return GINT_TO_POINTER(1);
219 static void filestore_destroy()
223 static BlueSkyRCStr *filestore_get(gpointer s, const gchar *key)
225 gchar *contents = NULL;
227 GError *error = NULL;
229 g_file_get_contents(key, &contents, &length, &error);
230 if (contents == NULL)
233 return bluesky_string_new(contents, length);
236 static void filestore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
238 g_file_set_contents(key, val->data, val->len, NULL);
241 static BlueSkyStoreImplementation filestore_impl = {
242 .create = filestore_create,
243 .destroy = filestore_destroy,
244 .get = filestore_get,
245 .put = filestore_put,
249 void bluesky_store_init()
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");