test038.tcl
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. # See the file LICENSE for redistribution information.
  2. #
  3. # Copyright (c) 1996, 1997, 1998, 1999, 2000
  4. # Sleepycat Software.  All rights reserved.
  5. #
  6. # $Id: test038.tcl,v 11.12 2000/08/25 14:21:56 sue Exp $
  7. #
  8. # DB Test 38 {access method}
  9. # Use the first 10,000 entries from the dictionary.
  10. # Insert each with self as key and "ndups" duplicates
  11. # For the data field, prepend the letters of the alphabet
  12. # in a random order so that we force the duplicate sorting
  13. # code to do something.
  14. # By setting ndups large, we can make this an off-page test
  15. # After all are entered; test the DB_GET_BOTH functionality
  16. # first by retrieving each dup in the file explicitly.  Then
  17. # remove each duplicate and try DB_GET_BOTH again.
  18. proc test038 { method {nentries 10000} {ndups 5} {tnum 38} args } {
  19. global alphabet
  20. global rand_init
  21. source ./include.tcl
  22. berkdb srand $rand_init
  23. set args [convert_args $method $args]
  24. set omethod [convert_method $method]
  25. # Create the database and open the dictionary
  26. set eindex [lsearch -exact $args "-env"]
  27. #
  28. # If we are using an env, then testfile should just be the db name.
  29. # Otherwise it is the test directory and the name.
  30. if { $eindex == -1 } {
  31. set testfile $testdir/test0$tnum.db
  32. set checkdb $testdir/checkdb.db
  33. set env NULL
  34. } else {
  35. set testfile test0$tnum.db
  36. set checkdb checkdb.db
  37. incr eindex
  38. set env [lindex $args $eindex]
  39. }
  40. set t1 $testdir/t1
  41. set t2 $testdir/t2
  42. set t3 $testdir/t3
  43. cleanup $testdir $env
  44. puts "Test0$tnum: 
  45.     $method ($args) $nentries small sorted dup key/data pairs"
  46. if { [is_record_based $method] == 1 || 
  47.     [is_rbtree $method] == 1 } {
  48. puts "Test0$tnum skipping for method $method"
  49. return
  50. }
  51. set db [eval {berkdb_open -create -truncate -mode 0644 
  52. $omethod -dup -dupsort} $args {$testfile}]
  53. error_check_good dbopen [is_valid_db $db] TRUE
  54. set did [open $dict]
  55. set check_db [berkdb_open 
  56.      -create -truncate -mode 0644 -hash $checkdb]
  57. error_check_good dbopen:check_db [is_valid_db $check_db] TRUE
  58. set pflags ""
  59. set gflags ""
  60. set txn ""
  61. set count 0
  62. # Here is the loop where we put and get each key/data pair
  63. puts "tTest0$tnum.a: Put/get loop"
  64. set dbc [eval {$db cursor} $txn]
  65. error_check_good cursor_open [is_substr $dbc $db] 1
  66. while { [gets $did str] != -1 && $count < $nentries } {
  67. set dups ""
  68. for { set i 1 } { $i <= $ndups } { incr i } {
  69. set pref 
  70.     [string index $alphabet [berkdb random_int 0 25]]
  71. set pref $pref[string 
  72.     index $alphabet [berkdb random_int 0 25]]
  73. while { [string first $pref $dups] != -1 } {
  74. set pref [string toupper $pref]
  75. if { [string first $pref $dups] != -1 } {
  76. set pref [string index $alphabet 
  77.     [berkdb random_int 0 25]]
  78. set pref $pref[string index $alphabet 
  79.     [berkdb random_int 0 25]]
  80. }
  81. }
  82. if { [string length $dups] == 0 } {
  83. set dups $pref
  84. } else {
  85. set dups "$dups $pref"
  86. }
  87. set datastr $pref:$str
  88. set ret [eval {$db put} 
  89.     $txn $pflags {$str [chop_data $method $datastr]}]
  90. error_check_good put $ret 0
  91. }
  92. set ret [eval {$check_db put} 
  93.     $txn $pflags {$str [chop_data $method $dups]}]
  94. error_check_good checkdb_put $ret 0
  95. # Now retrieve all the keys matching this key
  96. set x 0
  97. set lastdup ""
  98. for {set ret [$dbc get -set $str]} 
  99.     {[llength $ret] != 0} 
  100.     {set ret [$dbc get -nextdup] } {
  101. set k [lindex [lindex $ret 0] 0]
  102. if { [string compare $k $str] != 0 } {
  103. break
  104. }
  105. set datastr [lindex [lindex $ret 0] 1]
  106. if {[string length $datastr] == 0} {
  107. break
  108. }
  109. if {[string compare $lastdup $datastr] > 0} {
  110. error_check_good sorted_dups($lastdup,$datastr)
  111.     0 1
  112. }
  113. incr x
  114. set lastdup $datastr
  115. }
  116. error_check_good "Test0$tnum:ndups:$str" $x $ndups
  117. incr count
  118. }
  119. error_check_good cursor_close [$dbc close] 0
  120. close $did
  121. # Now check the duplicates, then delete then recheck
  122. puts "tTest0$tnum.b: Checking and Deleting duplicates"
  123. set dbc [eval {$db cursor} $txn]
  124. error_check_good cursor_open [is_substr $dbc $db] 1
  125. set check_c [eval {$check_db cursor} $txn]
  126. error_check_good cursor_open [is_substr $check_c $check_db] 1
  127. for {set ndx 0} {$ndx < $ndups} {incr ndx} {
  128. for {set ret [$check_c get -first]} 
  129.     {[llength $ret] != 0} 
  130.     {set ret [$check_c get -next] } {
  131. set k [lindex [lindex $ret 0] 0]
  132. set d [lindex [lindex $ret 0] 1]
  133. error_check_bad data_check:$d [string length $d] 0
  134. set nn [expr $ndx * 3]
  135. set pref [string range $d $nn [expr $nn + 1]]
  136. set data $pref:$k
  137. set ret [eval {$dbc get} $txn {-get_both $k $data}]
  138. error_check_good 
  139.     get_both_key:$k [lindex [lindex $ret 0] 0] $k
  140. error_check_good 
  141.     get_both_data:$k [lindex [lindex $ret 0] 1] $data
  142. set ret [$dbc del]
  143. error_check_good del $ret 0
  144. set ret [eval {$db get} $txn {-get_both $k $data}]
  145. error_check_good error_case:$k [llength $ret] 0
  146. if {$ndx != 0} {
  147. set n [expr ($ndx - 1) * 3]
  148. set pref [string range $d $n [expr $n + 1]]
  149. set data $pref:$k
  150. set ret 
  151.     [eval {$db get} $txn {-get_both $k $data}]
  152. error_check_good error_case:$k [llength $ret] 0
  153. }
  154. }
  155. }
  156. error_check_good check_c:close [$check_c close] 0
  157. error_check_good check_db:close [$check_db close] 0
  158. error_check_good dbc_close [$dbc close] 0
  159. error_check_good db_close [$db close] 0
  160. }