summaryrefslogtreecommitdiff
path: root/bdb/test/test074.tcl
blob: ddc5f16429d4b5a2247c884a7face68079285e9f (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
209
210
211
212
213
214
215
216
217
218
219
220
221
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1999, 2000
#	Sleepycat Software.  All rights reserved.
#
#	$Id: test074.tcl,v 11.10 2000/08/25 14:21:58 sue Exp $
#
# DB Test 74: Test of DB_NEXT_NODUP.
proc test074 { method {dir -nextnodup} {pagesize 512} {nitems 100} {tnum 74} args } {
	source ./include.tcl
	global alphabet
	global rand_init

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

	berkdb srand $rand_init

	# Data prefix--big enough that we get a mix of on-page, off-page,
	# and multi-off-page dups with the default nitems
	if { [is_fixed_length $method] == 1 } {
		set globaldata "somedata"
	} else {
		set globaldata [repeat $alphabet 4]
	}

	puts "Test0$tnum $omethod ($args): Test of $dir"

	# First, test non-dup (and not-very-interesting) case with
	# all db types.

	puts "\tTest0$tnum.a: No duplicates."

	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/test0$tnum-nodup.db
		set env NULL
	} else {
		set testfile test0$tnum-nodup.db
		incr eindex
		set env [lindex $args $eindex]
	}
	cleanup $testdir $env
	set db [eval {berkdb_open -create -truncate -mode 0644} $omethod\
	    $args {$testfile}]
	error_check_good db_open [is_valid_db $db] TRUE

	# Insert nitems items.
	puts "\t\tTest0$tnum.a.1: Put loop."
	for {set i 1} {$i <= $nitems} {incr i} {
		#
		# If record based, set key to $i * 2 to leave
		# holes/unused entries for further testing.
		#
		if {[is_record_based $method] == 1} {
			set key [expr $i * 2]
		} else {
			set key "key$i"
		}
		set data "$globaldata$i"
		error_check_good put($i) [$db put $key\
		    [chop_data $method $data]] 0
	}

	puts "\t\tTest0$tnum.a.2: Get($dir)"

	# foundarray($i) is set when key number i is found in the database
	set dbc [$db cursor]
	error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE

	# Initialize foundarray($i) to zero for all $i
	for {set i 1} {$i < $nitems} {incr i} {
		set foundarray($i) 0
	}

	# Walk database using $dir and record each key gotten.
	for {set i 1} {$i <= $nitems} {incr i} {
		set dbt [$dbc get $dir]
		set key [lindex [lindex $dbt 0] 0]
		if {[is_record_based $method] == 1} {
			set num [expr $key / 2]
			set desired_key $key
			error_check_good $method:num $key [expr $num * 2]
		} else {
			set num [string range $key 3 end]
			set desired_key key$num
		}

		error_check_good dbt_correct($i) $dbt\
		    [list [list $desired_key\
		    [pad_data $method $globaldata$num]]]

		set foundarray($num) 1
	}

	puts "\t\tTest0$tnum.a.3: Final key."
	error_check_good last_db_get [$dbc get $dir] [list]

	puts "\t\tTest0$tnum.a.4: Verify loop."
	for { set i 1 } { $i <= $nitems } { incr i } {
		error_check_good found_key($i) $foundarray($i) 1
	}

	error_check_good dbc_close(nodup) [$dbc close] 0

	# If we are a method that doesn't allow dups, verify that
	# we get an empty list if we try to use DB_NEXT_DUP
	if { [is_record_based $method] == 1 || [is_rbtree $method] == 1 } {
		puts "\t\tTest0$tnum.a.5: Check DB_NEXT_DUP for $method."
		set dbc [$db cursor]
		error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE

		set dbt [$dbc get $dir]
		error_check_good $method:nextdup [$dbc get -nextdup] [list]
		error_check_good dbc_close(nextdup) [$dbc close] 0
	}
	error_check_good db_close(nodup) [$db close] 0

	# Quit here if we're a method that won't allow dups.
	if { [is_record_based $method] == 1 || [is_rbtree $method] == 1 } {
		puts "\tTest0$tnum: Skipping remainder for method $method."
		return
	}

	foreach opt { "-dup" "-dupsort" } {

		#
		# 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/test0$tnum$opt.db
		} else {
			set testfile test0$tnum$opt.db
		}

		if { [string compare $opt "-dupsort"] == 0 } {
			set opt "-dup -dupsort"
		}

		puts "\tTest0$tnum.b: Duplicates ($opt)."

		puts "\t\tTest0$tnum.b.1 ($opt): Put loop."
		set db [eval {berkdb_open -create -truncate -mode 0644}\
		    $opt $omethod $args {$testfile}]
		error_check_good db_open [is_valid_db $db] TRUE

		# Insert nitems different keys such that key i has i dups.
		for {set i 1} {$i <= $nitems} {incr i} {
			set key key$i

			for {set j 1} {$j <= $i} {incr j} {
				if { $j < 10 } {
					set data "${globaldata}00$j"
				} elseif { $j < 100 } {
					set data "${globaldata}0$j"
				} else {
					set data "$globaldata$j"
				}

				error_check_good put($i,$j) \
				    [$db put $key $data] 0
			}
		}

		# Initialize foundarray($i) to 0 for all i.
		unset foundarray
		for { set i 1 } { $i <= $nitems } { incr i } {
			set foundarray($i) 0
		}

		# Get loop--after each get, move forward a random increment
		# within the duplicate set.
		puts "\t\tTest0$tnum.b.2 ($opt): Get loop."
		set one "001"
		set dbc [$db cursor]
		error_check_good dbc($opt) [is_valid_cursor $dbc $db] TRUE
		for { set i 1 } { $i <= $nitems } { incr i } {
			set dbt [$dbc get $dir]
			set key [lindex [lindex $dbt 0] 0]
			set num [string range $key 3 end]

			set desired_key key$num
			if { [string compare $dir "-prevnodup"] == 0 } {
				if { $num < 10 } {
					set one "00$num"
				} elseif { $num < 100 } {
					set one "0$num"
				} else {
					set one $num
				}
			}

			error_check_good dbt_correct($i) $dbt\
				[list [list $desired_key\
				    "$globaldata$one"]]

			set foundarray($num) 1

			# Go forward by some number w/i dup set.
			set inc [berkdb random_int 0 [expr $num - 1]]
			for { set j 0 } { $j < $inc } { incr j } {
				eval {$dbc get -nextdup}
			}
		}

		puts "\t\tTest0$tnum.b.3 ($opt): Final key."
		error_check_good last_db_get($opt) [$dbc get $dir] [list]

		# Verify
		puts "\t\tTest0$tnum.b.4 ($opt): Verify loop."
		for { set i 1 } { $i <= $nitems } { incr i } {
			error_check_good found_key($i) $foundarray($i) 1
		}

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