1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2005,2008 Oracle.  All rights reserved.
4#
5# $Id: test113.tcl,v 12.10 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test113
8# TEST	Test database compaction with duplicates.
9# TEST
10# TEST	This is essentially test111 with duplicates.
11# TEST	To make it simple we use numerical keys all the time.
12# TEST
13# TEST	Dump and save contents.  Compact the database, dump again,
14# TEST	and make sure we still have the same contents.
15# TEST  Add back some entries, delete more entries (this time by
16# TEST	cursor), dump, compact, and do the before/after check again.
17
18proc test113 { method {nentries 10000} {ndups 5} {tnum "113"} args } {
19	source ./include.tcl
20	global alphabet
21
22	# Compaction and duplicates can occur only with btree.
23	if { [is_btree $method] != 1 } {
24		puts "Skipping test$tnum for method $method."
25		return
26	}
27
28        # Skip for specified pagesizes.  This test uses a small
29	# pagesize to generate a deep tree.
30	set pgindex [lsearch -exact $args "-pagesize"]
31	if { $pgindex != -1 } {
32		puts "Test$tnum: Skipping for specific pagesizes"
33		return
34	}
35
36	set args [convert_args $method $args]
37	set omethod [convert_method $method]
38
39	# If we are using an env, then testfile should just be the db name.
40	# Otherwise it is the test directory and the name.
41	set eindex [lsearch -exact $args "-env"]
42	set txnenv 0
43	set txn ""
44	if { $eindex == -1 } {
45		set testfile $testdir/test$tnum.db
46		set env NULL
47	} else {
48		set testfile test$tnum.db
49		incr eindex
50		set env [lindex $args $eindex]
51		set rpcenv [is_rpcenv $env]
52		if { $rpcenv == 1 } {
53			puts "Test$tnum: skipping for RPC"
54			return
55		}
56		set txnenv [is_txnenv $env]
57		if { $txnenv == 1 } {
58			append args " -auto_commit "
59		}
60		set testdir [get_home $env]
61	}
62
63	puts "Test$tnum: $method ($args)\
64	    Database compaction with duplicates."
65
66	set t1 $testdir/t1
67	set t2 $testdir/t2
68	cleanup $testdir $env
69
70	set db [eval {berkdb_open -create -pagesize 512 \
71	    -dup -dupsort -mode 0644} $args $omethod $testfile]
72	error_check_good dbopen [is_valid_db $db] TRUE
73
74	puts "\tTest$tnum.a: Populate database with dups."
75	if { $txnenv == 1 } {
76		set t [$env txn]
77		error_check_good txn [is_valid_txn $t $env] TRUE
78		set txn "-txn $t"
79	}
80	for { set i 1 } { $i <= $nentries } { incr i } {
81		set key $i
82		for { set j 1 } { $j <= $ndups } { incr j } {
83			set str $i.$j.$alphabet
84			set ret [eval \
85			    {$db put} $txn {$key [chop_data $method $str]}]
86			error_check_good put $ret 0
87		}
88	}
89	if { $txnenv == 1 } {
90		error_check_good txn_commit [$t commit] 0
91	}
92	error_check_good db_sync [$db sync] 0
93
94	if { $env != "NULL" } {
95		set testdir [get_home $env]
96		set filename $testdir/$testfile
97	} else {
98		set filename $testfile
99	}
100	set size1 [file size $filename]
101	set free1 [stat_field $db stat "Pages on freelist"]
102
103	puts "\tTest$tnum.b: Delete most entries from database."
104	if { $txnenv == 1 } {
105		set t [$env txn]
106		error_check_good txn [is_valid_txn $t $env] TRUE
107		set txn "-txn $t"
108	}
109	for { set i $nentries } { $i >= 0 } { incr i -1 } {
110		set key $i
111
112		# Leave every n'th item.
113		set n 7
114		if { [expr $i % $n] != 0 } {
115			set ret [eval {$db del} $txn {$key}]
116			error_check_good del $ret 0
117		}
118	}
119	if { $txnenv == 1 } {
120		error_check_good txn_commit [$t commit] 0
121	}
122	error_check_good db_sync [$db sync] 0
123
124	puts "\tTest$tnum.c: Do a dump_file on contents."
125	if { $txnenv == 1 } {
126		set t [$env txn]
127		error_check_good txn [is_valid_txn $t $env] TRUE
128		set txn "-txn $t"
129	}
130	dump_file $db $txn $t1
131	if { $txnenv == 1 } {
132		error_check_good txn_commit [$t commit] 0
133	}
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 db_verify [verify_dir $testdir] 0
139
140	set size2 [file size $filename]
141	set free2 [stat_field $db stat "Pages on freelist"]
142	error_check_good pages_freed [expr $free2 > $free1] 1
143	set reduction .80
144	error_check_good file_size [expr [expr $size1 * $reduction] > $size2] 1
145
146	puts "\tTest$tnum.e: Check that contents are the same after compaction."
147	if { $txnenv == 1 } {
148		set t [$env txn]
149		error_check_good txn [is_valid_txn $t $env] TRUE
150		set txn "-txn $t"
151	}
152	dump_file $db $txn $t2
153	if { $txnenv == 1 } {
154		error_check_good txn_commit [$t commit] 0
155	}
156	error_check_good filecmp [filecmp $t1 $t2] 0
157
158	puts "\tTest$tnum.f: Add more entries to database."
159	if { $txnenv == 1 } {
160		set t [$env txn]
161		error_check_good txn [is_valid_txn $t $env] TRUE
162		set txn "-txn $t"
163	}
164	for { set i 1 } { $i <= $nentries } { incr i } {
165		set key $i
166		for { set j 1 } { $j <= $ndups } { incr j } {
167			set str $i.$j.$alphabet.extra
168			set ret [eval \
169			    {$db put} $txn {$key [chop_data $method $str]}]
170			error_check_good put $ret 0
171		}
172	}
173	if { $txnenv == 1 } {
174		error_check_good txn_commit [$t commit] 0
175	}
176	error_check_good db_sync [$db sync] 0
177
178	set size3 [file size $filename]
179	set free3 [stat_field $db stat "Pages on freelist"]
180
181	puts "\tTest$tnum.g: Remove more entries, this time by cursor."
182	set i 0
183	set n 11
184	if { $txnenv == 1 } {
185		set t [$env txn]
186		error_check_good txn [is_valid_txn $t $env] TRUE
187		set txn "-txn $t"
188	}
189	set dbc [eval {$db cursor} $txn]
190
191	for { set dbt [$dbc get -first] } { [llength $dbt] > 0 }\
192	    { set dbt [$dbc get -next] ; incr i } {
193
194		if { [expr $i % $n] != 0 } {
195			error_check_good dbc_del [$dbc del] 0
196		}
197	}
198	error_check_good cursor_close [$dbc close] 0
199	if { $txnenv == 1 } {
200		error_check_good txn_commit [$t commit] 0
201	}
202	error_check_good db_sync [$db sync] 0
203
204	puts "\tTest$tnum.h: Save contents."
205	dump_file $db "" $t1
206
207	puts "\tTest$tnum.i: Compact database again."
208	set ret [$db compact -freespace]
209	error_check_good db_sync [$db sync] 0
210	error_check_good db_verify [verify_dir $testdir] 0
211
212	set size4 [file size $filename]
213	set free4 [stat_field $db stat "Pages on freelist"]
214	error_check_good pages_freed [expr $free4 > $free3] 1
215	error_check_good file_size [expr [expr $size3 * $reduction] > $size4] 1
216
217	puts "\tTest$tnum.j: Check that contents are the same after compaction."
218	dump_file $db "" $t2
219	error_check_good filecmp [filecmp $t1 $t2] 0
220
221	error_check_good db_close [$db close] 0
222}
223