1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: test065.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test065
8# TEST	Test of DB->stat, both -DB_FAST_STAT and row
9# TEST	counts with DB->stat -txn.
10proc test065 { method args } {
11	source ./include.tcl
12	global errorCode
13	global alphabet
14
15	set nentries 10000
16	set args [convert_args $method $args]
17	set omethod [convert_method $method]
18	set tnum "065"
19
20	set txnenv 0
21	set eindex [lsearch -exact $args "-env"]
22	#
23	# If we are using an env, then testfile should just be the db name.
24	# Otherwise it is the test directory and the name.
25	if { $eindex == -1 } {
26		set testfile $testdir/test$tnum.db
27		set env NULL
28	} else {
29		set testfile test$tnum.db
30		incr eindex
31		set env [lindex $args $eindex]
32		set txnenv [is_txnenv $env]
33		if { $txnenv == 1 } {
34			append args " -auto_commit "
35			#
36			# If we are using txns and running with the
37			# default, set the default down a bit.
38			#
39			if { $nentries == 10000 } {
40				set nentries 100
41			}
42		}
43		set testdir [get_home $env]
44	}
45	cleanup $testdir $env
46
47	puts "Test$tnum: $method ($args) DB->stat(DB_FAST_STAT) test."
48
49	puts "\tTest$tnum.a: Create database and check it while empty."
50
51	set db [eval {berkdb_open_noerr -create -mode 0644} \
52	    $omethod $args $testfile]
53	error_check_good db_open [is_valid_db $db] TRUE
54
55	set ret [catch {eval $db stat -faststat} res]
56
57	error_check_good db_close [$db close] 0
58
59	if { ([is_record_based $method] && ![is_queue $method]) \
60	    || [is_rbtree $method] } {
61		error_check_good recordcount_ok [is_substr $res \
62		    "{{Number of keys} 0}"] 1
63	} else {
64		puts "\tTest$tnum: Test complete for method $method."
65		return
66	}
67
68	# If we've got this far, we're on an access method for
69	# which record counts makes sense.  Thus, we no longer
70	# catch EINVALs, and no longer care about __db_errs.
71	set db [eval {berkdb_open -create -mode 0644} $omethod $args $testfile]
72
73	puts "\tTest$tnum.b: put $nentries keys."
74
75	if { [is_record_based $method] } {
76		set gflags " -recno "
77		set keypfx ""
78	} else {
79		set gflags ""
80		set keypfx "key"
81	}
82
83	set txn ""
84	set data [pad_data $method $alphabet]
85
86	for { set ndx 1 } { $ndx <= $nentries } { incr ndx } {
87		if { $txnenv == 1 } {
88			set t [$env txn]
89			error_check_good txn [is_valid_txn $t $env] TRUE
90			set txn "-txn $t"
91		}
92		set ret [eval {$db put} $txn {$keypfx$ndx $data}]
93		error_check_good db_put $ret 0
94		set statret [eval {$db stat} $txn]
95		set rowcount [getstats $statret "Number of records"]
96		error_check_good rowcount $rowcount $ndx
97		if { $txnenv == 1 } {
98			error_check_good txn [$t commit] 0
99		}
100	}
101
102	set ret [$db stat -faststat]
103	error_check_good recordcount_after_puts \
104	    [is_substr $ret "{{Number of keys} $nentries}"] 1
105
106	puts "\tTest$tnum.c: delete 90% of keys."
107	set end [expr {$nentries / 10 * 9}]
108	for { set ndx 1 } { $ndx <= $end } { incr ndx } {
109		if { $txnenv == 1 } {
110			set t [$env txn]
111			error_check_good txn [is_valid_txn $t $env] TRUE
112			set txn "-txn $t"
113		}
114		if { [is_rrecno $method] == 1 } {
115			# if we're renumbering, when we hit key 5001 we'll
116			# have deleted 5000 and we'll croak!  So delete key
117			# 1, repeatedly.
118			set ret [eval {$db del} $txn {[concat $keypfx 1]}]
119			set statret [eval {$db stat} $txn]
120			set rowcount [getstats $statret "Number of records"]
121			error_check_good rowcount $rowcount [expr $nentries - $ndx]
122		} else {
123			set ret [eval {$db del} $txn {$keypfx$ndx}]
124			set rowcount [getstats $statret "Number of records"]
125			error_check_good rowcount $rowcount $nentries
126		}
127		error_check_good db_del $ret 0
128		if { $txnenv == 1 } {
129			error_check_good txn [$t commit] 0
130		}
131	}
132
133	set ret [$db stat -faststat]
134	if { [is_rrecno $method] == 1 || [is_rbtree $method] == 1 } {
135		# We allow renumbering--thus the stat should return 10%
136		# of nentries.
137		error_check_good recordcount_after_dels [is_substr $ret \
138		    "{{Number of keys} [expr {$nentries / 10}]}"] 1
139	} else {
140		# No renumbering--no change in RECORDCOUNT!
141		error_check_good recordcount_after_dels \
142		    [is_substr $ret "{{Number of keys} $nentries}"] 1
143	}
144
145	puts "\tTest$tnum.d: put new keys at the beginning."
146	set end [expr {$nentries / 10 * 8}]
147	for { set ndx 1 } { $ndx <= $end } {incr ndx } {
148		if { $txnenv == 1 } {
149			set t [$env txn]
150			error_check_good txn [is_valid_txn $t $env] TRUE
151			set txn "-txn $t"
152		}
153		set ret [eval {$db put} $txn {$keypfx$ndx $data}]
154		error_check_good db_put_beginning $ret 0
155		if { $txnenv == 1 } {
156			error_check_good txn [$t commit] 0
157		}
158	}
159
160	set ret [$db stat -faststat]
161	if { [is_rrecno $method] == 1 } {
162		# With renumbering we're back up to 80% of $nentries
163		error_check_good recordcount_after_dels [is_substr $ret \
164		    "{{Number of keys} [expr {$nentries / 10 * 8}]}"] 1
165	} elseif { [is_rbtree $method] == 1 } {
166		# Total records in a btree is now 90% of $nentries
167		error_check_good recordcount_after_dels [is_substr $ret \
168		    "{{Number of keys} [expr {$nentries / 10 * 9}]}"] 1
169	} else {
170		# No renumbering--still no change in RECORDCOUNT.
171		error_check_good recordcount_after_dels [is_substr $ret \
172		    "{{Number of keys} $nentries}"] 1
173	}
174
175	puts "\tTest$tnum.e: put new keys at the end."
176	set start [expr {1 + $nentries / 10 * 9}]
177	set end [expr {($nentries / 10 * 9) + ($nentries / 10 * 8)}]
178	for { set ndx $start } { $ndx <= $end } { incr ndx } {
179		if { $txnenv == 1 } {
180			set t [$env txn]
181			error_check_good txn [is_valid_txn $t $env] TRUE
182			set txn "-txn $t"
183		}
184		set ret [eval {$db put} $txn {$keypfx$ndx $data}]
185		error_check_good db_put_end $ret 0
186		if { $txnenv == 1 } {
187			error_check_good txn [$t commit] 0
188		}
189	}
190
191	set ret [$db stat -faststat]
192	if { [is_rbtree $method] != 1 } {
193		# If this is a recno database, the record count should be up
194		# to (1.7 x nentries), the largest number we've seen, with
195		# or without renumbering.
196		error_check_good recordcount_after_puts2 [is_substr $ret \
197		    "{{Number of keys} [expr {$start - 1 + $nentries / 10 * 8}]}"] 1
198	} else {
199		# In an rbtree, 1000 of those keys were overwrites, so there
200		# are (.7 x nentries) new keys and (.9 x nentries) old keys
201		# for a total of (1.6 x nentries).
202		error_check_good recordcount_after_puts2 [is_substr $ret \
203		    "{{Number of keys} [expr {$start -1 + $nentries / 10 * 7}]}"] 1
204	}
205
206	error_check_good db_close [$db close] 0
207}
208