#!/bin/sh

PATH=${PATH}:/home/hlinnaka/pgsql.cvshead/bin/

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

psql postgres < /home/hlinnaka/indexcontents/indexcontents.sql

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

psql postgres -c "CREATE TABLE test (key serial primary key, data text);"

psql postgres -c "insert into test (data) select rpad('a', 1000, 'b') from generate_series(1,1000) a;"
psql postgres -c "DELETE FROM test;"
psql postgres -c "VACUUM test; CHECKPOINT;"

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


##### Test 1: New item on right page.

# Insert just enough tuples to fill the page but not split.
psql postgres -a -c "INSERT INTO test (key, data) SELECT a, 'foo' FROM generate_series(1, 509) a;"

# split
psql postgres -a -c "INSERT INTO test (key, data) VALUES (10000, 'foo')";

# Let's see what the page looks like *before* crash
psql postgres -a -c "SELECT * FROM simpleindexcontents('test_pkey')" > /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 simpleindexcontents('test_pkey')" > /tmp/result.txt


##### Test 2: New item on right page. Original page was not rightmost

psql postgres -a -c "TRUNCATE test;"

# Insert enough tuples to fill a page and split it once
psql postgres -a -c "INSERT INTO test (key, data) SELECT a*1000, 'foo' FROM generate_series(1, 510) a;"

# Insert enough tuples to fill first the page again
psql postgres -a -c "INSERT INTO test (key, data) SELECT a, 'foo' FROM generate_series(458001, 458289) a";

# split

psql postgres -a -c "INSERT INTO test (key, data) VALUES (551234, 'foo')";

# Let's see what the page looks like *before* crash
psql postgres -a -c "SELECT * FROM simpleindexcontents('test_pkey')" > /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 simpleindexcontents('test_pkey')" > /tmp/result.txt


##### Test 3: New item on left page. Original page was not rightmost

psql postgres -a -c "TRUNCATE test;"

# Insert enough tuples to fill a page and split it once
psql postgres -a -c "INSERT INTO test (key, data) SELECT a*1000, 'foo' FROM generate_series(1, 510) a;"

# Insert enough tuples to fill first the page again
psql postgres -a -c "INSERT INTO test (key, data) SELECT a, 'foo' FROM generate_series(458001, 458289) a";

# split

psql postgres -a -c "INSERT INTO test (key, data) VALUES (1234, 'foo')";

# Let's see what the page looks like *before* crash
psql postgres -a -c "SELECT * FROM simpleindexcontents('test_pkey')" > /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 simpleindexcontents('test_pkey')" > /tmp/result.txt




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

killall -9 postgres
