1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2000,2008 Oracle.  All rights reserved.
4#
5# $Id: test078.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
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 { $eindex == -1 } {
104			set testfile $testdir/test$tnum-b.db
105			set env NULL
106		} else {
107			set testfile test$tnum-b.db
108			set env [lindex $args $eindex]
109			if { $is_je_test && $dupopt == "-dup" } {
110				continue
111			}
112			set testdir [get_home $env]
113		}
114		cleanup $testdir $env
115
116		puts "\tTest$tnum.$let: Duplicates ($descrip)."
117		puts "\t\tTest$tnum.$let.1: Populating database."
118
119		set db [eval {berkdb_open -create -mode 0644\
120		    -pagesize $pagesize} $dupopt $omethod $args {$testfile}]
121		error_check_good db_open [is_valid_db $db] TRUE
122
123		for { set i 1 } { $i <= $nkeys } { incr i } {
124			for { set j 0 } { $j < $i } { incr j } {
125				if { $txnenv == 1 } {
126					set t [$env txn]
127					error_check_good txn \
128					    [is_valid_txn $t $env] TRUE
129					set txn "-txn $t"
130				}
131				set ret [eval {$db put} $txn {$i\
132				    [pad_data $method $j$alphabet]}]
133				error_check_good put.$let,$i $ret 0
134				if { $txnenv == 1 } {
135					error_check_good txn [$t commit] 0
136				}
137			}
138		}
139
140		puts -nonewline "\t\tTest$tnum.$let.2: "
141		puts "Verifying duplicate counts."
142		for { set i 1 } { $i <= $nkeys } { incr i } {
143			error_check_good count.$let,$i \
144			    [$db count $i] $i
145		}
146
147		puts -nonewline "\t\tTest$tnum.$let.3: "
148		puts "Delete every other dup by cursor, verify counts."
149
150		# Delete every other item by cursor and check counts.
151 		for { set i 1 } { $i <= $nkeys } { incr i } {
152			if { $txnenv == 1 } {
153				set t [$env txn]
154				error_check_good txn [is_valid_txn $t $env] TRUE
155				set txn "-txn $t"
156			}
157			set c [eval {$db cursor} $txn]
158			error_check_good db_cursor [is_valid_cursor $c $db] TRUE
159			set j 0
160
161			for { set ret [$c get -first]} { [llength $ret] > 0 } \
162			    { set ret [$c get -next]} {
163				set key [lindex [lindex $ret 0] 0]
164				if { $key == $i } {
165					set data [lindex [lindex $ret 0 ] 1]
166					set num [string range $data 0 \
167					    end-[string length $alphabet]]
168					if { [expr $num % 2] == 0 } {
169						error_check_good \
170						    c_del [$c del] 0
171						incr j
172					}
173					if { $txnenv == 0 } {
174						error_check_good count.$let.$i-$j \
175						    [$db count $i] [expr $i - $j]
176					}
177				}
178			}
179			error_check_good curs_close [$c close] 0
180			if { $txnenv == 1 } {
181				error_check_good txn_commit [$t commit] 0
182			}
183			error_check_good count.$let.$i-$j \
184			    [$db count $i] [expr $i - $j]
185		}
186
187		puts -nonewline "\t\tTest$tnum.$let.4: "
188		puts "Delete all items by cursor, verify counts."
189		for { set i 1 } { $i <= $nkeys } { incr i } {
190			if { $txnenv == 1 } {
191				set t [$env txn]
192				error_check_good txn [is_valid_txn $t $env] TRUE
193				set txn "-txn $t"
194			}
195			set c [eval {$db cursor} $txn]
196			error_check_good db_cursor [is_valid_cursor $c $db] TRUE
197			for { set ret [$c get -first]} { [llength $ret] > 0 } \
198			    { set ret [$c get -next]} {
199				set key [lindex [lindex $ret 0] 0]
200				if { $key == $i } {
201					error_check_good c_del [$c del] 0
202				}
203			}
204			error_check_good curs_close [$c close] 0
205			if { $txnenv == 1 } {
206				error_check_good txn_commit [$t commit] 0
207			}
208			error_check_good db_count_zero [$db count $i] 0
209		}
210
211		puts -nonewline "\t\tTest$tnum.$let.5: "
212		puts "Add back one item, verify counts."
213		for { set i 1 } { $i <= $nkeys } { incr i } {
214			if { $txnenv == 1 } {
215				set t [$env txn]
216				error_check_good txn [is_valid_txn $t $env] TRUE
217				set txn "-txn $t"
218			}
219			set ret [eval {$db put} $txn {$i\
220			    [pad_data $method $alphabet]}]
221			error_check_good put.$let,$i $ret 0
222			if { $txnenv == 1 } {
223				error_check_good txn [$t commit] 0
224			}
225			error_check_good add_one [$db count $i] 1
226		}
227
228		puts -nonewline "\t\tTest$tnum.$let.6: "
229		puts "Delete remaining entries, verify counts."
230		for { set i 1 } { $i <= $nkeys } { incr i } {
231			if { $txnenv == 1 } {
232				set t [$env txn]
233				error_check_good txn [is_valid_txn $t $env] TRUE
234				set txn "-txn $t"
235			}
236			error_check_good db_del [eval {$db del} $txn {$i}] 0
237			if { $txnenv == 1 } {
238				error_check_good txn [$t commit] 0
239			}
240			error_check_good count.$let.$i [$db count $i] 0
241		}
242		error_check_good db_close.$let [$db close] 0
243	}
244}
245