summaryrefslogtreecommitdiff
path: root/db/test/test028.tcl
blob: b460dd53a982b15997dcc4d3eb311dcc9a5f4c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1996, 1997, 1998, 1999, 2000
#	Sleepycat Software.  All rights reserved.
#
#	$Id: test028.tcl,v 11.12 2000/08/25 14:21:55 sue Exp $
#
# Put after cursor delete test.
proc test028 { method args } {
	global dupnum
	global dupstr
	global alphabet
	global errorInfo
	source ./include.tcl

	set args [convert_args $method $args]
	set omethod [convert_method $method]

	puts "Test028: $method put after cursor delete test"

	if { [is_rbtree $method] == 1 } {
		puts "Test028 skipping for method $method"
		return
	}
	if { [is_record_based $method] == 1 } {
		set key 10
	} else {
		append args " -dup"
		set key "put_after_cursor_del"
	}

	# Create the database and open the dictionary
	set eindex [lsearch -exact $args "-env"]
	#
	# If we are using an env, then testfile should just be the db name.
	# Otherwise it is the test directory and the name.
	if { $eindex == -1 } {
		set testfile $testdir/test028.db
		set env NULL
	} else {
		set testfile test028.db
		incr eindex
		set env [lindex $args $eindex]
	}
	set t1 $testdir/t1
	cleanup $testdir $env
	set db [eval {berkdb_open \
	     -create -truncate -mode 0644} $args {$omethod $testfile}]
	error_check_good dbopen [is_valid_db $db] TRUE

	set ndups 20
	set txn ""
	set pflags ""
	set gflags ""

	if { [is_record_based $method] == 1 } {
		set gflags " -recno"
	}

	set dbc [eval {$db cursor} $txn]
	error_check_good db_cursor [is_substr $dbc $db] 1

	foreach i { offpage onpage } {
		foreach b { bigitem smallitem } {
			if { $i == "onpage" } {
				if { $b == "bigitem" } {
					set dupstr [repeat $alphabet 100]
				} else {
					set dupstr DUP
				}
			} else {
				if { $b == "bigitem" } {
					set dupstr [repeat $alphabet 100]
				} else {
					set dupstr [repeat $alphabet 50]
				}
			}

			if { $b == "bigitem" } {
				set dupstr [repeat $dupstr 10]
			}
			puts "\tTest028: $i/$b"

			puts "\tTest028.a: Insert key with single data item"
			set ret [eval {$db put} \
			    $txn $pflags {$key [chop_data $method $dupstr]}]
			error_check_good db_put $ret 0

			# Now let's get the item and make sure its OK.
			puts "\tTest028.b: Check initial entry"
			set ret [eval {$db get} $txn $gflags {$key}]
			error_check_good db_get \
			    $ret [list [list $key [pad_data $method $dupstr]]]

			# Now try a put with NOOVERWRITE SET (should be error)
			puts "\tTest028.c: No_overwrite test"
			set ret [eval {$db put} $txn \
			    {-nooverwrite $key [chop_data $method $dupstr]}]
			error_check_good \
			    db_put [is_substr $ret "DB_KEYEXIST"] 1

			# Now delete the item with a cursor
			puts "\tTest028.d: Delete test"
			set ret [$dbc get -set $key]
			error_check_bad dbc_get:SET [llength $ret] 0

			set ret [$dbc del]
			error_check_good dbc_del $ret 0

			puts "\tTest028.e: Reput the item"
			set ret [eval {$db put} $txn \
			    {-nooverwrite $key [chop_data $method $dupstr]}]
			error_check_good db_put $ret 0

			puts "\tTest028.f: Retrieve the item"
			set ret [eval {$db get} $txn $gflags {$key}]
			error_check_good db_get $ret \
			    [list [list $key [pad_data $method $dupstr]]]

			# Delete the key to set up for next test
			set ret [eval {$db del} $txn {$key}]
			error_check_good db_del $ret 0

			# Now repeat the above set of tests with
			# duplicates (if not RECNO).
			if { [is_record_based $method] == 1 } {
				continue;
			}

			puts "\tTest028.g: Insert key with duplicates"
			for { set count 0 } { $count < $ndups } { incr count } {
			set ret [eval {$db put} \
			    $txn {$key [chop_data $method $count$dupstr]}]
				error_check_good db_put $ret 0
			}

			puts "\tTest028.h: Check dups"
			set dupnum 0
			dump_file $db $txn $t1 test028.check

			# Try no_overwrite
			puts "\tTest028.i: No_overwrite test"
			set ret [eval {$db put} \
			    $txn {-nooverwrite $key $dupstr}]
			error_check_good \
			    db_put [is_substr $ret "DB_KEYEXIST"] 1

			# Now delete all the elements with a cursor
			puts "\tTest028.j: Cursor Deletes"
			set count 0
			for { set ret [$dbc get -set $key] } {
			    [string length $ret] != 0 } {
			    set ret [$dbc get -next] } {
				set k [lindex [lindex $ret 0] 0]
				set d [lindex [lindex $ret 0] 1]
				error_check_good db_seq(key) $k $key
				error_check_good db_seq(data) $d $count$dupstr
				set ret [$dbc del]
				error_check_good dbc_del $ret 0
				incr count
				if { $count == [expr $ndups - 1] } {
					puts "\tTest028.k:\
						Duplicate No_Overwrite test"
					set $errorInfo ""
					set ret [eval {$db put} $txn \
					    {-nooverwrite $key $dupstr}]
					error_check_good db_put [is_substr \
					    $ret "DB_KEYEXIST"] 1
				}
			}

			# Make sure all the items are gone
			puts "\tTest028.l: Get after delete"
			set ret [$dbc get -set $key]
			error_check_good get_after_del [string length $ret] 0

			puts "\tTest028.m: Reput the item"
			set ret [eval {$db put} \
			    $txn {-nooverwrite $key 0$dupstr}]
			error_check_good db_put $ret 0
			for { set count 1 } { $count < $ndups } { incr count } {
			set ret [eval {$db put} $txn {$key $count$dupstr}]
				error_check_good db_put $ret 0
			}

			puts "\tTest028.n: Retrieve the item"
			set dupnum 0
			dump_file $db $txn $t1 test028.check

			# Clean out in prep for next test
			set ret [eval {$db del} $txn {$key}]
			error_check_good db_del $ret 0
		}
	}

	error_check_good dbc_close [$dbc close] 0
	error_check_good db_close [$db close] 0

}

# Check function for test028; keys and data are identical
proc test028.check { key data } {
	global dupnum
	global dupstr
	error_check_good "Bad key" $key put_after_cursor_del
	error_check_good "data mismatch for $key" $data $dupnum$dupstr
	incr dupnum
}