/** * Flush the entire cache * @param when when the cache should be flushed (0 == immediately) */ 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; ib_err_t err= DB_SUCCESS; checked(ib_cursor_open_table(tablename, transaction, &cursor)); checked(ib_cursor_first(cursor)); checked(ib_cursor_lock(cursor, IB_LOCK_X)); do { checked(ib_cursor_delete_row(cursor)); } while ((err= ib_cursor_next(cursor)) == DB_SUCCESS); if (err != DB_END_OF_INDEX) { fprintf(stderr, "Failed to flush the cache: %s\n", ib_strerror(err)); goto error_exit; } ib_cursor_close(cursor); cursor= NULL; checked(ib_trx_commit(transaction)); return; error_exit: if (cursor != NULL) ib_cursor_close(cursor); 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)); }
/** * Store an item into the database. Update the CAS id on the item before * storing it in the database. * * @param trx the transaction to use * @param item the item to store * @return true if we can go ahead and commit the transaction, false otherwise */ static bool do_put_item(ib_trx_t trx, struct item* item) { update_cas(item); ib_crsr_t cursor= NULL; ib_tpl_t tuple= NULL; bool retval= false; checked(ib_cursor_open_table(tablename, trx, &cursor)); checked(ib_cursor_lock(cursor, IB_LOCK_X)); tuple= ib_clust_read_tuple_create(cursor); checked(ib_col_set_value(tuple, key_col_idx, item->key, item->nkey)); 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, (ib_u32_t)item->exp)); checked(ib_cursor_insert_row(cursor, tuple)); retval= true; /* Release resources: */ /* FALLTHROUGH */ error_exit: if (tuple != NULL) ib_tuple_delete(tuple); if (cursor != NULL) ib_cursor_close(cursor); return retval; }
static ib_err_t test_phase_I(void) /*==============*/ { int i; ib_err_t err; int dups = 0; err = create_database(DATABASE); assert(err == DB_SUCCESS); err = create_table(DATABASE, TABLE); assert(err == DB_SUCCESS); for (i = 0; i < N_TRX; ++i) { ib_crsr_t crsr; ib_trx_t ib_trx; ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); assert(ib_trx != NULL); err = open_table(DATABASE, TABLE, ib_trx, &crsr); assert(err == DB_SUCCESS); err = ib_cursor_lock(crsr, IB_LOCK_IX); assert(err == DB_SUCCESS); err = insert_rows(crsr, i * N_RECS, N_RECS); assert(err == DB_SUCCESS || err == DB_DUPLICATE_KEY); if (err == DB_DUPLICATE_KEY) { ++dups; } err = ib_cursor_close(crsr); assert(err == DB_SUCCESS); crsr = NULL; err = ib_trx_commit(ib_trx); assert(err == DB_SUCCESS); } assert(dups == 0 || dups == N_TRX); return(dups == N_TRX ? DB_DUPLICATE_KEY : DB_SUCCESS); }
/** * Delete an item from the cache * @param trx the transaction to use * @param key the key of the item to delete * @param nkey the length of the key * @return true if we should go ahead and commit the transaction * or false if we should roll back (if the key didn't exists) */ static bool do_delete_item(ib_trx_t trx, const void* key, size_t nkey) { ib_crsr_t cursor= NULL; bool retval= false; if (do_locate_item(trx, key, nkey, &cursor)) { checked(ib_cursor_lock(cursor, IB_LOCK_X)); checked(ib_cursor_delete_row(cursor)); retval= true; } /* Release resources */ /* FALLTHROUGH */ error_exit: if (cursor != NULL) ib_cursor_close(cursor); return retval; }
int main(int argc, char* argv[]) { int i; ib_err_t err; ib_crsr_t crsr; ib_trx_t ib_trx; #ifdef __WIN__ srand((int) time(NULL)); #else srandom(time(NULL)); #endif err = ib_init(); assert(err == DB_SUCCESS); test_configure(); #ifndef __WIN__ set_options(argc, argv); #endif /* __WIN__ */ err = ib_startup("barracuda"); assert(err == DB_SUCCESS); err = create_database(DATABASE); assert(err == DB_SUCCESS); for (i = 0; i < 10; ++i) { int j; printf("Create table\n"); err = create_table(DATABASE, TABLE); assert(err == DB_SUCCESS); for (j = 0; j < 10; ++j) { ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); assert(ib_trx != NULL); err = open_table(DATABASE, TABLE, ib_trx, &crsr); assert(err == DB_SUCCESS); err = ib_cursor_lock(crsr, IB_LOCK_IX); assert(err == DB_SUCCESS); insert_random_rows(crsr); update_random_rows(crsr); err = ib_cursor_close(crsr); assert(err == DB_SUCCESS); crsr = NULL; err = ib_trx_commit(ib_trx); assert(err == DB_SUCCESS); } printf("Drop table\n"); err = drop_table(DATABASE, TABLE); assert(err == DB_SUCCESS); } err = ib_shutdown(IB_SHUTDOWN_NORMAL); assert(err == DB_SUCCESS); #ifdef UNIV_DEBUG_VALGRIND VALGRIND_DO_LEAK_CHECK; #endif return(EXIT_SUCCESS); }
int main(int argc, char* argv[]) { ib_err_t err; ib_crsr_t crsr; ib_trx_t ib_trx; (void) argc; (void) argv; err = ib_init(); assert(err == DB_SUCCESS); test_configure(); err = ib_startup("barracuda"); assert(err == DB_SUCCESS); err = create_database(DATABASE); assert(err == DB_SUCCESS); err = create_table(DATABASE, TABLE); assert(err == DB_SUCCESS); ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); assert(ib_trx != NULL); err = open_table(DATABASE, TABLE, ib_trx, &crsr); assert(err == DB_SUCCESS); err = ib_cursor_lock(crsr, IB_LOCK_IX); assert(err == DB_SUCCESS); err = insert_random_rows(crsr); assert(err == DB_SUCCESS); err = ib_cursor_close(crsr); assert(err == DB_SUCCESS); crsr = NULL; err = ib_trx_commit(ib_trx); assert(err == DB_SUCCESS); err = create_sec_index_1(DATABASE, TABLE); assert(err == DB_SUCCESS); err = test_create_temp_index(DATABASE, TABLE, "c2"); err = open_sec_index_1(DATABASE, TABLE); assert(err == DB_SUCCESS); err = drop_table(DATABASE, TABLE); assert(err == DB_SUCCESS); err = ib_shutdown(IB_SHUTDOWN_NORMAL); assert(err == DB_SUCCESS); #ifdef UNIV_DEBUG_VALGRIND VALGRIND_DO_LEAK_CHECK; #endif return(EXIT_SUCCESS); }
/********************************************************************** DELETE FROM t2 WHERE c1 == 9 @return DB_SUCCESS or error code */ static ib_err_t delete_t2( /*======*/ void* arg) /*!< in: arguments for callback */ { ib_err_t err; int res = ~0; int nine = 9; ib_tpl_t key_tpl = NULL; ib_crsr_t crsr = NULL; cb_args_t* cb_arg = (cb_args_t *)arg; tbl_class_t* tbl = cb_arg->tbl; //fprintf(stderr, "t2: DELETE\n"); err = open_table(tbl->db_name, tbl->name, cb_arg->trx, &crsr); if (err != DB_SUCCESS) { goto err_exit; } err = ib_cursor_lock(crsr, IB_LOCK_IX); if (err != DB_SUCCESS) { goto err_exit; } err = ib_cursor_set_lock_mode(crsr, IB_LOCK_X); if (err != DB_SUCCESS) { goto err_exit; } /* Create a tuple for searching an index. */ key_tpl = ib_sec_search_tuple_create(crsr); assert(key_tpl != NULL); /* Set the value to delete. */ err = ib_col_set_value(key_tpl, 0, &nine, 4); assert(err == DB_SUCCESS); /* Search for the key using the cluster index (PK) */ err = ib_cursor_moveto(crsr, key_tpl, IB_CUR_GE, &res); if (res != 0) { goto clean_exit; } if (err != DB_SUCCESS) { goto err_exit; } /* InnoDB handles the updating of all secondary indexes. */ err = ib_cursor_delete_row(crsr); if (err != DB_SUCCESS) { goto err_exit; } update_err_stats(cb_arg->err_st, err); goto clean_exit; err_exit: update_err_stats(cb_arg->err_st, err); clean_exit: if (key_tpl != NULL) { ib_tuple_delete(key_tpl); } if (crsr != NULL) { ib_err_t err2; err2 = ib_cursor_close(crsr); assert(err2 == DB_SUCCESS); crsr = NULL; } return(err); }
/********************************************************************** UPDATE t2 SET score = score + 100 AND upd_run = run_number WHERE c1 == 5 @return DB_SUCCESS or error code */ static ib_err_t update_t2( /*======*/ void* arg) /*!< in: arguments for callback */ { ib_err_t err; int res = ~0; int five = 5; ib_tpl_t key_tpl = NULL; ib_tpl_t old_tpl = NULL; ib_tpl_t new_tpl = NULL; ib_crsr_t crsr = NULL; cb_args_t* cb_arg = (cb_args_t *)arg; tbl_class_t* tbl = cb_arg->tbl; //fprintf(stderr, "t2: UPDATE\n"); err = open_table(tbl->db_name, tbl->name, cb_arg->trx, &crsr); if (err != DB_SUCCESS) { goto err_exit; } err = ib_cursor_lock(crsr, IB_LOCK_IX); if (err != DB_SUCCESS) { goto err_exit; } err = ib_cursor_set_lock_mode(crsr, IB_LOCK_X); if (err != DB_SUCCESS) { goto err_exit; } err = ib_cursor_set_lock_mode(crsr, IB_LOCK_X); assert(err == DB_SUCCESS); /* Create a tuple for searching an index. */ key_tpl = ib_sec_search_tuple_create(crsr); assert(key_tpl != NULL); /* Set the value to look for. */ err = ib_col_set_value(key_tpl, 0, &five, 4); assert(err == DB_SUCCESS); /* Search for the key using the cluster index (PK) */ err = ib_cursor_moveto(crsr, key_tpl, IB_CUR_GE, &res); ib_tuple_delete(key_tpl); if (res != 0) { goto clean_exit; } else if (err != DB_SUCCESS) { goto err_exit; } /* Create the tuple instance that we will use to update the table. old_tpl is used for reading the existing row and new_tpl will contain the update row data. */ old_tpl = ib_clust_read_tuple_create(crsr); assert(old_tpl != NULL); new_tpl = ib_clust_read_tuple_create(crsr); assert(new_tpl != NULL); /* Iterate over the records while the first column matches "a". */ while (1) { ib_u32_t score; ib_u32_t val; ib_ulint_t data_len; ib_col_meta_t col_meta; err = ib_cursor_read_row(crsr, old_tpl); assert(err == DB_SUCCESS); ib_col_get_meta(old_tpl, 0, &col_meta); err = ib_tuple_read_u32(old_tpl, 0, &val); assert(err == DB_SUCCESS); if (val != five) { goto clean_exit; } /* Copy the old contents to the new tuple. */ err = ib_tuple_copy(new_tpl, old_tpl); /* Update the score column in the new tuple. */ data_len = ib_col_get_meta(old_tpl, 1, &col_meta); assert(data_len != IB_SQL_NULL); err = ib_tuple_read_u32(old_tpl, 1, &score); assert(err == DB_SUCCESS); score += 100; /* Set the updated value in the new tuple. */ err = ib_tuple_write_u32(new_tpl, 1, score); assert(err == DB_SUCCESS); /* Set the updated value in the new tuple. */ err = ib_tuple_write_u32(new_tpl, 3, cb_arg->run_number); assert(err == DB_SUCCESS); err = ib_cursor_update_row(crsr, old_tpl, new_tpl); if (err != DB_SUCCESS) { goto err_exit; } update_err_stats(cb_arg->err_st, err); /* Move to the next record to update. */ err = ib_cursor_next(crsr); if (err != DB_SUCCESS) { goto err_exit; } /* Reset the old and new tuple instances. */ old_tpl = ib_tuple_clear(old_tpl); assert(old_tpl != NULL); new_tpl = ib_tuple_clear(new_tpl); assert(new_tpl != NULL); } err_exit: update_err_stats(cb_arg->err_st, err); clean_exit: if (old_tpl != NULL) { ib_tuple_delete(old_tpl); } if (new_tpl != NULL) { ib_tuple_delete(new_tpl); } if (crsr != NULL) { ib_err_t err2; err2 = ib_cursor_close(crsr); assert(err2 == DB_SUCCESS); crsr = NULL; } return(err); }
/********************************************************************** INSERT INTO t2 VALUES (<rand 1-100>, 0, run_number, 0) @return DB_SUCCESS or error code */ static ib_err_t insert_t2( /*======*/ void* arg) /*!< in: arguments for callback */ { int i; ib_err_t err; ib_crsr_t crsr = NULL; ib_tpl_t tpl = NULL; cb_args_t* cb_arg = (cb_args_t *)arg; tbl_class_t* tbl = cb_arg->tbl; //fprintf(stderr, "t2: INSERT\n"); err = open_table(tbl->db_name, tbl->name, cb_arg->trx, &crsr); if (err != DB_SUCCESS) { goto err_exit; } err = ib_cursor_lock(crsr, IB_LOCK_IX); if (err != DB_SUCCESS) { goto err_exit; } err = ib_cursor_set_lock_mode(crsr, IB_LOCK_X); if (err != DB_SUCCESS) { goto err_exit; } tpl = ib_clust_read_tuple_create(crsr); assert(tpl != NULL); for (i = 0; i < cb_arg->batch_size; ++i) { int val = 0; int zero = 0; val = random() % key_range; err = ib_col_set_value(tpl, 0, &val, 4); assert(err == DB_SUCCESS); err = ib_col_set_value(tpl, 1, &zero, 4); assert(err == DB_SUCCESS); err = ib_col_set_value(tpl, 2, &cb_arg->run_number, 4); assert(err == DB_SUCCESS); err = ib_col_set_value(tpl, 3, &zero, 4); assert(err == DB_SUCCESS); err = ib_cursor_insert_row(crsr, tpl); if (err != DB_SUCCESS) { goto err_exit; } update_err_stats(cb_arg->err_st, err); tpl = ib_tuple_clear(tpl); assert(tpl != NULL); } goto clean_exit; err_exit: update_err_stats(cb_arg->err_st, err); clean_exit: if (tpl != NULL) { ib_tuple_delete(tpl); } if (crsr != NULL) { ib_err_t err2; err2 = ib_cursor_close(crsr); assert(err2 == DB_SUCCESS); crsr = NULL; } return(err); }
int main(int argc, char* argv[]) { ib_err_t err; ib_crsr_t crsr; ib_trx_t ib_trx; (void)argc; (void)argv; err = ib_init(); assert(err == DB_SUCCESS); test_configure(); err = ib_startup("barracuda"); assert(err == DB_SUCCESS); err = create_database(DATABASE); assert(err == DB_SUCCESS); printf("Create table\n"); err = create_table(DATABASE, TABLE); assert(err == DB_SUCCESS); printf("Begin transaction\n"); ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); assert(ib_trx != NULL); printf("Open cursor\n"); err = open_table(DATABASE, TABLE, ib_trx, &crsr); assert(err == DB_SUCCESS); printf("Lock table in IX\n"); err = ib_cursor_lock(crsr, IB_LOCK_IX); assert(err == DB_SUCCESS); printf("Insert rows\n"); err = insert_rows(crsr); assert(err == DB_SUCCESS); printf("Query table\n"); err = do_query(crsr); assert(err == DB_SUCCESS); printf("Close cursor\n"); err = ib_cursor_close(crsr); assert(err == DB_SUCCESS); crsr = NULL; printf("Commit transaction\n"); err = ib_trx_commit(ib_trx); assert(err == DB_SUCCESS); printf("Drop table\n"); err = drop_table(DATABASE, TABLE); assert(err == DB_SUCCESS); err = ib_shutdown(IB_SHUTDOWN_NORMAL); assert(err == DB_SUCCESS); #ifdef UNIV_DEBUG_VALGRIND VALGRIND_DO_LEAK_CHECK; #endif return(EXIT_SUCCESS); }