<?xml version="1.0" encoding="utf-8"?>
<test>

<name>hitbuffer edge cases</name>

<config>
indexer
{
	mem_limit		= 16M
}

searchd
{
	<searchd_settings/>
	workers = threads
	binlog_path = #
    subtree_docs_cache  = 128k
    subtree_hits_cache  = 128k
}

source test1
{
	type			= mysql
	<sql_settings/>
	sql_query		= select * from test_table1
	sql_attr_uint	= gid
}

index test1
{
	source			= test1
	path			= <data_path/>/test1
}

source test2
{
	type			= mysql
	<sql_settings/>
	sql_query		= select * from test_table2
	sql_attr_uint	= gid
}

index test2
{
	source			= test2
	path			= <data_path/>/test2
	docinfo		= extern
	html_strip		= 1
	index_sp		= 1
}

source test3
{
	type			= mysql
	<sql_settings/>
	sql_query		= select * from test_table3
	sql_attr_uint	= gid
}
index test3
{
	source			= test3
	path			= <data_path/>/test3
	docinfo		= extern
}

index rt
{
	type = rt
	charset_type = utf-8
	path = <data_path/>/rt

	rt_field = content
	morphology = none
}
</config>

<db_create>
create table test_table1
(
	id int not null,
	gid int not null,
	title varchar(16384) not null
);
</db_create>
<db_create>
create table test_table2
(
	id int not null,
	gid int not null,
	title varchar(256) not null
);
</db_create>
<db_create>
create table test_table3
(
	id int not null,
	gid int not null,
	title varchar(64) not null
);
</db_create>
<db_drop>drop table if exists test_table1;</db_drop>
<db_drop>drop table if exists test_table2;</db_drop>
<db_drop>drop table if exists test_table3;</db_drop>
<custom_insert><![CDATA[

// OR boundary
// weight on 11 must be 2xxx; used to be 1xxx (because of positions mismatch)
$t = str_repeat ( "aaa ", 509 );
mysql_query ( "INSERT INTO test_table1 VALUES ( 10, 1, 'bbb bbb bbb bbb $t' )" ) or die("q1:".mysql_error());
mysql_query ( "INSERT INTO test_table1 VALUES ( 11, 1, 'aaa aaa aaa aaa bbb bbb aaa aaa' )" ) or die("q2:".mysql_error());

$i3 = '';
for ( $i=1; $i < 600; $i++ )
	mysql_query ( "INSERT INTO test_table2 VALUES ( $i, 1, 'The box. A dummy.' )" )  or die("q3:".mysql_error()."\n$i");

mysql_query ( "INSERT INTO test_table2 VALUES ( 701, 1, 'The good box. A dummy.' )" )  or die("q4:".mysql_error());

// common subtree case
for ( $i=1; $i < 401; $i++ )
	mysql_query ( "INSERT INTO test_table3 VALUES ( $i, $i, 'aaa bbb' )" )  or die("q5:".mysql_error()."\n$i");

for ( $i=402; $i < 602; $i++ )
	mysql_query ( "INSERT INTO test_table3 VALUES ( $i, $i, 'aaa' )" )  or die("q6:".mysql_error()."\n$i");
]]></custom_insert>

<custom_test>
<![CDATA[
$results = array ();

$client->SetMatchMode ( SPH_MATCH_EXTENDED2 );

// legacy
$res = $client->Query ( "aaa | bbb", "test1" );
unset ( $res["time"] );
$results[] = $res;

$res  = $client->Query ( "box SENTENCE good", "test2" );
unset ( $res["time"] );
$results[] = $res;

$results[] = 'common subtree crash';

$client->SetFilter ( "@id", array ( 999999 ) );
$client->AddQuery ( "aaa -bbb", "test3" );

$client->ResetFilters ();
$client->AddQuery ( "aaa -bbb", "test3" );

$res = $client->RunQueries();

if ( !$res )
{
	$results[] = $client->GetLastError();
	return;
}

foreach ( $res as $r )
{
	unset ( $r["time"] );
	$results[] = $r;
}

// regression RT dictionary lost words on merge with ID64
$query = create_function ( '$q, $sock','
	
	$results = array( $q );
	$res = @mysql_query ( $q, $sock );
	if ( $res===false )
	{
		$results[]  = mysql_errno( $sock ) . "; " . mysql_error ( $sock );
	} else
	{	
		while ($row = @mysql_fetch_array($res, MYSQL_ASSOC))
		{
			if ( array_key_exists ( "Variable_name", $row ) && $row["Variable_name"]=="time" )
			{
				continue;
			}
			if ( !array_key_exists ( "Variable_name", $row ) || !array_key_exists ( "Value", $row ) )
			{
				$line = "";
				foreach ( $row as $k=>$v )
				{
					$line .= $k . " = " . $v . "\t";
				}
				$results[] = $line;
			} else
			{
				$results[] = $row["Variable_name"] . " = " . $row["Value"];
			}
		}	
		@mysql_free_result ( $res );
	}
	return $results;
');

global $sd_address, $sd_sphinxql_port;
$sockStr = "$sd_address:$sd_sphinxql_port";
if ($sd_address == "localhost")
	$sockStr = "127.0.0.1:$sd_sphinxql_port";
$sock = @mysql_connect ($sockStr,"","",	true );

for ( $doc=1; $doc<121; $doc++ )
{
	$text = 'dummy text is going here';
	if ( $doc==31 )
		$text = 'ACT I.';
	else if ( $doc==100 )
		$text = 'And let us once again assail your ears, basketball';
	
	$insert = " REPLACE INTO rt ( id, content ) VALUES ( $doc, '$text' ) ";
	$query ( $insert, $sock );
}	

$results = array_merge ( $results, $query ( "select * from rt limit 0,2", $sock ) );
$results = array_merge ( $results, $query ( "show meta", $sock ) );	
$results = array_merge ( $results, $query ( "select * from rt where match ( 'basketball' )", $sock ) );
$results = array_merge ( $results, $query ( "show meta", $sock ) );	

@mysql_close ( $sock );

]]>
</custom_test>

</test>
