1
# See the file LICENSE for redistribution information.
3
# Copyright (c) 1996-2002
4
# Sleepycat Software. All rights reserved.
9
# TEST Cursor maintenance during key deletes.
10
# TEST Check if we handle the case where we delete a key with the cursor on
11
# TEST it and then add the same key. The cursor should not get the new item
12
# TEST returned, but the item shouldn't disappear.
13
# TEST Run test tests, one where the overwriting put is done with a put and
14
# TEST one where it's done with a cursor put.
15
proc test057 { method args } {
19
set args [convert_args $method $args]
20
set omethod [convert_method $method]
22
append args " -create -mode 0644 -dup "
23
if { [is_record_based $method] == 1 || [is_rbtree $method] == 1 } {
24
puts "Test057: skipping for method $method"
27
puts "Test057: $method delete and replace in presence of cursor."
29
# Create the database and open the dictionary
31
set eindex [lsearch -exact $args "-env"]
33
# If we are using an env, then testfile should just be the db name.
34
# Otherwise it is the test directory and the name.
35
if { $eindex == -1 } {
36
set testfile $testdir/test057.db
39
set testfile test057.db
41
set env [lindex $args $eindex]
42
set txnenv [is_txnenv $env]
44
append args " -auto_commit "
46
set testdir [get_home $env]
53
set db [eval {berkdb_open} $args {$omethod $testfile}]
54
error_check_good dbopen:dup [is_valid_db $db] TRUE
56
puts "\tTest057.a: Set cursor, delete cursor, put with key."
57
# Put three keys in the database
58
for { set key 1 } { $key <= 3 } {incr key} {
61
error_check_good txn [is_valid_txn $t $env] TRUE
64
set r [eval {$db put} $txn $flags {$key datum$key}]
65
error_check_good put $r 0
67
error_check_good txn [$t commit] 0
71
# Retrieve keys sequentially so we can figure out their order
75
error_check_good txn [is_valid_txn $t $env] TRUE
78
set curs [eval {$db cursor} $txn]
79
error_check_good curs_open:dup [is_valid_cursor $curs $db] TRUE
81
for {set d [$curs get -first] } {[llength $d] != 0 } \
82
{set d [$curs get -next] } {
83
set key_set($i) [lindex [lindex $d 0] 0]
87
# Now put in a bunch of duplicates for key 2
88
for { set d 1 } { $d <= 5 } {incr d} {
89
set r [eval {$db put} $txn $flags {$key_set(2) dup_$d}]
90
error_check_good dup:put $r 0
93
# Now put the cursor on key 1
95
# Now set the cursor on the first of the duplicate set.
96
set r [$curs get -set $key_set(1)]
97
error_check_bad cursor_get:DB_SET [llength $r] 0
98
set k [lindex [lindex $r 0] 0]
99
set d [lindex [lindex $r 0] 1]
100
error_check_good curs_get:DB_SET:key $k $key_set(1)
101
error_check_good curs_get:DB_SET:data $d datum$key_set(1)
105
error_check_good delete $r 0
107
# Now check the get current on the cursor.
108
error_check_good curs_get:del [$curs get -current] [list [list [] []]]
110
# Now do a put on the key
111
set r [eval {$db put} $txn $flags {$key_set(1) new_datum$key_set(1)}]
112
error_check_good put $r 0
115
set r [eval {$db get} $txn {$key_set(1)}]
116
error_check_good get [lindex [lindex $r 0] 1] new_datum$key_set(1)
119
error_check_good curs_get:deleted [$curs get -current] [list [list [] []]]
121
# Move cursor and see if we get the key.
122
set r [$curs get -first]
123
error_check_bad cursor_get:DB_FIRST [llength $r] 0
124
set k [lindex [lindex $r 0] 0]
125
set d [lindex [lindex $r 0] 1]
126
error_check_good curs_get:DB_FIRST:key $k $key_set(1)
127
error_check_good curs_get:DB_FIRST:data $d new_datum$key_set(1)
129
puts "\tTest057.b: Set two cursor on a key, delete one, overwrite other"
130
set curs2 [eval {$db cursor} $txn]
131
error_check_good curs2_open [is_valid_cursor $curs2 $db] TRUE
133
# Set both cursors on the 4rd key
134
set r [$curs get -set $key_set(3)]
135
error_check_bad cursor_get:DB_SET [llength $r] 0
136
set k [lindex [lindex $r 0] 0]
137
set d [lindex [lindex $r 0] 1]
138
error_check_good curs_get:DB_SET:key $k $key_set(3)
139
error_check_good curs_get:DB_SET:data $d datum$key_set(3)
141
set r [$curs2 get -set $key_set(3)]
142
error_check_bad cursor2_get:DB_SET [llength $r] 0
143
set k [lindex [lindex $r 0] 0]
144
set d [lindex [lindex $r 0] 1]
145
error_check_good curs2_get:DB_SET:key $k $key_set(3)
146
error_check_good curs2_get:DB_SET:data $d datum$key_set(3)
148
# Now delete through cursor 1
149
error_check_good curs1_del [$curs del] 0
151
# Verify gets on both 1 and 2
152
error_check_good curs_get:deleted [$curs get -current] \
154
error_check_good curs_get:deleted [$curs2 get -current] \
157
# Now do a replace through cursor 2
158
set pflags "-current"
159
if {[is_hash $method] == 1} {
160
error_check_good curs1_get_after_del [is_substr \
161
[$curs2 put $pflags new_datum$key_set(3)] "DB_NOTFOUND"] 1
164
error_check_good curs1_get:deleted \
165
[$curs get -current] \
167
error_check_good curs2_get:deleted \
168
[$curs get -current] \
171
# btree only, recno is skipped this test
172
set ret [$curs2 put $pflags new_datum$key_set(3)]
173
error_check_good curs_replace $ret 0
177
#error_check_good curs1_get:deleted [catch {$curs get -current} r] 1
178
#error_check_good curs1_get_after_del \
179
[is_substr $errorInfo "DB_KEYEMPTY"] 1
180
#error_check_good curs2_get:deleted [catch {$curs2 get -current} r] 1
181
#error_check_good curs2_get_after_del \
182
[is_substr $errorInfo "DB_KEYEMPTY"] 1
185
Set two cursors on a dup, delete one, overwrite other"
187
# Set both cursors on the 2nd duplicate of key 2
188
set r [$curs get -set $key_set(2)]
189
error_check_bad cursor_get:DB_SET [llength $r] 0
190
set k [lindex [lindex $r 0] 0]
191
set d [lindex [lindex $r 0] 1]
192
error_check_good curs_get:DB_SET:key $k $key_set(2)
193
error_check_good curs_get:DB_SET:data $d datum$key_set(2)
195
set r [$curs get -next]
196
error_check_bad cursor_get:DB_NEXT [llength $r] 0
197
set k [lindex [lindex $r 0] 0]
198
set d [lindex [lindex $r 0] 1]
199
error_check_good curs_get:DB_NEXT:key $k $key_set(2)
200
error_check_good curs_get:DB_NEXT:data $d dup_1
202
set r [$curs2 get -set $key_set(2)]
203
error_check_bad cursor2_get:DB_SET [llength $r] 0
204
set k [lindex [lindex $r 0] 0]
205
set d [lindex [lindex $r 0] 1]
206
error_check_good curs2_get:DB_SET:key $k $key_set(2)
207
error_check_good curs2_get:DB_SET:data $d datum$key_set(2)
209
set r [$curs2 get -next]
210
error_check_bad cursor2_get:DB_NEXT [llength $r] 0
211
set k [lindex [lindex $r 0] 0]
212
set d [lindex [lindex $r 0] 1]
213
error_check_good curs2_get:DB_NEXT:key $k $key_set(2)
214
error_check_good curs2_get:DB_NEXT:data $d dup_1
216
# Now delete through cursor 1
217
error_check_good curs1_del [$curs del] 0
219
# Verify gets on both 1 and 2
220
error_check_good curs_get:deleted [$curs get -current] \
222
error_check_good curs_get:deleted [$curs2 get -current] \
225
# Now do a replace through cursor 2 -- this will work on btree but
227
if {[is_hash $method] == 1} {
228
error_check_good hash_replace \
229
[is_substr [$curs2 put -current new_dup_1] "DB_NOTFOUND"] 1
231
error_check_good curs_replace [$curs2 put -current new_dup_1] 0
234
# Both gets should fail
235
#error_check_good curs1_get:deleted [catch {$curs get -current} r] 1
236
#error_check_good curs1_get_after_del \
237
[is_substr $errorInfo "DB_KEYEMPTY"] 1
238
#error_check_good curs2_get:deleted [catch {$curs2 get -current} r] 1
239
#error_check_good curs2_get_after_del \
240
[is_substr $errorInfo "DB_KEYEMPTY"] 1
242
error_check_good curs2_close [$curs2 close] 0
243
error_check_good curs_close [$curs close] 0
244
if { $txnenv == 1 } {
245
error_check_good txn [$t commit] 0
247
error_check_good db_close [$db close] 0