/********************************************************************* CREATE TABLE T(C1 INT, C2 VARCHAR(10), C3 BLOB); */ static ib_err_t create_table( /*=========*/ const char* dbname, /*!< in: database name */ const char* name) /*!< in: table name */ { ib_trx_t ib_trx; ib_id_t table_id = 0; ib_err_t err = DB_SUCCESS; ib_tbl_sch_t ib_tbl_sch = NULL; char table_name[IB_MAX_TABLE_NAME_LEN]; #ifdef __WIN__ sprintf(table_name, "%s/%s", dbname, name); #else snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name); #endif /* Pass a table page size of 0, ie., use default page size. */ err = ib_table_schema_create( table_name, &ib_tbl_sch, IB_TBL_COMPACT, 0); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "c1", IB_INT, IB_COL_NONE, 0, sizeof(ib_i32_t)); assert(err == DB_SUCCESS); err = ib_tbl_sch_add_varchar_col(ib_tbl_sch, "c2", 10); assert(err == DB_SUCCESS); err = ib_tbl_sch_add_blob_col(ib_tbl_sch, "c3"); assert(err == DB_SUCCESS); /* create table */ ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); err = ib_schema_lock_exclusive(ib_trx); assert(err == DB_SUCCESS); err = ib_table_create(ib_trx, ib_tbl_sch, &table_id); if (err == DB_SUCCESS) { err = ib_trx_commit(ib_trx); } else { fprintf(stderr, "Table: %s create failed: %s\n", table_name, ib_strerror(err)); err = ib_trx_rollback(ib_trx); } assert(err == DB_SUCCESS); if (ib_tbl_sch != NULL) { ib_table_schema_delete(ib_tbl_sch); } return(err); }
/********************************************************************* CREATE TABLE D.Tn(C1 INT); */ static ib_err_t create_table( /*=========*/ const char* dbname, /*!< in: database name */ const char* name, /*!< in: table name */ int n) /*!< in: table suffix */ { ib_trx_t ib_trx; ib_id_t table_id = 0; ib_err_t err = DB_SUCCESS; ib_tbl_sch_t ib_tbl_sch = NULL; char table_name[IB_MAX_TABLE_NAME_LEN]; #ifdef __WIN__ sprintf(table_name, "%s/%s%d", dbname, name, n); #else snprintf(table_name, sizeof(table_name), "%s/%s%d", dbname, name, n); #endif /* Pass a table page size of 0, ie., use default page size. */ err = ib_table_schema_create( table_name, &ib_tbl_sch, IB_TBL_COMPACT, 0); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "c1", IB_INT, IB_COL_UNSIGNED, 0, sizeof(ib_u32_t)); assert(err == DB_SUCCESS); /* create table */ ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); err = ib_schema_lock_exclusive(ib_trx); assert(err == DB_SUCCESS); err = ib_table_create(ib_trx, ib_tbl_sch, &table_id); assert(err == DB_SUCCESS); err = ib_trx_commit(ib_trx); assert(err == DB_SUCCESS); if (ib_tbl_sch != NULL) { ib_table_schema_delete(ib_tbl_sch); } return(err); }
/** * Create the database schema. * @return true if the database schema was created without any problems * false otherwise. */ static bool create_schema(void) { ib_tbl_sch_t schema= NULL; ib_idx_sch_t dbindex= NULL; if (ib_database_create("memcached") != IB_TRUE) { fprintf(stderr, "Failed to create database\n"); return false; } ib_trx_t transaction= ib_trx_begin(IB_TRX_SERIALIZABLE); ib_id_t table_id; checked(ib_table_schema_create(tablename, &schema, IB_TBL_COMPACT, 0)); checked(ib_table_schema_add_col(schema, "key", IB_BLOB, IB_COL_NOT_NULL, 0, 32767)); checked(ib_table_schema_add_col(schema, "data", IB_BLOB, IB_COL_NONE, 0, 1024*1024)); checked(ib_table_schema_add_col(schema, "flags", IB_INT, IB_COL_UNSIGNED, 0, 4)); checked(ib_table_schema_add_col(schema, "cas", IB_INT, 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", &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)); ib_table_schema_delete(schema); return true; error_exit: /* @todo release resources! */ { 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)); } return false; }
bool create_schema(char *table_name) { ib_trx_t ib_trx; ib_id_t table_id = 0; ib_tbl_sch_t ib_tbl_sch = NULL; ib_idx_sch_t ib_idx_sch = NULL; ib_idx_sch_t ib_idx_sec = NULL; char full_table_name[256]; /* Pass a table page size of 0, ie., use default page size. */ ib_table_schema_create(table_name, &ib_tbl_sch, IB_TBL_COMPACT, 0); /* The fifth argument is currently not used. */ ib_table_schema_add_col(ib_tbl_sch, "k", IB_INT, IB_COL_UNSIGNED, 0, 4); ib_table_schema_add_col(ib_tbl_sch, "v1", IB_INT, IB_COL_UNSIGNED, 0, 4); ib_table_schema_add_col(ib_tbl_sch, "v2", IB_INT, IB_COL_UNSIGNED, 0, 4); /* Index schema handle is "owned" by the table schema handle in this * case and will be deleted when the table schema handle is deleted. */ ib_table_schema_add_index(ib_tbl_sch, "PRIMARY_KEY", &ib_idx_sch); /* Set prefix length to 0. */ ib_index_schema_add_col(ib_idx_sch, "v1", 0); // pk: pos(sk) ib_index_schema_set_clustered(ib_idx_sch); ib_table_schema_add_index(ib_tbl_sch, "SECONDARY_KEY", &ib_idx_sec); ib_index_schema_add_col(ib_idx_sec, "k", 0); // sk: val /* Create the transaction that will cover data dictionary update. */ ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); /* Lock the data dictionary in exclusive mode */ ib_schema_lock_exclusive(ib_trx); /* Create the actual table from the schema. The table id of the new * table will be returned in table_id. */ ib_table_create(ib_trx, ib_tbl_sch, &table_id); /* Commit the transaction */ ib_trx_commit(ib_trx); ib_table_schema_delete(ib_tbl_sch); return true; }
int main(int argc, char* argv[]) { ib_tbl_sch_t ib_tbl_sch = NULL; ib_id_t table_id; int valid_page_sizes[] = {0, 1, 2, 4, 8, 16}; int invalid_page_sizes[] = {3, 5, 6, 14, 17, 32, 128, 301}; size_t i; (void)argc; (void)argv; OK(ib_init()); test_configure(); OK(ib_startup("barracuda")); OK(ib_cfg_set("file_per_table", IB_TRUE)); OK(ib_database_create(DBNAME)); for (i = 0; i < sizeof(valid_page_sizes) / sizeof(valid_page_sizes[0]); i++) { ib_trx_t ib_trx; OK(ib_table_schema_create(TABLENAME, &ib_tbl_sch, IB_TBL_COMPRESSED, valid_page_sizes[i])); OK(ib_table_schema_add_col(ib_tbl_sch, "c1", IB_INT, IB_COL_UNSIGNED, 0 /* ignored */, sizeof(int))); ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); OK(ib_schema_lock_exclusive(ib_trx)); OK(ib_table_create(ib_trx, ib_tbl_sch, &table_id)); OK(ib_trx_commit(ib_trx)); ib_table_schema_delete(ib_tbl_sch); ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); OK(ib_schema_lock_exclusive(ib_trx)); OK(ib_table_drop(ib_trx, TABLENAME)); OK(ib_trx_commit(ib_trx)); } for (i = 0; i < sizeof(invalid_page_sizes) / sizeof(invalid_page_sizes[0]); i++) { ib_err_t ib_err; ib_err = ib_table_schema_create(TABLENAME, &ib_tbl_sch, IB_TBL_COMPRESSED, invalid_page_sizes[i]); if (ib_err == DB_SUCCESS) { fprintf(stderr, "Creating a compressed table with " "page size %d succeeded but should have " "failed", invalid_page_sizes[i]); exit(EXIT_FAILURE); } } /* ignore errors as there may be tables left over from other tests */ OK(ib_database_drop(DBNAME)); OK(ib_shutdown(IB_SHUTDOWN_NORMAL)); return(EXIT_SUCCESS); }
/********************************************************************* CREATE TABLE T( vchar VARCHAR(128), blob VARCHAR(n), count INT, PRIMARY KEY(vchar); */ static ib_err_t create_table( /*=========*/ const char* dbname, /*!< in: database name */ const char* name) /*!< in: table name */ { ib_trx_t ib_trx; ib_id_t table_id = 0; ib_err_t err = DB_SUCCESS; ib_tbl_sch_t ib_tbl_sch = NULL; ib_idx_sch_t ib_idx_sch = NULL; ib_tbl_fmt_t tbl_fmt = IB_TBL_COMPACT; char table_name[IB_MAX_TABLE_NAME_LEN]; #ifdef __WIN__ sprintf(table_name, "%s/%s", dbname, name); #else snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name); #endif if (page_size > 0) { tbl_fmt = IB_TBL_COMPRESSED; printf("Creating compressed table with page size %d\n", page_size); } err = ib_table_schema_create( table_name, &ib_tbl_sch, tbl_fmt, page_size); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "vchar", IB_VARCHAR, IB_COL_NONE, 0, 128); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "blob", IB_BLOB, IB_COL_NONE, 0, 0); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "count", IB_INT, IB_COL_UNSIGNED, 0, 4); assert(err == DB_SUCCESS); err = ib_table_schema_add_index(ib_tbl_sch, "PRIMARY", &ib_idx_sch); assert(err == DB_SUCCESS); /* Set prefix length to 0. */ err = ib_index_schema_add_col( ib_idx_sch, "vchar", 0); assert(err == DB_SUCCESS); err = ib_index_schema_set_clustered(ib_idx_sch); assert(err == DB_SUCCESS); err = ib_index_schema_set_unique(ib_idx_sch); assert(err == DB_SUCCESS); /* create table */ ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); err = ib_schema_lock_exclusive(ib_trx); assert(err == DB_SUCCESS); err = ib_table_create(ib_trx, ib_tbl_sch, &table_id); if (err != DB_SUCCESS) { fprintf(stderr, "Warning: table create failed: %s\n", ib_strerror(err)); err = ib_trx_rollback(ib_trx); } else { err = ib_trx_commit(ib_trx); } assert(err == DB_SUCCESS); if (ib_tbl_sch != NULL) { ib_table_schema_delete(ib_tbl_sch); } return(err); }
/********************************************************************* CREATE TABLE T(c1 INT, c2 VARCHAR(n), c3 BLOB, PK(c1)); */ static ib_err_t create_table( /*=========*/ const char* dbname, /*!< in: database name */ const char* name) /*!< in: table name */ { ib_trx_t ib_trx; ib_id_t table_id = 0; ib_err_t err = DB_SUCCESS; ib_tbl_sch_t ib_tbl_sch = NULL; ib_idx_sch_t ib_idx_sch = NULL; char table_name[IB_MAX_TABLE_NAME_LEN]; #ifdef __WIN__ sprintf(table_name, "%s/%s", dbname, name); #else snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name); #endif /* Pass a table page size of 0, ie., use default page size. */ err = ib_table_schema_create( table_name, &ib_tbl_sch, IB_TBL_COMPACT, 0); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "c1", IB_INT, IB_COL_NONE, 0, 4); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "c2", IB_VARCHAR, IB_COL_NONE, 0, C2_MAX_LEN); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "c3", IB_BLOB, IB_COL_NONE, 0, 0); assert(err == DB_SUCCESS); err = ib_table_schema_add_index(ib_tbl_sch, "c1", &ib_idx_sch); assert(err == DB_SUCCESS); /* Set prefix length to 0. */ err = ib_index_schema_add_col( ib_idx_sch, "c1", 0); assert(err == DB_SUCCESS); err = ib_index_schema_set_clustered(ib_idx_sch); assert(err == DB_SUCCESS); /* create table */ ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ); err = ib_schema_lock_exclusive(ib_trx); assert(err == DB_SUCCESS); err = ib_table_create(ib_trx, ib_tbl_sch, &table_id); assert(err == DB_SUCCESS || err == DB_TABLE_IS_BEING_USED); err = ib_trx_commit(ib_trx); assert(err == DB_SUCCESS); if (ib_tbl_sch != NULL) { ib_table_schema_delete(ib_tbl_sch); } return(err); }
static void do_init_table(void* arg) { PortState* state = (PortState*)arg; unsigned char fmt_code = UNPACK_BYTE(state->work_buffer, 0); // Unpack the table name (pre-formatted to be Database/TableName) char* table = UNPACK_STRING(state->work_buffer, 1); ib_id_t table_id; ib_tbl_fmt_t table_fmt; switch(fmt_code) { case FORMAT_REDUNDANT: table_fmt = IB_TBL_REDUNDANT; break; case FORMAT_COMPACT: table_fmt = IB_TBL_COMPACT; break; case FORMAT_DYNAMIC: table_fmt = IB_TBL_DYNAMIC; break; case FORMAT_COMPRESSED: table_fmt = IB_TBL_COMPRESSED; break; default: send_error_atom(state, "bad_format"); return; } // If the table doesn't exist, create it if (ib_table_get_id(table, &table_id) != DB_SUCCESS) { // Start a txn for schema access and be sure to make it serializable ib_trx_t txn = ib_trx_begin(IB_TRX_SERIALIZABLE); ib_schema_lock_exclusive(txn); // Create the table schema ib_tbl_sch_t schema; ib_table_schema_create(table, &schema, table_fmt, 0); ib_table_schema_add_col(schema, "key", IB_VARBINARY, IB_COL_NONE, 0, 255); ib_table_schema_add_col(schema, "value", IB_BLOB, IB_COL_NONE, 0, 0); // Create primary index on key ib_idx_sch_t index; ib_table_schema_add_index(schema, "PRIMARY_KEY", &index); ib_index_schema_add_col(index, "key", 0); ib_index_schema_set_clustered(index); // Create the actual table ib_err_t rc = ib_table_create(txn, schema, &table_id); // Release the schema -- doesn't matter if table was created or not at this point ib_schema_unlock(txn); if (rc == DB_SUCCESS) { // Commit changes to schema (if any) ib_trx_commit(txn); } else { // Failed to create table -- rollback and exit ib_trx_rollback(txn); send_error_str(state, ib_strerror(rc)); return; } } // Guaranteed at this point to have a valid table_id ErlDrvTermData response[] = { ERL_DRV_ATOM, driver_mk_atom("innostore_ok"), ERL_DRV_BUF2BINARY, (ErlDrvTermData)&table_id, (ErlDrvUInt)sizeof(table_id), ERL_DRV_TUPLE, 2}; driver_send_term(state->port, state->port_owner, response, sizeof(response) / sizeof(response[0])); }
/********************************************************************** CREATE TABLE t2... @return DB_SUCCESS or error code */ static ib_err_t create_t2( /*======*/ void* arg) /*!< in: arguments for callback */ { ib_trx_t ib_trx; ib_id_t table_id = 0; ib_err_t err = DB_SUCCESS; ib_err_t err2 = DB_SUCCESS; ib_tbl_sch_t ib_tbl_sch = NULL; ib_idx_sch_t ib_idx_sch = NULL; char table_name[IB_MAX_TABLE_NAME_LEN]; cb_args_t* cb_arg = (cb_args_t *)arg; tbl_class_t* tbl = cb_arg->tbl; snprintf(table_name, sizeof(table_name), "%s/%s", tbl->db_name, tbl->name); /* Pass a table page size of 0, ie., use default page size. */ err = ib_table_schema_create( table_name, &ib_tbl_sch, tbl->format, tbl->page_size); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "c1", IB_INT, IB_COL_UNSIGNED, 0, 4); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "score", IB_INT, IB_COL_UNSIGNED, 0, 4); assert(err == DB_SUCCESS); /* These two columns are where we put the value of run_number when doing INSERT or UPDATE */ err = ib_table_schema_add_col( ib_tbl_sch, "ins_run", IB_INT, IB_COL_UNSIGNED, 0, 4); assert(err == DB_SUCCESS); err = ib_table_schema_add_col( ib_tbl_sch, "upd_run", IB_INT, IB_COL_UNSIGNED, 0, 4); assert(err == DB_SUCCESS); err = ib_table_schema_add_index(ib_tbl_sch, "PK_index", &ib_idx_sch); assert(err == DB_SUCCESS); /* Set prefix length to 0. */ err = ib_index_schema_add_col( ib_idx_sch, "c1", 0); assert(err == DB_SUCCESS); err = ib_index_schema_set_clustered(ib_idx_sch); assert(err == DB_SUCCESS); /* create table */ ib_trx = ib_trx_begin(cb_arg->isolation_level); err = ib_schema_lock_exclusive(ib_trx); assert(err == DB_SUCCESS); err = ib_table_create(ib_trx, ib_tbl_sch, &table_id); err2 = ib_trx_commit(ib_trx); assert(err2 == DB_SUCCESS); if (ib_tbl_sch != NULL) { ib_table_schema_delete(ib_tbl_sch); } update_err_stats(cb_arg->err_st, err); return(err); }