archive.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: archive.tcl,v 11.14 2000/10/27 13:23:55 sue Exp $
  7. #
  8. # Options are:
  9. # -checkrec <checkpoint frequency"
  10. # -dir <dbhome directory>
  11. # -maxfilesize <maxsize of log file>
  12. # -stat
  13. proc archive_usage {} {
  14. puts "archive -checkrec <checkpt freq> -dir <directory> 
  15.     -maxfilesize <max size of log files>"
  16. }
  17. proc archive_command { args } {
  18. source ./include.tcl
  19. # Catch a list of files output by db_archive.
  20. catch { eval exec $util_path/db_archive $args } output
  21. if { $is_windows_test == 1 || 1 } {
  22. # On Windows, convert all filenames to use forward slashes.
  23. regsub -all {[\]} $output / output
  24. }
  25. # Output the [possibly-transformed] list.
  26. return $output
  27. }
  28. proc archive { args } {
  29. global alphabet
  30. source ./include.tcl
  31. # Set defaults
  32. set maxbsize [expr 8 * 1024]
  33. set maxfile [expr 32 * 1024]
  34. set dostat 0
  35. set checkrec 500
  36. for { set i 0 } { $i < [llength $args] } {incr i} {
  37. switch -regexp -- [lindex $args $i] {
  38. -c.* { incr i; set checkrec [lindex $args $i] }
  39. -d.* { incr i; set testdir [lindex $args $i] }
  40. -m.* { incr i; set maxfile [lindex $args $i] }
  41. -s.* { set dostat 1 }
  42. default {
  43. puts -nonewline "FAIL:[timestamp] Usage: "
  44. archive_usage
  45. return
  46. }
  47. }
  48. }
  49. # Clean out old log if it existed
  50. puts "Unlinking log: error message OK"
  51. env_cleanup $testdir
  52. # Now run the various functionality tests
  53. set eflags "-create -txn -home $testdir 
  54.     -log_buffer $maxbsize -log_max $maxfile"
  55. set dbenv [eval {berkdb env} $eflags]
  56. error_check_bad dbenv $dbenv NULL
  57. error_check_good dbenv [is_substr $dbenv env] 1
  58. # The basic test structure here is that we write a lot of log
  59. # records (enough to fill up 100 log files; each log file it
  60. # small).  We take periodic checkpoints.  Between each pair
  61. # of checkpoints, we refer to 2 files, overlapping them each
  62. # checkpoint.  We also start transactions and let them overlap
  63. # checkpoints as well.  The pattern that we try to create is:
  64. # ---- write log records----|||||--- write log records ---
  65. # -T1 T2 T3 --- D1 D2 ------CHECK--- CT1 --- D2 D3 CD1 ----CHECK
  66. # where TX is begin transaction, CTx is commit transaction, DX is
  67. # open data file and CDx is close datafile.
  68. set baserec "1:$alphabet:2:$alphabet:3:$alphabet:4:$alphabet"
  69. puts "Archive.a: Writing log records; checkpoint every $checkrec records"
  70. set nrecs $maxfile
  71. set rec 0:$baserec
  72. # Begin transaction and write a log record
  73. set t1 [$dbenv txn]
  74. error_check_good t1:txn_begin [is_substr $t1 "txn"] 1
  75. set l1 [$dbenv log_put $rec]
  76. error_check_bad l1:log_put [llength $l1] 0
  77. set lsnlist [list [lindex $l1 0]]
  78. set t2 [$dbenv txn]
  79. error_check_good t2:txn_begin [is_substr $t2 "txn"] 1
  80. set l1 [$dbenv log_put $rec]
  81. lappend lsnlist [lindex $l1 0]
  82. set t3 [$dbenv txn]
  83. set l1 [$dbenv log_put $rec]
  84. lappend lsnlist [lindex $l1 0]
  85. set txnlist [list $t1 $t2 $t3]
  86. set db1 [eval {berkdb_open} "-create -mode 0644 -hash -env $dbenv ar1"]
  87. set db2 [eval {berkdb_open} "-create -mode 0644 -btree -env $dbenv ar2"]
  88. set dbcount 3
  89. set dblist [list $db1 $db2]
  90. for { set i 1 } { $i <= $nrecs } { incr i } {
  91. set rec $i:$baserec
  92. set lsn [$dbenv log_put $rec]
  93. error_check_bad log_put [llength $lsn] 0
  94. if { [expr $i % $checkrec] == 0 } {
  95. # Take a checkpoint
  96. $dbenv txn_checkpoint
  97. set ckp_file [lindex [lindex [$dbenv log_get -last] 0] 0]
  98. catch { archive_command -h $testdir -a } res_log_full
  99. if { [string first db_archive $res_log_full] == 0 } {
  100. set res_log_full ""
  101. }
  102. catch { archive_command -h $testdir } res_log
  103. if { [string first db_archive $res_log] == 0 } {
  104. set res_log ""
  105. }
  106. catch { archive_command -h $testdir -l } res_alllog
  107. catch { archive_command -h $testdir -a -s } 
  108.     res_data_full
  109. catch { archive_command -h $testdir -s } res_data
  110. error_check_good nlogfiles [llength $res_alllog] 
  111.     [lindex [lindex [$dbenv log_get -last] 0] 0]
  112. error_check_good logs_match [llength $res_log_full] 
  113.     [llength $res_log]
  114. error_check_good data_match [llength $res_data_full] 
  115.     [llength $res_data]
  116. # Check right number of log files
  117. error_check_good nlogs [llength $res_log] 
  118.     [expr [lindex $lsnlist 0] - 1]
  119. # Check that the relative names are a subset of the
  120. # full names
  121. set n 0
  122. foreach x $res_log {
  123. error_check_bad log_name_match:$res_log 
  124.     [string first $x 
  125.     [lindex $res_log_full $n]] -1
  126. incr n
  127. }
  128. set n 0
  129. foreach x $res_data {
  130. error_check_bad log_name_match:$res_data 
  131.     [string first $x 
  132.     [lindex $res_data_full $n]] -1
  133. incr n
  134. }
  135. # Begin/commit any transactions
  136. set t [lindex $txnlist 0]
  137. if { [string length $t] != 0 } {
  138. error_check_good txn_commit:$t [$t commit] 0
  139. set txnlist [lrange $txnlist 1 end]
  140. }
  141. set lsnlist [lrange $lsnlist 1 end]
  142. if { [llength $txnlist] == 0 } {
  143. set t1 [$dbenv txn]
  144. error_check_bad tx_begin $t1 NULL
  145. error_check_good 
  146.     tx_begin [is_substr $t1 $dbenv] 1
  147. set l1 [lindex [$dbenv log_put $rec] 0]
  148. lappend lsnlist [min $l1 $ckp_file]
  149. set t2 [$dbenv txn]
  150. error_check_bad tx_begin $t2 NULL
  151. error_check_good 
  152.     tx_begin [is_substr $t2 $dbenv] 1
  153. set l1 [lindex [$dbenv log_put $rec] 0]
  154. lappend lsnlist [min $l1 $ckp_file]
  155. set t3 [$dbenv txn]
  156. error_check_bad tx_begin $t3 NULL
  157. error_check_good 
  158.     tx_begin [is_substr $t3 $dbenv] 1
  159. set l1 [lindex [$dbenv log_put $rec] 0]
  160. lappend lsnlist [min $l1 $ckp_file]
  161. set txnlist [list $t1 $t2 $t3]
  162. }
  163. # Open/close some DB files
  164. if { [expr $dbcount % 2] == 0 } {
  165. set type "-hash"
  166. } else {
  167. set type "-btree"
  168. }
  169. set db [eval {berkdb_open} 
  170.     "-create -mode 0644 $type -env $dbenv ar$dbcount"]
  171. error_check_bad db_open:$dbcount $db NULL
  172. error_check_good db_open:$dbcount [is_substr $db db] 1
  173. incr dbcount
  174. lappend dblist $db
  175. set db [lindex $dblist 0]
  176. error_check_good db_close:$db [$db close] 0
  177. set dblist [lrange $dblist 1 end]
  178. }
  179. }
  180. # Commit any transactions still running.
  181. puts "Archive: Commit any transactions still running."
  182. foreach t $txnlist {
  183. error_check_good txn_commit:$t [$t commit] 0
  184. }
  185. # Close any files that are still open.
  186. puts "Archive: Close open files."
  187. foreach d $dblist {
  188. error_check_good db_close:$db [$d close] 0
  189. }
  190. # Close and unlink the file
  191. reset_env $dbenv
  192. puts "Archive: Complete."
  193. }
  194. proc min { a b } {
  195. if {$a < $b} {
  196. return $a
  197. } else {
  198. return $b
  199. }
  200. }