1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2001,2008 Oracle.  All rights reserved.
4#
5# $Id: si007.tcl,v 12.9 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	si007
8# TEST	Secondary index put/delete with lorder test
9# TEST
10# TEST	This test is the same as si001 with the exception
11# TEST	that we create and populate the primary and THEN
12# TEST	create the secondaries and associate them with -create.
13
14proc si007 { methods {nentries 10} {tnum "007"} args } {
15	source ./include.tcl
16	global dict nsecondaries
17
18	# Primary method/args.
19	set pmethod [lindex $methods 0]
20	set pargs [convert_args $pmethod $args]
21	if [big_endian] {
22		set nativeargs " -lorder 4321"
23		set swappedargs " -lorder 1234"
24	} else {
25		set swappedargs " -lorder 4321"
26		set nativeargs " -lorder 1234"
27	}
28	set argtypes "{$nativeargs} {$swappedargs}"
29	set pomethod [convert_method $pmethod]
30
31	# Renumbering recno databases can't be used as primaries.
32	if { [is_rrecno $pmethod] == 1 } {
33		puts "Skipping si$tnum for method $pmethod"
34		return
35	}
36
37	# Method/args for all the secondaries.  If only one method
38	# was specified, assume the same method (for btree or hash)
39	# and a standard number of secondaries.  If primary is not
40	# btree or hash, force secondaries to be one btree, one hash.
41	set methods [lrange $methods 1 end]
42	if { [llength $methods] == 0 } {
43		for { set i 0 } { $i < $nsecondaries } { incr i } {
44			if { [is_btree $pmethod] || [is_hash $pmethod] } {
45				lappend methods $pmethod
46			} else {
47				if { [expr $i % 2] == 0 } {
48					lappend methods "-btree"
49				} else {
50					lappend methods "-hash"
51				}
52			}
53		}
54	}
55
56	set argses [convert_argses $methods $args]
57	set omethods [convert_methods $methods]
58
59	# If we are given an env, use it.  Otherwise, open one.
60	set eindex [lsearch -exact $args "-env"]
61	if { $eindex == -1 } {
62		env_cleanup $testdir
63		set env [berkdb_env -create -home $testdir]
64		error_check_good env_open [is_valid_env $env] TRUE
65	} else {
66		incr eindex
67		set env [lindex $args $eindex]
68		set envflags [$env get_open_flags]
69		if { [lsearch -exact $envflags "-thread"] != -1 &&\
70			[is_queue $pmethod] == 1 } {
71			puts "Skipping si$tnum for threaded env"
72			return
73		}
74		set testdir [get_home $env]
75	}
76
77	set pname "primary$tnum.db"
78	set snamebase "secondary$tnum"
79
80	foreach pbyteargs $argtypes {
81		foreach sbyteargs $argtypes {
82			if { $pbyteargs == $nativeargs } {
83				puts "Si$tnum: Using native\
84				    byteorder $nativeargs for primary."
85			} else {
86				puts "Si$tnum: Using swapped\
87				    byteorder $swappedargs for primary."
88			}
89			if { $sbyteargs == $nativeargs } {
90				puts "Si$tnum: Using native\
91				    byteorder $nativeargs for secondaries."
92			} else {
93				puts "Si$tnum: Using swapped\
94				    byteorder $swappedargs for secondaries."
95			}
96
97			puts "si$tnum\
98			    \{\[ list $pmethod $methods \]\} $nentries"
99			cleanup $testdir $env
100
101			# Open primary.
102			set pdb [eval {berkdb_open -create -env} $env \
103			    $pomethod $pargs $pbyteargs $pname]
104			error_check_good primary_open [is_valid_db $pdb] TRUE
105
106			puts "\tSi$tnum.a: Populate primary."
107			# Open dictionary.  Leave it open until done
108			# with test .e so append won't require
109			# configuration for duplicates.
110			set did [open $dict]
111			for { set n 0 } \
112			    { [gets $did str] != -1 && $n < $nentries } \
113			    { incr n } {
114				if { [is_record_based $pmethod] == 1 } {
115					set key [expr $n + 1]
116					set datum $str
117				} else {
118					set key $str
119					gets $did datum
120				}
121				set keys($n) $key
122				set data($n) [pad_data $pmethod $datum]
123
124				set ret [eval {$pdb put}\
125				    {$key [chop_data $pmethod $datum]}]
126				error_check_good put($n) $ret 0
127			}
128
129			# Open and associate the secondaries, with -create.
130			puts "\tSi$tnum.b: Associate secondaries with -create."
131			set sdbs {}
132			for { set i 0 } \
133			    { $i < [llength $omethods] } { incr i } {
134				set sdb [eval {berkdb_open -create -env} $env \
135				    [lindex $omethods $i] [lindex $argses $i] \
136				    $sbyteargs $snamebase.$i.db]
137				error_check_good\
138				    second_open($i) [is_valid_db $sdb] TRUE
139
140				error_check_good db_associate($i) \
141				    [$pdb associate -create [callback_n $i] $sdb] 0
142				lappend sdbs $sdb
143			}
144			check_secondaries\
145			    $pdb $sdbs $nentries keys data "Si$tnum.c"
146
147			puts "\tSi$tnum.c: Closing/disassociating primary first"
148			error_check_good primary_close [$pdb close] 0
149			foreach sdb $sdbs {
150				error_check_good secondary_close [$sdb close] 0
151			}
152
153			# Don't close the env if this test was given one.
154			# Skip the test of truncating the secondary since
155			# we can't close and reopen the outside env.
156			if { $eindex == -1 } {
157				error_check_good env_close [$env close] 0
158
159				# Reopen with _noerr for test of
160				# truncate secondary.
161				puts "\tSi$tnum.h:\
162				    Truncate secondary (should fail)"
163
164				set env [berkdb_env_noerr\
165				    -create -home $testdir]
166				error_check_good\
167				    env_open [is_valid_env $env] TRUE
168
169				set pdb [eval {berkdb_open_noerr -create -env}\
170				    $env $pomethod $pargs $pname]
171				set sdb [eval {berkdb_open_noerr -create -env}\
172				    $env [lindex $omethods 0]\
173				    [lindex $argses 0] $snamebase.0.db ]
174				$pdb associate [callback_n 0] $sdb
175
176				set ret [catch {$sdb truncate} ret]
177				error_check_good trunc_secondary $ret 1
178
179				error_check_good primary_close [$pdb close] 0
180				error_check_good secondary_close [$sdb close] 0
181			}
182		}
183	}
184	# If this test made the last env, close it.
185	if { $eindex == -1 } {
186		error_check_good env_close [$env close] 0
187	}
188}
189