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. */
19 struct _BlueSkyStore {
20 const BlueSkyStoreImplementation *impl;
24 GHashTable *store_implementations;
26 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
29 g_hash_table_insert(store_implementations, g_strdup(name), (gpointer)impl);
32 BlueSkyStore *bluesky_store_new(const gchar *type)
34 const BlueSkyStoreImplementation *impl;
36 impl = g_hash_table_lookup(store_implementations, type);
40 gpointer handle = impl->create();
44 BlueSkyStore *store = g_new(BlueSkyStore, 1);
46 store->handle = handle;
50 void bluesky_store_free(BlueSkyStore *store)
52 store->impl->destroy(store->handle);
56 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
58 return store->impl->get(store->handle, key);
61 void bluesky_store_put(BlueSkyStore *store,
62 const gchar *key, BlueSkyRCStr *val)
64 store->impl->put(store->handle, key, val);
67 /* Simple in-memory data store for test purposes. */
71 /* TODO: A hashtable isn't optimal for list queries... */
75 static gpointer memstore_create()
77 MemStore *store = g_new(MemStore, 1);
78 store->lock = g_mutex_new();
79 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
81 (GDestroyNotify)bluesky_string_unref);
83 return (gpointer)store;
86 static void memstore_destroy(gpointer store)
91 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
93 MemStore *store = (MemStore *)st;
94 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
96 bluesky_string_ref(s);
100 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
102 MemStore *store = (MemStore *)s;
103 bluesky_string_ref(val);
104 g_hash_table_insert(store->store, g_strdup(key), val);
107 static BlueSkyStoreImplementation memstore_impl = {
108 .create = memstore_create,
109 .destroy = memstore_destroy,
114 /* Store implementation which writes data as files to disk. */
115 static gpointer filestore_create()
117 return GINT_TO_POINTER(1);
120 static void filestore_destroy()
124 static BlueSkyRCStr *filestore_get(gpointer s, const gchar *key)
126 gchar *contents = NULL;
128 GError *error = NULL;
130 g_file_get_contents(key, &contents, &length, &error);
131 if (contents == NULL)
134 return bluesky_string_new(contents, length);
137 static void filestore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
139 g_file_set_contents(key, val->data, val->len, NULL);
142 static BlueSkyStoreImplementation filestore_impl = {
143 .create = filestore_create,
144 .destroy = filestore_destroy,
145 .get = filestore_get,
146 .put = filestore_put,
149 void bluesky_store_init()
151 store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
152 bluesky_store_register(&memstore_impl, "mem");
153 bluesky_store_register(&filestore_impl, "file");