Add pluggable support for multiple storage backends.
[bluesky.git] / bluesky / s3store.c
index 47a8f60..4602136 100644 (file)
 /* Interface to Amazon S3 storage. */
 
 /* Simple in-memory data store for test purposes. */
-struct S3Store {
+typedef struct {
     S3BucketContext bucket;
-};
+} S3Store;
 
-struct S3Store *s3store_new()
+static gpointer s3store_new()
 {
-    struct S3Store *store = g_new(struct S3Store, 1);
+    S3Store *store = g_new(S3Store, 1);
     store->bucket.bucketName = "mvrable-bluesky";
     store->bucket.protocol = S3ProtocolHTTP;
     store->bucket.uriStyle = S3UriStylePath;
@@ -36,6 +36,11 @@ struct S3Store *s3store_new()
     return store;
 }
 
+static void s3store_destroy(gpointer store)
+{
+    g_free(store);
+}
+
 struct get_info {
     gchar *buf;
     gint offset;
@@ -73,7 +78,7 @@ static S3Status s3store_properties_callback(const S3ResponseProperties *properti
     return S3StatusOK;
 }
 
-void s3store_response_callback(S3Status status,
+static void s3store_response_callback(S3Status status,
                                const S3ErrorDetails *errorDetails,
                                void *callbackData)
 {
@@ -84,8 +89,10 @@ void s3store_response_callback(S3Status status,
     }
 }
 
-BlueSkyRCStr *s3store_get(struct S3Store *store, const gchar *key)
+static BlueSkyRCStr *s3store_get(gpointer s, const gchar *key)
 {
+    S3Store *store = (S3Store *)s;
+
     struct get_info info;
     info.buf = (char *)g_malloc0(BLUESKY_BLOCK_SIZE);
     info.offset = 0;
@@ -102,8 +109,10 @@ BlueSkyRCStr *s3store_get(struct S3Store *store, const gchar *key)
     return bluesky_string_new(info.buf, BLUESKY_BLOCK_SIZE);
 }
 
-void s3store_put(struct S3Store *store, const gchar *key, BlueSkyRCStr *val)
+static void s3store_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
 {
+    S3Store *store = (S3Store *)s;
+
     struct put_info info;
     info.val = val;
     info.offset = 0;
@@ -117,3 +126,16 @@ void s3store_put(struct S3Store *store, const gchar *key, BlueSkyRCStr *val)
     S3_put_object(&store->bucket, key, val->len, NULL, NULL,
                   &handler, &info);
 }
+
+static BlueSkyStoreImplementation store_impl = {
+    .create = s3store_new,
+    .destroy = s3store_destroy,
+    .get = s3store_get,
+    .put = s3store_put,
+};
+
+void bluesky_store_init_s3(void)
+{
+    S3_initialize(NULL, S3_INIT_ALL);
+    bluesky_store_register(&store_impl, "s3");
+}