1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2000-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	test078
8# TEST	Test of DBC->c_count(). [#303]
9proc test078 { method { nkeys 100 } { pagesize 512 } { tnum "078" } args } {
10	source ./include.tcl
11	global alphabet
12	global is_je_test
13	global rand_init
14
15	set args [convert_args $method $args]
16	set omethod [convert_method $method]
17
18	puts "Test$tnum ($method): Test of key counts."
19
20	berkdb srand $rand_init
21
22	set txnenv 0
23	set eindex [lsearch -exact $args "-env"]
24	if { $eindex != -1 } {
25		incr eindex
26	}
27
28	if { $eindex == -1 } {
29		set testfile $testdir/test$tnum-a.db
30		set env NULL
31	} else {
32		set testfile test$tnum-a.db
33		set env [lindex $args $eindex]
34		set txnenv [is_txnenv $env]
35		if { $txnenv == 1 } {
36			set nkeys 50
37			append args " -auto_commit "
38		}
39		set testdir [get_home $env]
40	}
41	cleanup $testdir $env
42
43	set pgindex [lsearch -exact $args "-pagesize"]
44	if { $pgindex != -1 } {
45		puts "Test078: skipping for specific pagesizes"
46		return
47	}
48	puts "\tTest$tnum.a: No duplicates, trivial answer."
49	puts "\t\tTest$tnum.a.1: Populate database, verify dup counts."
50	set db [eval {berkdb_open -create -mode 0644\
51	    -pagesize $pagesize} $omethod $args {$testfile}]
52	error_check_good db_open [is_valid_db $db] TRUE
53	set txn ""
54
55	for { set i 1 } { $i <= $nkeys } { incr i } {
56		if { $txnenv == 1 } {
57			set t [$env txn]
58			error_check_good txn [is_valid_txn $t $env] TRUE
59			set txn "-txn $t"
60		}
61		set ret [eval {$db put} $txn {$i\
62		    [pad_data $method $alphabet$i]}]
63		error_check_good put.a($i) $ret 0
64		if { $txnenv == 1 } {
65			error_check_good txn [$t commit] 0
66		}
67		error_check_good count.a [$db count $i] 1
68	}
69
70	if { [is_rrecno $method] == 1 } {
71		error_check_good db_close.a [$db close] 0
72		puts "\tTest$tnum.a2: Skipping remainder of test078 for -rrecno."
73		return
74	}
75
76	puts "\t\tTest$tnum.a.2: Delete items, verify dup counts again."
77	for { set i 1 } { $i <= $nkeys } { incr i } {
78		if { $txnenv == 1 } {
79			set t [$env txn]
80			error_check_good txn [is_valid_txn $t $env] TRUE
81			set txn "-txn $t"
82		}
83		set ret [eval {$db del} $txn $i]
84		error_check_good del.a($i) $ret 0
85		if { $txnenv == 1 } {
86			error_check_good txn [$t commit] 0
87		}
88		error_check_good count.a [$db count $i] 0
89	}
90
91
92	error_check_good db_close.a [$db close] 0
93
94	if { [is_record_based $method] == 1 || [is_rbtree $method] == 1 } {
95		puts \
96	    "\tTest$tnum.b: Duplicates not supported in $method, skipping."
97		return
98	}
99
100	foreach {let descrip dupopt} \
101	    {b sorted "-dup -dupsort" c unsorted "-dup"} {
102
103		if { [is_compressed $args] } {
104			if { $dupopt == "-dup" } {
105				continue
106			}
107		}
108		if { $eindex == -1 } {
109			set testfile $testdir/test$tnum-b.db
110			set env NULL
111		} else {
112			set testfile test$tnum-b.db
113			set env [lindex $args $eindex]
114			if { $is_je_test } {
115				if { $dupopt == "-dup" } {
116					continue
117				}
118			}
119			set testdir [get_home $env]
120		}
121		cleanup $testdir $env
122
123		puts "\tTest$tnum.$let: Duplicates ($descrip)."
124		puts "\t\tTest$tnum.$let.1: Populating database."
125
126		set db [eval {berkdb_open -create -mode 0644\
127		    -pagesize $pagesize} $dupopt $omethod $args {$testfile}]
128		error_check_good db_open [is_valid_db $db] TRUE
129
130		for { set i 1 } { $i <= $nkeys } { incr i } {
131			for { set j 0 } { $j < $i } { incr j } {
132				if { $txnenv == 1 } {
133					set t [$env txn]
134					error_check_good txn \
135					    [is_valid_txn $t $env] TRUE
136					set txn "-txn $t"
137				}
138				set ret [eval {$db put} $txn {$i\
139				    [pad_data $method $j$alphabet]}]
140				error_check_good put.$let,$i $ret 0
141				if { $txnenv == 1 } {
142					error_check_good txn [$t commit] 0
143				}
144			}
145		}
146
147		puts -nonewline "\t\tTest$tnum.$let.2: "
148		puts "Verifying duplicate counts."
149$db sync
150		for { set i 1 } { $i <= $nkeys } { incr i } {
151			error_check_good count.$let,$i \
152			    [$db count $i] $i
153		}
154
155		puts -nonewline "\t\tTest$tnum.$let.3: "
156		puts "Delete every other dup by cursor, verify counts."
157
158		# Delete every other item by cursor and check counts.
159 		for { set i 1 } { $i <= $nkeys } { incr i } {
160			if { $txnenv == 1 } {
161				set t [$env txn]
162				error_check_good txn [is_valid_txn $t $env] TRUE
163				set txn "-txn $t"
164			}
165			set c [eval {$db cursor} $txn]
166			error_check_good db_cursor [is_valid_cursor $c $db] TRUE
167			set j 0
168
169			for { set ret [$c get -first]} { [llength $ret] > 0 } \
170			    { set ret [$c get -next]} {
171				set key [lindex [lindex $ret 0] 0]
172				if { $key == $i } {
173					set data [lindex [lindex $ret 0 ] 1]
174					set num [string range $data 0 \
175					    end-[string length $alphabet]]
176					if { [expr $num % 2] == 0 } {
177						error_check_good \
178						    c_del [$c del] 0
179						incr j
180					}
181					if { $txnenv == 0 } {
182						error_check_good count.$let.$i-$j \
183						    [$db count $i] [expr $i - $j]
184					}
185				}
186			}
187			error_check_good curs_close [$c close] 0
188			if { $txnenv == 1 } {
189				error_check_good txn_commit [$t commit] 0
190			}
191			error_check_good count.$let.$i-$j \
192			    [$db count $i] [expr $i - $j]
193		}
194
195		puts -nonewline "\t\tTest$tnum.$let.4: "
196		puts "Delete all items by cursor, verify counts."
197		for { set i 1 } { $i <= $nkeys } { incr i } {
198			if { $txnenv == 1 } {
199				set t [$env txn]
200				error_check_good txn [is_valid_txn $t $env] TRUE
201				set txn "-txn $t"
202			}
203			set c [eval {$db cursor} $txn]
204			error_check_good db_cursor [is_valid_cursor $c $db] TRUE
205			for { set ret [$c get -first]} { [llength $ret] > 0 } \
206			    { set ret [$c get -next]} {
207				set key [lindex [lindex $ret 0] 0]
208				if { $key == $i } {
209					error_check_good c_del [$c del] 0
210				}
211			}
212			error_check_good curs_close [$c close] 0
213			if { $txnenv == 1 } {
214				error_check_good txn_commit [$t commit] 0
215			}
216			error_check_good db_count_zero [$db count $i] 0
217		}
218
219		puts -nonewline "\t\tTest$tnum.$let.5: "
220		puts "Add back one item, verify counts."
221		for { set i 1 } { $i <= $nkeys } { incr i } {
222			if { $txnenv == 1 } {
223				set t [$env txn]
224				error_check_good txn [is_valid_txn $t $env] TRUE
225				set txn "-txn $t"
226			}
227			set ret [eval {$db put} $txn {$i\
228			    [pad_data $method $alphabet]}]
229			error_check_good put.$let,$i $ret 0
230			if { $txnenv == 1 } {
231				error_check_good txn [$t commit] 0
232			}
233			error_check_good add_one [$db count $i] 1
234		}
235
236		puts -nonewline "\t\tTest$tnum.$let.6: "
237		puts "Delete remaining entries, verify counts."
238		for { set i 1 } { $i <= $nkeys } { incr i } {
239			if { $txnenv == 1 } {
240				set t [$env txn]
241				error_check_good txn [is_valid_txn $t $env] TRUE
242				set txn "-txn $t"
243			}
244			error_check_good db_del [eval {$db del} $txn {$i}] 0
245			if { $txnenv == 1 } {
246				error_check_good txn [$t commit] 0
247			}
248			error_check_good count.$let.$i [$db count $i] 0
249		}
250		error_check_good db_close.$let [$db close] 0
251	}
252}
253