1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996,2008 Oracle.  All rights reserved.
4#
5# $Id: test028.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test028
8# TEST	Cursor delete test
9# TEST	Test put operations after deleting through a cursor.
10proc test028 { method args } {
11	global dupnum
12	global dupstr
13	global alphabet
14	source ./include.tcl
15
16	set args [convert_args $method $args]
17	set omethod [convert_method $method]
18
19	puts "Test028: $method put after cursor delete test"
20
21	if { [is_rbtree $method] == 1 } {
22		puts "Test028 skipping for method $method"
23		return
24	}
25	if { [is_record_based $method] == 1 } {
26		set key 10
27	} else {
28		append args " -dup"
29		set key "put_after_cursor_del"
30	}
31
32	# Create the database and open the dictionary
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/test028.db
40		set env NULL
41	} else {
42		set testfile test028.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		}
49		set testdir [get_home $env]
50	}
51	set t1 $testdir/t1
52	cleanup $testdir $env
53	set db [eval {berkdb_open \
54	     -create -mode 0644} $args {$omethod $testfile}]
55	error_check_good dbopen [is_valid_db $db] TRUE
56
57	set ndups 20
58	set txn ""
59	set pflags ""
60	set gflags ""
61
62	if { [is_record_based $method] == 1 } {
63		set gflags " -recno"
64	}
65
66	if { $txnenv == 1 } {
67		set t [$env txn]
68		error_check_good txn [is_valid_txn $t $env] TRUE
69		set txn "-txn $t"
70	}
71	set dbc [eval {$db cursor} $txn]
72	error_check_good db_cursor [is_substr $dbc $db] 1
73
74	foreach i { offpage onpage } {
75		foreach b { bigitem smallitem } {
76			if { $i == "onpage" } {
77				if { $b == "bigitem" } {
78					set dupstr [repeat $alphabet 100]
79				} else {
80					set dupstr DUP
81				}
82			} else {
83				if { $b == "bigitem" } {
84					set dupstr [repeat $alphabet 100]
85				} else {
86					set dupstr [repeat $alphabet 50]
87				}
88			}
89
90			if { $b == "bigitem" } {
91				set dupstr [repeat $dupstr 10]
92			}
93			puts "\tTest028: $i/$b"
94
95			puts "\tTest028.a: Insert key with single data item"
96			set ret [eval {$db put} \
97			    $txn $pflags {$key [chop_data $method $dupstr]}]
98			error_check_good db_put $ret 0
99
100			# Now let's get the item and make sure its OK.
101			puts "\tTest028.b: Check initial entry"
102			set ret [eval {$db get} $txn $gflags {$key}]
103			error_check_good db_get \
104			    $ret [list [list $key [pad_data $method $dupstr]]]
105
106			# Now try a put with NOOVERWRITE SET (should be error)
107			puts "\tTest028.c: No_overwrite test"
108			set ret [eval {$db put} $txn \
109			    {-nooverwrite $key [chop_data $method $dupstr]}]
110			error_check_good \
111			    db_put [is_substr $ret "DB_KEYEXIST"] 1
112
113			# Now delete the item with a cursor
114			puts "\tTest028.d: Delete test"
115			set ret [$dbc get -set $key]
116			error_check_bad dbc_get:SET [llength $ret] 0
117
118			set ret [$dbc del]
119			error_check_good dbc_del $ret 0
120
121			puts "\tTest028.e: Reput the item"
122			set ret [eval {$db put} $txn \
123			    {-nooverwrite $key [chop_data $method $dupstr]}]
124			error_check_good db_put $ret 0
125
126			puts "\tTest028.f: Retrieve the item"
127			set ret [eval {$db get} $txn $gflags {$key}]
128			error_check_good db_get $ret \
129			    [list [list $key [pad_data $method $dupstr]]]
130
131			# Delete the key to set up for next test
132			set ret [eval {$db del} $txn {$key}]
133			error_check_good db_del $ret 0
134
135			# Now repeat the above set of tests with
136			# duplicates (if not RECNO).
137			if { [is_record_based $method] == 1 } {
138				continue;
139			}
140
141			puts "\tTest028.g: Insert key with duplicates"
142			for { set count 0 } { $count < $ndups } { incr count } {
143				set ret [eval {$db put} $txn \
144				    {$key [chop_data $method $count$dupstr]}]
145				error_check_good db_put $ret 0
146			}
147
148			puts "\tTest028.h: Check dups"
149			set dupnum 0
150			dump_file $db $txn $t1 test028.check
151
152			# Try no_overwrite
153			puts "\tTest028.i: No_overwrite test"
154			set ret [eval {$db put} \
155			    $txn {-nooverwrite $key $dupstr}]
156			error_check_good \
157			    db_put [is_substr $ret "DB_KEYEXIST"] 1
158
159			# Now delete all the elements with a cursor
160			puts "\tTest028.j: Cursor Deletes"
161			set count 0
162			for { set ret [$dbc get -set $key] } {
163			    [string length $ret] != 0 } {
164			    set ret [$dbc get -next] } {
165				set k [lindex [lindex $ret 0] 0]
166				set d [lindex [lindex $ret 0] 1]
167				error_check_good db_seq(key) $k $key
168				error_check_good db_seq(data) $d $count$dupstr
169				set ret [$dbc del]
170				error_check_good dbc_del $ret 0
171				incr count
172				if { $count == [expr $ndups - 1] } {
173					puts "\tTest028.k:\
174						Duplicate No_Overwrite test"
175					set ret [eval {$db put} $txn \
176					    {-nooverwrite $key $dupstr}]
177					error_check_good db_put [is_substr \
178					    $ret "DB_KEYEXIST"] 1
179				}
180			}
181
182			# Make sure all the items are gone
183			puts "\tTest028.l: Get after delete"
184			set ret [$dbc get -set $key]
185			error_check_good get_after_del [string length $ret] 0
186
187			puts "\tTest028.m: Reput the item"
188			set ret [eval {$db put} \
189			    $txn {-nooverwrite $key 0$dupstr}]
190			error_check_good db_put $ret 0
191			for { set count 1 } { $count < $ndups } { incr count } {
192				set ret [eval {$db put} $txn \
193				    {$key $count$dupstr}]
194				error_check_good db_put $ret 0
195			}
196
197			puts "\tTest028.n: Retrieve the item"
198			set dupnum 0
199			dump_file $db $txn $t1 test028.check
200
201			# Clean out in prep for next test
202			set ret [eval {$db del} $txn {$key}]
203			error_check_good db_del $ret 0
204		}
205	}
206	error_check_good dbc_close [$dbc close] 0
207	if { $txnenv == 1 } {
208		error_check_good txn [$t commit] 0
209	}
210	error_check_good db_close [$db close] 0
211
212}
213
214# Check function for test028; keys and data are identical
215proc test028.check { key data } {
216	global dupnum
217	global dupstr
218	error_check_good "Bad key" $key put_after_cursor_del
219	error_check_good "data mismatch for $key" $data $dupnum$dupstr
220	incr dupnum
221}
222