1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996,2008 Oracle.  All rights reserved.
4#
5# $Id: test017.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test017
8# TEST	Basic offpage duplicate test.
9# TEST
10# TEST	Run duplicates with small page size so that we test off page duplicates.
11# TEST	Then after we have an off-page database, test with overflow pages too.
12proc test017 { method {contents 0} {ndups 19} {tnum "017"} args } {
13	source ./include.tcl
14
15	set args [convert_args $method $args]
16	set omethod [convert_method $method]
17
18	if { [is_record_based $method] == 1 || [is_rbtree $method] == 1 } {
19		puts "Test$tnum skipping for method $method"
20		return
21	}
22	set pgindex [lsearch -exact $args "-pagesize"]
23	if { $pgindex != -1 } {
24		incr pgindex
25		if { [lindex $args $pgindex] > 8192 } {
26			puts "Test$tnum: Skipping for large pagesizes"
27			return
28		}
29	}
30
31	# Create the database and open the dictionary
32	set limit 0
33	set txnenv 0
34	set eindex [lsearch -exact $args "-env"]
35	#
36	# If we are using an env, then testfile should just be the db name.
37	# Otherwise it is the test directory and the name.
38	if { $eindex == -1 } {
39		set testfile $testdir/test$tnum.db
40		set env NULL
41	} else {
42		set testfile test$tnum.db
43		incr eindex
44		set env [lindex $args $eindex]
45		set txnenv [is_txnenv $env]
46		if { $txnenv == 1 } {
47			append args " -auto_commit "
48			set limit 100
49		}
50		set testdir [get_home $env]
51	}
52	set t1 $testdir/t1
53	set t2 $testdir/t2
54	set t3 $testdir/t3
55	set t4 $testdir/t4
56
57	cleanup $testdir $env
58
59	set db [eval {berkdb_open \
60	     -create -mode 0644 -dup} $args {$omethod $testfile}]
61	error_check_good dbopen [is_valid_db $db] TRUE
62
63	set pflags ""
64	set gflags ""
65	set txn ""
66	set count 0
67
68	set file_list [get_file_list 1]
69	if { $txnenv == 1 } {
70		if { [llength $file_list] > $limit } {
71			set file_list [lrange $file_list 0 $limit]
72		}
73		set flen [llength $file_list]
74		reduce_dups flen ndups
75	}
76	puts "Test$tnum: $method ($args) Off page duplicate tests\
77	    with $ndups duplicates"
78
79	set ovfl ""
80	# Here is the loop where we put and get each key/data pair
81	puts -nonewline "\tTest$tnum.a: Creating duplicates with "
82	if { $contents != 0 } {
83		puts "file contents as key/data"
84	} else {
85		puts "file name as key/data"
86	}
87	foreach f $file_list {
88		if { $contents != 0 } {
89			set fid [open $f r]
90			fconfigure $fid -translation binary
91			#
92			# Prepend file name to guarantee uniqueness
93			set filecont [read $fid]
94			set str $f:$filecont
95			close $fid
96		} else {
97			set str $f
98		}
99		for { set i 1 } { $i <= $ndups } { incr i } {
100			set datastr $i:$str
101			if { $txnenv == 1 } {
102				set t [$env txn]
103				error_check_good txn [is_valid_txn $t $env] TRUE
104				set txn "-txn $t"
105			}
106			set ret [eval {$db put} \
107			    $txn $pflags {$str [chop_data $method $datastr]}]
108			error_check_good put $ret 0
109			if { $txnenv == 1 } {
110				error_check_good txn [$t commit] 0
111			}
112		}
113
114		#
115		# Save 10% files for overflow test
116		#
117		if { $contents == 0 && [expr $count % 10] == 0 } {
118			lappend ovfl $f
119		}
120		# Now retrieve all the keys matching this key
121		set ret [$db get $str]
122		error_check_bad $f:dbget_dups [llength $ret] 0
123		error_check_good $f:dbget_dups1 [llength $ret] $ndups
124		set x 1
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		set dbc [eval {$db cursor} $txn]
131		for {set ret [$dbc get "-set" $str]} \
132		    {[llength $ret] != 0} \
133		    {set ret [$dbc get "-next"] } {
134			set k [lindex [lindex $ret 0] 0]
135			if { [string compare $k $str] != 0 } {
136				break
137			}
138			set datastr [lindex [lindex $ret 0] 1]
139			set d [data_of $datastr]
140			if {[string length $d] == 0} {
141				break
142			}
143			error_check_good "Test$tnum:get" $d $str
144			set id [ id_of $datastr ]
145			error_check_good "Test$tnum:$f:dup#" $id $x
146			incr x
147		}
148		error_check_good "Test$tnum:ndups:$str" [expr $x - 1] $ndups
149		error_check_good cursor_close [$dbc close] 0
150		if { $txnenv == 1 } {
151			error_check_good txn [$t commit] 0
152		}
153		incr count
154	}
155
156	# Now we will get each key from the DB and compare the results
157	# to the original.
158	puts "\tTest$tnum.b: Checking file for correct duplicates"
159	set dlist ""
160	for { set i 1 } { $i <= $ndups } {incr i} {
161		lappend dlist $i
162	}
163	set oid [open $t2.tmp w]
164	set o1id [open $t4.tmp w]
165	foreach f $file_list {
166		for {set i 1} {$i <= $ndups} {incr i} {
167			puts $o1id $f
168		}
169		puts $oid $f
170	}
171	close $oid
172	close $o1id
173	filesort $t2.tmp $t2
174	filesort $t4.tmp $t4
175	fileremove $t2.tmp
176	fileremove $t4.tmp
177
178	if { $txnenv == 1 } {
179		set t [$env txn]
180		error_check_good txn [is_valid_txn $t $env] TRUE
181		set txn "-txn $t"
182	}
183	dup_check $db $txn $t1 $dlist
184	if { $txnenv == 1 } {
185		error_check_good txn [$t commit] 0
186	}
187	if {$contents == 0} {
188		filesort $t1 $t3
189
190		error_check_good Test$tnum:diff($t3,$t2) [filecmp $t3 $t2] 0
191
192		# Now compare the keys to see if they match the file names
193		if { $txnenv == 1 } {
194			set t [$env txn]
195			error_check_good txn [is_valid_txn $t $env] TRUE
196			set txn "-txn $t"
197		}
198		dump_file $db $txn $t1 test017.check
199		if { $txnenv == 1 } {
200			error_check_good txn [$t commit] 0
201		}
202		filesort $t1 $t3
203
204		error_check_good Test$tnum:diff($t3,$t4) [filecmp $t3 $t4] 0
205	}
206
207	error_check_good db_close [$db close] 0
208	set db [eval {berkdb_open} $args $testfile]
209	error_check_good dbopen [is_valid_db $db] TRUE
210
211	puts "\tTest$tnum.c: Checking file for correct duplicates after close"
212	if { $txnenv == 1 } {
213		set t [$env txn]
214		error_check_good txn [is_valid_txn $t $env] TRUE
215		set txn "-txn $t"
216	}
217	dup_check $db $txn $t1 $dlist
218	if { $txnenv == 1 } {
219		error_check_good txn [$t commit] 0
220	}
221
222	if {$contents == 0} {
223		# Now compare the keys to see if they match the filenames
224		filesort $t1 $t3
225		error_check_good Test$tnum:diff($t3,$t2) [filecmp $t3 $t2] 0
226	}
227	error_check_good db_close [$db close] 0
228
229	puts "\tTest$tnum.d: Verify off page duplicates and overflow status"
230	set db [eval {berkdb_open} $args $testfile]
231	error_check_good dbopen [is_valid_db $db] TRUE
232	set stat [$db stat]
233	if { [is_btree $method] } {
234		error_check_bad stat:offpage \
235		    [is_substr $stat "{{Internal pages} 0}"] 1
236	}
237	if {$contents == 0} {
238		# This check doesn't work in hash, since overflow
239		# pages count extra pages in buckets as well as true
240		# P_OVERFLOW pages.
241		if { [is_hash $method] == 0 } {
242			error_check_good overflow \
243			    [is_substr $stat "{{Overflow pages} 0}"] 1
244		}
245	} else {
246		if { [is_hash $method] } {
247			error_check_bad overflow \
248			    [is_substr $stat "{{Number of big pages} 0}"] 1
249		} else {
250			error_check_bad overflow \
251			    [is_substr $stat "{{Overflow pages} 0}"] 1
252		}
253	}
254
255	#
256	# If doing overflow test, do that now.  Else we are done.
257	# Add overflow pages by adding a large entry to a duplicate.
258	#
259	if { [llength $ovfl] == 0} {
260		error_check_good db_close [$db close] 0
261		return
262	}
263
264	puts "\tTest$tnum.e: Add overflow duplicate entries"
265	set ovfldup [expr $ndups + 1]
266	foreach f $ovfl {
267		#
268		# This is just like put_file, but prepends the dup number
269		#
270		set fid [open $f r]
271		fconfigure $fid -translation binary
272		set fdata [read $fid]
273		close $fid
274		set data $ovfldup:$fdata:$fdata:$fdata:$fdata
275
276		if { $txnenv == 1 } {
277			set t [$env txn]
278			error_check_good txn [is_valid_txn $t $env] TRUE
279			set txn "-txn $t"
280		}
281		set ret [eval {$db put} $txn $pflags {$f $data}]
282		error_check_good ovfl_put $ret 0
283		if { $txnenv == 1 } {
284			error_check_good txn [$t commit] 0
285		}
286	}
287
288	puts "\tTest$tnum.f: Verify overflow duplicate entries"
289	if { $txnenv == 1 } {
290		set t [$env txn]
291		error_check_good txn [is_valid_txn $t $env] TRUE
292		set txn "-txn $t"
293	}
294	dup_check $db $txn $t1 $dlist $ovfldup
295	if { $txnenv == 1 } {
296		error_check_good txn [$t commit] 0
297	}
298	filesort $t1 $t3
299	error_check_good Test$tnum:diff($t3,$t2) [filecmp $t3 $t2] 0
300
301	set stat [$db stat]
302	if { [is_hash [$db get_type]] } {
303		error_check_bad overflow1_hash [is_substr $stat \
304		    "{{Number of big pages} 0}"] 1
305	} else {
306		error_check_bad \
307		    overflow1 [is_substr $stat "{{Overflow pages} 0}"] 1
308	}
309	error_check_good db_close [$db close] 0
310}
311
312# Check function; verify data contains key
313proc test017.check { key data } {
314	error_check_good "data mismatch for key $key" $key [data_of $data]
315}
316