# --------------------------------------------------------------- #
# -- Copyright (c) 1994, 1995 Computer Sciences Department,    -- #
# -- University of Wisconsin-Madison, subject to the terms     -- #
# -- and conditions given in the file COPYRIGHT.  All Rights   -- #
# -- Reserved.                                                 -- #
# --------------------------------------------------------------- #

proc rid_to_pid { rec } {
	set pid [string range $rec 0 [expr [string last "." $rec]-1] ]
	set pid [string range $pid [expr [string last "." $pid]+1] end ]
	return $pid
}

proc rem1 { rid  commit} {
	sm begin_xct
	set tx [sm xct]
	set tid [sm xct_to_tid $tx]
	sync
	echo tid $tid destroying rec1
	sm destroy_rec $rid
	echo rec1 destroyed
	# you can't sync here, because one of the
	# destroys can't happen until the other
	# tx commits
	# sync
	if {$commit} {
		sm commit_xct
		echo t1 ($tid) committed
	} else {
		echo t1 aborting
		sm abort_xct
		echo t1 aborted
	}
}

proc rem2 { rid  commit} {
	sm begin_xct
	set tx [sm xct]
	set tid [sm xct_to_tid $tx]
	sync
	echo tid $tid destroying rec2
	sm destroy_rec $rid
	echo rec2 destroyed
	# you can't sync here, because one of the
	# destroys can't happen until the other
	# tx commits
	# sync
	if {$commit} {
		sm commit_xct
		echo t2 ($tid) committed
	} else {
		echo t2 aborting
		sm abort_xct
		echo t2 aborted
	}
}

#################################################################################
source $script_dir/vol.init

###############################################################################

if {$Use_logical_id == 1} {
    echo WARNING: free_ext cannot be run with logical IDs 
} else {

sm begin_xct

echo creating file
set f1 [sm create_file $volid]

set PG_SZ       $page_size

# Create 8 records, each big enough to fill a whole page (so a whole extent is
# filled). Then create 2 more recs to be placed in the first and second pages of
# the second extent.

# change body width
set id_body_width %0[expr {$PG_SZ * .60}]d

for {set i 1} {$i <= 8} {incr i} {
	set r($i) [ sm create_rec $f1 "" 11 [format $id_body_width $i] ]
	echo $r($i)
}

set rec1 [sm create_rec $f1 "" 11 [format "rec1$id_body_width" 1] ]
set rec2 [sm create_rec $f1 "" 11 [format "rec2$id_body_width" 2] ]
echo $rec1 $rec2

#Remember the pid of the pages containing rec1 and rec2
set pid1 [rid_to_pid $rec1 ]
set pid2 [rid_to_pid $rec2]
assert {expr {$pid1 == [expr $pid2-1]}}

echo du and df after creating rec1 and rec2

sm commit_xct

dstats $f1
dstats $volid

###############################################################################

# Create two transactions: t1 and t2.
# First t1 destroys rec1 and then t2 destroys rec2.
# Then both xacts commit.

sm begin_xct

echo forking 1
set t1 [ fork_thread rem1 $rec1 1 ]
echo forking 2
set t2 [ fork_thread rem2 $rec2 1 ]

#destroy rec1 ==> page is freed
sync_thread $t1
#destroy rec2 ==> page is freed
sync_thread $t2

#commit t1
# sync_thread $t1
#commit t2
# sync_thread $t2

join_thread $t1 $t2

# Check that both pages are freed but the extent is not freed because of
# the contention

echo df and du after destroying rec1 and rec2

sm commit_xct

dstats $f1
dstats $volid

###############################################################################

# Re-create rec1 and rec2 
# Check that the same pages are reused.
# Then destroy them again (this time using a single xact) and check that
# the extent is deallocated.

sm begin_xct

echo re-creating recs rec1 and rec2
set rec1 [sm create_rec $f1 "" 11 [format "rec1$id_body_width" 1] ]
set rec2 [sm create_rec $f1 "" 11 [format "rec2$id_body_width" 2] ]
echo $rec1 $rec2
assert {expr { $pid1 == [rid_to_pid $rec1]}}
assert {expr { $pid2 == [rid_to_pid $rec2]}}

echo du after re-creating rec1 and rec2
dstats $f1

echo destroying rec1 and rec2
sm destroy_rec $rec1
sm destroy_rec $rec2

echo df after destroying rec1 and rec2

sm commit_xct
dstats $volid

##############################################################################

# Create second file and check that it reuses the extent de-allocated above.

sm begin_xct

echo creating second file
set f2 [sm create_file $volid]

echo creating a record in this file
set rnew [ sm create_rec $f2 "" 11 mike ]
echo $rnew
assert {expr { $pid1 == [rid_to_pid $rnew]}}

echo df after creating second file with 1 record in it
sm get_du_statistics $volid pretty audit

echo destoying both files
sm destroy_file $f1
sm destroy_file $f2

sm commit_xct
dstats $volid

###############################################################################

unset f1 f2 PG_SZ 
unset rnew pid1 pid2 rec1 rec2
unset r i
unset t1 t2

}
