1
# See the file LICENSE for redistribution information.
3
# Copyright (c) 1996-2002
4
# Sleepycat Software. All rights reserved.
6
# $Id: test031.tcl,v 11.24 2002/06/26 06:22:44 krinsky Exp $
9
# TEST Duplicate sorting functionality
10
# TEST Make sure DB_NODUPDATA works.
12
# TEST Use the first 10,000 entries from the dictionary.
13
# TEST Insert each with self as key and "ndups" duplicates
14
# TEST For the data field, prepend random five-char strings (see test032)
15
# TEST that we force the duplicate sorting code to do something.
16
# TEST Along the way, test that we cannot insert duplicate duplicates
17
# TEST using DB_NODUPDATA.
19
# TEST By setting ndups large, we can make this an off-page test
20
# TEST After all are entered, retrieve all; verify output.
21
# TEST Close file, reopen, do retrieve and re-verify.
22
# TEST This does not work for recno
23
proc test031 { method {nentries 10000} {ndups 5} {tnum 31} args } {
28
berkdb srand $rand_init
30
set args [convert_args $method $args]
31
set omethod [convert_method $method]
33
# Create the database and open the dictionary
35
set eindex [lsearch -exact $args "-env"]
37
# If we are using an env, then testfile should just be the db name.
38
# Otherwise it is the test directory and the name.
39
if { $eindex == -1 } {
40
set testfile $testdir/test0$tnum.db
41
set checkdb $testdir/checkdb.db
44
set testfile test0$tnum.db
45
set checkdb checkdb.db
47
set env [lindex $args $eindex]
48
set txnenv [is_txnenv $env]
50
append args " -auto_commit "
52
# If we are using txns and running with the
53
# default, set the default down a bit.
55
if { $nentries == 10000 } {
58
reduce_dups nentries ndups
60
set testdir [get_home $env]
68
$method ($args) $nentries small $ndups sorted dup key/data pairs"
69
if { [is_record_based $method] == 1 || \
70
[is_rbtree $method] == 1 } {
71
puts "Test0$tnum skipping for method $omethod"
74
set db [eval {berkdb_open -create \
75
-mode 0644} $args {$omethod -dup -dupsort $testfile}]
76
error_check_good dbopen [is_valid_db $db] TRUE
79
set check_db [eval {berkdb_open \
80
-create -mode 0644} $args {-hash $checkdb}]
81
error_check_good dbopen:check_db [is_valid_db $check_db] TRUE
88
# Here is the loop where we put and get each key/data pair
89
puts "\tTest0$tnum.a: Put/get loop, check nodupdata"
92
error_check_good txn [is_valid_txn $t $env] TRUE
95
set dbc [eval {$db cursor} $txn]
96
error_check_good cursor_open [is_valid_cursor $dbc $db] TRUE
97
while { [gets $did str] != -1 && $count < $nentries } {
98
# Re-initialize random string generator
99
randstring_init $ndups
102
for { set i 1 } { $i <= $ndups } { incr i } {
103
set pref [randstring]
105
set datastr $pref:$str
107
set nodupstr $datastr
109
set ret [eval {$db put} \
110
$txn $pflags {$str [chop_data $method $datastr]}]
111
error_check_good put $ret 0
114
# Test DB_NODUPDATA using the DB handle
115
set ret [eval {$db put -nodupdata} \
116
$txn $pflags {$str [chop_data $method $nodupstr]}]
117
error_check_good db_nodupdata [is_substr $ret "DB_KEYEXIST"] 1
119
set ret [eval {$check_db put} \
120
$txn $pflags {$str [chop_data $method $dups]}]
121
error_check_good checkdb_put $ret 0
123
# Now retrieve all the keys matching this key
126
# Test DB_NODUPDATA using cursor handle
127
set ret [$dbc get -set $str]
128
error_check_bad dbc_get [llength $ret] 0
129
set datastr [lindex [lindex $ret 0] 1]
130
error_check_bad dbc_data [string length $datastr] 0
131
set ret [eval {$dbc put -nodupdata} \
132
{$str [chop_data $method $datastr]}]
133
error_check_good dbc_nodupdata [is_substr $ret "DB_KEYEXIST"] 1
135
for {set ret [$dbc get -set $str]} \
136
{[llength $ret] != 0} \
137
{set ret [$dbc get -nextdup] } {
138
set k [lindex [lindex $ret 0] 0]
139
if { [string compare $k $str] != 0 } {
142
set datastr [lindex [lindex $ret 0] 1]
143
if {[string length $datastr] == 0} {
146
if {[string compare \
147
$lastdup [pad_data $method $datastr]] > 0} {
149
sorted_dups($lastdup,$datastr) 0 1
154
error_check_good "Test0$tnum:ndups:$str" $x $ndups
157
error_check_good cursor_close [$dbc close] 0
158
if { $txnenv == 1 } {
159
error_check_good txn [$t commit] 0
163
# Now we will get each key from the DB and compare the results
165
puts "\tTest0$tnum.b: Checking file for correct duplicates"
166
if { $txnenv == 1 } {
168
error_check_good txn [is_valid_txn $t $env] TRUE
171
set dbc [eval {$db cursor} $txn]
172
error_check_good cursor_open(2) [is_valid_cursor $dbc $db] TRUE
174
set lastkey "THIS WILL NEVER BE A KEY VALUE"
175
# no need to delete $lastkey
177
for {set ret [$dbc get -first]} \
178
{[llength $ret] != 0} \
179
{set ret [$dbc 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
if { [string compare $k $lastkey] != 0 } {
185
# Remove last key from the checkdb
186
if { $firsttimethru != 1 } {
187
error_check_good check_db:del:$lastkey \
188
[eval {$check_db del} $txn {$lastkey}] 0
193
set dups [lindex [lindex [eval {$check_db get} \
195
error_check_good check_db:get:$k \
196
[string length $dups] [expr $ndups * 4]
199
if { [string compare $lastdup $d] > 0 } {
200
error_check_good dup_check:$k:$d 0 1
204
set pref [string range $d 0 3]
205
set ndx [string first $pref $dups]
206
error_check_good valid_duplicate [expr $ndx >= 0] 1
207
set a [string range $dups 0 [expr $ndx - 1]]
208
set b [string range $dups [expr $ndx + 4] end]
211
# Remove last key from the checkdb
212
if { [string length $lastkey] != 0 } {
213
error_check_good check_db:del:$lastkey \
214
[eval {$check_db del} $txn {$lastkey}] 0
217
# Make sure there is nothing left in check_db
219
set check_c [eval {$check_db cursor} $txn]
220
set ret [$check_c get -first]
221
error_check_good check_c:get:$ret [llength $ret] 0
222
error_check_good check_c:close [$check_c close] 0
224
error_check_good dbc_close [$dbc close] 0
225
if { $txnenv == 1 } {
226
error_check_good txn [$t commit] 0
228
error_check_good check_db:close [$check_db close] 0
229
error_check_good db_close [$db close] 0