1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996,2008 Oracle.  All rights reserved.
4#
5# $Id: test008.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test008
8# TEST	Small keys/large data
9# TEST		Put/get per key
10# TEST		Loop through keys by steps (which change)
11# TEST		    ... delete each key at step
12# TEST		    ... add each key back
13# TEST		    ... change step
14# TEST		Confirm that overflow pages are getting reused
15# TEST
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, begin
18# TEST	looping through the entries; deleting some pairs and then readding them.
19proc test008 { method {reopen "008"} {debug 0} args} {
20	source ./include.tcl
21
22	set tnum test$reopen
23	set args [convert_args $method $args]
24	set omethod [convert_method $method]
25
26	if { [is_record_based $method] == 1 } {
27		puts "Test$reopen skipping for method $method"
28		return
29	}
30
31	puts -nonewline "$tnum: $method filename=key filecontents=data pairs"
32	if {$reopen == "009"} {
33		puts "(with close)"
34	} else {
35		puts ""
36	}
37
38	# Create the database and open the dictionary
39	set txnenv 0
40	set eindex [lsearch -exact $args "-env"]
41	#
42	# If we are using an env, then testfile should just be the db name.
43	# Otherwise it is the test directory and the name.
44	if { $eindex == -1 } {
45		set testfile $testdir/$tnum.db
46		set env NULL
47	} else {
48		set testfile $tnum.db
49		incr eindex
50		set env [lindex $args $eindex]
51		set txnenv [is_txnenv $env]
52		if { $txnenv == 1 } {
53			append args " -auto_commit "
54		}
55		set testdir [get_home $env]
56	}
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
64	set db [eval {berkdb_open -create -mode 0644} \
65	    $args {$omethod $testfile}]
66	error_check_good dbopen [is_valid_db $db] TRUE
67
68	set pflags ""
69	set gflags ""
70	set txn ""
71
72	# Here is the loop where we put and get each key/data pair
73	set file_list [get_file_list]
74
75	set count 0
76	puts "\tTest$reopen.a: Initial put/get loop"
77	foreach f $file_list {
78		set names($count) $f
79		set key $f
80
81		if { $txnenv == 1 } {
82			set t [$env txn]
83			error_check_good txn [is_valid_txn $t $env] TRUE
84			set txn "-txn $t"
85		}
86		put_file $db $txn $pflags $f
87		if { $txnenv == 1 } {
88			error_check_good txn [$t commit] 0
89		}
90
91		if { $txnenv == 1 } {
92			set t [$env txn]
93			error_check_good txn [is_valid_txn $t $env] TRUE
94			set txn "-txn $t"
95		}
96		get_file $db $txn $gflags $f $t4
97		if { $txnenv == 1 } {
98			error_check_good txn [$t commit] 0
99		}
100
101		error_check_good Test$reopen:diff($f,$t4) \
102		    [filecmp $f $t4] 0
103
104		incr count
105	}
106
107	if {$reopen == "009"} {
108		error_check_good db_close [$db close] 0
109
110		set db [eval {berkdb_open} $args $testfile]
111		error_check_good dbopen [is_valid_db $db] TRUE
112	}
113
114	# Now we will get step through keys again (by increments) and
115	# delete all the entries, then re-insert them.
116
117	puts "\tTest$reopen.b: Delete re-add loop"
118	foreach i "1 2 4 8 16" {
119		for {set ndx 0} {$ndx < $count} { incr ndx $i} {
120			if { $txnenv == 1 } {
121				set t [$env txn]
122				error_check_good txn [is_valid_txn $t $env] TRUE
123				set txn "-txn $t"
124			}
125			set r [eval {$db del} $txn {$names($ndx)}]
126			error_check_good db_del:$names($ndx) $r 0
127			if { $txnenv == 1 } {
128				error_check_good txn [$t commit] 0
129			}
130		}
131		for {set ndx 0} {$ndx < $count} { incr ndx $i} {
132			if { $txnenv == 1 } {
133				set t [$env txn]
134				error_check_good txn [is_valid_txn $t $env] TRUE
135				set txn "-txn $t"
136			}
137			put_file $db $txn $pflags $names($ndx)
138			if { $txnenv == 1 } {
139				error_check_good txn [$t commit] 0
140			}
141		}
142	}
143
144	if {$reopen == "009"} {
145		error_check_good db_close [$db close] 0
146		set db [eval {berkdb_open} $args $testfile]
147		error_check_good dbopen [is_valid_db $db] TRUE
148	}
149
150	# Now, reopen the file and make sure the key/data pairs look right.
151	puts "\tTest$reopen.c: Dump contents forward"
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	dump_bin_file $db $txn $t1 test008.check
158	if { $txnenv == 1 } {
159		error_check_good txn [$t commit] 0
160	}
161
162	set oid [open $t2.tmp w]
163	foreach f $file_list {
164		puts $oid $f
165	}
166	close $oid
167	filesort $t2.tmp $t2
168	fileremove $t2.tmp
169	filesort $t1 $t3
170
171	error_check_good Test$reopen:diff($t3,$t2) \
172	    [filecmp $t3 $t2] 0
173
174	# Now, reopen the file and run the last test again in reverse direction.
175	puts "\tTest$reopen.d: Dump contents backward"
176	if { $txnenv == 1 } {
177		set t [$env txn]
178		error_check_good txn [is_valid_txn $t $env] TRUE
179		set txn "-txn $t"
180	}
181	dump_bin_file_direction $db $txn $t1 test008.check "-last" "-prev"
182	if { $txnenv == 1 } {
183		error_check_good txn [$t commit] 0
184	}
185
186	filesort $t1 $t3
187
188	error_check_good Test$reopen:diff($t3,$t2) \
189	    [filecmp $t3 $t2] 0
190	error_check_good close:$db [$db close] 0
191}
192
193proc test008.check { binfile tmpfile } {
194	global tnum
195	source ./include.tcl
196
197	error_check_good diff($binfile,$tmpfile) \
198	    [filecmp $binfile $tmpfile] 0
199}
200