1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996,2008 Oracle.  All rights reserved.
4#
5# $Id: test010.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test010
8# TEST	Duplicate test
9# TEST		Small key/data pairs.
10# TEST
11# TEST	Use the first 10,000 entries from the dictionary.
12# TEST	Insert each with self as key and data; add duplicate records for each.
13# TEST	After all are entered, retrieve all; verify output.
14# TEST	Close file, reopen, do retrieve and re-verify.
15# TEST	This does not work for recno
16proc test010 { method {nentries 10000} {ndups 5} {tnum "010"} args } {
17	source ./include.tcl
18
19	set omethod $method
20	set args [convert_args $method $args]
21	set omethod [convert_method $method]
22
23	if { [is_record_based $method] == 1 || \
24	    [is_rbtree $method] == 1 } {
25		puts "Test$tnum skipping for method $method"
26		return
27	}
28
29	# Create the database and open the dictionary
30	set txnenv 0
31	set eindex [lsearch -exact $args "-env"]
32	#
33	# If we are using an env, then testfile should just be the db name.
34	# Otherwise it is the test directory and the name.
35	if { $eindex == -1 } {
36		set testfile $testdir/test$tnum.db
37		set env NULL
38	} else {
39		set testfile test$tnum.db
40		incr eindex
41		set env [lindex $args $eindex]
42		set txnenv [is_txnenv $env]
43		if { $txnenv == 1 } {
44			append args " -auto_commit "
45			#
46			# If we are using txns and running with the
47			# default, set the default down a bit.
48			#
49			if { $nentries == 10000 } {
50				set nentries 100
51			}
52			reduce_dups nentries ndups
53		}
54		set testdir [get_home $env]
55	}
56	puts "Test$tnum: $method ($args) $nentries \
57	    small $ndups dup key/data pairs"
58
59	set t1 $testdir/t1
60	set t2 $testdir/t2
61	set t3 $testdir/t3
62
63	cleanup $testdir $env
64
65	set db [eval {berkdb_open \
66	     -create -mode 0644 -dup} $args {$omethod $testfile}]
67	error_check_good dbopen [is_valid_db $db] TRUE
68
69	set did [open $dict]
70
71	set pflags ""
72	set gflags ""
73	set txn ""
74	set count 0
75
76	# Here is the loop where we put and get each key/data pair
77	while { [gets $did str] != -1 && $count < $nentries } {
78		for { set i 1 } { $i <= $ndups } { incr i } {
79			set datastr $i:$str
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			set ret [eval {$db put} \
86			    $txn $pflags {$str [chop_data $method $datastr]}]
87			error_check_good put $ret 0
88			if { $txnenv == 1 } {
89				error_check_good txn [$t commit] 0
90			}
91		}
92
93		# Now retrieve all the keys matching this key
94		set x 1
95		if { $txnenv == 1 } {
96			set t [$env txn]
97			error_check_good txn [is_valid_txn $t $env] TRUE
98			set txn "-txn $t"
99		}
100		set dbc [eval {$db cursor} $txn]
101		for {set ret [$dbc get "-set" $str]} \
102		    {[llength $ret] != 0} \
103		    {set ret [$dbc get "-next"] } {
104			if {[llength $ret] == 0} {
105				break
106			}
107			set k [lindex [lindex $ret 0] 0]
108			if { [string compare $k $str] != 0 } {
109				break
110			}
111			set datastr [lindex [lindex $ret 0] 1]
112			set d [data_of $datastr]
113			error_check_good "Test$tnum:get" $d $str
114			set id [ id_of $datastr ]
115			error_check_good "Test$tnum:dup#" $id $x
116			incr x
117		}
118		error_check_good "Test$tnum:ndups:$str" [expr $x - 1] $ndups
119		error_check_good cursor_close [$dbc close] 0
120		if { $txnenv == 1 } {
121			error_check_good txn [$t commit] 0
122		}
123
124		incr count
125	}
126	close $did
127
128	# Now we will get each key from the DB and compare the results
129	# to the original.
130	puts "\tTest$tnum.a: Checking file for correct duplicates"
131	set dlist ""
132	for { set i 1 } { $i <= $ndups } {incr i} {
133		lappend dlist $i
134	}
135	if { $txnenv == 1 } {
136		set t [$env txn]
137		error_check_good txn [is_valid_txn $t $env] TRUE
138		set txn "-txn $t"
139	}
140	dup_check $db $txn $t1 $dlist
141	if { $txnenv == 1 } {
142		error_check_good txn [$t commit] 0
143	}
144
145	# Now compare the keys to see if they match the dictionary entries
146	set q q
147	filehead $nentries $dict $t3
148	filesort $t3 $t2
149	filesort $t1 $t3
150
151	error_check_good Test$tnum:diff($t3,$t2) \
152	    [filecmp $t3 $t2] 0
153
154	error_check_good db_close [$db close] 0
155	set db [eval {berkdb_open} $args $testfile]
156	error_check_good dbopen [is_valid_db $db] TRUE
157
158	puts "\tTest$tnum.b: Checking file for correct duplicates after close"
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	dup_check $db $txn $t1 $dlist
165	if { $txnenv == 1 } {
166		error_check_good txn [$t commit] 0
167	}
168
169	# Now compare the keys to see if they match the dictionary entries
170	filesort $t1 $t3
171	error_check_good Test$tnum:diff($t3,$t2) \
172	    [filecmp $t3 $t2] 0
173
174	error_check_good db_close [$db close] 0
175}
176