1# See the file LICENSE for redistribution information. 2# 3# Copyright (c) 2001,2008 Oracle. All rights reserved. 4# 5# $Id: si004.tcl,v 12.15 2008/01/08 20:58:53 bostic Exp $ 6# 7# TEST si004 8# TEST si002 with secondaries created and closed mid-test 9# TEST Basic cursor-based secondary index put/delete test, with 10# TEST secondaries created mid-test. 11proc si004 { methods {nentries 200} {tnum "004"} args } { 12 source ./include.tcl 13 global dict nsecondaries 14 15 # There's no reason to run this test on large lists. 16 if { $nentries > 1000 } { 17 puts "Skipping si004 for large lists (over 1000 items)." 18 return 19 } 20 21 # Primary method/args. 22 set pmethod [lindex $methods 0] 23 set pargs [convert_args $pmethod $args] 24 set pomethod [convert_method $pmethod] 25 26 # Renumbering recno databases can't be used as primaries. 27 if { [is_rrecno $pmethod] == 1 } { 28 puts "Skipping si$tnum for method $pmethod" 29 return 30 } 31 32 # Method/args for all the secondaries. If only one method 33 # was specified, assume the same method (for btree or hash) 34 # and a standard number of secondaries. If primary is not 35 # btree or hash, force secondaries to be one btree, one hash. 36 set methods [lrange $methods 1 end] 37 if { [llength $methods] == 0 } { 38 for { set i 0 } { $i < $nsecondaries } { incr i } { 39 if { [is_btree $pmethod] || [is_hash $pmethod] } { 40 lappend methods $pmethod 41 } else { 42 if { [expr $i % 2] == 0 } { 43 lappend methods "-btree" 44 } else { 45 lappend methods "-hash" 46 } 47 } 48 } 49 } 50 51 set argses [convert_argses $methods $args] 52 set omethods [convert_methods $methods] 53 54 # If we are given an env, use it. Otherwise, open one. 55 set eindex [lsearch -exact $args "-env"] 56 if { $eindex == -1 } { 57 env_cleanup $testdir 58 set env [berkdb_env -create -home $testdir] 59 error_check_good env_open [is_valid_env $env] TRUE 60 } else { 61 incr eindex 62 set env [lindex $args $eindex] 63 set envflags [$env get_open_flags] 64 if { [lsearch -exact $envflags "-thread"] != -1 } { 65 puts "Skipping si$tnum for threaded env" 66 return 67 } 68 set testdir [get_home $env] 69 } 70 71 puts "si$tnum \{\[ list $pmethod $methods \]\} $nentries" 72 cleanup $testdir $env 73 74 set pname "primary$tnum.db" 75 set snamebase "secondary$tnum" 76 77 # Open the primary. 78 set pdb [eval {berkdb_open -create -env} $env $pomethod $pargs $pname] 79 error_check_good primary_open [is_valid_db $pdb] TRUE 80 81 # Populate with a cursor put, exercising keyfirst/keylast. 82 set did [open $dict] 83 puts -nonewline \ 84 "\tSi$tnum.a: Cursor put (-keyfirst/-keylast) loop ... " 85 set pdbc [$pdb cursor] 86 error_check_good pdb_cursor [is_valid_cursor $pdbc $pdb] TRUE 87 for { set n 0 } \ 88 { [gets $did str] != -1 && $n < $nentries } { incr n } { 89 if { [is_record_based $pmethod] == 1 } { 90 set key [expr $n + 1] 91 set datum $str 92 } else { 93 set key $str 94 gets $did datum 95 } 96 97 set ns($key) $n 98 set keys($n) $key 99 set data($n) [pad_data $pmethod $datum] 100 101 if { $n % 2 == 0 } { 102 set pflag " -keyfirst " 103 } else { 104 set pflag " -keylast " 105 } 106 107 set ret [eval {$pdbc put} $pflag \ 108 {$key [chop_data $pmethod $datum]}] 109 error_check_good put($n) $ret 0 110 } 111 error_check_good pdbc_close [$pdbc close] 0 112 close $did 113 114 # Open and associate the secondaries 115 set sdbs {} 116 puts "\n\t\topening secondaries." 117 for { set i 0 } { $i < [llength $omethods] } { incr i } { 118 set sdb [eval {berkdb_open -create -env} $env \ 119 [lindex $omethods $i] [lindex $argses $i] $snamebase.$i.db] 120 error_check_good second_open($i) [is_valid_db $sdb] TRUE 121 122 error_check_good db_associate($i) \ 123 [$pdb associate -create [callback_n $i] $sdb] 0 124 lappend sdbs $sdb 125 } 126 check_secondaries $pdb $sdbs $nentries keys data "Si$tnum.a" 127 128 puts "\tSi$tnum.b: Cursor put overwrite (-current) loop" 129 set pdbc [$pdb cursor] 130 error_check_good pdb_cursor [is_valid_cursor $pdbc $pdb] TRUE 131 for { set dbt [$pdbc get -first] } { [llength $dbt] > 0 } \ 132 { set dbt [$pdbc get -next] } { 133 set key [lindex [lindex $dbt 0] 0] 134 set datum [lindex [lindex $dbt 0] 1] 135 set newd $datum.$key 136 set ret [eval {$pdbc put -current} [chop_data $pmethod $newd]] 137 error_check_good put_overwrite($key) $ret 0 138 set data($ns($key)) [pad_data $pmethod $newd] 139 } 140 error_check_good pdbc_close [$pdbc close] 0 141 check_secondaries $pdb $sdbs $nentries keys data "Si$tnum.b" 142 143 puts -nonewline "\tSi$tnum.c:\ 144 Secondary c_pget/primary put overwrite loop ... " 145 # We walk the first secondary, then put-overwrite each primary key/data 146 # pair we find. This doubles as a DBC->c_pget test. 147 set sdb [lindex $sdbs 0] 148 set sdbc [$sdb cursor] 149 error_check_good sdb_cursor [is_valid_cursor $sdbc $sdb] TRUE 150 for { set dbt [$sdbc pget -first] } { [llength $dbt] > 0 } \ 151 { set dbt [$sdbc pget -next] } { 152 set pkey [lindex [lindex $dbt 0] 1] 153 set pdatum [lindex [lindex $dbt 0] 2] 154 155 # Extended entries will be showing up underneath us, in 156 # unpredictable places. Keep track of which pkeys 157 # we've extended, and don't extend them repeatedly. 158 if { [info exists pkeys_done($pkey)] == 1 } { 159 continue 160 } else { 161 set pkeys_done($pkey) 1 162 } 163 164 set newd $pdatum.[string range $pdatum 0 2] 165 set ret [eval {$pdb put} {$pkey [chop_data $pmethod $newd]}] 166 error_check_good pdb_put($pkey) $ret 0 167 set data($ns($pkey)) [pad_data $pmethod $newd] 168 } 169 error_check_good sdbc_close [$sdbc close] 0 170 171 # Close the secondaries again. 172 puts "\n\t\tclosing secondaries." 173 for { set sdb [lindex $sdbs end] } { [string length $sdb] > 0 } \ 174 { set sdb [lindex $sdbs end] } { 175 error_check_good second_close($sdb) [$sdb close] 0 176 set sdbs [lrange $sdbs 0 end-1] 177 check_secondaries \ 178 $pdb $sdbs $nentries keys data "Si$tnum.c" 179 } 180 181 # Delete the second half of the entries through the primary. 182 # We do the second half so we can just pass keys(0 ... n/2) 183 # to check_secondaries. 184 set half [expr $nentries / 2] 185 puts -nonewline "\tSi$tnum.d:\ 186 Primary cursor delete loop: deleting $half entries ... " 187 set pdbc [$pdb cursor] 188 error_check_good pdb_cursor [is_valid_cursor $pdbc $pdb] TRUE 189 set dbt [$pdbc get -first] 190 for { set i 0 } { [llength $dbt] > 0 && $i < $half } { incr i } { 191 error_check_good pdbc_del [$pdbc del] 0 192 set dbt [$pdbc get -next] 193 } 194 error_check_good pdbc_close [$pdbc close] 0 195 196 set sdbs {} 197 puts "\n\t\topening secondaries." 198 for { set i 0 } { $i < [llength $omethods] } { incr i } { 199 set sdb [eval {berkdb_open -create -env} $env \ 200 [lindex $omethods $i] [lindex $argses $i] \ 201 $snamebase.r2.$i.db] 202 error_check_good second_open($i) [is_valid_db $sdb] TRUE 203 204 error_check_good db_associate($i) \ 205 [$pdb associate -create [callback_n $i] $sdb] 0 206 lappend sdbs $sdb 207 } 208 cursor_check_secondaries $pdb $sdbs $half "Si$tnum.d" 209 210 # Delete half of what's left, through the first secondary. 211 set quar [expr $half / 2] 212 puts "\tSi$tnum.e:\ 213 Secondary cursor delete loop: deleting $quar entries" 214 set sdb [lindex $sdbs 0] 215 set sdbc [$sdb cursor] 216 set dbt [$sdbc get -first] 217 for { set i 0 } { [llength $dbt] > 0 && $i < $quar } { incr i } { 218 error_check_good sdbc_del [$sdbc del] 0 219 set dbt [$sdbc get -next] 220 } 221 error_check_good sdbc_close [$sdbc close] 0 222 cursor_check_secondaries $pdb $sdbs $quar "Si$tnum.e" 223 224 foreach sdb $sdbs { 225 error_check_good secondary_close [$sdb close] 0 226 } 227 error_check_good primary_close [$pdb close] 0 228 229 # Close the env if it was created within this test. 230 if { $eindex == -1 } { 231 error_check_good env_close [$env close] 0 232 } 233} 234