1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2001,2008 Oracle.  All rights reserved.
4#
5# $Id: si002.tcl,v 12.15 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	si002
8# TEST	Basic cursor-based secondary index put/delete test
9# TEST
10# TEST  Cursor put data in primary db and check that pget
11# TEST  on secondary index finds the right entries.
12# TEST  Overwrite while walking primary, check pget again.
13# TEST      Overwrite while walking secondary (use c_pget), check
14# TEST  pget again.
15# TEST  Cursor delete half of entries through primary, check.
16# TEST  Cursor delete half of remainder through secondary, check.
17proc si002 { methods {nentries 200} {tnum "002"} args } {
18	source ./include.tcl
19	global dict nsecondaries
20
21	# Primary method/args.
22	set pmethod [lindex $methods 0]
23	set pargs [convert_args $pmethod $args]
24	set pomethod [convert_method $pmethod]
25
26	# Renumbering recno databases can't be used as primaries.
27	if { [is_rrecno $pmethod] == 1 } {
28		puts "Skipping si$tnum for method $pmethod"
29		return
30	}
31
32	# Method/args for all the secondaries.  If only one method
33	# was specified, assume the same method (for btree or hash)
34	# and a standard number of secondaries.  If primary is not
35	# btree or hash, force secondaries to be one btree, one hash.
36	set methods [lrange $methods 1 end]
37	if { [llength $methods] == 0 } {
38		for { set i 0 } { $i < $nsecondaries } { incr i } {
39			if { [is_btree $pmethod] || [is_hash $pmethod] } {
40				lappend methods $pmethod
41			} else {
42				if { [expr $i % 2] == 0 } {
43					lappend methods "-btree"
44				} else {
45					lappend methods "-hash"
46				}
47			}
48		}
49	}
50
51	set argses [convert_argses $methods $args]
52	set omethods [convert_methods $methods]
53
54	# If we are given an env, use it.  Otherwise, open one.
55	set eindex [lsearch -exact $args "-env"]
56	if { $eindex == -1 } {
57		env_cleanup $testdir
58		set env [berkdb_env -create -home $testdir]
59		error_check_good env_open [is_valid_env $env] TRUE
60	} else {
61		incr eindex
62		set env [lindex $args $eindex]
63		set envflags [$env get_open_flags]
64		if { [lsearch -exact $envflags "-thread"] != -1 } {
65			puts "Skipping si$tnum for threaded env"
66			return
67		}
68		set testdir [get_home $env]
69	}
70
71	puts "si$tnum \{\[ list $pmethod $methods \]\} $nentries"
72	cleanup $testdir $env
73
74	set pname "primary$tnum.db"
75	set snamebase "secondary$tnum"
76
77	# Open the primary.
78	set pdb [eval {berkdb_open -create -env} $env $pomethod $pargs $pname]
79	error_check_good primary_open [is_valid_db $pdb] TRUE
80
81	# Open and associate the secondaries
82	set sdbs {}
83	for { set i 0 } { $i < [llength $omethods] } { incr i } {
84		set sdb [eval {berkdb_open -create -env} $env \
85		    [lindex $omethods $i] [lindex $argses $i] $snamebase.$i.db]
86		error_check_good second_open($i) [is_valid_db $sdb] TRUE
87
88		error_check_good db_associate($i) \
89		    [$pdb associate [callback_n $i] $sdb] 0
90		lappend sdbs $sdb
91	}
92
93	set did [open $dict]
94
95	# Populate with a cursor, exercising keyfirst/keylast.
96	puts "\tSi$tnum.a: Cursor put (-keyfirst/-keylast) loop"
97	set pdbc [$pdb cursor]
98	error_check_good pdb_cursor [is_valid_cursor $pdbc $pdb] TRUE
99	for { set n 0 } { [gets $did str] != -1 && $n < $nentries } { incr n } {
100
101		if { [is_record_based $pmethod] == 1 } {
102			set key [expr $n + 1]
103			set datum $str
104		} else {
105			set key $str
106			gets $did datum
107		}
108
109		set ns($key) $n
110		set keys($n) $key
111		set data($n) [pad_data $pmethod $datum]
112
113		if { $n % 2 == 0 } {
114			set pflag " -keyfirst "
115		} else {
116			set pflag " -keylast "
117		}
118
119		set ret [eval {$pdbc put} $pflag \
120		    {$key [chop_data $pmethod $datum]}]
121		error_check_good put($n) $ret 0
122	}
123	error_check_good pdbc_close [$pdbc close] 0
124
125	close $did
126	check_secondaries $pdb $sdbs $nentries keys data "Si$tnum.a"
127
128	puts "\tSi$tnum.b: Cursor put overwrite (-current) loop"
129	set pdbc [$pdb cursor]
130	error_check_good pdb_cursor [is_valid_cursor $pdbc $pdb] TRUE
131	for { set dbt [$pdbc get -first] } { [llength $dbt] > 0 } \
132	    { set dbt [$pdbc get -next] } {
133		set key [lindex [lindex $dbt 0] 0]
134		set datum [lindex [lindex $dbt 0] 1]
135		set newd $datum.$key
136		set ret [eval {$pdbc put -current} [chop_data $pmethod $newd]]
137		error_check_good put_overwrite($key) $ret 0
138		set data($ns($key)) [pad_data $pmethod $newd]
139	}
140	error_check_good pdbc_close [$pdbc close] 0
141	check_secondaries $pdb $sdbs $nentries keys data "Si$tnum.b"
142
143	puts "\tSi$tnum.c: Secondary c_pget/primary put overwrite loop"
144	# We walk the first secondary, then put-overwrite each primary key/data
145	# pair we find.  This doubles as a DBC->c_pget test.
146	set sdb [lindex $sdbs 0]
147	set sdbc [$sdb cursor]
148	error_check_good sdb_cursor [is_valid_cursor $sdbc $sdb] TRUE
149	for { set dbt [$sdbc pget -first] } { [llength $dbt] > 0 } \
150	    { set dbt [$sdbc pget -next] } {
151		set pkey [lindex [lindex $dbt 0] 1]
152		set pdatum [lindex [lindex $dbt 0] 2]
153
154		# Extended entries will be showing up underneath us, in
155		# unpredictable places.  Keep track of which pkeys
156		# we've extended, and don't extend them repeatedly.
157		if { [info exists pkeys_done($pkey)] == 1 } {
158			continue
159		} else {
160			set pkeys_done($pkey) 1
161		}
162
163		set newd $pdatum.[string range $pdatum 0 2]
164		set ret [eval {$pdb put} {$pkey [chop_data $pmethod $newd]}]
165		error_check_good pdb_put($pkey) $ret 0
166		set data($ns($pkey)) [pad_data $pmethod $newd]
167	}
168	error_check_good sdbc_close [$sdbc close] 0
169	check_secondaries $pdb $sdbs $nentries keys data "Si$tnum.c"
170
171	# Delete the second half of the entries through the primary.
172	# We do the second half so we can just pass keys(0 ... n/2)
173	# to check_secondaries.
174	set half [expr $nentries / 2]
175	puts "\tSi$tnum.d:\
176	    Primary cursor delete loop: deleting $half entries"
177	set pdbc [$pdb cursor]
178	error_check_good pdb_cursor [is_valid_cursor $pdbc $pdb] TRUE
179	set dbt [$pdbc get -first]
180	for { set i 0 } { [llength $dbt] > 0 && $i < $half } { incr i } {
181		error_check_good pdbc_del [$pdbc del] 0
182		set dbt [$pdbc get -next]
183	}
184	error_check_good pdbc_close [$pdbc close] 0
185	cursor_check_secondaries $pdb $sdbs $half "Si$tnum.d"
186
187	# Delete half of what's left, through the first secondary.
188	set quar [expr $half / 2]
189	puts "\tSi$tnum.e:\
190	    Secondary cursor delete loop: deleting $quar entries"
191	set sdb [lindex $sdbs 0]
192	set sdbc [$sdb cursor]
193	set dbt [$sdbc get -first]
194	for { set i 0 } { [llength $dbt] > 0 && $i < $quar } { incr i } {
195		error_check_good sdbc_del [$sdbc del] 0
196		set dbt [$sdbc get -next]
197	}
198	error_check_good sdbc_close [$sdbc close] 0
199	cursor_check_secondaries $pdb $sdbs $quar "Si$tnum.e"
200
201	foreach sdb $sdbs {
202		error_check_good secondary_close [$sdb close] 0
203	}
204	error_check_good primary_close [$pdb close] 0
205
206	# Close the env if it was created within this test.
207	if { $eindex == -1 } {
208		error_check_good env_close [$env close] 0
209	}
210}
211