Example #1
0
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;
}
/**
 * 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));
}
/**
 * Delete an item from the cache
 * @param key the key of the item to delete
 * @param nkey the length of the key
 * @return true if the item was deleted from the cache
 */
bool delete_item(const void* key, size_t nkey) {
  ib_trx_t transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);

  bool ret= do_delete_item(transaction, key, nkey);

  if (ret)
  {
    /* object found. commit transaction */
    ib_err_t error= ib_trx_commit(transaction);
    if (error != DB_SUCCESS)
    {
      fprintf(stderr, "Failed to delete key:\n\t%s\n",
              ib_strerror(error));
      ret= false;
    }
  }
  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",
              ib_strerror(error));
  }

  return ret;
}
Example #4
0
/*********************************************************************
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);
}
Example #5
0
/*********************************************************************
Open the secondary indexes on T(C1), T(C2), T(C3). */
static
ib_err_t
open_sec_index_1(
/*=============*/
	const char*	dbname,		/*!< in: database name */
	const char* 	name)		/*!< in: table name */
{
	ib_crsr_t	crsr;
	ib_trx_t	ib_trx;
	ib_err_t	err = DB_SUCCESS;
	char		index_name[IB_MAX_TABLE_NAME_LEN];
	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

	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);

	err = ib_cursor_open_table(table_name, ib_trx, &crsr);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, "c1");
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, "c1");
#endif
	err = open_sec_index(crsr, index_name);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, "c2");
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, "c2");
#endif
	err = open_sec_index(crsr, index_name);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, "c3");
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, "c3");
#endif
	err = open_sec_index(crsr, index_name);
	assert(err == DB_SUCCESS);

	err = ib_cursor_close(crsr);
	assert(err == DB_SUCCESS);

	err = ib_trx_commit(ib_trx);
	assert(err == DB_SUCCESS);

	return(err);
}
Example #6
0
int main(int argc, char* argv[])
{
	int		i;
	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);

	/* Create the tables. */
	for (i = 0; i < 10; i++) {
		err = create_table(DATABASE, TABLE, i);
		assert(err == DB_SUCCESS);
	}

	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
	assert(ib_trx != NULL);

	/* Open and close the cursor. */
	for (i = 0; i < 10; i++) {
		err = open_table(DATABASE, TABLE, i, ib_trx, &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 = ib_database_drop(DATABASE);
	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);
}
/**
 * Get an item from the engine
 * @param key the key to grab
 * @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) 
{
  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));

  return ret;
}
Example #8
0
/*********************************************************************
Tests creating a index with a name that InnoDB uses for temporary indexs
@return	DB_SUCCESS or error code */
static
ib_err_t
test_create_temp_index(
/*=============*/
	const char*     dbname,
	const char*	name,	/*!< in: table name */
	const char*	col_name)	/*!< in: column name */
{
	ib_err_t	err;
	ib_trx_t	ib_trx;
	ib_id_t		index_id = 0;
	ib_idx_sch_t	ib_idx_sch = NULL;
	char		index_name[IB_MAX_TABLE_NAME_LEN];
	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

	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);

	err = ib_schema_lock_exclusive(ib_trx);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%c%s_%s", TEMP_INDEX_PREFIX, table_name, col_name);
#else
	snprintf(index_name, sizeof(index_name), "%c%s_%s", TEMP_INDEX_PREFIX,
		table_name, col_name);
#endif
	err = ib_index_schema_create(
		ib_trx, index_name, table_name, &ib_idx_sch);

	assert(err == DB_INVALID_INPUT);

	if (ib_idx_sch != NULL) {
		ib_index_schema_delete(ib_idx_sch);
		ib_idx_sch = NULL;
	}

	if (err == DB_SUCCESS) {
		err = ib_trx_commit(ib_trx);
	} else {
		err = ib_trx_rollback(ib_trx);
	}
	assert(err == DB_SUCCESS);

	return(err);
}
Example #9
0
/*********************************************************************
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);
}
Example #10
0
/*********************************************************************
Create a secondary indexes on a table.
@return	DB_SUCCESS or error code */
static
ib_err_t
create_sec_index(
/*=============*/
	const char*	table_name,	/*!< in: table name */
	const char*	col_name,	/*!< in: column name */
	int		prefix_len)	/*!< in: prefix index length */

{
	ib_err_t	err;
	ib_trx_t	ib_trx;
	ib_id_t		index_id = 0;
	ib_idx_sch_t	ib_idx_sch = NULL;
	char		index_name[IB_MAX_TABLE_NAME_LEN];

	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);

	err = ib_schema_lock_exclusive(ib_trx);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, col_name);
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, col_name);
#endif
	err = ib_index_schema_create(
		ib_trx, index_name, table_name, &ib_idx_sch);

	assert(err == DB_SUCCESS);

	err = ib_index_schema_add_col(ib_idx_sch, col_name, prefix_len);
	assert(err == DB_SUCCESS);

	err = ib_index_create(ib_idx_sch, &index_id);

	if (ib_idx_sch != NULL) {
		ib_index_schema_delete(ib_idx_sch);
		ib_idx_sch = NULL;
	}

	if (err == DB_SUCCESS) {
		err = ib_trx_commit(ib_trx);
	} else {
		err = ib_trx_rollback(ib_trx);
	}
	assert(err == DB_SUCCESS);

	return(err);
}
Example #11
0
static void do_get(void* arg)
{
    PortState* state = (PortState*)arg;

    // Unpack key from work buffer
    // table - 8 bytes
    // keysz - 1 byte
    // key   - variable
    ib_id_t table      ; UNPACK_INT(state->work_buffer, 0, &table);
    unsigned char keysz = UNPACK_BYTE(state->work_buffer, sizeof(table));
    char* key          = UNPACK_BLOB(state->work_buffer, sizeof(table)+1);

    ib_trx_t txn = ib_trx_begin(IB_TRX_REPEATABLE_READ);

    ib_crsr_t cursor;
    FAIL_ON_ERROR(ib_cursor_open_table_using_id(table, txn, &cursor),
                  ROLLBACK_RETURN(txn));

    ib_tpl_t key_tuple = ib_clust_search_tuple_create(cursor);
    ib_col_set_value(key_tuple, KEY_COL, key, keysz);

    int searchloc;
    ib_err_t error = ib_cursor_moveto(cursor, key_tuple, IB_CUR_GE, &searchloc);

    // Drop the key tuple -- cursor is at desired location
    ib_tuple_delete(key_tuple);

    // If we encountered an error, bail
    if (error != DB_SUCCESS && error != DB_RECORD_NOT_FOUND && error != DB_END_OF_INDEX)
    {
        ib_cursor_close(cursor);
        send_error_str(state, ib_strerror(error));
        ROLLBACK_RETURN(txn);
    }

    // Found it, read the value and send back to caller
    if (searchloc == 0)
    {
        ib_tpl_t tuple = ib_clust_read_tuple_create(cursor);

        // TODO: May need better error handling here
        FAIL_ON_ERROR(ib_cursor_read_row(cursor, tuple),
                      {
                          ib_tuple_delete(tuple);
                          ib_cursor_close(cursor);
                          ROLLBACK_RETURN(txn);
                      });
Example #12
0
int main(int argc, char *argv[])
{
  if (!init_db()) { exit(1); }

  ib_crsr_t crsr, index_crsr;
  ib_tpl_t tpl;
  ib_err_t err;
  char *l6_orderkey_rle = "l5_customer_key_rle";
  char cstore_l6_orderkey_rle[64];
  sprintf(cstore_l6_orderkey_rle, "%s/%s", db_name, l6_orderkey_rle);
  ib_trx_t ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
  err = ib_cursor_open_table(cstore_l6_orderkey_rle, ib_trx, &crsr);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l6_orderkey_rle << std::endl; }
  err = ib_cursor_open_index_using_name(crsr, "SECONDARY_KEY", &index_crsr);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << std::endl; }

  ib_cursor_set_cluster_access(index_crsr);
  ib_tpl_t key = ib_sec_search_tuple_create(index_crsr);
  ib_tuple_write_u32(key, 0, 330575);
  int res;
  err = ib_cursor_moveto(index_crsr, key, IB_CUR_GE, &res);
  std::cout << "res: " << res << std::endl;
  assert(err == DB_SUCCESS || err == DB_END_OF_INDEX || err == DB_RECORD_NOT_FOUND);
  tpl = ib_clust_read_tuple_create(crsr);
  while (err == DB_SUCCESS) {
    err = ib_cursor_read_row(index_crsr, tpl);
    if (err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX) {
      std::cerr << "not found" << std::endl;
    }
    ib_u32_t orderkey, pos, freq;
    ib_tuple_read_u32(tpl, 0, &orderkey);
    ib_tuple_read_u32(tpl, 1, &pos);
    ib_tuple_read_u32(tpl, 2, &freq);
    std::cout << "orderkey: " << orderkey << ", pos: " << pos << ", freq: " << freq << std::endl;
    err = ib_cursor_next(index_crsr);
    tpl = ib_tuple_clear(tpl);
    break;
  }
  ib_cursor_close(index_crsr); 
  ib_cursor_close(crsr); 
  ib_trx_commit(ib_trx);

  fin_db();

  return 0;
}
Example #13
0
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);
}
/**
 * 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;
}
Example #15
0
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;
}
/**
 * Store an item in the databse
 *
 * @param item the item to store
 */
void put_item(struct item* item) 
{
  ib_trx_t transaction= ib_trx_begin(IB_TRX_SERIALIZABLE);
  if (do_put_item(transaction, item)) 
  {
    ib_err_t error= ib_trx_commit(transaction);
    if (error != DB_SUCCESS) 
    {
      fprintf(stderr, "Failed to store key:\n\t%s\n",
              ib_strerror(error));
    }
  } 
  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",
              ib_strerror(error));
  }
}
Example #17
0
void q3(void)
{
  char *l1_shipdate_rle = "l1_shipdate_rle";
  char *l1_extendedprice_uncompressed = "l1_extendedprice_uncompressed";
  char *l3_orderdate_rle = "l3_orderdate_rle";
  char *l3_custkey_uncompressed = "l3_custkey_uncompressed";
  char *l3_orderkey_uncompressed = "l3_orderkey_uncompressed";
  char *l5_customer_key_rle = "l5_customer_key_rle";
  char *l5_nationkey_uncompressed = "l5_nationkey_uncompressed";
  char *l6_orderkey_rle = "l6_orderkey_rle";
  char *l6_extendedprice_uncompressed = "l6_extendedprice_uncompressed";
  char *l6_shipdate_uncompressed = "l6_shipdate_uncompressed";
  char *ji_l3_l1 = "ji_l3-l1";

  char cstore_l1_shipdate_rle[64];
  char cstore_l1_extendedprice_uncompressed[64];
  char cstore_l3_orderdate_rle[64];
  char cstore_l3_custkey_uncompressed[64];
  char cstore_l3_orderkey_uncompressed[64];
  char cstore_l5_customer_key_rle[64];
  char cstore_l5_nationkey_uncompressed[64];
  char cstore_l6_orderkey_rle[64];
  char cstore_l6_extendedprice_uncompressed[64];
  char cstore_l6_shipdate_uncompressed[64];
  char cstore_ji_l3_l1[64];
  sprintf(cstore_l1_shipdate_rle, "%s/%s", db_name, l1_shipdate_rle);
  sprintf(cstore_l1_extendedprice_uncompressed, "%s/%s", db_name, l1_extendedprice_uncompressed);
  sprintf(cstore_l3_orderdate_rle, "%s/%s", db_name, l3_orderdate_rle);
  sprintf(cstore_l3_custkey_uncompressed, "%s/%s", db_name, l3_custkey_uncompressed);
  sprintf(cstore_l3_orderkey_uncompressed, "%s/%s", db_name, l3_orderkey_uncompressed);
  sprintf(cstore_l5_customer_key_rle, "%s/%s", db_name, l5_customer_key_rle);
  sprintf(cstore_l5_nationkey_uncompressed, "%s/%s", db_name, l5_nationkey_uncompressed);
  sprintf(cstore_l6_orderkey_rle, "%s/%s", db_name, l6_orderkey_rle);
  sprintf(cstore_l6_extendedprice_uncompressed, "%s/%s", db_name, l6_extendedprice_uncompressed);
  sprintf(cstore_l6_shipdate_uncompressed, "%s/%s", db_name, l6_shipdate_uncompressed);
  sprintf(cstore_ji_l3_l1, "%s/%s", db_name, ji_l3_l1);

  double t0 = gettimeofday_sec();
  ib_err_t err;
  //ib_crsr_t crsr_l1_s, crsr_l1_e, crsr_l3_o, crsr_l3_c, crsr_l5_c, crsr_l5_n, crsr_ji;

  ib_trx_t ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
  err = ib_cursor_open_table(cstore_l1_shipdate_rle, ib_trx, &crsr_l1_s);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l1_shipdate_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l1_extendedprice_uncompressed, ib_trx, &crsr_l1_e);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l1_extendedprice_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l3_orderdate_rle, ib_trx, &crsr_l3_o);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l3_orderdate_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l3_custkey_uncompressed, ib_trx, &crsr_l3_c);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l3_custkey_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l3_orderkey_uncompressed, ib_trx, &crsr_l3_ok);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l3_orderkey_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l5_customer_key_rle, ib_trx, &crsr_l5_c);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l5_customer_key_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l5_nationkey_uncompressed, ib_trx, &crsr_l5_n);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l5_nationkey_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l6_orderkey_rle, ib_trx, &crsr_l6_o);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l6_orderkey_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l6_extendedprice_uncompressed, ib_trx, &crsr_l6_e);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l6_extendedprice_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l6_shipdate_uncompressed, ib_trx, &crsr_l6_s);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l6_shipdate_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_ji_l3_l1, ib_trx, &crsr_ji);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_ji_l3_l1 << std::endl; }

  tpl_l1_s = ib_clust_read_tuple_create(crsr_l1_s);
  tpl_l1_e = ib_clust_read_tuple_create(crsr_l1_e);
  tpl_l3_o = ib_clust_read_tuple_create(crsr_l3_o);
  tpl_l3_c = ib_clust_read_tuple_create(crsr_l3_c);
  tpl_l3_ok = ib_clust_read_tuple_create(crsr_l3_ok);
  tpl_l5_c = ib_clust_read_tuple_create(crsr_l5_c);
  tpl_l5_n = ib_clust_read_tuple_create(crsr_l5_n);
  tpl_l6_o = ib_clust_read_tuple_create(crsr_l6_o);
  tpl_l6_e = ib_clust_read_tuple_create(crsr_l6_e);
  tpl_l6_s = ib_clust_read_tuple_create(crsr_l6_s);
  tpl_ji = ib_clust_read_tuple_create(crsr_ji);

  ib_tpl_t key = ib_clust_search_tuple_create(crsr_l3_o);
  ib_tuple_write_u32(key, 0, pred_o_orderdate);
  int res;
  // move cursor
  //err = ib_cursor_moveto(l3_o, key, IB_CUR_L, &res);
  err = ib_cursor_first(crsr_l3_o);
  ib_tuple_delete(key);
  if (err == DB_SUCCESS) {
    //std::cout << "success" << std::endl;
  } else {
    std::cout << "failed" << std::endl;
    ib_cursor_close(crsr_l3_o); 
    return;
  }

  while (err == DB_SUCCESS) {
    err = ib_cursor_read_row(crsr_l3_o, tpl_l3_o);

    /* Possible handle locking and timeout errors too in multi-threaded applications. */
    if (err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX) {
      break;
    }
    ib_u32_t orderdate, pos, freq;
    ib_tuple_read_u32(tpl_l3_o, 0, &orderdate);
    if (orderdate >= pred_o_orderdate) { break; }
    ib_tuple_read_u32(tpl_l3_o, 1, &pos);
    ib_tuple_read_u32(tpl_l3_o, 2, &freq);

    double t1 = gettimeofday_sec();
    // get L3 o_custkey from position filtering
    std::vector<im_val_t> pos_custkey_array = get_l3_custkeys_from_sequences(pos, freq);
    std::sort(pos_custkey_array.begin(), pos_custkey_array.end(), val_asc);
    /*
    for (int i = 0; i < pos_custkey_array.size(); ++i) {
      std::cout << pos_custkey_array[i].pos << ": " << pos_custkey_array[i].val << std::endl;
    }
    */
    double t2 = gettimeofday_sec();
    std::cout << "orderdate: " << pred_o_orderdate << " get_l3_custkeys_from_sequences: " << t2 - t1 << std::endl;

    // join with L5 and apply c_nationkey predicate
    //std::vector<uint32_t> pos_array = apply_l5_predicate(pos_custkey_array);
    std::vector<uint32_t> pos_array = apply_l5_predicate_scan(pos_custkey_array);
    /*
    for (int i = 0; i < pos_array.size(); ++i) {
      std::cout << "pos: " << pos_array[i] << std::endl;
    }
    */
    double t3 = gettimeofday_sec();
    std::cout << "orderdate: " << pred_o_orderdate << " apply_l5_predicate: " << t3 - t2 << std::endl;

    // get L3 o_orderkey from position filtring
    std::vector<im_val_t> pos_orderkey_array = get_l3_orderkeys_from_positions(pos_array);
    /*
    for (int i = 0; i < pos_orderkey_array.size(); ++i) {
      std::cout << pos_orderkey_array[i].pos << ": " << pos_orderkey_array[i].val << std::endl;
    }
    */
    double t4 = gettimeofday_sec();
    std::cout << "orderdate: " << pred_o_orderdate << " get_l3_orderkeys_from_positions: " << t4 - t3 << std::endl;
    
    // join with L1 apply L1 l_shipdate predicate
    // TODO: we don't need orderkey if we have L3->L1 join-index (we only need positions)
    if (use_ji) {
      std::vector<im_agg_t> extendedprice_array = apply_l1_predicate_via_ji(pos_orderkey_array);
      double t5 = gettimeofday_sec();
      std::cout << "orderdate: " << pred_o_orderdate << " apply_l1_predicate(JI): " << t5 - t4 << std::endl;
    } else {
      std::sort(pos_orderkey_array.begin(), pos_orderkey_array.end(), val_asc);
      std::vector<im_agg_t> extendedprice_array = apply_l6_predicate(pos_orderkey_array);
      double t5 = gettimeofday_sec();
      std::cout << "orderdate: " << pred_o_orderdate << " apply_l6_predicate: " << t5 - t4 << std::endl;
    }

    /*
    for (int i = 0; i < extendedprice_array.size(); ++i) {
      std::cout << "orderdate: " << orderdate << ", orderkey: " << pos_orderkey_array[i].val << ", SUM(extendedprice): " << extendedprice_array[i].agg << std::endl;
    }
    */

    // here we have L3.o_orderdate, L1.l_extendedprice
    // aggratation if needed (omitted)

    err = ib_cursor_next(crsr_l3_o);

    /* Possible handle locking and timeout errors too
    *  in multi-threaded applications. */
    if (err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX) {
      break;
    }
    tpl_l3_o = ib_tuple_clear(tpl_l3_o);
  }
  ib_tuple_delete(tpl_l3_o);
  ib_cursor_close(crsr_l1_s);
  ib_cursor_close(crsr_l1_e);
  ib_cursor_close(crsr_l3_o); 
  ib_cursor_close(crsr_l3_c); 
  ib_cursor_close(crsr_l3_ok); 
  ib_cursor_close(crsr_l5_c); 
  ib_cursor_close(crsr_l5_n); 
  ib_cursor_close(crsr_l6_o); 
  ib_cursor_close(crsr_l6_e); 
  ib_cursor_close(crsr_l6_s); 
  ib_cursor_close(crsr_ji); 
  ib_trx_commit(ib_trx);
  double t6 = gettimeofday_sec();
  std::cout << "orderdate: " << pred_o_orderdate << " total:" << t6-t0 << std::endl;
}
Example #18
0
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);
}
Example #19
0
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);
}
Example #20
0
/*********************************************************************
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);
}
Example #21
0
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);
}
Example #22
0
/*********************************************************************
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);
}
Example #23
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);
}
Example #24
0
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]));
}
Example #25
0
int main(int argc,char **argv){
    if(argc != 2){
        printf("give me a database\table name\n");
        return 1;
    }
    char *dtname=argv[1];
    
    ib_err_t err;
    err=ib_init();
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    
    err = ib_cfg_set_int("log_buffer_size", 8*1024*1024);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err = ib_cfg_set_int("force_recovery", 1);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err = ib_cfg_set_int("log_file_size", 128*1024*1024);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err = ib_cfg_set_int("log_files_in_group", 3);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err = ib_cfg_set_text("log_group_home_dir", "./");
    //err = ib_cfg_set_text("log_group_home_dir", "/var/lib/mysql/");
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err = ib_cfg_set_text("data_home_dir", "./");
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err = ib_cfg_set_text("data_file_path", "ibdata1:500M:autoextend");
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err = ib_cfg_set_bool_on("file_per_table");
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }

    err=ib_startup("Antelope");
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }

    ib_trx_t trx;
    ib_crsr_t crsr;
    trx=ib_trx_begin(IB_TRX_REPEATABLE_READ);
    assert(trx != NULL);
    err=ib_cursor_open_table(dtname,trx,&crsr);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    ib_tpl_t tpl;
    tpl=ib_clust_read_tuple_create(crsr);
    assert(tpl != NULL);
    
    err=ib_cursor_first(crsr);
    while(err == DB_SUCCESS){
        err=ib_cursor_read_row(crsr,tpl);
        print_tuple(stdout,tpl);
        err=ib_cursor_next(crsr);
        tpl=ib_tuple_clear(tpl);
    }
    ib_tuple_delete(tpl); 
    err=ib_cursor_close(crsr);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    err=ib_trx_commit(trx);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    
    err=ib_shutdown(IB_SHUTDOWN_NORMAL);
    if(err != DB_SUCCESS){
        puts(ib_strerror(err));
        return err;
    }
    return 0;
}
Example #26
0
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);
}