#
# Basic test for InnoDB adaptive hash index partitions.
# TODO: add another testcase that uses DEBUG_SYNC to check the partition locking
#
--source include/have_innodb.inc

# Check setup
SELECT @@GLOBAL.innodb_adaptive_hash_index;
SELECT @@GLOBAL.innodb_adaptive_hash_index_parts;

SET GLOBAL innodb_monitor_enable=module_adaptive_hash;

# 6 index trees across 4 AHI partitions
CREATE TABLE t1 (a INT PRIMARY KEY, b INT, c CHAR(200), UNIQUE INDEX b(b)) ENGINE=InnoDB;
CREATE TABLE t2 (a INT PRIMARY KEY, b INT, c CHAR(200), UNIQUE INDEX b(b)) ENGINE=InnoDB;
CREATE TABLE t3 (a INT PRIMARY KEY, b INT, c CHAR(200), UNIQUE INDEX b(b)) ENGINE=InnoDB;

--echo Filling tables
--disable_query_log
let $i=3;
while ($i)
{
        eval INSERT INTO t1 VALUES ($i, $i, REPEAT("a", 200));
        eval INSERT INTO t2 VALUES ($i, $i, REPEAT("a", 200));
        eval INSERT INTO t3 VALUES ($i, $i, REPEAT("a", 200));
        dec $i;
}

--echo Querying
--disable_result_log
let $i=200;
while ($i)
{
        SELECT b FROM t1 WHERE a=1;
        SELECT a FROM t1 WHERE b=1;
        SELECT b FROM t2 WHERE a=2;
        SELECT a FROM t2 WHERE b=2;
        SELECT b FROM t3 WHERE a=3;
        SELECT a FROM t3 WHERE b=3;
        dec $i;
}
--enable_result_log
--enable_query_log

# Some buffer pool pages should be hashed
SELECT COUNT >= 6 as should_be_1 FROM INFORMATION_SCHEMA.INNODB_METRICS WHERE NAME = 'adaptive_hash_pages_added';
# Some rows should be hashed
SELECT COUNT >= 6 as should_be_1 FROM INFORMATION_SCHEMA.INNODB_METRICS WHERE NAME = 'adaptive_hash_rows_added';
# AHI should have been used for queries, but the exact lower bound is hard to determine
SELECT COUNT > 0 as should_be_1 FROM INFORMATION_SCHEMA.INNODB_METRICS WHERE NAME = 'adaptive_hash_searches';

# Buffer pool must contain AHI pages now
SELECT COUNT(*) > 0 AS should_be_1 FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE PAGE_STATE LIKE "MEMORY";

# Buffer pool must contain no less than the number of index trees hashed pages now
SELECT COUNT(*) >= 6 AS should_be_1 FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE IS_HASHED LIKE "YES";

SET GLOBAL innodb_monitor_disable=module_adaptive_hash;

DROP TABLE t1, t2, t3;

--disable_warnings
SET GLOBAL innodb_monitor_enable=default;
SET GLOBAL innodb_monitor_disable=default;
--enable_warnings
