1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: sdb004.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	sdb004
8# TEST	Tests large subdb names
9# TEST		subdb name = filecontents,
10# TEST		key = filename, data = filecontents
11# TEST			Put/get per key
12# TEST			Dump file
13# TEST			Dump subdbs, verify data and subdb name match
14# TEST
15# TEST	Create 1 db with many large subdbs.  Use the contents as subdb names.
16# TEST	Take the source files and dbtest executable and enter their names as
17# TEST	the key with their contents as data.  After all are entered, retrieve
18# TEST	all; compare output to original. Close file, reopen, do retrieve and
19# TEST	re-verify.
20proc sdb004 { method args} {
21	global names
22	source ./include.tcl
23
24	set args [convert_args $method $args]
25	set omethod [convert_method $method]
26
27	if { [is_queue $method] == 1 || [is_fixed_length $method] == 1 } {
28		puts "Subdb004: skipping for method $method"
29		return
30	}
31
32	puts "Subdb004: $method ($args) \
33	    filecontents=subdbname filename=key filecontents=data pairs"
34
35	set txnenv 0
36	set envargs ""
37	set eindex [lsearch -exact $args "-env"]
38	#
39	# If we are using an env, then testfile should just be the db name.
40	# Otherwise it is the test directory and the name.
41	if { $eindex == -1 } {
42		set testfile $testdir/subdb004.db
43		set env NULL
44	} else {
45		set testfile subdb004.db
46		incr eindex
47		set env [lindex $args $eindex]
48		set envargs " -env $env "
49		set txnenv [is_txnenv $env]
50		if { $txnenv == 1 } {
51			append args " -auto_commit "
52			append envargs " -auto_commit "
53		}
54		set testdir [get_home $env]
55	}
56	# Create the database and open the dictionary
57	set t1 $testdir/t1
58	set t2 $testdir/t2
59	set t3 $testdir/t3
60	set t4 $testdir/t4
61
62	cleanup $testdir $env
63	set pflags ""
64	set gflags ""
65	set txn ""
66	if { [is_record_based $method] == 1 } {
67		set checkfunc subdb004_recno.check
68		append gflags "-recno"
69	} else {
70		set checkfunc subdb004.check
71	}
72
73	# Here is the loop where we put and get each key/data pair
74	# Note that the subdatabase name is passed in as a char *, not
75	# in a DBT, so it may not contain nulls;  use only source files.
76	set file_list [glob $src_root/*/*.c]
77	set fcount [llength $file_list]
78	if { $txnenv == 1 && $fcount > 100 } {
79		set file_list [lrange $file_list 0 99]
80		set fcount 100
81	}
82
83	set count 0
84	if { [is_record_based $method] == 1 } {
85		set oid [open $t2 w]
86		for {set i 1} {$i <= $fcount} {set i [incr i]} {
87			puts $oid $i
88		}
89		close $oid
90	} else {
91		set oid [open $t2.tmp w]
92		foreach f $file_list {
93			puts $oid $f
94		}
95		close $oid
96		filesort $t2.tmp $t2
97	}
98	puts "\tSubdb004.a: Set/Check each subdb"
99	foreach f $file_list {
100		if { [is_record_based $method] == 1 } {
101			set key [expr $count + 1]
102			set names([expr $count + 1]) $f
103		} else {
104			set key $f
105		}
106		# Should really catch errors
107		set fid [open $f r]
108		fconfigure $fid -translation binary
109		set data [read $fid]
110		set subdb $data
111		close $fid
112		set db [eval {berkdb_open -create -mode 0644} \
113		    $args {$omethod $testfile $subdb}]
114		error_check_good dbopen [is_valid_db $db] TRUE
115		if { $txnenv == 1 } {
116			set t [$env txn]
117			error_check_good txn [is_valid_txn $t $env] TRUE
118			set txn "-txn $t"
119		}
120		set ret [eval \
121		    {$db put} $txn $pflags {$key [chop_data $method $data]}]
122		error_check_good put $ret 0
123		if { $txnenv == 1 } {
124			error_check_good txn [$t commit] 0
125		}
126
127		# Should really catch errors
128		set fid [open $t4 w]
129		fconfigure $fid -translation binary
130		if [catch {eval {$db get} $gflags {$key}} data] {
131			puts -nonewline $fid $data
132		} else {
133			# Data looks like {{key data}}
134			set key [lindex [lindex $data 0] 0]
135			set data [lindex [lindex $data 0] 1]
136			puts -nonewline $fid $data
137		}
138		close $fid
139
140		error_check_good Subdb004:diff($f,$t4) \
141		    [filecmp $f $t4] 0
142
143		incr count
144
145		# Now we will get each key from the DB and compare the results
146		# to the original.
147		# puts "\tSubdb004.b: dump file"
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		dump_bin_file $db $txn $t1 $checkfunc
154		if { $txnenv == 1 } {
155			error_check_good txn [$t commit] 0
156		}
157		error_check_good db_close [$db close] 0
158
159	}
160
161	#
162	# Now for each file, check that the subdb name is the same
163	# as the data in that subdb and that the filename is the key.
164	#
165	puts "\tSubdb004.b: Compare subdb names with key/data"
166	set db [eval {berkdb_open -rdonly} $envargs {$testfile}]
167	error_check_good dbopen [is_valid_db $db] TRUE
168	if { $txnenv == 1 } {
169		set t [$env txn]
170		error_check_good txn [is_valid_txn $t $env] TRUE
171		set txn "-txn $t"
172	}
173	set c [eval {$db cursor} $txn]
174	error_check_good db_cursor [is_valid_cursor $c $db] TRUE
175
176	for {set d [$c get -first] } { [llength $d] != 0 } \
177	    {set d [$c get -next] } {
178		set subdbname [lindex [lindex $d 0] 0]
179		set subdb [eval {berkdb_open} $args {$testfile $subdbname}]
180		error_check_good dbopen [is_valid_db $db] TRUE
181
182		# Output the subdb name
183		set ofid [open $t3 w]
184		fconfigure $ofid -translation binary
185		if { [string compare "\0" \
186		    [string range $subdbname end end]] == 0 } {
187			set slen [expr [string length $subdbname] - 2]
188			set subdbname [string range $subdbname 1 $slen]
189		}
190		puts -nonewline $ofid $subdbname
191		close $ofid
192
193		# Output the data
194		set subc [eval {$subdb cursor} $txn]
195		error_check_good db_cursor [is_valid_cursor $subc $subdb] TRUE
196		set d [$subc get -first]
197		error_check_good dbc_get [expr [llength $d] != 0] 1
198		set key [lindex [lindex $d 0] 0]
199		set data [lindex [lindex $d 0] 1]
200
201		set ofid [open $t1 w]
202		fconfigure $ofid -translation binary
203		puts -nonewline $ofid $data
204		close $ofid
205
206		$checkfunc $key $t1
207		$checkfunc $key $t3
208
209		error_check_good Subdb004:diff($t3,$t1) \
210		    [filecmp $t3 $t1] 0
211		error_check_good curs_close [$subc close] 0
212		error_check_good db_close [$subdb close] 0
213	}
214	error_check_good curs_close [$c close] 0
215	if { $txnenv == 1 } {
216		error_check_good txn [$t commit] 0
217	}
218	error_check_good db_close [$db close] 0
219
220	if { [is_record_based $method] != 1 } {
221		fileremove $t2.tmp
222	}
223}
224
225# Check function for subdb004; key should be file name; data should be contents
226proc subdb004.check { binfile tmpfile } {
227	source ./include.tcl
228
229	error_check_good Subdb004:datamismatch($binfile,$tmpfile) \
230	    [filecmp $binfile $tmpfile] 0
231}
232proc subdb004_recno.check { binfile tmpfile } {
233	global names
234	source ./include.tcl
235
236	set fname $names($binfile)
237	error_check_good key"$binfile"_exists [info exists names($binfile)] 1
238	error_check_good Subdb004:datamismatch($fname,$tmpfile) \
239	    [filecmp $fname $tmpfile] 0
240}
241