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

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

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

<config>

searchd
{
	<searchd_settings/>
	workers = threads
}

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

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

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

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

source plain_stem_src
{
	type			= mysql
	<sql_settings/>
	sql_query		= select 1, 11 as gid, 'apple' as title
	sql_attr_uint	= gid
}
index plain_stem
{
	source			= plain_stem_src
	path			= <data_path/>/plain_stem
	docinfo			= extern
	morphology = stem_en
	dict			= crc
}
index rt_stem
{
	type			= rt
	path			= <data_path/>/rt_stem
	rt_attr_uint	= gid
	rt_field		= title
	morphology = stem_en
	dict			= crc
} 

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

$results = array_merge ( $results, $query ( "CALL KEYWORDS ( 'apple', 'plain_stem', 1 )", $sock ) );
$results = array_merge ( $results, $query ( "CALL KEYWORDS ( 'apple', 'rt_stem', 1 )", $sock ) );
$results = array_merge ( $results, $query ( "ATTACH INDEX plain_stem TO RTINDEX rt_stem", $sock ) );
$results = array_merge ( $results, $query ( "UPDATE rt_stem SET gid=111 WHERE id=1", $sock ) );

// regression plain unavailable after ATTACH till daemon restart
$results = array_merge ( $results, $query ( "SELECT * FROM plain_stem", $sock ) );
RunIndexer ( $errors, '--rotate plain_stem' );
sleep(2);
$results = array_merge ( $results, $query ( "SELECT * FROM plain_stem", $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 ) );

$results = array_merge ( $results, $query ( "CALL KEYWORDS ( 'apple', 'rt_stem', 1 )", $sock ) );
$results = array_merge ( $results, $query ( "SELECT * FROM rt_stem", $sock ) );

// TRUNCATE vs ATTACH
$results = array_merge ( $results, $query ( "DELETE FROM rt_stem WHERE id=1", $sock ) );
$results = array_merge ( $results, $query ( "SELECT * FROM rt_stem", $sock ) );
$results = array_merge ( $results, $query ( "TRUNCATE RTINDEX rt_stem", $sock ) );
$results = array_merge ( $results, $query ( "ATTACH INDEX plain_stem TO RTINDEX rt_stem", $sock ) );
$results = array_merge ( $results, $query ( "SELECT * FROM rt_stem", $sock ) );

$close ( $sock );

]]></custom_test>

</test>
