#!/bin/sh

PATH=${PATH}:/home/hlinnaka/pgsql.bi3/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, data text);"

psql postgres -c "CREATE UNIQUE INDEX test_bi ON test (key) WITH (groupitems = 1);"

psql postgres -c "insert into test (key, data) select a*10, rpad('a', 1000, 'b') from generate_series(1,10) a;"
psql postgres -c "insert into test (key, data) select 10000+a, rpad('a', 1000, 'b') from generate_series(1,510) a;"
psql postgres -c "DELETE FROM test WHERE key>=10000;"
psql postgres -c "VACUUM test; CHECKPOINT;"

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


##### Test 1: groupsplit

# dirty the page, so that subsequent WAL record doesn't take a full page copy
psql postgres -a -c "INSERT INTO test (key, data) VALUES (1000, '');"


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

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


##### Test 2: groupify


psql postgres -c "TRUNCATE test"

psql postgres -c "insert into test (key, data) select a*10, rpad('a', 1000, 'b') from generate_series(1,500) a;"
psql postgres -c "CHECKPOINT"
psql postgres -c "insert into test (key, data) select a*10, rpad('a', 1000, 'b') from generate_series(501,510) a;"


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

##### Test 3: replace

psql postgres -c "TRUNCATE test"
psql postgres -c "insert into test (key, data) select a*10, rpad('a', 1000, 'b') from generate_series(100,120) a;"

psql postgres -c "CHECKPOINT"

# this insert fills the page and invokes a groupify operation
psql postgres -c "insert into test (key, data) select a*10, rpad('a', 1000, 'b') from generate_series(1000,1500) a;"


psql postgres -c "DELETE FROM test WHERE key IN (1070, 1080)"
psql postgres -c "INSERT INTO test (key, data) VALUES (1085, rpad('a', 1000, 'c'))"

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



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

killall -9 postgres

