1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: test048.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test048
8# TEST	Cursor stability across Btree splits.
9proc test048 { method args } {
10	global errorCode
11	global is_je_test
12	source ./include.tcl
13
14	set tnum 048
15	set args [convert_args $method $args]
16
17	if { [is_btree $method] != 1 } {
18		puts "Test$tnum skipping for method $method."
19		return
20	}
21	set pgindex [lsearch -exact $args "-pagesize"]
22	if { $pgindex != -1 } {
23		incr pgindex
24		if { [lindex $args $pgindex] > 8192 } {
25			puts "Test048: Skipping for large pagesizes"
26			return
27		}
28	}
29
30	set method "-btree"
31
32	puts "\tTest$tnum: Test of cursor stability across btree splits."
33
34	set key	"key"
35	set data	"data"
36	set txn ""
37	set flags ""
38
39	puts "\tTest$tnum.a: Create $method database."
40	set txnenv 0
41	set eindex [lsearch -exact $args "-env"]
42	#
43	# If we are using an env, then testfile should just be the db name.
44	# Otherwise it is the test directory and the name.
45	if { $eindex == -1 } {
46		set testfile $testdir/test$tnum.db
47		set env NULL
48	} else {
49		set testfile test$tnum.db
50		incr eindex
51		set env [lindex $args $eindex]
52		set txnenv [is_txnenv $env]
53		if { $txnenv == 1 } {
54			append args " -auto_commit "
55		}
56		set testdir [get_home $env]
57	}
58	set t1 $testdir/t1
59	cleanup $testdir $env
60
61	set oflags "-create -mode 0644 $args $method"
62	set db [eval {berkdb_open} $oflags $testfile]
63	error_check_good dbopen [is_valid_db $db] TRUE
64
65	set nkeys 5
66	# Fill page w/ small key/data pairs, keep at leaf
67	#
68	puts "\tTest$tnum.b: Fill page with $nkeys small key/data pairs."
69	for { set i 0 } { $i < $nkeys } { incr i } {
70		if { $txnenv == 1 } {
71			set t [$env txn]
72			error_check_good txn [is_valid_txn $t $env] TRUE
73			set txn "-txn $t"
74		}
75		set ret [eval {$db put} $txn {key000$i $data$i}]
76		error_check_good dbput $ret 0
77		if { $txnenv == 1 } {
78			error_check_good txn [$t commit] 0
79		}
80	}
81
82	# get db ordering, set cursors
83	puts "\tTest$tnum.c: Set cursors on each of $nkeys pairs."
84	if { $txnenv == 1 } {
85		set t [$env txn]
86		error_check_good txn [is_valid_txn $t $env] TRUE
87		set txn "-txn $t"
88	}
89	for {set i 0; set ret [$db get key000$i]} {\
90			$i < $nkeys && [llength $ret] != 0} {\
91			incr i; set ret [$db get key000$i]} {
92		set key_set($i) [lindex [lindex $ret 0] 0]
93		set data_set($i) [lindex [lindex $ret 0] 1]
94		set dbc [eval {$db cursor} $txn]
95		set dbc_set($i) $dbc
96		error_check_good db_cursor:$i \
97		    [is_valid_cursor $dbc_set($i) $db] TRUE
98		set ret [$dbc_set($i) get -set $key_set($i)]
99		error_check_bad dbc_set($i)_get:set [llength $ret] 0
100	}
101
102	# if mkeys is above 1000, need to adjust below for lexical order
103	set mkeys 1000
104	puts "\tTest$tnum.d: Add $mkeys pairs to force split."
105	for {set i $nkeys} { $i < $mkeys } { incr i } {
106		if { $i >= 100 } {
107			set ret [eval {$db put} $txn {key0$i $data$i}]
108		} elseif { $i >= 10 } {
109			set ret [eval {$db put} $txn {key00$i $data$i}]
110		} else {
111			set ret [eval {$db put} $txn {key000$i $data$i}]
112		}
113		error_check_good dbput:more $ret 0
114	}
115
116	puts "\tTest$tnum.e: Make sure split happened."
117	# XXX We cannot call stat with active txns or we deadlock.
118	if { $txnenv != 1 && !$is_je_test } {
119		error_check_bad stat:check-split [is_substr [$db stat] \
120					"{{Internal pages} 0}"] 1
121	}
122
123	puts "\tTest$tnum.f: Check to see that cursors maintained reference."
124	for {set i 0} { $i < $nkeys } {incr i} {
125		set ret [$dbc_set($i) get -current]
126		error_check_bad dbc$i:get:current [llength $ret] 0
127		set ret2 [$dbc_set($i) get -set $key_set($i)]
128		error_check_bad dbc$i:get:set [llength $ret2] 0
129		error_check_good dbc$i:get(match) $ret $ret2
130	}
131
132	puts "\tTest$tnum.g: Delete added keys to force reverse split."
133	for {set i $nkeys} { $i < $mkeys } { incr i } {
134		if { $i >= 100 } {
135			error_check_good db_del:$i \
136			    [eval {$db del} $txn {key0$i}] 0
137		} elseif { $i >= 10 } {
138			error_check_good db_del:$i \
139			    [eval {$db del} $txn {key00$i}] 0
140		} else {
141			error_check_good db_del:$i \
142			    [eval {$db del} $txn {key000$i}] 0
143		}
144	}
145
146	puts "\tTest$tnum.h: Verify cursor reference."
147	for {set i 0} { $i < $nkeys } {incr i} {
148		set ret [$dbc_set($i) get -current]
149		error_check_bad dbc$i:get:current [llength $ret] 0
150		set ret2 [$dbc_set($i) get -set $key_set($i)]
151		error_check_bad dbc$i:get:set [llength $ret2] 0
152		error_check_good dbc$i:get(match) $ret $ret2
153	}
154
155	puts "\tTest$tnum.i: Cleanup."
156	# close cursors
157	for {set i 0} { $i < $nkeys } {incr i} {
158		error_check_good dbc_close:$i [$dbc_set($i) close] 0
159	}
160	if { $txnenv == 1 } {
161		error_check_good txn [$t commit] 0
162	}
163	puts "\tTest$tnum.j: Verify reverse split."
164	error_check_good stat:check-reverse_split [is_substr [$db stat] \
165					"{{Internal pages} 0}"] 1
166
167	error_check_good dbclose [$db close] 0
168
169	puts "\tTest$tnum complete."
170}
171