1# See the file LICENSE for redistribution information. 2# 3# Copyright (c) 1996,2008 Oracle. All rights reserved. 4# 5# $Id: test029.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $ 6# 7# TEST test029 8# TEST Test the Btree and Record number renumbering. 9proc test029 { method {nentries 10000} args} { 10 source ./include.tcl 11 12 set do_renumber [is_rrecno $method] 13 set args [convert_args $method $args] 14 set omethod [convert_method $method] 15 16 puts "Test029: $method ($args)" 17 18 if { [string compare $omethod "-hash"] == 0 } { 19 puts "Test029 skipping for method HASH" 20 return 21 } 22 if { [is_record_based $method] == 1 && $do_renumber != 1 } { 23 puts "Test029 skipping for method RECNO (w/out renumbering)" 24 return 25 } 26 27 # Create the database and open the dictionary 28 set txnenv 0 29 set eindex [lsearch -exact $args "-env"] 30 # 31 # If we are using an env, then testfile should just be the db name. 32 # Otherwise it is the test directory and the name. 33 if { $eindex == -1 } { 34 set testfile $testdir/test029.db 35 set env NULL 36 } else { 37 set testfile test029.db 38 incr eindex 39 set env [lindex $args $eindex] 40 set txnenv [is_txnenv $env] 41 if { $txnenv == 1 } { 42 append args " -auto_commit " 43 # 44 # If we are using txns and running with the 45 # default, set the default down a bit. 46 # 47 if { $nentries == 10000 } { 48 # Do not set nentries down to 100 until we 49 # fix SR #5958. 50 set nentries 1000 51 } 52 } 53 set testdir [get_home $env] 54 } 55 cleanup $testdir $env 56 57 # Read the first nentries dictionary elements and reverse them. 58 # Keep a list of these (these will be the keys). 59 puts "\tTest029.a: initialization" 60 set keys "" 61 set did [open $dict] 62 set count 0 63 while { [gets $did str] != -1 && $count < $nentries } { 64 lappend keys [reverse $str] 65 incr count 66 } 67 close $did 68 69 # Generate sorted order for the keys 70 set sorted_keys [lsort $keys] 71 72 # Save the first and last keys 73 set last_key [lindex $sorted_keys end] 74 set last_keynum [llength $sorted_keys] 75 76 set first_key [lindex $sorted_keys 0] 77 set first_keynum 1 78 79 # Create the database 80 if { [string compare $omethod "-btree"] == 0 } { 81 set db [eval {berkdb_open -create \ 82 -mode 0644 -recnum} $args {$omethod $testfile}] 83 error_check_good dbopen [is_valid_db $db] TRUE 84 } else { 85 set db [eval {berkdb_open -create \ 86 -mode 0644} $args {$omethod $testfile}] 87 error_check_good dbopen [is_valid_db $db] TRUE 88 } 89 90 set pflags "" 91 set gflags "" 92 set txn "" 93 94 if { [is_record_based $method] == 1 } { 95 append gflags " -recno" 96 } 97 98 puts "\tTest029.b: put/get loop" 99 foreach k $keys { 100 if { [is_record_based $method] == 1 } { 101 set key [lsearch $sorted_keys $k] 102 incr key 103 } else { 104 set key $k 105 } 106 if { $txnenv == 1 } { 107 set t [$env txn] 108 error_check_good txn [is_valid_txn $t $env] TRUE 109 set txn "-txn $t" 110 } 111 set ret [eval {$db put} \ 112 $txn $pflags {$key [chop_data $method $k]}] 113 error_check_good dbput $ret 0 114 115 set ret [eval {$db get} $txn $gflags {$key}] 116 error_check_good dbget [lindex [lindex $ret 0] 1] $k 117 if { $txnenv == 1 } { 118 error_check_good txn [$t commit] 0 119 } 120 } 121 122 # Now delete the first key in the database 123 puts "\tTest029.c: delete and verify renumber" 124 125 # Delete the first key in the file 126 if { [is_record_based $method] == 1 } { 127 set key $first_keynum 128 } else { 129 set key $first_key 130 } 131 132 if { $txnenv == 1 } { 133 set t [$env txn] 134 error_check_good txn [is_valid_txn $t $env] TRUE 135 set txn "-txn $t" 136 } 137 set ret [eval {$db del} $txn {$key}] 138 error_check_good db_del $ret 0 139 if { $txnenv == 1 } { 140 error_check_good txn [$t commit] 0 141 } 142 143 # Now we are ready to retrieve records based on 144 # record number 145 if { [string compare $omethod "-btree"] == 0 } { 146 append gflags " -recno" 147 } 148 149 # First try to get the old last key (shouldn't exist) 150 if { $txnenv == 1 } { 151 set t [$env txn] 152 error_check_good txn [is_valid_txn $t $env] TRUE 153 set txn "-txn $t" 154 } 155 set ret [eval {$db get} $txn $gflags {$last_keynum}] 156 error_check_good get_after_del $ret [list] 157 if { $txnenv == 1 } { 158 error_check_good txn [$t commit] 0 159 } 160 161 # Now try to get what we think should be the last key 162 if { $txnenv == 1 } { 163 set t [$env txn] 164 error_check_good txn [is_valid_txn $t $env] TRUE 165 set txn "-txn $t" 166 } 167 set ret [eval {$db get} $txn $gflags {[expr $last_keynum - 1]}] 168 error_check_good \ 169 getn_last_after_del [lindex [lindex $ret 0] 1] $last_key 170 if { $txnenv == 1 } { 171 error_check_good txn [$t commit] 0 172 } 173 174 # Create a cursor; we need it for the next test and we 175 # need it for recno here. 176 if { $txnenv == 1 } { 177 set t [$env txn] 178 error_check_good txn [is_valid_txn $t $env] TRUE 179 set txn "-txn $t" 180 } 181 set dbc [eval {$db cursor} $txn] 182 error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE 183 184 # OK, now re-put the first key and make sure that we 185 # renumber the last key appropriately. 186 if { [string compare $omethod "-btree"] == 0 } { 187 set ret [eval {$db put} $txn \ 188 {$key [chop_data $method $first_key]}] 189 error_check_good db_put $ret 0 190 } else { 191 # Recno 192 set ret [$dbc get -first] 193 set ret [eval {$dbc put} $pflags {-before $first_key}] 194 error_check_bad dbc_put:DB_BEFORE $ret 0 195 } 196 197 # Now check that the last record matches the last record number 198 set ret [eval {$db get} $txn $gflags {$last_keynum}] 199 error_check_good \ 200 getn_last_after_put [lindex [lindex $ret 0] 1] $last_key 201 202 # Now delete the first key in the database using a cursor 203 puts "\tTest029.d: delete with cursor and verify renumber" 204 205 set ret [$dbc get -first] 206 error_check_good dbc_first $ret [list [list $key $first_key]] 207 208 # Now delete at the cursor 209 set ret [$dbc del] 210 error_check_good dbc_del $ret 0 211 212 # Now check the record numbers of the last keys again. 213 # First try to get the old last key (shouldn't exist) 214 set ret [eval {$db get} $txn $gflags {$last_keynum}] 215 error_check_good get_last_after_cursor_del:$ret $ret [list] 216 217 # Now try to get what we think should be the last key 218 set ret [eval {$db get} $txn $gflags {[expr $last_keynum - 1]}] 219 error_check_good \ 220 getn_after_cursor_del [lindex [lindex $ret 0] 1] $last_key 221 222 # Re-put the first key and make sure that we renumber the last 223 # key appropriately. We can't do a c_put -current, so do 224 # a db put instead. 225 if { [string compare $omethod "-btree"] == 0 } { 226 puts "\tTest029.e: put (non-cursor) and verify renumber" 227 set ret [eval {$db put} $txn \ 228 {$key [chop_data $method $first_key]}] 229 error_check_good db_put $ret 0 230 } else { 231 puts "\tTest029.e: put with cursor and verify renumber" 232 set ret [eval {$dbc put} $pflags {-before $first_key}] 233 error_check_bad dbc_put:DB_BEFORE $ret 0 234 } 235 236 # Now check that the last record matches the last record number 237 set ret [eval {$db get} $txn $gflags {$last_keynum}] 238 error_check_good \ 239 get_after_cursor_reput [lindex [lindex $ret 0] 1] $last_key 240 241 error_check_good dbc_close [$dbc close] 0 242 if { $txnenv == 1 } { 243 error_check_good txn [$t commit] 0 244 } 245 error_check_good db_close [$db close] 0 246} 247