library/src/blockclass/BP-BlockClass.cc
// Check if a target exists (ERR_SUCCESS on exists)
Check if a target exists (ERR_SUCCESS on exists)
Check if a target exists ( ERR_SUCCESS on exists )
size_t Block::checkResultExists(char *href, size_t status_code)
{
	if(!this->mysqlSession)
		return ERR_FAILURE;
	BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION;
	if(this->disable_result_lookups_due_to_duplicate_key_contention_fix)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(!href)
	{
		this->AddErrorToStack((char*) "Empty href passed to checkResultExists().");
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	size_t result_index = 0;
	char * base_query = (char *) NULL;
	if(status_code == (size_t) -1)
	{
		base_query = "SELECT id FROM results where href = ? LIMIT 1";
	}
	else
	{
		base_query = "SELECT id FROM results where href = ? and status = ? LIMIT 1";
	}
	size_t query_size = 0;
	if (this->mysqlPrepareQuery(base_query) == ERR_FAILURE)
		this->AddErrorToStack((char*) "Cannot prepare query");
	this->mysqlAddParameter(MYSQL_TYPE_STRING, (char *) href, bpstrlen(href));
	if(status_code != (size_t) -1)
	{
		printf("\n Adding parameter here?");
		this->mysqlAddParameter(MYSQL_TYPE_LONG, (char *) &status_code, sizeof(status_code));
	}
	if (this->mysqlAddResult(MYSQL_TYPE_LONG, (char *) &result_index, sizeof(result_index)) == ERR_FAILURE)
		this->AddErrorToStack((char*) "Cannot add result");
	this->mysqlBindParameters();
	if (this->mysqlBindResults() == ERR_FAILURE)
		this->AddErrorToStack((char*) "Cannot bind results");
	if (this->mysqlExecuteStatement() == ERR_FAILURE)
	{
		this->AddErrorToStack((char *) "Failed to execute statement.");
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	this->mysqlStoreResults();
	this->mysqlFetchResult();
	if(this->mysql_results_count > 0)
	if(this->mysql_results[0].buffer)
	{
		size_t *record_id = (size_t *) this->mysql_results[0].buffer;
		result_index = *record_id;
	}
	this->mysqlDestroyStatement();
	if (result_index)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_SUCCESS);
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
}
size_t Block :: checkResultExists ( char * href , size_t status_code ) { if ( ! this -> mysqlSession ) return ERR_FAILURE ; BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION ; if ( this -> disable_result_lookups_due_to_duplicate_key_contention_fix ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( ! href ) { this -> AddErrorToStack ( ( char * ) " " ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } size_t result_index = 0 ; char * base_query = ( char * ) NULL ; if ( status_code == ( size_t ) -1 ) { base_query = " " ; } else { base_query = " " ; } size_t query_size = 0 ; if ( this -> mysqlPrepareQuery ( base_query ) == ERR_FAILURE ) this -> AddErrorToStack ( ( char * ) " " ) ; this -> mysqlAddParameter ( MYSQL_TYPE_STRING , ( char * ) href , bpstrlen ( href ) ) ; if ( status_code != ( size_t ) -1 ) { printf ( " \n " ) ; this -> mysqlAddParameter ( MYSQL_TYPE_LONG , ( char * ) & status_code , sizeof ( status_code ) ) ; } if ( this -> mysqlAddResult ( MYSQL_TYPE_LONG , ( char * ) & result_index , sizeof ( result_index ) ) == ERR_FAILURE ) this -> AddErrorToStack ( ( char * ) " " ) ; this -> mysqlBindParameters ( ) ; if ( this -> mysqlBindResults ( ) == ERR_FAILURE ) this -> AddErrorToStack ( ( char * ) " " ) ; if ( this -> mysqlExecuteStatement ( ) == ERR_FAILURE ) { this -> AddErrorToStack ( ( char * ) " " ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } this -> mysqlStoreResults ( ) ; this -> mysqlFetchResult ( ) ; if ( this -> mysql_results_count > 0 ) if ( this -> mysql_results [ 0 ] . buffer ) { size_t * record_id = ( size_t * ) this -> mysql_results [ 0 ] . buffer ; result_index = * record_id ; } this -> mysqlDestroyStatement ( ) ; if ( result_index ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_SUCCESS ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; }
Check if a target exists (ERR_SUCCESS on exists)
Check if a target exists ( ERR_SUCCESS on exists )
// ensure we have a mysql handle // enter critical section // If the sha1 field in the db is set to use a duplicate key entry // this flag should be set to true, as it will prevent a lot of // db load. // result index // set base query // if we're not searching for status code also, limit the query // set query size // prepare the query // add href // add status_code // add index // bind the parameters // run lookup query on url to make sure it doesn't already exist // execute the statement // store results // fetch a result (max one result for this query) // if we have a record count, check // set from buffer // set the record id // destroy statement // return success if the record exists // return failure in the case that it does not exist