summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/test078.tcl
blob: 5c9f2d6d63dd7b35bdaed562a5ebdda7eae85e0d (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# See the file LICENSE for redistribution information.
#
# Copyright (c) 2000-2009 Oracle.  All rights reserved.
#
# $Id$
#
# TEST	test078
# TEST	Test of DBC->c_count(). [#303]
proc test078 { method { nkeys 100 } { pagesize 512 } { tnum "078" } args } {
	source ./include.tcl
	global alphabet
	global is_je_test
	global rand_init

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

	puts "Test$tnum ($method): Test of key counts."

	berkdb srand $rand_init

	set txnenv 0
	set eindex [lsearch -exact $args "-env"]
	if { $eindex != -1 } {
		incr eindex
	}

	if { $eindex == -1 } {
		set testfile $testdir/test$tnum-a.db
		set env NULL
	} else {
		set testfile test$tnum-a.db
		set env [lindex $args $eindex]
		set txnenv [is_txnenv $env]
		if { $txnenv == 1 } {
			set nkeys 50
			append args " -auto_commit "
		}
		set testdir [get_home $env]
	}
	cleanup $testdir $env

	set pgindex [lsearch -exact $args "-pagesize"]
	if { $pgindex != -1 } {
		puts "Test078: skipping for specific pagesizes"
		return
	}
	puts "\tTest$tnum.a: No duplicates, trivial answer."
	puts "\t\tTest$tnum.a.1: Populate database, verify dup counts."
	set db [eval {berkdb_open -create -mode 0644\
	    -pagesize $pagesize} $omethod $args {$testfile}]
	error_check_good db_open [is_valid_db $db] TRUE
	set txn ""

	for { set i 1 } { $i <= $nkeys } { incr i } {
		if { $txnenv == 1 } {
			set t [$env txn]
			error_check_good txn [is_valid_txn $t $env] TRUE
			set txn "-txn $t"
		}
		set ret [eval {$db put} $txn {$i\
		    [pad_data $method $alphabet$i]}]
		error_check_good put.a($i) $ret 0
		if { $txnenv == 1 } {
			error_check_good txn [$t commit] 0
		}
		error_check_good count.a [$db count $i] 1
	}

	if { [is_rrecno $method] == 1 } {
		error_check_good db_close.a [$db close] 0
		puts "\tTest$tnum.a2: Skipping remainder of test078 for -rrecno."
		return
	}

	puts "\t\tTest$tnum.a.2: Delete items, verify dup counts again."
	for { set i 1 } { $i <= $nkeys } { incr i } {
		if { $txnenv == 1 } {
			set t [$env txn]
			error_check_good txn [is_valid_txn $t $env] TRUE
			set txn "-txn $t"
		}
		set ret [eval {$db del} $txn $i]
		error_check_good del.a($i) $ret 0
		if { $txnenv == 1 } {
			error_check_good txn [$t commit] 0
		}
		error_check_good count.a [$db count $i] 0
	}


	error_check_good db_close.a [$db close] 0

	if { [is_record_based $method] == 1 || [is_rbtree $method] == 1 } {
		puts \
	    "\tTest$tnum.b: Duplicates not supported in $method, skipping."
		return
	}

	foreach {let descrip dupopt} \
	    {b sorted "-dup -dupsort" c unsorted "-dup"} {

		if { [is_compressed $args] } {
			if { $dupopt == "-dup" } {
				continue
			}
		}
		if { $eindex == -1 } {
			set testfile $testdir/test$tnum-b.db
			set env NULL
		} else {
			set testfile test$tnum-b.db
			set env [lindex $args $eindex]
			if { $is_je_test } {
				if { $dupopt == "-dup" } {
					continue
				}
			}
			set testdir [get_home $env]
		}
		cleanup $testdir $env

		puts "\tTest$tnum.$let: Duplicates ($descrip)."
		puts "\t\tTest$tnum.$let.1: Populating database."

		set db [eval {berkdb_open -create -mode 0644\
		    -pagesize $pagesize} $dupopt $omethod $args {$testfile}]
		error_check_good db_open [is_valid_db $db] TRUE

		for { set i 1 } { $i <= $nkeys } { incr i } {
			for { set j 0 } { $j < $i } { incr j } {
				if { $txnenv == 1 } {
					set t [$env txn]
					error_check_good txn \
					    [is_valid_txn $t $env] TRUE
					set txn "-txn $t"
				}
				set ret [eval {$db put} $txn {$i\
				    [pad_data $method $j$alphabet]}]
				error_check_good put.$let,$i $ret 0
				if { $txnenv == 1 } {
					error_check_good txn [$t commit] 0
				}
			}
		}

		puts -nonewline "\t\tTest$tnum.$let.2: "
		puts "Verifying duplicate counts."
$db sync
		for { set i 1 } { $i <= $nkeys } { incr i } {
			error_check_good count.$let,$i \
			    [$db count $i] $i
		}

		puts -nonewline "\t\tTest$tnum.$let.3: "
		puts "Delete every other dup by cursor, verify counts."

		# Delete every other item by cursor and check counts.
 		for { set i 1 } { $i <= $nkeys } { incr i } {
			if { $txnenv == 1 } {
				set t [$env txn]
				error_check_good txn [is_valid_txn $t $env] TRUE
				set txn "-txn $t"
			}
			set c [eval {$db cursor} $txn]
			error_check_good db_cursor [is_valid_cursor $c $db] TRUE
			set j 0

			for { set ret [$c get -first]} { [llength $ret] > 0 } \
			    { set ret [$c get -next]} {
				set key [lindex [lindex $ret 0] 0]
				if { $key == $i } {
					set data [lindex [lindex $ret 0 ] 1]
					set num [string range $data 0 \
					    end-[string length $alphabet]]
					if { [expr $num % 2] == 0 } {
						error_check_good \
						    c_del [$c del] 0
						incr j
					}
					if { $txnenv == 0 } {
						error_check_good count.$let.$i-$j \
						    [$db count $i] [expr $i - $j]
					}
				}
			}
			error_check_good curs_close [$c close] 0
			if { $txnenv == 1 } {
				error_check_good txn_commit [$t commit] 0
			}
			error_check_good count.$let.$i-$j \
			    [$db count $i] [expr $i - $j]
		}

		puts -nonewline "\t\tTest$tnum.$let.4: "
		puts "Delete all items by cursor, verify counts."
		for { set i 1 } { $i <= $nkeys } { incr i } {
			if { $txnenv == 1 } {
				set t [$env txn]
				error_check_good txn [is_valid_txn $t $env] TRUE
				set txn "-txn $t"
			}
			set c [eval {$db cursor} $txn]
			error_check_good db_cursor [is_valid_cursor $c $db] TRUE
			for { set ret [$c get -first]} { [llength $ret] > 0 } \
			    { set ret [$c get -next]} {
				set key [lindex [lindex $ret 0] 0]
				if { $key == $i } {
					error_check_good c_del [$c del] 0
				}
			}
			error_check_good curs_close [$c close] 0
			if { $txnenv == 1 } {
				error_check_good txn_commit [$t commit] 0
			}
			error_check_good db_count_zero [$db count $i] 0
		}

		puts -nonewline "\t\tTest$tnum.$let.5: "
		puts "Add back one item, verify counts."
		for { set i 1 } { $i <= $nkeys } { incr i } {
			if { $txnenv == 1 } {
				set t [$env txn]
				error_check_good txn [is_valid_txn $t $env] TRUE
				set txn "-txn $t"
			}
			set ret [eval {$db put} $txn {$i\
			    [pad_data $method $alphabet]}]
			error_check_good put.$let,$i $ret 0
			if { $txnenv == 1 } {
				error_check_good txn [$t commit] 0
			}
			error_check_good add_one [$db count $i] 1
		}

		puts -nonewline "\t\tTest$tnum.$let.6: "
		puts "Delete remaining entries, verify counts."
		for { set i 1 } { $i <= $nkeys } { incr i } {
			if { $txnenv == 1 } {
				set t [$env txn]
				error_check_good txn [is_valid_txn $t $env] TRUE
				set txn "-txn $t"
			}
			error_check_good db_del [eval {$db del} $txn {$i}] 0
			if { $txnenv == 1 } {
				error_check_good txn [$t commit] 0
			}
			error_check_good count.$let.$i [$db count $i] 0
		}
		error_check_good db_close.$let [$db close] 0
	}
}