1
# See the file LICENSE for redistribution information.
3
# Copyright (c) 1996-2002
4
# Sleepycat Software. All rights reserved.
9
# TEST DB_GET_BOTH, DB_GET_BOTH_RANGE
11
# TEST Use the first 10,000 entries from the dictionary. Insert each with
12
# TEST self as key and "ndups" duplicates. For the data field, prepend the
13
# TEST letters of the alphabet in a random order so we force the duplicate
14
# TEST sorting code to do something. By setting ndups large, we can make
15
# TEST this an off-page test.
17
# TEST Test the DB_GET_BOTH functionality by retrieving each dup in the file
18
# TEST explicitly. Test the DB_GET_BOTH_RANGE functionality by retrieving
19
# TEST the unique key prefix (cursor only). Finally test the failure case.
20
proc test032 { method {nentries 10000} {ndups 5} {tnum 32} args } {
21
global alphabet rand_init
24
set args [convert_args $method $args]
25
set omethod [convert_method $method]
27
berkdb srand $rand_init
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/test0$tnum.db
37
set checkdb $testdir/checkdb.db
40
set testfile test0$tnum.db
41
set checkdb checkdb.db
43
set env [lindex $args $eindex]
44
set txnenv [is_txnenv $env]
46
append args " -auto_commit "
48
# If we are using txns and running with the
49
# default, set the default down a bit.
51
if { $nentries == 10000 } {
54
reduce_dups nentries ndups
56
set testdir [get_home $env]
64
$method ($args) $nentries small sorted $ndups dup key/data pairs"
65
if { [is_record_based $method] == 1 || \
66
[is_rbtree $method] == 1 } {
67
puts "Test0$tnum skipping for method $omethod"
70
set db [eval {berkdb_open -create -mode 0644 \
71
$omethod -dup -dupsort} $args {$testfile} ]
72
error_check_good dbopen [is_valid_db $db] TRUE
75
set check_db [eval {berkdb_open \
76
-create -mode 0644} $args {-hash $checkdb}]
77
error_check_good dbopen:check_db [is_valid_db $check_db] TRUE
84
# Here is the loop where we put and get each key/data pair
85
puts "\tTest0$tnum.a: Put/get loop"
88
error_check_good txn [is_valid_txn $t $env] TRUE
91
set dbc [eval {$db cursor} $txn]
92
error_check_good cursor_open [is_valid_cursor $dbc $db] TRUE
93
while { [gets $did str] != -1 && $count < $nentries } {
94
# Re-initialize random string generator
95
randstring_init $ndups
98
for { set i 1 } { $i <= $ndups } { incr i } {
101
set datastr $pref:$str
102
set ret [eval {$db put} \
103
$txn $pflags {$str [chop_data $method $datastr]}]
104
error_check_good put $ret 0
106
set ret [eval {$check_db put} \
107
$txn $pflags {$str [chop_data $method $dups]}]
108
error_check_good checkdb_put $ret 0
110
# Now retrieve all the keys matching this key
113
for {set ret [$dbc get -set $str]} \
114
{[llength $ret] != 0} \
115
{set ret [$dbc get -nextdup] } {
116
set k [lindex [lindex $ret 0] 0]
117
if { [string compare $k $str] != 0 } {
120
set datastr [lindex [lindex $ret 0] 1]
121
if {[string length $datastr] == 0} {
124
if {[string compare $lastdup $datastr] > 0} {
126
sorted_dups($lastdup,$datastr) 0 1
132
error_check_good "Test0$tnum:ndups:$str" $x $ndups
135
error_check_good cursor_close [$dbc close] 0
136
if { $txnenv == 1 } {
137
error_check_good txn [$t commit] 0
141
# Now we will get each key from the DB and compare the results
143
puts "\tTest0$tnum.b: Checking file for correct duplicates (no cursor)"
144
if { $txnenv == 1 } {
146
error_check_good txn [is_valid_txn $t $env] TRUE
149
set check_c [eval {$check_db cursor} $txn]
150
error_check_good check_c_open(2) \
151
[is_valid_cursor $check_c $check_db] TRUE
153
for {set ndx 0} {$ndx < [expr 4 * $ndups]} {incr ndx 4} {
154
for {set ret [$check_c get -first]} \
155
{[llength $ret] != 0} \
156
{set ret [$check_c get -next] } {
157
set k [lindex [lindex $ret 0] 0]
158
set d [lindex [lindex $ret 0] 1]
159
error_check_bad data_check:$d [string length $d] 0
161
set pref [string range $d $ndx [expr $ndx + 3]]
163
set ret [eval {$db get} $txn {-get_both $k $data}]
165
get_both_data:$k $ret [list [list $k $data]]
171
# Now repeat the above test using cursor ops
172
puts "\tTest0$tnum.c: Checking file for correct duplicates (cursor)"
173
set dbc [eval {$db cursor} $txn]
174
error_check_good cursor_open [is_valid_cursor $dbc $db] TRUE
176
for {set ndx 0} {$ndx < [expr 4 * $ndups]} {incr ndx 4} {
177
for {set ret [$check_c get -first]} \
178
{[llength $ret] != 0} \
179
{set ret [$check_c get -next] } {
180
set k [lindex [lindex $ret 0] 0]
181
set d [lindex [lindex $ret 0] 1]
182
error_check_bad data_check:$d [string length $d] 0
184
set pref [string range $d $ndx [expr $ndx + 3]]
186
set ret [eval {$dbc get} {-get_both $k $data}]
188
curs_get_both_data:$k $ret [list [list $k $data]]
190
set ret [eval {$dbc get} {-get_both_range $k $pref}]
192
curs_get_both_range:$k $ret [list [list $k $data]]
196
# Now check the error case
197
puts "\tTest0$tnum.d: Check error case (no cursor)"
198
for {set ret [$check_c get -first]} \
199
{[llength $ret] != 0} \
200
{set ret [$check_c get -next] } {
201
set k [lindex [lindex $ret 0] 0]
202
set d [lindex [lindex $ret 0] 1]
203
error_check_bad data_check:$d [string length $d] 0
206
set ret [eval {$db get} $txn {-get_both $k $data}]
207
error_check_good error_case:$k [llength $ret] 0
210
# Now check the error case
211
puts "\tTest0$tnum.e: Check error case (cursor)"
212
for {set ret [$check_c get -first]} \
213
{[llength $ret] != 0} \
214
{set ret [$check_c get -next] } {
215
set k [lindex [lindex $ret 0] 0]
216
set d [lindex [lindex $ret 0] 1]
217
error_check_bad data_check:$d [string length $d] 0
220
set ret [eval {$dbc get} {-get_both $k $data}]
221
error_check_good error_case:$k [llength $ret] 0
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
229
error_check_good check_db:close [$check_db close] 0
230
error_check_good db_close [$db close] 0