1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996, 2013 Oracle and/or its affiliates.  All rights reserved.
4#
5# $Id$
6#
7# TEST	test038
8# TEST	DB_GET_BOTH, DB_GET_BOTH_RANGE on deleted items
9# TEST
10# TEST  Use the first 10,000 entries from the dictionary.  Insert each with
11# TEST	self as key and "ndups" duplicates.  For the data field, prepend the
12# TEST	letters of the alphabet in a random order so we force the duplicate
13# TEST	sorting code to do something.  By setting ndups large, we can make
14# TEST	this an off-page test
15# TEST
16# TEST	Test the DB_GET_BOTH and DB_GET_BOTH_RANGE functionality by retrieving
17# TEST	each dup in the file explicitly.  Then remove each duplicate and try
18# TEST	the retrieval again.
19proc test038 { method {nentries 10000} {ndups 5} {tnum "038"} args } {
20	global alphabet
21	global rand_init
22	source ./include.tcl
23
24	berkdb srand $rand_init
25
26	set args [convert_args $method $args]
27	set checkargs [split_partition_args $args]
28
29	# The checkdb is of type hash so it can't use compression.
30	set checkargs [strip_compression_args $checkargs]
31
32	set omethod [convert_method $method]
33
34	if { [is_record_based $method] == 1 || \
35	    [is_rbtree $method] == 1 } {
36		puts "Test$tnum skipping for method $method"
37		return
38	}
39	# Create the database and open the dictionary
40	set txnenv 0
41	set eindex [lsearch -exact $args "-env"]
42	#
43	# If we are using an env, then testfile should just be the db name.
44	# Otherwise it is the test directory and the name.
45	if { $eindex == -1 } {
46		set testfile $testdir/test$tnum.db
47		set checkdb $testdir/checkdb.db
48		set env NULL
49	} else {
50		set testfile test$tnum.db
51		set checkdb checkdb.db
52		incr eindex
53		set env [lindex $args $eindex]
54		set txnenv [is_txnenv $env]
55		if { $txnenv == 1 } {
56			append args " -auto_commit "
57			append checkargs " -auto_commit "
58			#
59			# If we are using txns and running with the
60			# default, set the default down a bit.
61			#
62			if { $nentries == 10000 } {
63				set nentries 100
64			}
65			reduce_dups nentries ndups
66		}
67		set testdir [get_home $env]
68	}
69	set t1 $testdir/t1
70	set t2 $testdir/t2
71	set t3 $testdir/t3
72	cleanup $testdir $env
73
74	puts "Test$tnum: \
75	    $method ($args) $nentries small sorted dup key/data pairs"
76	set db [eval {berkdb_open -create -mode 0644 \
77		$omethod -dup -dupsort} $args {$testfile}]
78	error_check_good dbopen [is_valid_db $db] TRUE
79	set did [open $dict]
80
81	set check_db [eval {berkdb_open \
82	     -create -mode 0644 -hash} $checkargs {$checkdb}]
83	error_check_good dbopen:check_db [is_valid_db $check_db] TRUE
84
85	set pflags ""
86	set gflags ""
87	set txn ""
88	set count 0
89
90	# Here is the loop where we put and get each key/data pair
91	puts "\tTest$tnum.a: Put/get loop"
92	if { $txnenv == 1 } {
93		set t [$env txn]
94		error_check_good txn [is_valid_txn $t $env] TRUE
95		set txn "-txn $t"
96	}
97	set dbc [eval {$db cursor} $txn]
98	error_check_good cursor_open [is_valid_cursor $dbc $db] TRUE
99	while { [gets $did str] != -1 && $count < $nentries } {
100		set dups ""
101		for { set i 1 } { $i <= $ndups } { incr i } {
102			set pref \
103			    [string index $alphabet [berkdb random_int 0 25]]
104			set pref $pref[string \
105			    index $alphabet [berkdb random_int 0 25]]
106			while { [string first $pref $dups] != -1 } {
107				set pref [string toupper $pref]
108				if { [string first $pref $dups] != -1 } {
109					set pref [string index $alphabet \
110					    [berkdb random_int 0 25]]
111					set pref $pref[string index $alphabet \
112					    [berkdb random_int 0 25]]
113				}
114			}
115			if { [string length $dups] == 0 } {
116				set dups $pref
117			} else {
118				set dups "$dups $pref"
119			}
120			set datastr $pref:$str
121			set ret [eval {$db put} \
122			    $txn $pflags {$str [chop_data $method $datastr]}]
123			error_check_good put $ret 0
124		}
125		set ret [eval {$check_db put} \
126		    $txn $pflags {$str [chop_data $method $dups]}]
127		error_check_good checkdb_put $ret 0
128
129		# Now retrieve all the keys matching this key
130		set x 0
131		set lastdup ""
132		for {set ret [$dbc get -set $str]} \
133		    {[llength $ret] != 0} \
134		    {set ret [$dbc get -nextdup] } {
135			set k [lindex [lindex $ret 0] 0]
136			if { [string compare $k $str] != 0 } {
137				break
138			}
139			set datastr [lindex [lindex $ret 0] 1]
140			if {[string length $datastr] == 0} {
141				break
142			}
143			if {[string compare $lastdup $datastr] > 0} {
144				error_check_good sorted_dups($lastdup,$datastr)\
145				    0 1
146			}
147			incr x
148			set lastdup $datastr
149		}
150		error_check_good "Test$tnum:ndups:$str" $x $ndups
151		incr count
152	}
153	error_check_good cursor_close [$dbc close] 0
154	if { $txnenv == 1 } {
155		error_check_good txn [$t commit] 0
156	}
157	close $did
158
159	# Now check the duplicates, then delete then recheck
160	puts "\tTest$tnum.b: Checking and Deleting duplicates"
161	if { $txnenv == 1 } {
162		set t [$env txn]
163		error_check_good txn [is_valid_txn $t $env] TRUE
164		set txn "-txn $t"
165	}
166	set dbc [eval {$db cursor} $txn]
167	error_check_good cursor_open [is_valid_cursor $dbc $db] TRUE
168	set check_c [eval {$check_db cursor} $txn]
169	error_check_good cursor_open [is_valid_cursor $check_c $check_db] TRUE
170
171	for {set ndx 0} {$ndx < $ndups} {incr ndx} {
172		for {set ret [$check_c get -first]} \
173		    {[llength $ret] != 0} \
174		    {set ret [$check_c get -next] } {
175			set k [lindex [lindex $ret 0] 0]
176			set d [lindex [lindex $ret 0] 1]
177			error_check_bad data_check:$d [string length $d] 0
178
179			set nn [expr $ndx * 3]
180			set pref [string range $d $nn [expr $nn + 1]]
181			set data $pref:$k
182			set ret [$dbc get -get_both $k $data]
183			error_check_good \
184			    get_both_key:$k [lindex [lindex $ret 0] 0] $k
185			error_check_good \
186			    get_both_data:$k [lindex [lindex $ret 0] 1] $data
187
188			set ret [$dbc get -get_both_range $k $pref]
189			error_check_good \
190			    get_both_key:$k [lindex [lindex $ret 0] 0] $k
191			error_check_good \
192			    get_both_data:$k [lindex [lindex $ret 0] 1] $data
193
194			set ret [$dbc del]
195			error_check_good del $ret 0
196
197			set ret [eval {$db get} $txn {-get_both $k $data}]
198			error_check_good error_case:$k [llength $ret] 0
199
200			# We should either not find anything (if deleting the
201			# largest duplicate in the set) or a duplicate that
202			# sorts larger than the one we deleted.
203			set ret [$dbc get -get_both_range $k $pref]
204			if { [llength $ret] != 0 } {
205				set datastr [lindex [lindex $ret 0] 1]]
206				if {[string compare \
207				    $pref [lindex [lindex $ret 0] 1]] >= 0} {
208					error_check_good \
209				error_case_range:sorted_dups($pref,$datastr) 0 1
210				}
211			}
212
213			if {$ndx != 0} {
214				set n [expr ($ndx - 1) * 3]
215				set pref [string range $d $n [expr $n + 1]]
216				set data $pref:$k
217				set ret \
218				    [eval {$db get} $txn {-get_both $k $data}]
219				error_check_good error_case:$k [llength $ret] 0
220			}
221		}
222	}
223
224	error_check_good check_c:close [$check_c close] 0
225	error_check_good dbc_close [$dbc close] 0
226	if { $txnenv == 1 } {
227		error_check_good txn [$t commit] 0
228	}
229
230	error_check_good check_db:close [$check_db close] 0
231	error_check_good db_close [$db close] 0
232}
233