/** * 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; }
/********************************************************************* INSERT INTO T VALUE(N, RANDOM(n), RANDOM(n)); */ static ib_err_t insert_rows( /*========*/ ib_crsr_t crsr, /*!< in, out: cursor to use for write */ int start, /*!< in: in: start of c1 value */ int count) /*!< in: number of rows to insert */ { int i; int dups = 0; ib_tpl_t tpl = NULL; ib_err_t err = DB_ERROR; char* ptr = malloc(8192); assert(ptr != NULL); tpl = ib_clust_read_tuple_create(crsr); assert(tpl != NULL); for (i = start; i < start + count; ++i) { int l; err = ib_tuple_write_i32(tpl, 0, i); assert(err == DB_SUCCESS); l = gen_rand_text(ptr, C2_MAX_LEN); err = ib_col_set_value(tpl, 1, ptr, l); assert(err == DB_SUCCESS); l = gen_rand_text(ptr, C3_MAX_LEN); err = ib_col_set_value(tpl, 2, ptr, l); assert(err == DB_SUCCESS); err = ib_cursor_insert_row(crsr, tpl); assert(err == DB_SUCCESS || err == DB_DUPLICATE_KEY); if (err == DB_DUPLICATE_KEY) { err = DB_SUCCESS; ++dups; } } if (tpl != NULL) { ib_tuple_delete(tpl); } free(ptr); if (err == DB_SUCCESS) { if (dups == count) { err = DB_DUPLICATE_KEY; printf("Recover OK\n"); } else { assert(dups == 0); printf("Insert OK\n"); } } return(err); }
/********************************************************************* INSERT INTO T VALUE(c1, c2, c3, c4, c5, c6); */ static ib_err_t insert_rows( /*========*/ ib_crsr_t crsr) /*!< in, out: cursor to use for write */ { ib_err_t err; ib_tpl_t tpl = NULL; tpl = ib_clust_read_tuple_create(crsr); assert(tpl != NULL); err = ib_col_set_value(tpl, 0, "abcdefghij", 10); assert(err == DB_SUCCESS); err = ib_tuple_write_float(tpl, 2, 2.0); assert(err == DB_SUCCESS); err = ib_tuple_write_double(tpl, 3, 3.0); assert(err == DB_SUCCESS); err = ib_col_set_value(tpl, 4, "BLOB", 4); assert(err == DB_SUCCESS); err = ib_col_set_value(tpl, 5, "1.23", 4); assert(err == DB_SUCCESS); /* Check for NULL constraint violation. */ err = ib_cursor_insert_row(crsr, tpl); assert(err == DB_DATA_MISMATCH); /* Set column C2 value and retry operation. */ err = ib_tuple_write_u32(tpl, 1, 1); assert(err == DB_SUCCESS); err = ib_cursor_insert_row(crsr, tpl); assert(err == DB_SUCCESS); ib_tuple_delete(tpl); return(DB_SUCCESS); }
int main(int argc, char *argv[]) { if (argc != 2) { exit(1); } char db_name[32] = "cstore"; char *table_name = argv[1]; char full_table_name[32]; if (!init_db()) { exit(1); } if (!create_db(db_name)) { std::cerr << "create_db failed" << std::endl; exit(1); } sprintf(full_table_name, "%s/%s", db_name, table_name); if (!create_schema(full_table_name)) { std::cerr << "create_schema failed" << std::endl; exit(1); } ib_crsr_t ib_crsr; std::string line; int i = 0; ib_trx_t ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); ib_cursor_open_table(full_table_name, ib_trx, &ib_crsr); while (getline(std::cin, line)) { std::vector<std::string> strs; boost::split(strs, line, boost::is_any_of(" ") ); ib_tpl_t tpl = ib_clust_read_tuple_create(ib_crsr); ib_tuple_write_u32(tpl, 0, atoi(strs[0].c_str())); ib_tuple_write_u32(tpl, 1, atoi(strs[1].c_str())); ib_tuple_write_u32(tpl, 2, atoi(strs[2].c_str())); ib_err_t err = ib_cursor_insert_row(ib_crsr, tpl); if (err != DB_SUCCESS) { std::cerr << "insert_row failed" << std::endl; } ib_tuple_delete(tpl); if (++i % 10000 == 0) { std::cout << i << std::endl; ib_cursor_close(ib_crsr); ib_trx_commit(ib_trx); ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); ib_cursor_open_table(full_table_name, ib_trx, &ib_crsr); } } ib_cursor_close(ib_crsr); ib_trx_commit(ib_trx); fin_db(); return 0; }
/********************************************************************* INSERT INTO T VALUE(RANDOM(TEXT), RANDOM(TEXT), 0); */ static ib_err_t insert_random_rows( /*===============*/ ib_crsr_t crsr) /*!< in, out: cursor to use for write */ { int i; ib_err_t err; ib_tpl_t tpl = NULL; char* ptr = malloc(8192); tpl = ib_clust_read_tuple_create(crsr); assert(tpl != NULL); for (i = 0; i < 100; ++i) { int l; l = gen_rand_text(ptr, 128); err = ib_col_set_value(tpl, 0, ptr, l); assert(err == DB_SUCCESS); l = gen_rand_text(ptr, 8192); err = ib_col_set_value(tpl, 1, ptr, l); assert(err == DB_SUCCESS); err = ib_tuple_write_u32(tpl, 2, 0); assert(err == DB_SUCCESS); err = ib_cursor_insert_row(crsr, tpl); assert(err == DB_SUCCESS || err == DB_DUPLICATE_KEY); tpl = ib_tuple_clear(tpl); assert(tpl != NULL); } if (tpl != NULL) { ib_tuple_delete(tpl); } free(ptr); 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); }