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>
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 /* Create and return a new reference-counted string. The reference count is
20 * initially one. The newly-returned string takes ownership of the memory
21 * pointed at by data, and will call g_free on it when the reference count
23 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len)
25 BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
28 g_atomic_int_set(&string->refcount, 1);
32 void bluesky_string_ref(BlueSkyRCStr *string)
34 g_atomic_int_inc(&string->refcount);
37 void bluesky_string_unref(BlueSkyRCStr *string)
39 if (g_atomic_int_dec_and_test(&string->refcount)) {
45 /* Duplicate and return a new reference-counted string, containing a copy of
46 * the original data, with a reference count of 1. As an optimization, if the
47 * passed-in string already has a reference count of 1, the original is
48 * returned. Can be used to make a mutable copy of a shared string. */
49 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
51 if (g_atomic_int_dec_and_test(&string->refcount)) {
52 /* There are no other shared copies, so return this one. */
53 g_atomic_int_inc(&string->refcount);
56 return bluesky_string_new(g_memdup(string->data, string->len),
61 /* Simple in-memory data store for test purposes. */
65 /* TODO: A hashtable isn't optimal for list queries... */
69 MemStore *memstore_new()
71 MemStore *store = g_new(MemStore, 1);
72 store->lock = g_mutex_new();
73 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
75 (GDestroyNotify)bluesky_string_unref);
80 BlueSkyRCStr *memstore_get(MemStore *store, const gchar *key)
82 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
84 bluesky_string_ref(s);
88 void memstore_put(MemStore *store, const gchar *key, BlueSkyRCStr *val)
90 bluesky_string_ref(val);
91 g_hash_table_insert(store->store, g_strdup(key), val);