1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: recd009.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	recd009
8# TEST	Verify record numbering across split/reverse splits and recovery.
9proc recd009 { method {select 0} args} {
10	global fixed_len
11	source ./include.tcl
12
13	if { [is_rbtree $method] != 1 && [is_rrecno $method] != 1} {
14		puts "Recd009 skipping for method $method."
15		return
16	}
17
18	set opts [convert_args $method $args]
19	set method [convert_method $method]
20
21	puts "\tRecd009: Test record numbers across splits and recovery"
22
23	set testfile recd009.db
24	env_cleanup $testdir
25	set mkeys 1000
26	set nkeys 5
27	set data "data"
28
29	puts "\tRecd009.a: Create $method environment and database."
30	set flags "-create -txn -home $testdir"
31
32	set env_cmd "berkdb_env $flags"
33	set dbenv [eval $env_cmd]
34	error_check_good dbenv [is_valid_env $dbenv] TRUE
35
36	set oflags "-env $dbenv -pagesize 8192 -create -mode 0644 $opts $method"
37	set db [eval {berkdb_open} $oflags $testfile]
38	error_check_good dbopen [is_valid_db $db] TRUE
39
40	# Fill page with small key/data pairs.  Keep at leaf.
41	puts "\tRecd009.b: Fill page with $nkeys small key/data pairs."
42	for { set i 1 } { $i <= $nkeys } { incr i } {
43		if { [is_recno $method] == 1 } {
44			set key $i
45		} else {
46			set key key000$i
47		}
48		set ret [$db put $key $data$i]
49		error_check_good dbput $ret 0
50	}
51	error_check_good db_close [$db close] 0
52	error_check_good env_close [$dbenv close] 0
53
54	set newnkeys [expr $nkeys + 1]
55	# List of recovery tests: {CMD MSG} pairs.
56	set rlist {
57	{ {recd009_split DB TXNID 1 $method $newnkeys $mkeys}
58	    "Recd009.c: split"}
59	{ {recd009_split DB TXNID 0 $method $newnkeys $mkeys}
60	    "Recd009.d: reverse split"}
61	}
62
63	foreach pair $rlist {
64		set cmd [subst [lindex $pair 0]]
65		set msg [lindex $pair 1]
66		if { $select != 0 } {
67			set tag [lindex $msg 0]
68			set tail [expr [string length $tag] - 2]
69			set tag [string range $tag $tail $tail]
70			if { [lsearch $select $tag] == -1 } {
71				continue
72			}
73		}
74		set reverse [string first "reverse" $msg]
75		if { $reverse == -1 } {
76			set abortkeys $nkeys
77			set commitkeys $mkeys
78			set abortpg 0
79			set commitpg 1
80		} else {
81			set abortkeys $mkeys
82			set commitkeys $nkeys
83			set abortpg 1
84			set commitpg 0
85		}
86		op_recover abort $testdir $env_cmd $testfile $cmd $msg
87		recd009_recnocheck $testdir $testfile $opts $abortkeys $abortpg
88		op_recover commit $testdir $env_cmd $testfile $cmd $msg
89		recd009_recnocheck $testdir $testfile $opts \
90		    $commitkeys $commitpg
91	}
92	puts "\tRecd009.e: Verify db_printlog can read logfile"
93	set tmpfile $testdir/printlog.out
94	set stat [catch {exec $util_path/db_printlog -h $testdir \
95	    > $tmpfile} ret]
96	error_check_good db_printlog $stat 0
97	fileremove $tmpfile
98}
99
100#
101# This procedure verifies that the database has only numkeys number
102# of keys and that they are in order.
103#
104proc recd009_recnocheck { tdir testfile opts numkeys numpg} {
105	source ./include.tcl
106
107	set db [eval {berkdb_open} $opts $tdir/$testfile]
108	error_check_good dbopen [is_valid_db $db] TRUE
109
110	puts "\tRecd009_recnocheck: Verify page count of $numpg on split."
111	set stat [$db stat]
112	error_check_bad stat:check-split [is_substr $stat \
113		"{{Internal pages} 0}"] $numpg
114
115	set type [$db get_type]
116	set dbc [$db cursor]
117	error_check_good dbcursor [is_valid_cursor $dbc $db] TRUE
118	set i 1
119	puts "\tRecd009_recnocheck: Checking $numkeys record numbers."
120	for {set d [$dbc get -first]} { [llength $d] != 0 } {
121	    set d [$dbc get -next]} {
122		if { [is_btree $type] } {
123			set thisi [$dbc get -get_recno]
124		} else {
125			set thisi [lindex [lindex $d 0] 0]
126		}
127		error_check_good recno_check $i $thisi
128		error_check_good record_count [expr $i <= $numkeys] 1
129		incr i
130	}
131	error_check_good curs_close [$dbc close] 0
132	error_check_good db_close [$db close] 0
133}
134
135proc recd009_split { db txn split method nkeys mkeys } {
136	global errorCode
137	source ./include.tcl
138
139	set data "data"
140
141	set isrecno [is_recno $method]
142	# if mkeys is above 1000, need to adjust below for lexical order
143	if { $split == 1 } {
144		puts "\tRecd009_split: Add $mkeys pairs to force split."
145		for {set i $nkeys} { $i <= $mkeys } { incr i } {
146			if { $isrecno == 1 } {
147				set key $i
148			} else {
149				if { $i >= 100 } {
150					set key key0$i
151				} elseif { $i >= 10 } {
152					set key key00$i
153				} else {
154					set key key000$i
155				}
156			}
157			set ret [$db put -txn $txn $key $data$i]
158			error_check_good dbput:more $ret 0
159		}
160	} else {
161		puts "\tRecd009_split: Delete added keys to force reverse split."
162		# Since rrecno renumbers, we delete downward.
163		for {set i $mkeys} { $i >= $nkeys } { set i [expr $i - 1] } {
164			if { $isrecno == 1 } {
165				set key $i
166			} else {
167				if { $i >= 100 } {
168					set key key0$i
169				} elseif { $i >= 10 } {
170					set key key00$i
171				} else {
172					set key key000$i
173				}
174			}
175			error_check_good db_del:$i [$db del -txn $txn $key] 0
176		}
177	}
178	return 0
179}
180