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)
37 g_atomic_int_inc(&string->refcount);
40 void bluesky_string_unref(BlueSkyRCStr *string)
45 if (g_atomic_int_dec_and_test(&string->refcount)) {
51 /* Duplicate and return a new reference-counted string, containing a copy of
52 * the original data, with a reference count of 1. As an optimization, if the
53 * passed-in string already has a reference count of 1, the original is
54 * returned. Can be used to make a mutable copy of a shared string. For this
55 * to truly be safe, it is probably needed that there be some type of lock
56 * protecting access to the string. */
57 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
62 if (g_atomic_int_dec_and_test(&string->refcount)) {
63 /* There are no other shared copies, so return this one. */
64 g_atomic_int_inc(&string->refcount);
67 return bluesky_string_new(g_memdup(string->data, string->len),
72 /* Simple in-memory data store for test purposes. */
76 /* TODO: A hashtable isn't optimal for list queries... */
80 MemStore *memstore_new()
82 MemStore *store = g_new(MemStore, 1);
83 store->lock = g_mutex_new();
84 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
86 (GDestroyNotify)bluesky_string_unref);
91 BlueSkyRCStr *memstore_get(MemStore *store, const gchar *key)
93 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
95 bluesky_string_ref(s);
99 void memstore_put(MemStore *store, const gchar *key, BlueSkyRCStr *val)
101 bluesky_string_ref(val);
102 g_hash_table_insert(store->store, g_strdup(key), val);