1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.
#
# $Id$
#
# TEST test093
# TEST Test set_bt_compare (btree key comparison function) and
# TEST set_h_compare (hash key comparison function).
# TEST
# TEST Open a database with a comparison function specified,
# TEST populate, and close, saving a list with that key order as
# TEST we do so. Reopen and read in the keys, saving in another
# TEST list; the keys should be in the order specified by the
# TEST comparison function. Sort the original saved list of keys
# TEST using the comparison function, and verify that it matches
# TEST the keys as read out of the database.
proc test093 { method {nentries 10000} {tnum "093"} args} {
source ./include.tcl
set dbargs [convert_args $method $args]
set omethod [convert_method $method]
if { [is_btree $method] == 1 } {
set compflag -btcompare
} elseif { [is_hash $method] == 1 } {
set compflag -hashcompare
} else {
puts "Test$tnum: skipping for method $method."
return
}
# Verification fails for 64k pages on some systems because
# verify's private mpool gets filled. The Tcl API doesn't
# currently allow for resizing this cache. Just skip the
# test for 64k pages because it's not adding much anyway.
#
set pagesize 0
set pgindex [lsearch -exact $args "-pagesize"]
if { $pgindex != -1 } {
incr pgindex
set pagesize [lindex $args $pgindex]
if { $pagesize >= "65536" } {
puts "Skipping test$tnum for 64k pages."
return
}
}
set txnenv 0
set eindex [lsearch -exact $dbargs "-env"]
if { $eindex != -1 } {
incr eindex
set env [lindex $dbargs $eindex]
set envflags [$env get_open_flags]
# We can't run this test for the -thread option because
# the comparison function requires the ability to allocate
# memory at the DBT level and our Tcl interface does not
# offer that.
if { [lsearch -exact $envflags "-thread"] != -1 } {
puts "Skipping Test$tnum for threaded env"
return
}
set txnenv [is_txnenv $env]
if { $txnenv == 1 } {
append dbargs " -auto_commit "
if { $nentries == 10000 } {
set nentries 100
}
}
set testdir [get_home $env]
cleanup $testdir $env
} else {
set env NULL
}
puts "Test$tnum: $method ($args) $nentries entries using $compflag"
test093_run $omethod $dbargs $nentries $tnum \
$compflag test093_cmp1 test093_sort1
test093_runbig $omethod $dbargs $nentries $tnum \
$compflag test093_cmp1 test093_sort1
test093_run $omethod $dbargs $nentries $tnum \
$compflag test093_cmp2 test093_sort2
# Don't bother running the second, really slow, comparison
# function on test093_runbig (file contents).
# Clean up so general verification (without the custom comparison
# function) doesn't fail.
if { $env != "NULL" } {
set testdir [get_home $env]
}
cleanup $testdir $env
}
proc test093_run { method dbargs nentries tnum compflag cmpfunc sortfunc } {
source ./include.tcl
global btvals
global btvalsck
# We'll need any encryption args separated from the db args
# so we can pass them to dbverify.
set encargs ""
set dbargs [split_encargs $dbargs encargs]
# If we are using an env, then testfile should just be the db name.
# Otherwise it is the test directory and the name.
set eindex [lsearch -exact $dbargs "-env"]
set txnenv 0
if { $eindex == -1 } {
set testfile $testdir/test$tnum.db
set env NULL
set envargs ""
} else {
set testfile test$tnum.db
incr eindex
set env [lindex $dbargs $eindex]
set envargs " -env $env "
set txnenv [is_txnenv $env]
set testdir [get_home $env]
}
cleanup $testdir $env
set db [eval {berkdb_open $compflag $cmpfunc \
-create -mode 0644} $method $encargs $dbargs $testfile]
error_check_good dbopen [is_valid_db $db] TRUE
set did [open $dict]
set t1 $testdir/t1
set t2 $testdir/t2
set t3 $testdir/t3
set txn ""
# Use btvals to save the order of the keys as they are
# written to the database. The btvalsck variable will contain
# the values as sorted by the comparison function.
set btvals {}
set btvalsck {}
puts "\tTest$tnum.a: put/get loop"
# Here is the loop where we put and get each key/data pair
set count 0
while { [gets $did str] != -1 && $count < $nentries } {
set key $str
set str [reverse $str]
if { $txnenv == 1 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
}
set ret [eval \
{$db put} $txn {$key [chop_data $method $str]}]
error_check_good put $ret 0
if { $txnenv == 1 } {
error_check_good txn [$t commit] 0
}
lappend btvals $key
set ret [eval {$db get $key}]
error_check_good \
get $ret [list [list $key [pad_data $method $str]]]
incr count
}
close $did
# Now we will get each key from the DB and compare the results
# to the original.
puts "\tTest$tnum.b: dump file"
if { $txnenv == 1 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
}
dump_file $db $txn $t1 test093_check
if { $txnenv == 1 } {
error_check_good txn [$t commit] 0
}
error_check_good db_close [$db close] 0
# Run verify to check the internal structure and order.
if { [catch {eval {berkdb dbverify} $compflag $cmpfunc\
$envargs $encargs {$testfile}} res] } {
error "FAIL: Verification failed with $res"
}
# Now compare the keys to see if they match the dictionary (or ints)
filehead $nentries $dict $t2
filesort $t2 $t3
file rename -force $t3 $t2
filesort $t1 $t3
error_check_good Test$tnum:diff($t3,$t2) \
[filecmp $t3 $t2] 0
puts "\tTest$tnum.c: dump file in order"
# Now, reopen the file and run the last test again.
# We open it here, ourselves, because all uses of the db
# need to have the correct comparison func set. Then
# call dump_file_direction directly.
set btvalsck {}
set db [eval {berkdb_open $compflag $cmpfunc -rdonly} \
$dbargs $encargs $method $testfile]
error_check_good dbopen [is_valid_db $db] TRUE
if { $txnenv == 1 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
}
dump_file_direction $db $txn $t1 test093_check "-first" "-next"
if { $txnenv == 1 } {
error_check_good txn [$t commit] 0
}
error_check_good db_close [$db close] 0
if { [is_hash $method] == 1 || [is_partition_callback $dbargs] == 1 } {
return
}
# We need to sort btvals according to the comparison function.
# Once that is done, btvalsck and btvals should be the same.
puts "\tTest$tnum.d: check file order"
$sortfunc
error_check_good btvals:len [llength $btvals] [llength $btvalsck]
for {set i 0} {$i < $nentries} {incr i} {
error_check_good vals:$i [lindex $btvals $i] \
[lindex $btvalsck $i]
}
}
proc test093_runbig { method dbargs nentries tnum compflag cmpfunc sortfunc } {
source ./include.tcl
global btvals
global btvalsck
# We'll need any encryption args separated from the db args
# so we can pass them to dbverify.
set encargs ""
set dbargs [split_encargs $dbargs encargs]
# Create the database and open the dictionary
set eindex [lsearch -exact $dbargs "-env"]
#
# If we are using an env, then testfile should just be the db name.
# Otherwise it is the test directory and the name.
set txnenv 0
if { $eindex == -1 } {
set testfile $testdir/test$tnum.db
set env NULL
set envargs ""
} else {
set testfile test$tnum.db
incr eindex
set env [lindex $dbargs $eindex]
set envargs " -env $env "
set txnenv [is_txnenv $env]
set testdir [get_home $env]
}
cleanup $testdir $env
set db [eval {berkdb_open $compflag $cmpfunc \
-create -mode 0644} $method $encargs $dbargs $testfile]
error_check_good dbopen [is_valid_db $db] TRUE
set t1 $testdir/t1
set t2 $testdir/t2
set t3 $testdir/t3
set t4 $testdir/t4
set t5 $testdir/t5
set txn ""
set btvals {}
set btvalsck {}
puts "\tTest$tnum.e:\
big key put/get loop key=filecontents data=filename"
# Here is the loop where we put and get each key/data pair
set file_list [get_file_list 1]
set count 0
foreach f $file_list {
set fid [open $f r]
fconfigure $fid -translation binary
set key [read $fid]
close $fid
set key $f$key
set fcopy [open $t5 w]
fconfigure $fcopy -translation binary
puts -nonewline $fcopy $key
close $fcopy
if { $txnenv == 1 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
}
set ret [eval {$db put} $txn {$key \
[chop_data $method $f]}]
error_check_good put_file $ret 0
if { $txnenv == 1 } {
error_check_good txn [$t commit] 0
}
lappend btvals $key
# Should really catch errors
set fid [open $t4 w]
fconfigure $fid -translation binary
if [catch {eval {$db get} {$key}} data] {
puts -nonewline $fid $data
} else {
# Data looks like {{key data}}
set key [lindex [lindex $data 0] 0]
puts -nonewline $fid $key
}
close $fid
error_check_good \
Test093:diff($t5,$t4) [filecmp $t5 $t4] 0
incr count
}
# Now we will get each key from the DB and compare the results
# to the original.
puts "\tTest$tnum.f: big dump file"
if { $txnenv == 1 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
}
dump_file $db $txn $t1 test093_checkbig
if { $txnenv == 1 } {
error_check_good txn [$t commit] 0
}
error_check_good db_close [$db close] 0
# Run verify to check the internal structure and order.
if { [catch {eval {berkdb dbverify} $compflag $cmpfunc\
$envargs $encargs {$testfile}} res] } {
error "FAIL: Verification failed with $res"
}
puts "\tTest$tnum.g: dump file in order"
# Now, reopen the file and run the last test again.
# We open it here, ourselves, because all uses of the db
# need to have the correct comparison func set. Then
# call dump_file_direction directly.
set btvalsck {}
set db [eval {berkdb_open $compflag $cmpfunc -rdonly} \
$encargs $dbargs $method $testfile]
error_check_good dbopen [is_valid_db $db] TRUE
if { $txnenv == 1 } {
set t [$env txn]
error_check_good txn [is_valid_txn $t $env] TRUE
set txn "-txn $t"
}
dump_file_direction $db $txn $t1 test093_checkbig "-first" "-next"
if { $txnenv == 1 } {
error_check_good txn [$t commit] 0
}
error_check_good db_close [$db close] 0
if { [is_hash $method] == 1 || [is_partition_callback $dbargs] == 1 } {
return
}
# We need to sort btvals according to the comparison function.
# Once that is done, btvalsck and btvals should be the same.
puts "\tTest$tnum.h: check file order"
$sortfunc
error_check_good btvals:len [llength $btvals] [llength $btvalsck]
set end [llength $btvals]
for {set i 0} {$i < $end} {incr i} {
error_check_good vals:$i [lindex $btvals $i] \
[lindex $btvalsck $i]
}
}
# Simple bt comparison.
proc test093_cmp1 { a b } {
return [string compare $b $a]
}
# Simple bt sorting.
proc test093_sort1 {} {
global btvals
#
# This one is easy, just sort in reverse.
#
set btvals [lsort -decreasing $btvals]
}
proc test093_cmp2 { a b } {
set arev [reverse $a]
set brev [reverse $b]
return [string compare $arev $brev]
}
proc test093_sort2 {} {
global btvals
# We have to reverse them, then sorts them.
# Then reverse them back to real words.
set rbtvals {}
foreach i $btvals {
lappend rbtvals [reverse $i]
}
set rbtvals [lsort -increasing $rbtvals]
set newbtvals {}
foreach i $rbtvals {
lappend newbtvals [reverse $i]
}
set btvals $newbtvals
}
# Check function for test093; keys and data are identical
proc test093_check { key data } {
global btvalsck
error_check_good "key/data mismatch" $data [reverse $key]
lappend btvalsck $key
}
# Check function for test093 big keys;
proc test093_checkbig { key data } {
source ./include.tcl
global btvalsck
set fid [open $data r]
fconfigure $fid -translation binary
set cont [read $fid]
close $fid
error_check_good "key/data mismatch" $key $data$cont
lappend btvalsck $key
}
|