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>
16 #include "bluesky-private.h"
19 /* A storage layer that writes to Berkeley DB locally. */
22 GThreadPool *thread_pool;
27 static void bdbstore_task(gpointer a, gpointer s)
30 BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
31 BDBStore *store = (BDBStore *)s;
33 async->status = ASYNC_RUNNING;
34 async->exec_time = bluesky_now_hires();
37 memset(&key, 0, sizeof(key));
39 key.data = async->key;
40 key.size = strlen(async->key);
43 memset(&value, 0, sizeof(value));
45 if (async->op == STORE_OP_GET) {
46 value.flags = DB_DBT_MALLOC;
48 res = store->db->get(store->db, NULL, &key, &value, 0);
54 fprintf(stderr, "BDB read failure: %s\n", db_strerror(res));
56 async->data = bluesky_string_new(value.data, value.size);
60 } else if (async->op == STORE_OP_PUT) {
61 value.data = async->data->data;
62 value.size = async->data->len;
64 res = store->db->put(store->db, NULL, &key, &value, 0);
67 fprintf(stderr, "BDB write failure: %s\n", db_strerror(res));
73 bluesky_store_async_mark_complete(async);
74 bluesky_store_async_unref(async);
77 static gpointer bdbstore_new(const gchar *path)
80 BDBStore *store = g_new0(BDBStore, 1);
81 store->thread_pool = g_thread_pool_new(bdbstore_task, store, 16, FALSE,
84 res = db_env_create(&store->env, 0);
87 fprintf(stderr, "db_env_create failure: %s\n", db_strerror(res));
91 res = store->env->open(store->env, path,
92 DB_CREATE | DB_RECOVER | DB_INIT_LOCK | DB_INIT_LOG
93 | DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD,
97 fprintf(stderr, "BDB open failure: %s\n",
102 res = db_create(&store->db, store->env, 0);
105 fprintf(stderr, "DB create failed: %s\n", db_strerror(res));
109 uint32_t flags = DB_CREATE | DB_THREAD | DB_AUTO_COMMIT;
111 res = store->db->open(store->db,
120 fprintf(stderr, "DB open failed: %s\n",
127 static void bdbstore_destroy(gpointer s)
129 BDBStore *store = (BDBStore *)store;
132 store->db->close(store->db, 0);
136 store->env->close(store->env, 0);
142 static void bdbstore_submit(gpointer s, BlueSkyStoreAsync *async)
144 BDBStore *store = (BDBStore *)s;
145 g_return_if_fail(async->status == ASYNC_NEW);
146 g_return_if_fail(async->op != STORE_OP_NONE);
151 async->status = ASYNC_PENDING;
152 bluesky_store_async_ref(async);
153 g_thread_pool_push(store->thread_pool, async, NULL);
157 g_warning("Uknown operation type for BDBStore: %d\n", async->op);
158 bluesky_store_async_mark_complete(async);
163 static void bdbstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
167 static BlueSkyStoreImplementation store_impl = {
168 .create = bdbstore_new,
169 .destroy = bdbstore_destroy,
170 .submit = bdbstore_submit,
171 .cleanup = bdbstore_cleanup,
174 void bluesky_store_init_bdb(void)
176 bluesky_store_register(&store_impl, "bdb");