1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996,2008 Oracle.  All rights reserved.
4#
5# $Id: test002.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test002
8# TEST	Small keys/medium data
9# TEST		Put/get per key
10# TEST		Dump file
11# TEST		Close, reopen
12# TEST		Dump file
13# TEST
14# TEST	Use the first 10,000 entries from the dictionary.
15# TEST	Insert each with self as key and a fixed, medium length data string;
16# TEST	retrieve each. After all are entered, retrieve all; compare output
17# TEST	to original. Close file, reopen, do retrieve and re-verify.
18
19proc test002 { method {nentries 10000} args } {
20	global datastr
21	global pad_datastr
22	source ./include.tcl
23
24	set args [convert_args $method $args]
25	set omethod [convert_method $method]
26
27	set txnenv 0
28	set eindex [lsearch -exact $args "-env"]
29	#
30	# If we are using an env, then testfile should just be the db name.
31	# Otherwise it is the test directory and the name.
32	if { $eindex == -1 } {
33		set testfile $testdir/test002.db
34		set env NULL
35	} else {
36		set testfile test002.db
37		incr eindex
38		set env [lindex $args $eindex]
39		set txnenv [is_txnenv $env]
40		if { $txnenv == 1 } {
41			append args " -auto_commit "
42			#
43			# If we are using txns and running with the
44			# default, set the default down a bit.
45			#
46			if { $nentries == 10000 } {
47				set nentries 100
48			}
49		}
50		set testdir [get_home $env]
51	}
52	# Create the database and open the dictionary
53	puts "Test002: $method ($args) $nentries key <fixed data> pairs"
54
55	set t1 $testdir/t1
56	set t2 $testdir/t2
57	set t3 $testdir/t3
58	cleanup $testdir $env
59	set db [eval {berkdb_open \
60	     -create -mode 0644} $args {$omethod $testfile}]
61	error_check_good dbopen [is_valid_db $db] TRUE
62	set did [open $dict]
63
64	set pflags ""
65	set gflags ""
66	set txn ""
67	set count 0
68
69	# Here is the loop where we put and get each key/data pair
70
71	if { [is_record_based $method] == 1 } {
72		append gflags "-recno"
73	}
74	set pad_datastr [pad_data $method $datastr]
75	puts "\tTest002.a: put/get loop"
76	while { [gets $did str] != -1 && $count < $nentries } {
77		if { [is_record_based $method] == 1 } {
78			set key [expr $count + 1]
79		} else {
80			set key $str
81		}
82		if { $txnenv == 1 } {
83			set t [$env txn]
84			error_check_good txn [is_valid_txn $t $env] TRUE
85			set txn "-txn $t"
86		}
87		set ret [eval {$db put} $txn $pflags {$key [chop_data $method $datastr]}]
88		error_check_good put $ret 0
89		if { $txnenv == 1 } {
90			error_check_good txn [$t commit] 0
91		}
92
93		set ret [eval {$db get} $gflags {$key}]
94
95		error_check_good get $ret [list [list $key [pad_data $method $datastr]]]
96		incr count
97	}
98	close $did
99
100	# Now we will get each key from the DB and compare the results
101	# to the original.
102	puts "\tTest002.b: dump file"
103	if { $txnenv == 1 } {
104		set t [$env txn]
105		error_check_good txn [is_valid_txn $t $env] TRUE
106		set txn "-txn $t"
107	}
108	dump_file $db $txn $t1 test002.check
109	if { $txnenv == 1 } {
110		error_check_good txn [$t commit] 0
111	}
112	error_check_good db_close [$db close] 0
113
114	# Now compare the keys to see if they match the dictionary
115	if { [is_record_based $method] == 1 } {
116		set oid [open $t2 w]
117		for {set i 1} {$i <= $nentries} {set i [incr i]} {
118			puts $oid $i
119		}
120		close $oid
121		filesort $t2 $t3
122		file rename -force $t3 $t2
123	} else {
124		set q q
125		filehead $nentries $dict $t3
126		filesort $t3 $t2
127	}
128	filesort $t1 $t3
129
130	error_check_good Test002:diff($t3,$t2) \
131	    [filecmp $t3 $t2] 0
132
133	# Now, reopen the file and run the last test again.
134	puts "\tTest002.c: close, open, and dump file"
135	open_and_dump_file $testfile $env $t1 test002.check \
136	    dump_file_direction "-first" "-next"
137
138	if { [string compare $omethod "-recno"] != 0 } {
139		filesort $t1 $t3
140	}
141	error_check_good Test002:diff($t3,$t2) \
142	    [filecmp $t3 $t2] 0
143
144	# Now, reopen the file and run the last test again in reverse direction.
145	puts "\tTest002.d: close, open, and dump file in reverse direction"
146	open_and_dump_file $testfile $env $t1 test002.check \
147	    dump_file_direction "-last" "-prev"
148
149	if { [string compare $omethod "-recno"] != 0 } {
150		filesort $t1 $t3
151	}
152	error_check_good Test002:diff($t3,$t2) \
153	    [filecmp $t3 $t2] 0
154}
155
156# Check function for test002; data should be fixed are identical
157proc test002.check { key data } {
158	global pad_datastr
159	error_check_good "data mismatch for key $key" $data $pad_datastr
160}
161