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();
65 async->status = ASYNC_NEW;
66 async->op = STORE_OP_NONE;
70 async->store_private = NULL;
75 /* Block until the given operation has completed. */
76 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
78 g_return_if_fail(async != NULL);
79 g_mutex_lock(async->lock);
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);
87 while (async->status != ASYNC_COMPLETE) {
88 g_cond_wait(async->completion_cond, async->lock);
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
95 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
97 async->status = ASYNC_COMPLETE;
98 g_cond_broadcast(async->completion_cond);
101 /* Convenience wrappers that perform a single operation synchronously. */
102 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
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);
109 bluesky_store_async_wait(async);
111 BlueSkyRCStr *data = async->data;
112 bluesky_string_ref(data);
116 void bluesky_store_put(BlueSkyStore *store,
117 const gchar *key, BlueSkyRCStr *val)
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);
124 store->impl->submit(store->handle, async);
126 bluesky_store_async_wait(async);
130 /* Simple in-memory data store for test purposes. */
134 /* TODO: A hashtable isn't optimal for list queries... */
138 static gpointer memstore_create()
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,
144 (GDestroyNotify)bluesky_string_unref);
146 return (gpointer)store;
149 static void memstore_destroy(gpointer store)
154 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
156 MemStore *store = (MemStore *)st;
157 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
159 bluesky_string_ref(s);
163 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
165 MemStore *store = (MemStore *)s;
166 bluesky_string_ref(val);
167 g_hash_table_insert(store->store, g_strdup(key), val);
170 static BlueSkyStoreImplementation memstore_impl = {
171 .create = memstore_create,
172 .destroy = memstore_destroy,
177 /* Store implementation which writes data as files to disk. */
178 static gpointer filestore_create()
180 return GINT_TO_POINTER(1);
183 static void filestore_destroy()
187 static BlueSkyRCStr *filestore_get(gpointer s, const gchar *key)
189 gchar *contents = NULL;
191 GError *error = NULL;
193 g_file_get_contents(key, &contents, &length, &error);
194 if (contents == NULL)
197 return bluesky_string_new(contents, length);
200 static void filestore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
202 g_file_set_contents(key, val->data, val->len, NULL);
205 static BlueSkyStoreImplementation filestore_impl = {
206 .create = filestore_create,
207 .destroy = filestore_destroy,
208 .get = filestore_get,
209 .put = filestore_put,
213 void bluesky_store_init()
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");