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

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: test024.tcl,v 11.14 2000/08/25 14:21:55 sue Exp $
  7. #
  8. # DB Test 24 {method nentries}
  9. # Test the Btree and Record number get-by-number functionality.
  10. proc test024 { method {nentries 10000} args} {
  11. source ./include.tcl
  12. global rand_init
  13. set do_renumber [is_rrecno $method]
  14. set args [convert_args $method $args]
  15. set omethod [convert_method $method]
  16. puts "Test024: $method ($args)"
  17. if { [string compare $omethod "-hash"] == 0 } {
  18. puts "Test024 skipping for method HASH"
  19. return
  20. }
  21. berkdb srand $rand_init
  22. # Create the database and open the dictionary
  23. set eindex [lsearch -exact $args "-env"]
  24. #
  25. # If we are using an env, then testfile should just be the db name.
  26. # Otherwise it is the test directory and the name.
  27. if { $eindex == -1 } {
  28. set testfile $testdir/test024.db
  29. set env NULL
  30. } else {
  31. set testfile test024.db
  32. incr eindex
  33. set env [lindex $args $eindex]
  34. }
  35. set t1 $testdir/t1
  36. set t2 $testdir/t2
  37. set t3 $testdir/t3
  38. cleanup $testdir $env
  39. # Read the first nentries dictionary elements and reverse them.
  40. # Keep a list of these (these will be the keys).
  41. puts "tTest024.a: initialization"
  42. set keys ""
  43. set did [open $dict]
  44. set count 0
  45. while { [gets $did str] != -1 && $count < $nentries } {
  46. lappend keys [reverse $str]
  47. incr count
  48. }
  49. close $did
  50. # Generate sorted order for the keys
  51. set sorted_keys [lsort $keys]
  52. # Create the database
  53. if { [string compare $omethod "-btree"] == 0 } {
  54. set db [eval {berkdb_open -create -truncate 
  55. -mode 0644 -recnum} $args {$omethod $testfile}]
  56. error_check_good dbopen [is_valid_db $db] TRUE
  57. } else  {
  58. set db [eval {berkdb_open -create -truncate 
  59. -mode 0644} $args {$omethod $testfile}]
  60. error_check_good dbopen [is_valid_db $db] TRUE
  61. }
  62. set pflags ""
  63. set gflags ""
  64. set txn ""
  65. if { [is_record_based $method] == 1 } {
  66. set gflags " -recno"
  67. }
  68. puts "tTest024.b: put/get loop"
  69. foreach k $keys {
  70. if { [is_record_based $method] == 1 } {
  71. set key [lsearch $sorted_keys $k]
  72. incr key
  73. } else {
  74. set key $k
  75. }
  76. set ret [eval {$db put} 
  77.     $txn $pflags {$key [chop_data $method $k]}]
  78. error_check_good put $ret 0
  79. set ret [eval {$db get} $txn $gflags {$key}]
  80. error_check_good 
  81.     get $ret [list [list $key [pad_data $method $k]]]
  82. }
  83. # Now we will get each key from the DB and compare the results
  84. # to the original.
  85. puts "tTest024.c: dump file"
  86. # Put sorted keys in file
  87. set oid [open $t1 w]
  88. foreach k $sorted_keys {
  89. puts $oid [pad_data $method $k]
  90. }
  91. close $oid
  92. # Instead of using dump_file; get all the keys by keynum
  93. set oid [open $t2 w]
  94. if { [string compare $omethod "-btree"] == 0 } {
  95. set do_renumber 1
  96. }
  97. set gflags " -recno"
  98. for { set k 1 } { $k <= $count } { incr k } {
  99. set ret [eval {$db get} $txn $gflags {$k}]
  100. puts $oid [lindex [lindex $ret 0] 1]
  101. error_check_good recnum_get [lindex [lindex $ret 0] 1] 
  102.     [pad_data $method [lindex $sorted_keys [expr $k - 1]]]
  103. }
  104. close $oid
  105. error_check_good db_close [$db close] 0
  106. error_check_good Test024.c:diff($t1,$t2) 
  107.     [filecmp $t1 $t2] 0
  108. # Now, reopen the file and run the last test again.
  109. puts "tTest024.d: close, open, and dump file"
  110. set db [eval {berkdb_open -rdonly} $args $testfile]
  111. error_check_good dbopen [is_valid_db $db] TRUE
  112. set oid [open $t2 w]
  113. for { set k 1 } { $k <= $count } { incr k } {
  114. set ret [eval {$db get} $txn $gflags {$k}]
  115. puts $oid [lindex [lindex $ret 0] 1]
  116. error_check_good recnum_get [lindex [lindex $ret 0] 1] 
  117.     [pad_data $method [lindex $sorted_keys [expr $k - 1]]]
  118. }
  119. close $oid
  120. error_check_good db_close [$db close] 0
  121. error_check_good Test024.d:diff($t1,$t2) 
  122.     [filecmp $t1 $t2] 0
  123. # Now, reopen the file and run the last test again in reverse direction.
  124. puts "tTest024.e: close, open, and dump file in reverse direction"
  125. set db [eval {berkdb_open -rdonly} $args $testfile]
  126. error_check_good dbopen [is_valid_db $db] TRUE
  127. # Put sorted keys in file
  128. set rsorted ""
  129. foreach k $sorted_keys {
  130. set rsorted [linsert $rsorted 0 $k]
  131. }
  132. set oid [open $t1 w]
  133. foreach k $rsorted {
  134. puts $oid [pad_data $method $k]
  135. }
  136. close $oid
  137. set oid [open $t2 w]
  138. for { set k $count } { $k > 0 } { incr k -1 } {
  139. set ret [eval {$db get} $txn $gflags {$k}]
  140. puts $oid [lindex [lindex $ret 0] 1]
  141. error_check_good recnum_get [lindex [lindex $ret 0] 1] 
  142.     [pad_data $method [lindex $sorted_keys [expr $k - 1]]]
  143. }
  144. close $oid
  145. error_check_good db_close [$db close] 0
  146. error_check_good Test024.e:diff($t1,$t2) 
  147.     [filecmp $t1 $t2] 0
  148. # Now try deleting elements and making sure they work
  149. puts "tTest024.f: delete test"
  150. set db [eval {berkdb_open} $args $testfile]
  151. error_check_good dbopen [is_valid_db $db] TRUE
  152. while { $count > 0 } {
  153. set kndx [berkdb random_int 1 $count]
  154. set kval [lindex $keys [expr $kndx - 1]]
  155. set recno [expr [lsearch $sorted_keys $kval] + 1]
  156. if { [is_record_based $method] == 1 } {
  157. set ret [eval {$db del} $txn {$recno}]
  158. } else {
  159. set ret [eval {$db del} $txn {$kval}]
  160. }
  161. error_check_good delete $ret 0
  162. # Remove the key from the key list
  163. set ndx [expr $kndx - 1]
  164. set keys [lreplace $keys $ndx $ndx]
  165. if { $do_renumber == 1 } {
  166. set r [expr $recno - 1]
  167. set sorted_keys [lreplace $sorted_keys $r $r]
  168. }
  169. # Check that the keys after it have been renumbered
  170. if { $do_renumber == 1 && $recno != $count } {
  171. set r [expr $recno - 1]
  172. set ret [eval {$db get} $txn $gflags {$recno}]
  173. error_check_good get_after_del 
  174.     [lindex [lindex $ret 0] 1] [lindex $sorted_keys $r]
  175. }
  176. # Decrement count
  177. incr count -1
  178. }
  179. error_check_good db_close [$db close] 0
  180. }