1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2005,2008 Oracle.  All rights reserved.
4#
5# $Id: test112.tcl,v 12.14 2008/01/08 20:58:53 bostic Exp $
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 and recno databases only.
24	if { [is_hash $method] == 1 || [is_queue $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
40	# If we are using an env, then testfile should just be the db name.
41	# Otherwise it is the test directory and the name.
42	set txnenv 0
43	set txn ""
44	set eindex [lsearch -exact $args "-env"]
45	if { $eindex == -1 } {
46		set testfile $testdir/test$tnum.db
47		set env NULL
48	} else {
49		set testfile test$tnum.db
50		incr eindex
51		set env [lindex $args $eindex]
52		set rpcenv [is_rpcenv $env]
53		if { $rpcenv == 1 } {
54			puts "Test$tnum: skipping for RPC"
55			return
56		}
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	set size1 [file size $filename]
105	set levels [stat_field $db stat "Levels"]
106	error_check_good enough_levels [expr $levels >= 4] 1
107	set free1 [stat_field $db stat "Pages on freelist"]
108
109	puts "\tTest$tnum.b: Delete most entries from database."
110	# Leave every nth item.  Since rrecno renumbers, we
111	# delete starting at nentries and working down to 0.
112	if { $txnenv == 1 } {
113		set t [$env txn]
114		error_check_good txn [is_valid_txn $t $env] TRUE
115		set txn "-txn $t"
116	}
117	for { set i $nentries } { $i > 0 } { incr i -1 } {
118		set key $i
119
120		# Leave every n'th item.
121		set n 121
122		if { [expr $i % $n] != 0 } {
123			set ret [eval {$db del} $txn {$key}]
124			error_check_good del $ret 0
125		}
126	}
127	if { $txnenv == 1 } {
128		error_check_good txn_commit [$t commit] 0
129	}
130	error_check_good db_sync [$db sync] 0
131
132	puts "\tTest$tnum.c: Do a dump_file on contents."
133	dump_file $db "" $t1
134
135	puts "\tTest$tnum.d: Compact database."
136	set ret [$db compact -freespace]
137	error_check_good db_sync [$db sync] 0
138	error_check_good verify_dir [verify_dir $testdir] 0
139
140	set size2 [file size $filename]
141	set free2 [stat_field $db stat "Pages on freelist"]
142
143	# The on-disk file size should be significantly smaller.
144	set reduction .80
145	error_check_good file_size [expr [expr $size1 * $reduction] > $size2] 1
146
147	# Pages should be freed for all methods except maybe
148	# record-based non-queue methods.  Even with recno, the
149	# number of free pages may not decline.
150
151	if { [is_record_based $method] == 1 } {
152		error_check_good pages_freed [expr $free2 >= $free1] 1
153	} else {
154		error_check_good pages_freed [expr $free2 > $free1] 1
155	}
156
157	# Also, we should have reduced the number of levels.
158	set newlevels [stat_field $db stat "Levels"]
159	error_check_good fewer_levels [expr $newlevels < $levels ] 1
160
161	puts "\tTest$tnum.e: Check that contents are the same after compaction."
162	dump_file $db "" $t2
163	error_check_good filecmp [filecmp $t1 $t2] 0
164
165	puts "\tTest$tnum.f: Add more entries to database."
166	if { $txnenv == 1 } {
167		set t [$env txn]
168		error_check_good txn [is_valid_txn $t $env] TRUE
169		set txn "-txn $t"
170	}
171	for { set i 1 } { $i < $nentries } { incr i } {
172		set key $i
173		set str $i.$alphabet
174		set ret [eval \
175		    {$db put} $txn {$key [chop_data $method $str]}]
176		error_check_good put $ret 0
177	}
178	if { $txnenv == 1 } {
179		error_check_good txn_commit [$t commit] 0
180	}
181	error_check_good db_sync [$db sync] 0
182
183	set size3 [file size $filename]
184	set free3 [stat_field $db stat "Pages on freelist"]
185
186	puts "\tTest$tnum.g: Remove more entries, this time by cursor."
187	set i 0
188	set n 11
189	if { $txnenv == 1 } {
190		set t [$env txn]
191		error_check_good txn [is_valid_txn $t $env] TRUE
192		set txn "-txn $t"
193	}
194	set dbc [eval {$db cursor} $txn]
195
196	for { set dbt [$dbc get -first] } { [llength $dbt] > 0 }\
197	    { set dbt [$dbc get -next] ; incr i } {
198		if { [expr $i % $n] != 0 } {
199			error_check_good dbc_del [$dbc del] 0
200		}
201	}
202	error_check_good cursor_close [$dbc close] 0
203	if { $txnenv == 1 } {
204		error_check_good txn_commit [$t commit] 0
205	}
206	error_check_good db_sync [$db sync] 0
207
208	puts "\tTest$tnum.h: Save contents."
209	if { $txnenv == 1 } {
210		set t [$env txn]
211		error_check_good txn [is_valid_txn $t $env] TRUE
212		set txn "-txn $t"
213	}
214	dump_file $db $txn $t1
215	if { $txnenv == 1 } {
216		error_check_good txn_commit [$t commit] 0
217	}
218
219	puts "\tTest$tnum.i: Compact database again."
220	set ret [$db compact -freespace]
221	error_check_good db_sync [$db sync] 0
222	error_check_good verify_dir [verify_dir $testdir] 0
223
224	set size4 [file size $filename]
225	set free4 [stat_field $db stat "Pages on freelist"]
226
227	error_check_good file_size [expr [expr $size3 * $reduction] > $size4] 1
228	if { [is_record_based $method] == 1 } {
229		error_check_good pages_freed [expr $free4 >= $free3] 1
230	} else {
231		error_check_good pages_freed [expr $free4 > $free3] 1
232	}
233
234	puts "\tTest$tnum.j: Check that contents are the same after compaction."
235	dump_file $db "" $t2
236	error_check_good filecmp [filecmp $t1 $t2] 0
237
238	error_check_good db_close [$db close] 0
239}
240
241