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

<test>
<name>RT: index format backwards compatibility</name>

<requires>
<pre_copy_ref/>
<id32/>
</requires>

<skip_indexer/>

<config>

searchd
{
	<searchd_settings/>
	workers = threads
}

index v2
{
	type			= rt
	path			= data/rt_v2
	rt_field		= content
	rt_attr_uint	= idd
}

index v3
{
	type			= rt
	path			= data/rt_v3
	rt_field		= content
	rt_attr_uint	= idd
}

index v4
{
	type			= rt
	path			= data/rt_v4
	rt_field		= content
	rt_attr_uint	= idd
}

index v5
{
	type			= rt
	path			= data/rt_v5
	rt_field		= content
	rt_attr_uint	= idd
}

</config>

<custom_test><![CDATA[

$restartDaemon = create_function('$client','
	$results = array ();
	StopSearchd ( "config.conf", "searchd.pid" );
	usleep ( 50000 );

	$error = "";
	$startSta = StartSearchd ( "config.conf", "error.txt", "searchd.pid", $error );
	if ( $startSta == 0 || $startSta == 2 )
	{
		$results[] = "started=ok";
		$client->SetMatchMode ( SPH_MATCH_EXTENDED2 );
	}
	else
	{
		$results[] = sprintf("start.err=%d; local=%s; client=%s;", $startSta, $error, $client->GetLastError());
	}
		
	return $results;
');

$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;
');

$insert = create_function ( '$idx, $from, $to, $query, $sock','

	$insert = "REPLACE INTO $idx ( id, idd, content ) VALUES";
	$lastword = 1;
	for ( $doc=$from; $doc<$to; $doc++ )
	{
		$content = "";
		for ( $word=$lastword; $word<$lastword+60; $word++ )
		{
			$content .= "test_$word ";
		}
		$lastword += 40;
		if ( ( $doc%7 )==0 )
		{
			$content .= "test700";
		} else if ( ( $doc%19 )==0 )
		{
			$content .= "test1800";		
		}
		
		$insert .= " ( $doc, 111, \'$content\' ),";
	}
	
	$res = $query ( substr ( $insert, 0, -1 ), $sock ); // remove trailing ,
	return array();
');

$open = create_function ( '', '

	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 );
	return $sock;
');

$close = create_function ( '$sock', '@mysql_close ( $sock );' );

$results = array ();

$sock = $open ();

$results = array_merge ( $results, $query ( "select * from v2 where match ( 'test700 | test1800' )", $sock ) );
$results = array_merge ( $results, $query ( "show meta", $sock ) );
$results = array_merge ( $results, $query ( "select * from v3 where match ( 'test700 | test1800' )", $sock ) );
$results = array_merge ( $results, $query ( "show meta", $sock ) );
$results = array_merge ( $results, $query ( "select * from v4 where match ( 'test700 | test1800' )", $sock ) );
$results = array_merge ( $results, $query ( "show meta", $sock ) );
$results = array_merge ( $results, $query ( "select * from v5 where match ( 'test700 | test1800' )", $sock ) );
$results = array_merge ( $results, $query ( "show meta", $sock ) );

$close ( $sock );

$results = array_merge ( $results, $restartDaemon ( $client ) );

$sock = $open ();

$results = array_merge ( $results, $insert ( "v5", 1, 20, $query, $sock ) );

$results = array_merge ( $results, $query ( "select * from v5 where match ( 'test700 | test1800')", $sock ) );
$results = array_merge ( $results, $query ( "show meta", $sock ) );

$close ( $sock );

]]></custom_test>

</test>
