#include "storage.h"
-const char *tablename = "memcached/items";
+const char *tablename= "memcached/items";
#define key_col_idx 0
#define data_col_idx 1
* @return true if the database schema was created without any problems
* false otherwise.
*/
-static bool create_schema(void) {
+static bool create_schema(void)
+{
ib_tbl_sch_t schema= NULL;
- ib_idx_sch_t index= NULL;
+ ib_idx_sch_t dbindex= NULL;
if (ib_database_create("memcached") != IB_TRUE)
{
IB_COL_UNSIGNED, 0, 8));
checked(ib_table_schema_add_col(schema, "exp", IB_INT,
IB_COL_UNSIGNED, 0, 4));
- checked(ib_table_schema_add_index(schema, "PRIMARY_KEY", &index));
- checked(ib_index_schema_add_col(index, "key", 0));
- checked(ib_index_schema_set_clustered(index));
+ checked(ib_table_schema_add_index(schema, "PRIMARY_KEY", &dbindex));
+ checked(ib_index_schema_add_col(dbindex, "key", 0));
+ checked(ib_index_schema_set_clustered(dbindex));
checked(ib_schema_lock_exclusive(transaction));
checked(ib_table_create(transaction, schema, &table_id));
checked(ib_trx_commit(transaction));
* @param item the item to store
* @return true if we can go ahead and commit the transaction, false otherwise
*/
-bool do_put_item(ib_trx_t trx, struct item* item) {
+static bool do_put_item(ib_trx_t trx, struct item* item)
+{
update_cas(item);
- ib_crsr_t cursor = NULL;
+ ib_crsr_t cursor= NULL;
ib_tpl_t tuple= NULL;
- bool retval = false;
+ bool retval= false;
checked(ib_cursor_open_table(tablename, trx, &cursor));
checked(ib_cursor_lock(cursor, IB_LOCK_X));
checked(ib_col_set_value(tuple, data_col_idx, item->data, item->size));
checked(ib_tuple_write_u32(tuple, flags_col_idx, item->flags));
checked(ib_tuple_write_u64(tuple, cas_col_idx, item->cas));
- checked(ib_tuple_write_u32(tuple, exp_col_idx, item->exp));
+ checked(ib_tuple_write_u32(tuple, exp_col_idx, (ib_u32_t)item->exp));
checked(ib_cursor_insert_row(cursor, tuple));
retval= true;
if (tuple != NULL)
ib_tuple_delete(tuple);
+ ib_err_t currsor_error;
if (cursor != NULL)
- ib_cursor_close(cursor);
+ currsor_error= ib_cursor_close(cursor);
return retval;
}
ib_crsr_t *cursor)
{
int res;
- ib_tpl_t tuple;
+ ib_tpl_t tuple= NULL;
- *cursor = NULL;
+ *cursor= NULL;
checked(ib_cursor_open_table(tablename, trx, cursor));
tuple= ib_clust_search_tuple_create(*cursor);
error_exit:
if (tuple != NULL)
ib_tuple_delete(tuple);
+
+ ib_err_t cursor_error;
if (*cursor != NULL)
- ib_cursor_close(*cursor);
+ cursor_error= ib_cursor_close(*cursor);
+
*cursor= NULL;
return false;
* @param nkey the lenght of the key
* @return a pointer to the item if I found it in the database
*/
-static struct item* do_get_item(ib_trx_t trx, const void* key, size_t nkey) {
+static struct item* do_get_item(ib_trx_t trx, const void* key, size_t nkey)
+{
ib_crsr_t cursor= NULL;
ib_tpl_t tuple= NULL;
struct item* retval= NULL;
- if (do_locate_item(trx, key, nkey, &cursor)) {
+ if (do_locate_item(trx, key, nkey, &cursor))
+ {
tuple= ib_clust_read_tuple_create(cursor);
if (tuple == NULL)
{
const void *dataptr= ib_col_get_value(tuple, data_col_idx);
retval= create_item(key, nkey, dataptr, datalen, 0, 0);
- if (retval == NULL) {
+ if (retval == NULL)
+ {
fprintf(stderr, "Failed to allocate memory\n");
goto error_exit;
}
- if (flaglen != 0) {
+ if (flaglen != 0)
+ {
ib_u32_t val;
checked(ib_tuple_read_u32(tuple, flags_col_idx, &val));
retval->flags= (uint32_t)val;
}
- if (caslen != 0) {
+ if (caslen != 0)
+ {
ib_u64_t val;
checked(ib_tuple_read_u64(tuple, cas_col_idx, &val));
retval->cas= (uint64_t)val;
}
- if (explen != 0) {
+ if (explen != 0)
+ {
ib_u32_t val;
checked(ib_tuple_read_u32(tuple, exp_col_idx, &val));
- retval->exp= (uint32_t)val;
+ retval->exp= (time_t)val;
}
}
if (tuple != NULL)
ib_tuple_delete(tuple);
+ ib_err_t cursor_error;
if (cursor != NULL)
- ib_cursor_close(cursor);
+ cursor_error= ib_cursor_close(cursor);
return retval;
}
{
checked(ib_cursor_lock(cursor, IB_LOCK_X));
checked(ib_cursor_delete_row(cursor));
- retval = true;
+ retval= true;
}
/* Release resources */
/* FALLTHROUGH */
error_exit:
if (cursor != NULL)
- ib_cursor_close(cursor);
+ {
+ ib_err_t cursor_error;
+ cursor_error= ib_cursor_close(cursor);
+ }
return retval;
}
* Initialize the database storage
* @return true if the database was initialized successfully, false otherwise
*/
-bool initialize_storage(void) {
+bool initialize_storage(void)
+{
ib_err_t error;
ib_id_t tid;
/* check to see if the table exists or if we should create the schema */
error= ib_table_get_id(tablename, &tid);
- if (error == DB_TABLE_NOT_FOUND) {
- if (!create_schema()) {
+ if (error == DB_TABLE_NOT_FOUND)
+ {
+ if (!create_schema())
+ {
return false;
}
- } else if (error != DB_SUCCESS) {
+ }
+ else if (error != DB_SUCCESS)
+ {
fprintf(stderr, "Failed to get table id: %s\n", ib_strerror(error));
return false;
}
return true;
error_exit:
+
return false;
}
/**
* Shut down this storage engine
*/
-void shutdown_storage(void) {
- checked(ib_shutdown());
+void shutdown_storage(void)
+{
+ checked(ib_shutdown(IB_SHUTDOWN_NORMAL));
error_exit:
;
}
*
* @param item the item to store
*/
-void put_item(struct item* item) {
+void put_item(struct item* item)
+{
ib_trx_t transaction= ib_trx_begin(IB_TRX_SERIALIZABLE);
- if (do_put_item(transaction, item)) {
+ if (do_put_item(transaction, item))
+ {
ib_err_t error= ib_trx_commit(transaction);
- if (error != DB_SUCCESS) {
+ if (error != DB_SUCCESS)
+ {
fprintf(stderr, "Failed to store key:\n\t%s\n",
ib_strerror(error));
}
- } else {
+ }
+ else
+ {
ib_err_t error= ib_trx_rollback(transaction);
if (error != DB_SUCCESS)
fprintf(stderr, "Failed to roll back the transaction:\n\t%s\n",
* @param nkey number of bytes in the key
* @return pointer to the item if found
*/
-struct item* get_item(const void* key, size_t nkey) {
+struct item* get_item(const void* key, size_t nkey)
+{
ib_trx_t transaction= ib_trx_begin(IB_TRX_SERIALIZABLE);
struct item* ret= do_get_item(transaction, key, nkey);
ib_err_t error= ib_trx_rollback(transaction);
+
if (error != DB_SUCCESS)
fprintf(stderr, "Failed to roll back the transaction:\n\t%s\n",
ib_strerror(error));
* Flush the entire cache
* @param when when the cache should be flushed (0 == immediately)
*/
-void flush(uint32_t when) {
+void flush(uint32_t when __attribute__((unused)))
+{
/* @TODO implement support for when != 0 */
ib_trx_t transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
ib_crsr_t cursor= NULL;
checked(ib_cursor_first(cursor));
checked(ib_cursor_lock(cursor, IB_LOCK_X));
- do {
+ do
+ {
checked(ib_cursor_delete_row(cursor));
} while ((err= ib_cursor_next(cursor)) == DB_SUCCESS);
fprintf(stderr, "Failed to flush the cache: %s\n", ib_strerror(err));
goto error_exit;
}
- ib_cursor_close(cursor);
+ ib_err_t cursor_error;
+ cursor_error= ib_cursor_close(cursor);
cursor= NULL;
checked(ib_trx_commit(transaction));
return;
error_exit:
if (cursor != NULL)
- ib_cursor_close(cursor);
+ {
+ cursor_error= ib_cursor_close(cursor);
+ }
ib_err_t error= ib_trx_rollback(transaction);
if (error != DB_SUCCESS)
* Update the cas ID in the item structure
* @param item the item to update
*/
-void update_cas(struct item* item) {
+void update_cas(struct item* item)
+{
item->cas= ++cas;
}
+
+/**
+ * Release all the resources allocated by the item
+ * @param item the item to release
+ */
+void release_item(struct item* item)
+{
+ free(item->key);
+ free(item->data);
+ free(item);
+}