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/data deletion.
11
# TEST This test checks for cursor maintenance in the presence of deletes.
12
# TEST There are N different scenarios to tests:
13
# TEST 1. No duplicates. Cursor A deletes a key, do a GET for the key.
14
# TEST 2. No duplicates. Cursor is positioned right before key K, Delete K,
15
# TEST do a next on the cursor.
16
# TEST 3. No duplicates. Cursor is positioned on key K, do a regular delete
17
# TEST of K, do a current get on K.
18
# TEST 4. Repeat 3 but do a next instead of current.
19
# TEST 5. Duplicates. Cursor A is on the first item of a duplicate set, A
20
# TEST does a delete. Then we do a non-cursor get.
21
# TEST 6. Duplicates. Cursor A is in a duplicate set and deletes the item.
22
# TEST do a delete of the entire Key. Test cursor current.
23
# TEST 7. Continue last test and try cursor next.
24
# TEST 8. Duplicates. Cursor A is in a duplicate set and deletes the item.
25
# TEST Cursor B is in the same duplicate set and deletes a different item.
26
# TEST Verify that the cursor is in the right place.
27
# TEST 9. Cursors A and B are in the place in the same duplicate set. A
28
# TEST deletes its item. Do current on B.
29
# TEST 10. Continue 8 and do a next on B.
30
proc test054 { method args } {
34
set args [convert_args $method $args]
35
set omethod [convert_method $method]
37
append args " -create -mode 0644"
38
puts "Test054 ($method $args):\
39
interspersed cursor and normal operations"
40
if { [is_record_based $method] == 1 } {
41
puts "Test054 skipping for method $method"
45
# Find the environment in the argument list, we'll need it
48
set eindex [lsearch -exact $args "-env"]
49
if { $eindex != -1 } {
53
# Create the database and open the dictionary
55
# If we are using an env, then testfile should just be the db name.
56
# Otherwise it is the test directory and the name.
57
if { $eindex == -1 } {
58
set testfile $testdir/test054-nodup.db
61
set testfile test054-nodup.db
62
set env [lindex $args $eindex]
63
set txnenv [is_txnenv $env]
65
append args " -auto_commit "
67
set testdir [get_home $env]
74
puts "\tTest054.a: No Duplicate Tests"
75
set db [eval {berkdb_open} $args {$omethod $testfile}]
76
error_check_good db_open:nodup [is_valid_db $db] TRUE
78
# Put three keys in the database
79
for { set key 1 } { $key <= 3 } {incr key} {
82
error_check_good txn [is_valid_txn $t $env] TRUE
85
set r [eval {$db put} $txn $flags {$key datum$key}]
86
error_check_good put $r 0
88
error_check_good txn [$t commit] 0
94
error_check_good txn [is_valid_txn $t $env] TRUE
97
set curs [eval {$db cursor} $txn]
98
error_check_good curs_open:nodup [is_valid_cursor $curs $db] TRUE
100
# Retrieve keys sequentially so we can figure out their order
102
for {set d [$curs get -first] } \
103
{[llength $d] != 0 } \
104
{set d [$curs get -next] } {
105
set key_set($i) [lindex [lindex $d 0] 0]
110
puts "\tTest054.a1: Delete w/cursor, regular get"
112
# Now set the cursor on the middle on.
113
set r [$curs get -set $key_set(2)]
114
error_check_bad cursor_get:DB_SET [llength $r] 0
115
set k [lindex [lindex $r 0] 0]
116
set d [lindex [lindex $r 0] 1]
117
error_check_good curs_get:DB_SET:key $k $key_set(2)
118
error_check_good curs_get:DB_SET:data $d datum$key_set(2)
122
error_check_good curs_del $r 0
125
set r [eval {$db get} $txn {$key_set(2)}]
126
error_check_good get_after_del [llength $r] 0
128
# Free up the cursor.
129
error_check_good cursor_close [eval {$curs close}] 0
130
if { $txnenv == 1 } {
131
error_check_good txn [$t commit] 0
135
puts "\tTest054.a2: Cursor before K, delete K, cursor next"
138
if { $txnenv == 1 } {
140
error_check_good txn [is_valid_txn $t $env] TRUE
143
set r [eval {$db put} $txn {$key_set(2) datum$key_set(2)}]
144
error_check_good put $r 0
145
if { $txnenv == 1 } {
146
error_check_good txn [$t commit] 0
149
# Open and position cursor on first item.
150
if { $txnenv == 1 } {
152
error_check_good txn [is_valid_txn $t $env] TRUE
155
set curs [eval {$db cursor} $txn]
156
error_check_good curs_open:nodup [is_valid_cursor $curs $db] TRUE
158
# Retrieve keys sequentially so we can figure out their order
160
for {set d [eval {$curs get} -first] } \
161
{[llength $d] != 0 } \
162
{set d [$curs get -nextdup] } {
163
set key_set($i) [lindex [lindex $d 0] 0]
167
set r [eval {$curs get} -set {$key_set(1)} ]
168
error_check_bad cursor_get:DB_SET [llength $r] 0
169
set k [lindex [lindex $r 0] 0]
170
set d [lindex [lindex $r 0] 1]
171
error_check_good curs_get:DB_SET:key $k $key_set(1)
172
error_check_good curs_get:DB_SET:data $d datum$key_set(1)
174
# Now delete (next item) $key_set(2)
176
db_del:$key_set(2) [eval {$db del} $txn {$key_set(2)}] 0
178
# Now do next on cursor
179
set r [$curs get -next]
180
error_check_bad cursor_get:DB_NEXT [llength $r] 0
181
set k [lindex [lindex $r 0] 0]
182
set d [lindex [lindex $r 0] 1]
183
error_check_good curs_get:DB_NEXT:key $k $key_set(3)
184
error_check_good curs_get:DB_NEXT:data $d datum$key_set(3)
187
puts "\tTest054.a3: Cursor on K, delete K, cursor current"
191
db_del:$key_set(3) [eval {$db del} $txn {$key_set(3)}] 0
192
# NEEDS TO COME BACK IN, BUG CHECK
193
set ret [$curs get -current]
194
error_check_good current_after_del $ret [list [list [] []]]
195
error_check_good cursor_close [$curs close] 0
196
if { $txnenv == 1 } {
197
error_check_good txn [$t commit] 0
200
puts "\tTest054.a4: Cursor on K, delete K, cursor next"
202
# Restore keys 2 and 3
203
if { $txnenv == 1 } {
205
error_check_good txn [is_valid_txn $t $env] TRUE
208
set r [eval {$db put} $txn {$key_set(2) datum$key_set(2)}]
209
error_check_good put $r 0
210
set r [eval {$db put} $txn {$key_set(3) datum$key_set(3)}]
211
error_check_good put $r 0
212
if { $txnenv == 1 } {
213
error_check_good txn [$t commit] 0
216
if { $txnenv == 1 } {
218
error_check_good txn [is_valid_txn $t $env] TRUE
221
# Create the new cursor and put it on 1
222
set curs [eval {$db cursor} $txn]
223
error_check_good curs_open:nodup [is_valid_cursor $curs $db] TRUE
224
set r [$curs get -set $key_set(1)]
225
error_check_bad cursor_get:DB_SET [llength $r] 0
226
set k [lindex [lindex $r 0] 0]
227
set d [lindex [lindex $r 0] 1]
228
error_check_good curs_get:DB_SET:key $k $key_set(1)
229
error_check_good curs_get:DB_SET:data $d datum$key_set(1)
233
db_del:$key_set(2) [eval {$db del} $txn {$key_set(2)}] 0
235
# Now do next on cursor
236
set r [$curs get -next]
237
error_check_bad cursor_get:DB_NEXT [llength $r] 0
238
set k [lindex [lindex $r 0] 0]
239
set d [lindex [lindex $r 0] 1]
240
error_check_good curs_get:DB_NEXT:key $k $key_set(3)
241
error_check_good curs_get:DB_NEXT:data $d datum$key_set(3)
244
error_check_good curs_close [$curs close] 0
245
if { $txnenv == 1 } {
246
error_check_good txn [$t commit] 0
248
error_check_good db_close [$db close] 0
250
# Now get ready for duplicate tests
252
if { [is_rbtree $method] == 1 } {
253
puts "Test054: skipping remainder of test for method $method."
257
puts "\tTest054.b: Duplicate Tests"
260
# Open a new database for the dup tests so -truncate is not needed.
261
# If we are using an env, then testfile should just be the db name.
262
# Otherwise it is the test directory and the name.
263
if { $eindex == -1 } {
264
set testfile $testdir/test054-dup.db
267
set testfile test054-dup.db
268
set env [lindex $args $eindex]
269
set testdir [get_home $env]
271
cleanup $testdir $env
276
set db [eval {berkdb_open} $args {$omethod $testfile}]
277
error_check_good db_open:dup [is_valid_db $db] TRUE
279
# Put three keys in the database
280
for { set key 1 } { $key <= 3 } {incr key} {
281
if { $txnenv == 1 } {
283
error_check_good txn [is_valid_txn $t $env] TRUE
286
set r [eval {$db put} $txn $flags {$key datum$key}]
287
error_check_good put $r 0
288
if { $txnenv == 1 } {
289
error_check_good txn [$t commit] 0
293
# Retrieve keys sequentially so we can figure out their order
294
if { $txnenv == 1 } {
296
error_check_good txn [is_valid_txn $t $env] TRUE
299
set curs [eval {$db cursor} $txn]
300
error_check_good curs_open:dup [is_valid_cursor $curs $db] TRUE
303
for {set d [$curs get -first] } \
304
{[llength $d] != 0 } \
305
{set d [$curs get -nextdup] } {
306
set key_set($i) [lindex [lindex $d 0] 0]
310
# Now put in a bunch of duplicates for key 2
311
for { set d 1 } { $d <= 5 } {incr d} {
312
set r [eval {$db put} $txn $flags {$key_set(2) dup_$d}]
313
error_check_good dup:put $r 0
317
puts "\tTest054.b1: Delete dup w/cursor on first item. Get on key."
319
# Now set the cursor on the first of the duplicate set.
320
set r [eval {$curs get} -set {$key_set(2)}]
321
error_check_bad cursor_get:DB_SET [llength $r] 0
322
set k [lindex [lindex $r 0] 0]
323
set d [lindex [lindex $r 0] 1]
324
error_check_good curs_get:DB_SET:key $k $key_set(2)
325
error_check_good curs_get:DB_SET:data $d datum$key_set(2)
329
error_check_good curs_del $r 0
332
set r [eval {$db get} $txn {$key_set(2)}]
333
error_check_good get_after_del [lindex [lindex $r 0] 1] dup_1
336
puts "\tTest054.b2: Now get the next duplicate from the cursor."
338
# Now do next on cursor
339
set r [$curs get -nextdup]
340
error_check_bad cursor_get:DB_NEXT [llength $r] 0
341
set k [lindex [lindex $r 0] 0]
342
set d [lindex [lindex $r 0] 1]
343
error_check_good curs_get:DB_NEXT:key $k $key_set(2)
344
error_check_good curs_get:DB_NEXT:data $d dup_1
347
puts "\tTest054.b3: Two cursors in set; each delete different items"
350
set curs2 [eval {$db cursor} $txn]
351
error_check_good curs_open [is_valid_cursor $curs2 $db] TRUE
353
# Set on last of duplicate set.
354
set r [$curs2 get -set $key_set(3)]
355
error_check_bad cursor_get:DB_SET [llength $r] 0
356
set k [lindex [lindex $r 0] 0]
357
set d [lindex [lindex $r 0] 1]
358
error_check_good curs_get:DB_SET:key $k $key_set(3)
359
error_check_good curs_get:DB_SET:data $d datum$key_set(3)
361
set r [$curs2 get -prev]
362
error_check_bad cursor_get:DB_PREV [llength $r] 0
363
set k [lindex [lindex $r 0] 0]
364
set d [lindex [lindex $r 0] 1]
365
error_check_good curs_get:DB_PREV:key $k $key_set(2)
366
error_check_good curs_get:DB_PREV:data $d dup_5
368
# Delete the item at cursor 1 (dup_1)
369
error_check_good curs1_del [$curs del] 0
371
# Verify curs1 and curs2
372
# current should fail
373
set ret [$curs get -current]
375
curs1_get_after_del $ret [list [list [] []]]
377
set r [$curs2 get -current]
378
error_check_bad curs2_get [llength $r] 0
379
set k [lindex [lindex $r 0] 0]
380
set d [lindex [lindex $r 0] 1]
381
error_check_good curs_get:DB_CURRENT:key $k $key_set(2)
382
error_check_good curs_get:DB_CURRENT:data $d dup_5
384
# Now delete the item at cursor 2 (dup_5)
385
error_check_good curs2_del [$curs2 del] 0
387
# Verify curs1 and curs2
388
set ret [$curs get -current]
389
error_check_good curs1_get:del2 $ret [list [list [] []]]
391
set ret [$curs2 get -current]
392
error_check_good curs2_get:del2 $ret [list [list [] []]]
394
# Now verify that next and prev work.
396
set r [$curs2 get -prev]
397
error_check_bad cursor_get:DB_PREV [llength $r] 0
398
set k [lindex [lindex $r 0] 0]
399
set d [lindex [lindex $r 0] 1]
400
error_check_good curs_get:DB_PREV:key $k $key_set(2)
401
error_check_good curs_get:DB_PREV:data $d dup_4
403
set r [$curs get -next]
404
error_check_bad cursor_get:DB_NEXT [llength $r] 0
405
set k [lindex [lindex $r 0] 0]
406
set d [lindex [lindex $r 0] 1]
407
error_check_good curs_get:DB_NEXT:key $k $key_set(2)
408
error_check_good curs_get:DB_NEXT:data $d dup_2
410
puts "\tTest054.b4: Two cursors same item, one delete, one get"
412
# Move curs2 onto dup_2
413
set r [$curs2 get -prev]
414
error_check_bad cursor_get:DB_PREV [llength $r] 0
415
set k [lindex [lindex $r 0] 0]
416
set d [lindex [lindex $r 0] 1]
417
error_check_good curs_get:DB_PREV:key $k $key_set(2)
418
error_check_good curs_get:DB_PREV:data $d dup_3
420
set r [$curs2 get -prev]
421
error_check_bad cursor_get:DB_PREV [llength $r] 0
422
set k [lindex [lindex $r 0] 0]
423
set d [lindex [lindex $r 0] 1]
424
error_check_good curs_get:DB_PREV:key $k $key_set(2)
425
error_check_good curs_get:DB_PREV:data $d dup_2
428
error_check_good curs1_del [$curs del] 0
430
# Verify gets on both 1 and 2
431
set ret [$curs get -current]
433
curs1_get:deleted $ret [list [list [] []]]
434
set ret [$curs2 get -current]
436
curs2_get:deleted $ret [list [list [] []]]
438
puts "\tTest054.b5: Now do a next on both cursors"
440
set r [$curs get -next]
441
error_check_bad cursor_get:DB_NEXT [llength $r] 0
442
set k [lindex [lindex $r 0] 0]
443
set d [lindex [lindex $r 0] 1]
444
error_check_good curs_get:DB_NEXT:key $k $key_set(2)
445
error_check_good curs_get:DB_NEXT:data $d dup_3
447
set r [$curs2 get -next]
448
error_check_bad cursor_get:DB_NEXT [llength $r] 0
449
set k [lindex [lindex $r 0] 0]
450
set d [lindex [lindex $r 0] 1]
451
error_check_good curs_get:DB_NEXT:key $k $key_set(2)
452
error_check_good curs_get:DB_NEXT:data $d dup_3
455
error_check_good curs_close [$curs close] 0
456
error_check_good curs2_close [$curs2 close] 0
457
if { $txnenv == 1 } {
458
error_check_good txn [$t commit] 0
460
error_check_good db_close [$db close] 0