#!/bin/sh

PGPATH=/home/hlinnaka/pgsql.cvshead

PATH=${PATH}:${PGPATH}/bin

rm -rf /tmp/temppgdata
initdb -D /tmp/temppgdata
postgres -D /tmp/temppgdata &
sleep 2

psql postgres < ${PGPATH}/share/contrib/pgstattuple.sql

psql postgres -c "CREATE VIEW testindexcontents AS SELECT pn, (a.b).ctid, (a.b).itemoffset, (a.b).itemlen, substr((a.b).data, length((a.b).data)-20) FROM (select pn, bt_page_items('test_index', pn) AS b FROM generate_series(1, (SELECT pg_relpages('test_index'))) pn) AS a;"

psql postgres -c "DROP TABLE IF EXISTS test;"

psql postgres -c "CREATE TABLE test (key text);"
psql postgres -c "CREATE index test_index on test (key);"

psql postgres -c "CHECKPOINT;"

############ Tests begin. 


##### Test 1: Root split, new item on left

psql postgres -c "truncate test";

# Insert just enough tuples to fill the page but not split.
psql postgres -c " insert into test (key) select lpad(n, 100, '0') from generate_series(1,70) n;"

# split
psql postgres -a -c "INSERT INTO test (key) VALUES (lpad(10, 100, '0'))";

# Let's see what the page looks like *before* crash
psql postgres -a -c "SELECT * FROM testindexcontents" > /tmp/expected.txt

killall -9 postgres
sleep 1
postgres -D /tmp/temppgdata &
sleep 5 # should be enough for log replay to finish

# Let's see what the page looks like *after* crash
psql postgres -a -c "SELECT * FROM testindexcontents" > /tmp/result.txt



##### Test 2: Root split, new item on right

psql postgres -c "truncate test";

# Insert just enough tuples to fill the page but not split.
psql postgres -c " insert into test (key) select lpad(n, 100, '0') from generate_series(1,70) n;"

# split
psql postgres -a -c "INSERT INTO test (key) VALUES (lpad(99, 100, '0'))";

# Let's see what the page looks like *before* crash
psql postgres -a -c "SELECT * FROM testindexcontents" >> /tmp/expected.txt

killall -9 postgres
sleep 1
postgres -D /tmp/temppgdata &
sleep 5 # should be enough for log replay to finish

# Let's see what the page looks like *after* crash
psql postgres -a -c "SELECT * FROM testindexcontents" >> /tmp/result.txt



##### Test 3: Leaf page split, new item on left page

psql postgres -c "truncate test";

# Insert just enough tuples to create a two-level index.
psql postgres -c " insert into test (key) select lpad(n, 100, '0') from generate_series(1,71) n;"

# Insert just enough tuples to fill the left leaf page
psql postgres -c " insert into test (key) select lpad(n, 100, '0') from generate_series(1,7) n;"

# split
psql postgres -a -c "INSERT INTO test (key) VALUES (lpad(10, 100, '0'))";

# Let's see what the page looks like *before* crash
psql postgres -a -c "SELECT * FROM testindexcontents" >> /tmp/expected.txt

killall -9 postgres
sleep 1
postgres -D /tmp/temppgdata &
sleep 5 # should be enough for log replay to finish

# Let's see what the page looks like *after* crash
psql postgres -a -c "SELECT * FROM testindexcontents" >> /tmp/result.txt


##### Test 4: Leaf page split, new item on right page

psql postgres -c "truncate test";

# Insert just enough tuples to create a two-level index.
psql postgres -c " insert into test (key) select lpad(n, 100, '0') from generate_series(1,71) n;"

# Insert just enough tuples to fill the right leaf page
psql postgres -c " insert into test (key) select lpad(n, 100, '0') from generate_series(100,160) n;"

# split
psql postgres -a -c "INSERT INTO test (key) VALUES (lpad(999, 100, '0'))";

# Let's see what the page looks like *before* crash
psql postgres -a -c "SELECT * FROM testindexcontents" >> /tmp/expected.txt

killall -9 postgres
sleep 1
postgres -D /tmp/temppgdata &
sleep 5 # should be enough for log replay to finish

# Let's see what the page looks like *after* crash
psql postgres -a -c "SELECT * FROM testindexcontents" >> /tmp/result.txt




diff -c /tmp/expected.txt /tmp/result.txt > crashtest.diff

killall -9 postgres
