/**
 * 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 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;
}
Ejemplo n.º 3
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);
}