1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: sdb003.tcl,v 12.9 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	sdb003
8# TEST	Tests many subdbs
9# TEST		Creates many subdbs and puts a small amount of
10# TEST		data in each (many defaults to 1000)
11# TEST
12# TEST	Use the first 1000 entries from the dictionary as subdbnames.
13# TEST	Insert each with entry as name of subdatabase and a partial list
14# TEST	as key/data.  After all are entered, retrieve all; compare output
15# TEST	to original.  Close file, reopen, do retrieve and re-verify.
16proc sdb003 { method {nentries 1000} args } {
17	source ./include.tcl
18
19	set args [convert_args $method $args]
20	set omethod [convert_method $method]
21
22	if { [is_queue $method] == 1 } {
23		puts "Subdb003: skipping for method $method"
24		return
25	}
26
27	puts "Subdb003: $method ($args) many subdb tests"
28
29	set txnenv 0
30	set rpcenv 0
31	set eindex [lsearch -exact $args "-env"]
32	#
33	# If we are using an env, then testfile should just be the db name.
34	# Otherwise it is the test directory and the name.
35	if { $eindex == -1 } {
36		set testfile $testdir/subdb003.db
37		set env NULL
38	} else {
39		set testfile subdb003.db
40		incr eindex
41		set env [lindex $args $eindex]
42		set rpcenv [is_rpcenv $env]
43		set txnenv [is_txnenv $env]
44		if { $txnenv == 1 } {
45			append args " -auto_commit "
46			if { $nentries == 1000 } {
47				set nentries 100
48			}
49		}
50		set testdir [get_home $env]
51	}
52	# Create the database and open the dictionary
53	set t1 $testdir/t1
54	set t2 $testdir/t2
55	set t3 $testdir/t3
56	cleanup $testdir $env
57
58	set pflags ""
59	set gflags ""
60	set txn ""
61	set fcount 0
62
63	if { [is_record_based $method] == 1 } {
64		set checkfunc subdb003_recno.check
65		append gflags " -recno"
66	} else {
67		set checkfunc subdb003.check
68	}
69
70	# Here is the loop where we put and get each key/data pair
71	set ndataent 10
72	set fdid [open $dict]
73	while { [gets $fdid str] != -1 && $fcount < $nentries } {
74		# Unlike the standard API, RPC doesn't support empty
75		# database names. [#15600]
76		if { $str == "" && $rpcenv == 1 } {
77			continue
78		}
79
80		set subdb $str
81		set db [eval {berkdb_open -create -mode 0644} \
82		    $args {$omethod $testfile $subdb}]
83		error_check_good dbopen [is_valid_db $db] TRUE
84
85		set count 0
86		set did [open $dict]
87		while { [gets $did str] != -1 && $count < $ndataent } {
88			if { [is_record_based $method] == 1 } {
89				global kvals
90
91				set key [expr $count + 1]
92				set kvals($key) [pad_data $method $str]
93			} else {
94				set key $str
95			}
96			if { $txnenv == 1 } {
97				set t [$env txn]
98				error_check_good txn [is_valid_txn $t $env] TRUE
99				set txn "-txn $t"
100			}
101			set ret [eval {$db put} \
102			    $txn $pflags {$key [chop_data $method $str]}]
103			error_check_good put $ret 0
104			if { $txnenv == 1 } {
105				error_check_good txn [$t commit] 0
106			}
107
108			set ret [eval {$db get} $gflags {$key}]
109			error_check_good get $ret [list [list $key \
110			    [pad_data $method $str]]]
111			incr count
112		}
113		close $did
114		incr fcount
115
116		if { $txnenv == 1 } {
117			set t [$env txn]
118			error_check_good txn [is_valid_txn $t $env] TRUE
119			set txn "-txn $t"
120		}
121		dump_file $db $txn $t1 $checkfunc
122		if { $txnenv == 1 } {
123			error_check_good txn [$t commit] 0
124		}
125		error_check_good db_close [$db close] 0
126
127		# Now compare the keys to see if they match
128		if { [is_record_based $method] == 1 } {
129			set oid [open $t2 w]
130			for {set i 1} {$i <= $ndataent} {set i [incr i]} {
131				puts $oid $i
132			}
133			close $oid
134			file rename -force $t1 $t3
135		} else {
136			set q q
137			filehead $ndataent $dict $t3
138			filesort $t3 $t2
139			filesort $t1 $t3
140		}
141
142		error_check_good Subdb003:diff($t3,$t2) \
143		    [filecmp $t3 $t2] 0
144
145		# Now, reopen the file and run the last test again.
146		open_and_dump_subfile $testfile $env $t1 $checkfunc \
147		dump_file_direction "-first" "-next" $subdb
148		if { [is_record_based $method] != 1 } {
149			filesort $t1 $t3
150		}
151
152		error_check_good Subdb003:diff($t2,$t3) \
153		    [filecmp $t2 $t3] 0
154
155		# Now, reopen the file and run the last test again in the
156		# reverse direction.
157		open_and_dump_subfile $testfile $env $t1 $checkfunc \
158		    dump_file_direction "-last" "-prev" $subdb
159
160		if { [is_record_based $method] != 1 } {
161			filesort $t1 $t3
162		}
163
164		error_check_good Subdb003:diff($t3,$t2) \
165		    [filecmp $t3 $t2] 0
166		if { [expr $fcount % 100] == 0 } {
167			puts -nonewline "$fcount "
168			flush stdout
169		}
170	}
171	close $fdid
172	puts ""
173}
174
175# Check function for Subdb003; keys and data are identical
176proc subdb003.check { key data } {
177	error_check_good "key/data mismatch" $data $key
178}
179
180proc subdb003_recno.check { key data } {
181	global dict
182	global kvals
183
184	error_check_good key"$key"_exists [info exists kvals($key)] 1
185	error_check_good "key/data mismatch, key $key" $data $kvals($key)
186}
187