Ejemplo n.º 1
0
std::vector<uint32_t>
apply_l5_predicate_scan(std::vector<im_val_t> pos_custkey_array)
{
  ib_err_t err;
  std::vector<uint32_t> pos_array;
  ib_crsr_t index_crsr;
  err = ib_cursor_open_index_using_name(crsr_l5_c, "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_l5_c = ib_sec_search_tuple_create(index_crsr);
  ib_tpl_t key_l5_n = ib_clust_search_tuple_create(crsr_l5_n);
  size_t size = pos_custkey_array.size();
  int res;
  // TODO: first fetches all l5_pos for custkeys
  std::vector<uint32_t> l5_pos_array;
  for (int i = 0; i < size; ++i) {
    // custkey
    ib_tuple_write_u32(key_l5_c, 0, pos_custkey_array[i].val);
    err = ib_cursor_moveto(index_crsr, key_l5_c, IB_CUR_GE, &res);
    if (err != DB_SUCCESS) {
      std::cout << "failed moving cursor for l5_c" << std::endl;
    }
    err = ib_cursor_read_row(index_crsr, tpl_l5_c);
    if (err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX) {
    }
    ib_u32_t l5_pos;
    ib_tuple_read_u32(tpl_l5_c, 1, &l5_pos);
    l5_pos_array.push_back(l5_pos);
    tpl_l5_c = ib_tuple_clear(tpl_l5_c);
  }
  for (int i = 0; i < size; ++i) {
    ib_tuple_write_u32(key_l5_n, 0, l5_pos_array[i]);
    err = ib_cursor_moveto(crsr_l5_n, key_l5_n, IB_CUR_GE, &res);
    if (err != DB_SUCCESS) {
      std::cout << "failed moving cursor for l5_c" << std::endl;
    }
    if (res == 0) {
      err = ib_cursor_read_row(crsr_l5_n, tpl_l5_n);
      ib_u32_t c_nationkey;
      ib_tuple_read_u32(tpl_l5_n, 1, &c_nationkey);
      if (c_nationkey == pred_c_nationkey) {
        pos_array.push_back(pos_custkey_array[i].pos);
      }
    }
    tpl_l5_n = ib_tuple_clear(tpl_l5_n);
  }
  ib_tuple_delete(key_l5_c);
  ib_tuple_delete(key_l5_n);
  ib_cursor_close(index_crsr);
  return pos_array;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
/*********************************************************************
UPDATE T SET score = score + 100 WHERE first = 'a'; */
static
ib_err_t
update_random_rows(
/*===============*/
	ib_crsr_t	crsr)
{
	ib_err_t	err;
	int		l;
	char*		ptr;
	int		res = ~0;
	ib_tpl_t	key_tpl;
	ib_tpl_t	old_tpl = NULL;
	ib_tpl_t	new_tpl = NULL;

	/* Create a tuple for searching an index. */
	key_tpl = ib_sec_search_tuple_create(crsr);
	assert(key_tpl != NULL);

	ptr = (char*) malloc(8192);

	l = gen_rand_text(ptr, 128);
	/* Set the value to look for. */
	err = ib_col_set_value(key_tpl, 0, ptr, l);
	assert(err == DB_SUCCESS);

	/* Search for the key using the cluster index (PK) */
	err = ib_cursor_moveto(crsr, key_tpl, IB_CUR_GE, &res);
	assert(err == DB_SUCCESS
	       || err == DB_END_OF_INDEX
	       || err == DB_RECORD_NOT_FOUND);

	if (key_tpl != NULL) {
		ib_tuple_delete(key_tpl);
	}

	/* Match found */
	if (res == 0) {
		ib_u32_t	score;
		const char*	first;
		ib_ulint_t	data_len;
		ib_ulint_t	first_len;
		ib_col_meta_t	col_meta;

		/* 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);

		err = ib_cursor_read_row(crsr, old_tpl);
		assert(err == DB_SUCCESS);

		/* Get the first column value. */
		first = ib_col_get_value(old_tpl, 0);
		first_len = ib_col_get_meta(old_tpl, 0, &col_meta);

		/* There are no SQL_NULL values in our test data. */
		assert(first != NULL);

		/* 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, 2, &col_meta);
		assert(data_len != IB_SQL_NULL);
		err = ib_tuple_read_u32(old_tpl, 2, &score);
		assert(err == DB_SUCCESS);
		++score;

		/* Get the new text to insert. */
		l = gen_rand_text(ptr, 128);
		/* Set the new key value in the new tuple. */
		err = ib_col_set_value(new_tpl, 0, ptr, l);
		assert(err == DB_SUCCESS);
		first_len = ib_col_get_len(new_tpl, 0);
		assert(first_len == IB_SQL_NULL || first_len <= 128);

		/* Get the new text to insert. */
		l = gen_rand_text(ptr, 8192);
		/* Set the blob value in the new tuple. */
		err = ib_col_set_value(new_tpl, 1, ptr, l);
		assert(err == DB_SUCCESS);

		/* Set the updated score value in the new tuple. */
		err = ib_tuple_write_u32(new_tpl, 2, score);
		assert(err == DB_SUCCESS);

		err = ib_cursor_update_row(crsr, old_tpl, new_tpl);
		assert(err == DB_SUCCESS
		       || err == DB_DUPLICATE_KEY);
	}

	if (old_tpl != NULL) {
		ib_tuple_delete(old_tpl);
	}
	if (new_tpl != NULL) {
		ib_tuple_delete(new_tpl);
	}

	free(ptr);

	return(err);
}
Ejemplo n.º 4
0
std::vector<im_agg_t>
apply_l6_predicate(std::vector<im_val_t> pos_orderkey_array)
{
  ib_err_t err;
  std::vector<im_agg_t> extendedprice_array;
  ib_crsr_t index_crsr;
  err = ib_cursor_open_index_using_name(crsr_l6_o, "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_l6_o = ib_sec_search_tuple_create(index_crsr);
  ib_tpl_t key_l6_s = ib_clust_search_tuple_create(crsr_l6_s);
  ib_tpl_t key_l6_e = ib_clust_search_tuple_create(crsr_l6_e);
  size_t size = pos_orderkey_array.size();
  int res;
  for (int i = 0; i < size; ++i) {
    uint32_t sum_extendedprice = 0;
    ib_tuple_write_u32(key_l6_o, 0, pos_orderkey_array[i].val);
    err = ib_cursor_moveto(index_crsr, key_l6_o, IB_CUR_GE, &res);
    if (err != DB_SUCCESS) {
      std::cout << "failed moving cursor for index_crsr" << std::endl;
    }
    err = ib_cursor_read_row(index_crsr, tpl_l6_o);
    if (err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX) {
    }
    ib_u32_t orderkey, pos, freq;
    ib_tuple_read_u32(tpl_l6_o, 0, &orderkey);
    ib_tuple_read_u32(tpl_l6_o, 1, &pos);
    ib_tuple_read_u32(tpl_l6_o, 2, &freq);
    //std::cout << "orderkey: " << orderkey << ", pos: " << pos << ", freq: " << freq << std::endl;
    if (orderkey != pos_orderkey_array[i].val) {
      break;
    }
    tpl_l6_o = ib_tuple_clear(tpl_l6_o);

    for (int i = pos; i < pos + freq; ++i) {
      ib_tuple_write_u32(key_l6_s, 0, i);
      err = ib_cursor_moveto(crsr_l6_s, key_l6_s, IB_CUR_GE, &res);
      if (err != DB_SUCCESS) {
        std::cout << "failed moving cursor for l6_s" << std::endl;
      }
      err = ib_cursor_read_row(crsr_l6_s, tpl_l6_s);
      ib_u32_t shipdate;
      ib_tuple_read_u32(tpl_l6_s, 1, &shipdate);
      //std::cout << "shipdate: " << shipdate << std::endl;
      if (shipdate > pred_l_shipdate) {
        ib_tuple_write_u32(key_l6_e, 0, i);
        err = ib_cursor_moveto(crsr_l6_e, key_l6_e, IB_CUR_GE, &res);
        if (err != DB_SUCCESS) {
          std::cout << "failed moving cursor for l6_e" << std::endl;
        }
        err = ib_cursor_read_row(crsr_l6_e, tpl_l6_e);
        ib_u32_t extendedprice;
        ib_tuple_read_u32(tpl_l6_e, 1, &extendedprice);
        sum_extendedprice += extendedprice;
        tpl_l6_e = ib_tuple_clear(tpl_l6_e);
      }
      tpl_l6_s = ib_tuple_clear(tpl_l6_s);
    }
    im_agg_t im_agg = {sum_extendedprice};
    extendedprice_array.push_back(im_agg);
  }
  ib_tuple_delete(key_l6_o);
  ib_tuple_delete(key_l6_e);
  ib_tuple_delete(key_l6_s);
  ib_cursor_close(index_crsr);

  return extendedprice_array;
}
Ejemplo n.º 5
0
/*********************************************************************
UPDATE T SET c1 = RANDOM(string), c3 = MOD(c3 + 1, 10)
	WHERE c3 = MOD(RANDOM(INT), 10); */
static
ib_err_t
update_random_rows(
    /*===============*/
    ib_crsr_t	crsr)
{
    ib_i32_t	c3;
    ib_err_t	err;
    ib_i32_t	key;
    int		res = ~0;
    ib_crsr_t	index_crsr;
    ib_tpl_t	sec_key_tpl;

    /* Open the secondary index. */
    err = ib_cursor_open_index_using_name(crsr, "c3", &index_crsr);
    assert(err == DB_SUCCESS);

    /* Set the record lock mode */
    err = ib_cursor_set_lock_mode(index_crsr, IB_LOCK_X);
    assert(err == DB_SUCCESS);

    /* Since we will be updating the clustered index record, set the
    need to access clustered index flag in the cursor. */
    ib_cursor_set_cluster_access(index_crsr);

    /* Create a tuple for searching the secondary index. */
    sec_key_tpl = ib_sec_search_tuple_create(index_crsr);
    assert(sec_key_tpl != NULL);

    /* Set the value to look for. */
#ifdef __WIN__
    key = rand() % 10;
#else
    key = random() % 10;
#endif
    err = ib_tuple_write_i32(sec_key_tpl, 0, key);
    assert(err == DB_SUCCESS);

    /* Search for the key using the cluster index (PK) */
    err = ib_cursor_moveto(index_crsr, sec_key_tpl, IB_CUR_GE, &res);
    assert(err == DB_SUCCESS
           || err == DB_END_OF_INDEX
           || err == DB_RECORD_NOT_FOUND);

    ib_tuple_delete(sec_key_tpl);

    /* Match found */
    if (res == 0) {
        int		l;
        char*		ptr;
        const char*	first;
        ib_ulint_t	data_len;
        ib_col_meta_t	col_meta;
        ib_ulint_t	first_len;
        ib_tpl_t	old_tpl = NULL;
        ib_tpl_t	new_tpl = NULL;

        /* 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);

        err = ib_cursor_read_row(index_crsr, old_tpl);
        assert(err == DB_SUCCESS);

        /* Get the first column value. */
        first = ib_col_get_value(old_tpl, 0);
        first_len = ib_col_get_meta(old_tpl, 0, &col_meta);

        /* There are no SQL_NULL values in our test data. */
        assert(first != NULL);

        /* Copy the old contents to the new tuple. */
        err = ib_tuple_copy(new_tpl, old_tpl);

        /* Update the c3 column in the new tuple. */
        data_len = ib_col_get_meta(old_tpl, 2, &col_meta);
        assert(data_len != IB_SQL_NULL);
        err = ib_tuple_read_i32(old_tpl, 2, &c3);
        assert(err == DB_SUCCESS);
        assert(c3 == key);
        c3 = (c3 + 1) % 10;

        ptr = (char*) malloc(8192);
        l = gen_rand_text(ptr, 128);

        /* Get the new text to insert. */
        l = gen_rand_text(ptr, 8192);
        /* Set the new key value in the new tuple. */
        err = ib_col_set_value(new_tpl, 0, ptr, l);
        assert(err == DB_SUCCESS);

        /* Get the new text to insert. */
        l = gen_rand_text(ptr, 8192);
        /* Set the c2 value in the new tuple. */
        err = ib_col_set_value(new_tpl, 1, ptr, l);
        assert(err == DB_SUCCESS);

        /* Set the updated c3 value in the new tuple. */
        err = ib_tuple_write_i32(new_tpl, 2, c3);
        assert(err == DB_SUCCESS);

        /* NOTE: We are using the secondary index cursor to update
        the record and not the cluster index cursor. */
        err = ib_cursor_update_row(index_crsr, old_tpl, new_tpl);
        assert(err == DB_SUCCESS || err == DB_DUPLICATE_KEY);

        /* 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);

        free(ptr);

        ib_tuple_delete(old_tpl);
        ib_tuple_delete(new_tpl);
    }

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

    return(err);
}
Ejemplo n.º 6
0
/**********************************************************************
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);
}
Ejemplo n.º 7
0
/**********************************************************************
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);
}