1# See the file LICENSE for redistribution information. 2# 3# Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved. 4# 5# $Id$ 6# 7# TEST test112 8# TEST Test database compaction with a deep tree. 9# TEST 10# TEST This is a lot like test111, but with a large number of 11# TEST entries and a small page size to make the tree deep. 12# TEST To make it simple we use numerical keys all the time. 13# TEST 14# TEST Dump and save contents. Compact the database, dump again, 15# TEST and make sure we still have the same contents. 16# TEST Add back some entries, delete more entries (this time by 17# TEST cursor), dump, compact, and do the before/after check again. 18 19proc test112 { method {nentries 80000} {tnum "112"} args } { 20 source ./include.tcl 21 global alphabet 22 23 # Compaction is an option for btree, recno, and hash databases. 24 if { [is_queue $method] == 1 || [is_heap $method] == 1} { 25 puts "Skipping test$tnum for method $method." 26 return 27 } 28 29 # Skip for specified pagesizes. This test uses a small 30 # pagesize to generate a deep tree. 31 set pgindex [lsearch -exact $args "-pagesize"] 32 if { $pgindex != -1 } { 33 puts "Test$tnum: Skipping for specific pagesizes" 34 return 35 } 36 37 set args [convert_args $method $args] 38 set omethod [convert_method $method] 39 if { [is_partition_callback $args] == 1 } { 40 set nodump 1 41 } else { 42 set nodump 0 43 } 44 45 # If we are using an env, then testfile should just be the db name. 46 # Otherwise it is the test directory and the name. 47 set txnenv 0 48 set txn "" 49 set eindex [lsearch -exact $args "-env"] 50 if { $eindex == -1 } { 51 set testfile $testdir/test$tnum.db 52 set env NULL 53 } else { 54 set testfile test$tnum.db 55 incr eindex 56 set env [lindex $args $eindex] 57 set txnenv [is_txnenv $env] 58 if { $txnenv == 1 } { 59 append args " -auto_commit " 60 } 61 set testdir [get_home $env] 62 } 63 puts "Test$tnum: $method ($args) Database compaction with deep tree." 64 65 set t1 $testdir/t1 66 set t2 $testdir/t2 67 cleanup $testdir $env 68 69 set db [eval {berkdb_open -create\ 70 -pagesize 512 -mode 0644} $args $omethod $testfile] 71 error_check_good dbopen [is_valid_db $db] TRUE 72 73 if { [is_record_based $method] == 1 } { 74 set checkfunc test001_recno.check 75 } else { 76 set checkfunc test001.check 77 } 78 79 puts "\tTest$tnum.a: Populate database." 80 if { $txnenv == 1 } { 81 set t [$env txn] 82 error_check_good txn [is_valid_txn $t $env] TRUE 83 set txn "-txn $t" 84 } 85 for { set i 1 } { $i <= $nentries } { incr i } { 86 set key $i 87 set str $i.$alphabet 88 89 set ret [eval \ 90 {$db put} $txn {$key [chop_data $method $str]}] 91 error_check_good put $ret 0 92 } 93 if { $txnenv == 1 } { 94 error_check_good txn_commit [$t commit] 0 95 } 96 error_check_good db_sync [$db sync] 0 97 98 if { $env != "NULL" } { 99 set testdir [get_home $env] 100 set filename $testdir/$testfile 101 } else { 102 set filename $testfile 103 } 104 105 # Record file size, page count, and levels. They will 106 # be reduced by compaction. 107 set size1 [file size $filename] 108 set count1 [stat_field $db stat "Page count"] 109 set levels [stat_field $db stat "Levels"] 110# error_check_good enough_levels [expr $levels >= 4] 1 111 112 puts "\tTest$tnum.b: Delete most entries from database." 113 # Leave every nth item. Since rrecno renumbers, we 114 # delete starting at nentries and working down to 0. 115 if { $txnenv == 1 } { 116 set t [$env txn] 117 error_check_good txn [is_valid_txn $t $env] TRUE 118 set txn "-txn $t" 119 } 120 for { set i $nentries } { $i > 0 } { incr i -1 } { 121 set key $i 122 123 # Leave every n'th item. 124 set n 121 125 if { [expr $i % $n] != 0 } { 126 set ret [eval {$db del} $txn {$key}] 127 error_check_good del $ret 0 128 } 129 } 130 if { $txnenv == 1 } { 131 error_check_good txn_commit [$t commit] 0 132 } 133 error_check_good db_sync [$db sync] 0 134 135 puts "\tTest$tnum.c: Do a dump_file on contents." 136 dump_file $db "" $t1 137 138 puts "\tTest$tnum.d: Compact database." 139 for {set commit 0} {$commit <= $txnenv} {incr commit} { 140 if { $txnenv == 1 } { 141 set t [$env txn] 142 error_check_good txn [is_valid_txn $t $env] TRUE 143 set txn "-txn $t" 144 } 145 if {[catch {eval {$db compact} $txn {-freespace}} ret] } { 146 error "FAIL: db compact: $ret" 147 } 148 if { $txnenv == 1 } { 149 if { $commit == 0 } { 150 puts "\tTest$tnum.d: Aborting." 151 error_check_good txn_abort [$t abort] 0 152 } else { 153 puts "\tTest$tnum.d: Committing." 154 error_check_good txn_commit [$t commit] 0 155 } 156 } 157 error_check_good db_sync [$db sync] 0 158 error_check_good verify_dir \ 159 [ verify_dir $testdir "" 0 0 $nodump] 0 160 } 161 162 set size2 [file size $filename] 163 set count2 [stat_field $db stat "Page count"] 164 165 # The on-disk file size should be significantly smaller. 166#### We should look at the partitioned files ##### 167if { [is_partitioned $args] == 0 } { 168 set reduction .80 169 error_check_good file_size [expr [expr $size1 * $reduction] > $size2] 1 170} 171 172 # Also, we should have reduced the number of pages and levels. 173 error_check_good page_count_reduced [expr $count1 > $count2] 1 174 if { [is_hash $method] == 0 } { 175 set newlevels [stat_field $db stat "Levels"] 176 error_check_good fewer_levels [expr $newlevels < $levels ] 1 177 } 178 179 puts "\tTest$tnum.e: Check that contents are the same after compaction." 180 dump_file $db "" $t2 181 if { [is_hash $method] != 0 } { 182 filesort $t1 $t1.sort 183 filesort $t2 $t2.sort 184 error_check_good filecmp [filecmp $t1.sort $t2.sort] 0 185 } else { 186 error_check_good filecmp [filecmp $t1 $t2] 0 187 } 188 189 puts "\tTest$tnum.f: Add more entries to database." 190 if { $txnenv == 1 } { 191 set t [$env txn] 192 error_check_good txn [is_valid_txn $t $env] TRUE 193 set txn "-txn $t" 194 } 195 for { set i 1 } { $i < $nentries } { incr i } { 196 set key $i 197 set str $i.$alphabet 198 set ret [eval \ 199 {$db put} $txn {$key [chop_data $method $str]}] 200 error_check_good put $ret 0 201 } 202 if { $txnenv == 1 } { 203 error_check_good txn_commit [$t commit] 0 204 } 205 error_check_good db_sync [$db sync] 0 206 207 set size3 [file size $filename] 208 set count3 [stat_field $db stat "Page count"] 209 210 puts "\tTest$tnum.g: Remove more entries, this time by cursor." 211 set i 0 212 set n 11 213 if { $txnenv == 1 } { 214 set t [$env txn] 215 error_check_good txn [is_valid_txn $t $env] TRUE 216 set txn "-txn $t" 217 } 218 set dbc [eval {$db cursor} $txn] 219 220 for { set dbt [$dbc get -first] } { [llength $dbt] > 0 }\ 221 { set dbt [$dbc get -next] ; incr i } { 222 if { [expr $i % $n] != 0 } { 223 error_check_good dbc_del [$dbc del] 0 224 } 225 } 226 error_check_good cursor_close [$dbc close] 0 227 if { $txnenv == 1 } { 228 error_check_good txn_commit [$t commit] 0 229 } 230 error_check_good db_sync [$db sync] 0 231 232 puts "\tTest$tnum.h: Save contents." 233 if { $txnenv == 1 } { 234 set t [$env txn] 235 error_check_good txn [is_valid_txn $t $env] TRUE 236 set txn "-txn $t" 237 } 238 dump_file $db $txn $t1 239 if { $txnenv == 1 } { 240 error_check_good txn_commit [$t commit] 0 241 } 242 243 puts "\tTest$tnum.i: Compact database again." 244 for {set commit 0} {$commit <= $txnenv} {incr commit} { 245 if { $txnenv == 1 } { 246 set t [$env txn] 247 error_check_good txn [is_valid_txn $t $env] TRUE 248 set txn "-txn $t" 249 } 250 if {[catch {eval {$db compact} $txn {-freespace}} ret] } { 251 error "FAIL: db compact: $ret" 252 } 253 if { $txnenv == 1 } { 254 if { $commit == 0 } { 255 puts "\tTest$tnum.d: Aborting." 256 error_check_good txn_abort [$t abort] 0 257 } else { 258 puts "\tTest$tnum.d: Committing." 259 error_check_good txn_commit [$t commit] 0 260 } 261 } 262 error_check_good db_sync [$db sync] 0 263 error_check_good verify_dir \ 264 [ verify_dir $testdir "" 0 0 $nodump] 0 265 } 266 267 set size4 [file size $filename] 268 set count4 [stat_field $db stat "Page count"] 269 270#### We should look at the partitioned files ##### 271if { [is_partitioned $args] == 0 } { 272set reduction .9 273# puts "$size3 $size4" 274 error_check_good file_size [expr [expr $size3 * $reduction] > $size4] 1 275} 276 277 error_check_good page_count_reduced [expr $count3 > $count4] 1 278 279 puts "\tTest$tnum.j: Check that contents are the same after compaction." 280 dump_file $db "" $t2 281 if { [is_hash $method] != 0 } { 282 filesort $t1 $t1.sort 283 filesort $t2 $t2.sort 284 error_check_good filecmp [filecmp $t1.sort $t2.sort] 0 285 } else { 286 error_check_good filecmp [filecmp $t1 $t2] 0 287 } 288 289 error_check_good db_close [$db close] 0 290} 291 292