X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=example%2Fstorage_innodb.c;h=e3113f17fdb7c6d004b45f23226e67fe539ea288;hb=249369a70e340151cc35156f47e492b6ab13d258;hp=d2ae6f5af390aa4ce37f4e970a0ba5ed846b02f3;hpb=d292bcb63666dad0277e9658916514f64b7200b0;p=awesomized%2Flibmemcached diff --git a/example/storage_innodb.c b/example/storage_innodb.c index d2ae6f5a..e3113f17 100644 --- a/example/storage_innodb.c +++ b/example/storage_innodb.c @@ -10,7 +10,7 @@ #include "storage.h" -const char *tablename = "memcached/items"; +const char *tablename= "memcached/items"; #define key_col_idx 0 #define data_col_idx 1 @@ -48,9 +48,10 @@ do { \ * @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) { @@ -72,9 +73,9 @@ static bool create_schema(void) { 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)); @@ -101,12 +102,13 @@ static bool create_schema(void) { * @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)); @@ -116,7 +118,7 @@ bool do_put_item(ib_trx_t trx, struct item* item) { 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; @@ -127,8 +129,9 @@ bool do_put_item(ib_trx_t trx, struct item* item) { 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; } @@ -150,9 +153,9 @@ static bool do_locate_item(ib_trx_t trx, 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); @@ -180,8 +183,11 @@ static bool do_locate_item(ib_trx_t trx, 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; @@ -195,12 +201,14 @@ static bool do_locate_item(ib_trx_t trx, * @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) { @@ -216,25 +224,29 @@ static struct item* do_get_item(ib_trx_t trx, const void* key, size_t nkey) { 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; } } @@ -245,8 +257,9 @@ static struct item* do_get_item(ib_trx_t trx, const void* key, size_t nkey) { 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; } @@ -267,14 +280,17 @@ static bool do_delete_item(ib_trx_t trx, const void* key, size_t nkey) { { 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; } @@ -288,7 +304,8 @@ static bool do_delete_item(ib_trx_t trx, const void* key, size_t nkey) { * 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; @@ -300,11 +317,15 @@ bool initialize_storage(void) { /* 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; } @@ -312,14 +333,16 @@ bool initialize_storage(void) { 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: ; } @@ -329,15 +352,20 @@ void shutdown_storage(void) { * * @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", @@ -351,10 +379,12 @@ void put_item(struct item* item) { * @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)); @@ -445,7 +475,8 @@ bool delete_item(const void* key, size_t nkey) { * 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; @@ -455,7 +486,8 @@ void flush(uint32_t when) { 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); @@ -464,14 +496,17 @@ void flush(uint32_t when) { 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) @@ -483,6 +518,18 @@ void flush(uint32_t when) { * 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); +}