summaryrefslogtreecommitdiff
path: root/bdb/test/test093.tcl
blob: e3f8f0103c6a717b8b431c28a7a4c3ff4dfac9df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1996-2002
#	Sleepycat Software.  All rights reserved.
#
# $Id: test093.tcl,v 11.20 2002/06/20 19:01:02 sue Exp $
#
# TEST	test093
# TEST	Test using set_bt_compare.
# TEST
# TEST	Use the first 10,000 entries from the dictionary.
# TEST	Insert each with self as key and data; retrieve each.
# TEST	After all are entered, retrieve all; compare output to original.
# TEST	Close file, reopen, do retrieve and re-verify.
proc test093 { method {nentries 10000} {tnum "93"} args} {
	source ./include.tcl
	global btvals
	global btvalsck
	global errorInfo

	set dbargs [convert_args $method $args]
	set omethod [convert_method $method]

	if { [is_btree $method] != 1 } {
		puts "Test0$tnum: skipping for method $method."
		return
	}
	set txnenv 0
	set eindex [lsearch -exact $dbargs "-env"]
	if { $eindex != -1 } {
		set testfile test0$tnum.db
		incr eindex
		set env [lindex $dbargs $eindex]
		set rpcenv [is_rpcenv $env]
		if { $rpcenv == 1 } {
			puts "Test0$tnum: skipping for RPC"
			return
		}
		set txnenv [is_txnenv $env]
		if { $txnenv == 1 } {
			append dbargs " -auto_commit "
			if { $nentries == 10000 } {
				set nentries 100
			}
		}
		set testdir [get_home $env]
		cleanup $testdir $env
	}
	puts "Test0$tnum: $method ($args) $nentries using btcompare"


	test093_run $omethod $dbargs $nentries $tnum test093_cmp1 test093_sort1
	test093_runbig $omethod $dbargs $nentries $tnum \
	    test093_cmp1 test093_sort1
	test093_run $omethod $dbargs $nentries $tnum test093_cmp2 test093_sort2
	#
	# Don't bother running the second, really slow, comparison
	# function on test093_runbig (file contents).

	# Clean up so verification doesn't fail.  (There's currently
	# no way to specify a comparison function to berkdb dbverify.)
	# If we are using an env, then testfile should just be the db name.
	# Otherwise it is the test directory and the name.
	set eindex [lsearch -exact $dbargs "-env"]
	if { $eindex == -1 } {
		set env NULL
	} else {
		incr eindex
		set env [lindex $dbargs $eindex]
		set testdir [get_home $env]
	}
	cleanup $testdir $env
}

proc test093_run { method dbargs nentries tnum cmpfunc sortfunc } {
	source ./include.tcl
	global btvals
	global btvalsck

	# Create the database and open the dictionary
	set eindex [lsearch -exact $dbargs "-env"]
	#
	# If we are using an env, then testfile should just be the db name.
	# Otherwise it is the test directory and the name.
	set txnenv 0
	if { $eindex == -1 } {
		set testfile $testdir/test0$tnum.db
		set env NULL
	} else {
		set testfile test0$tnum.db
		incr eindex
		set env [lindex $dbargs $eindex]
		set txnenv [is_txnenv $env]
		set testdir [get_home $env]
	}
	cleanup $testdir $env

	set db [eval {berkdb_open -btcompare $cmpfunc \
	     -create -mode 0644} $method $dbargs $testfile]
	error_check_good dbopen [is_valid_db $db] TRUE
	set did [open $dict]

	set t1 $testdir/t1
	set t2 $testdir/t2
	set t3 $testdir/t3
	set pflags ""
	set gflags ""
	set txn ""
	set btvals {}
	set btvalsck {}
	set checkfunc test093_check
	puts "\tTest0$tnum.a: put/get loop"
	# Here is the loop where we put and get each key/data pair
	set count 0
	while { [gets $did str] != -1 && $count < $nentries } {
		set key $str
		set str [reverse $str]
		if { $txnenv == 1 } {
			set t [$env txn]
			error_check_good txn [is_valid_txn $t $env] TRUE
			set txn "-txn $t"
		}
		set ret [eval \
		    {$db put} $txn $pflags {$key [chop_data $method $str]}]
		error_check_good put $ret 0
		if { $txnenv == 1 } {
			error_check_good txn [$t commit] 0
		}

		lappend btvals $key

		set ret [eval {$db get} $gflags {$key}]
		error_check_good \
		    get $ret [list [list $key [pad_data $method $str]]]

		incr count
	}
	close $did
	# Now we will get each key from the DB and compare the results
	# to the original.
	puts "\tTest0$tnum.b: dump file"
	if { $txnenv == 1 } {
		set t [$env txn]
		error_check_good txn [is_valid_txn $t $env] TRUE
		set txn "-txn $t"
	}
	dump_file $db $txn $t1 $checkfunc
	if { $txnenv == 1 } {
		error_check_good txn [$t commit] 0
	}
	error_check_good db_close [$db close] 0

	# Now compare the keys to see if they match the dictionary (or ints)
	set q q
	filehead $nentries $dict $t2
	filesort $t2 $t3
	file rename -force $t3 $t2
	filesort $t1 $t3

	error_check_good Test0$tnum:diff($t3,$t2) \
	    [filecmp $t3 $t2] 0

	puts "\tTest0$tnum.c: dump file in order"
	# Now, reopen the file and run the last test again.
	# We open it here, ourselves, because all uses of the db
	# need to have the correct comparison func set.  Then
	# call dump_file_direction directly.
	set btvalsck {}
	set db [eval {berkdb_open -btcompare $cmpfunc -rdonly} \
	     $dbargs $method $testfile]
	error_check_good dbopen [is_valid_db $db] TRUE
	if { $txnenv == 1 } {
		set t [$env txn]
		error_check_good txn [is_valid_txn $t $env] TRUE
		set txn "-txn $t"
	}
	dump_file_direction $db $txn $t1 $checkfunc "-first" "-next"
	if { $txnenv == 1 } {
		error_check_good txn [$t commit] 0
	}
	error_check_good db_close [$db close] 0

	#
	# We need to sort btvals according to the comparison function.
	# Once that is done, btvalsck and btvals should be the same.
	puts "\tTest0$tnum.d: check file order"

	$sortfunc

	error_check_good btvals:len [llength $btvals] [llength $btvalsck]
	for {set i 0} {$i < $nentries} {incr i} {
		error_check_good vals:$i [lindex $btvals $i] \
		    [lindex $btvalsck $i]
	}
}

proc test093_runbig { method dbargs nentries tnum cmpfunc sortfunc } {
	source ./include.tcl
	global btvals
	global btvalsck

	# Create the database and open the dictionary
	set eindex [lsearch -exact $dbargs "-env"]
	#
	# If we are using an env, then testfile should just be the db name.
	# Otherwise it is the test directory and the name.
	set txnenv 0
	if { $eindex == -1 } {
		set testfile $testdir/test0$tnum.db
		set env NULL
	} else {
		set testfile test0$tnum.db
		incr eindex
		set env [lindex $dbargs $eindex]
		set txnenv [is_txnenv $env]
		set testdir [get_home $env]
	}
	cleanup $testdir $env

	set db [eval {berkdb_open -btcompare $cmpfunc \
	     -create -mode 0644} $method $dbargs $testfile]
	error_check_good dbopen [is_valid_db $db] TRUE

	set t1 $testdir/t1
	set t2 $testdir/t2
	set t3 $testdir/t3
	set t4 $testdir/t4
	set t5 $testdir/t5
	set pflags ""
	set gflags ""
	set txn ""
	set btvals {}
	set btvalsck {}
	set checkfunc test093_checkbig
	puts "\tTest0$tnum.e:\
	    big key put/get loop key=filecontents data=filename"

	# Here is the loop where we put and get each key/data pair
	set file_list [get_file_list 1]

	set count 0
	foreach f $file_list {
		set fid [open $f r]
		fconfigure $fid -translation binary
		set key [read $fid]
		close $fid

		set key $f$key

		set fcopy [open $t5 w]
		fconfigure $fcopy -translation binary
		puts -nonewline $fcopy $key
		close $fcopy

		if { $txnenv == 1 } {
			set t [$env txn]
			error_check_good txn [is_valid_txn $t $env] TRUE
			set txn "-txn $t"
		}
		set ret [eval {$db put} $txn $pflags {$key \
		    [chop_data $method $f]}]
		error_check_good put_file $ret 0
		if { $txnenv == 1 } {
			error_check_good txn [$t commit] 0
		}

		lappend btvals $key

		# Should really catch errors
		set fid [open $t4 w]
		fconfigure $fid -translation binary
		if [catch {eval {$db get} $gflags {$key}} data] {
			puts -nonewline $fid $data
		} else {
			# Data looks like {{key data}}
			set key [lindex [lindex $data 0] 0]
			puts -nonewline $fid $key
		}
		close $fid
		error_check_good \
		    Test093:diff($t5,$t4) [filecmp $t5 $t4] 0

		incr count
	}

	# Now we will get each key from the DB and compare the results
	# to the original.
	puts "\tTest0$tnum.f: big dump file"
	if { $txnenv == 1 } {
		set t [$env txn]
		error_check_good txn [is_valid_txn $t $env] TRUE
		set txn "-txn $t"
	}
	dump_file $db $txn $t1 $checkfunc
	if { $txnenv == 1 } {
		error_check_good txn [$t commit] 0
	}
	error_check_good db_close [$db close] 0

	puts "\tTest0$tnum.g: dump file in order"
	# Now, reopen the file and run the last test again.
	# We open it here, ourselves, because all uses of the db
	# need to have the correct comparison func set.  Then
	# call dump_file_direction directly.

	set btvalsck {}
	set db [eval {berkdb_open -btcompare $cmpfunc -rdonly} \
	     $dbargs $method $testfile]
	error_check_good dbopen [is_valid_db $db] TRUE
	if { $txnenv == 1 } {
		set t [$env txn]
		error_check_good txn [is_valid_txn $t $env] TRUE
		set txn "-txn $t"
	}
	dump_file_direction $db $txn $t1 $checkfunc "-first" "-next"
	if { $txnenv == 1 } {
		error_check_good txn [$t commit] 0
	}
	error_check_good db_close [$db close] 0

	#
	# We need to sort btvals according to the comparison function.
	# Once that is done, btvalsck and btvals should be the same.
	puts "\tTest0$tnum.h: check file order"

	$sortfunc
	error_check_good btvals:len [llength $btvals] [llength $btvalsck]

	set end [llength $btvals]
	for {set i 0} {$i < $end} {incr i} {
		error_check_good vals:$i [lindex $btvals $i] \
		    [lindex $btvalsck $i]
	}
}

# Simple bt comparison.
proc test093_cmp1 { a b } {
	return [string compare $b $a]
}

# Simple bt sorting.
proc test093_sort1 {} {
	global btvals
	#
	# This one is easy, just sort in reverse.
	#
	set btvals [lsort -decreasing $btvals]
}

proc test093_cmp2 { a b } {
	set arev [reverse $a]
	set brev [reverse $b]
	return [string compare $arev $brev]
}

proc test093_sort2 {} {
	global btvals

	# We have to reverse them, then sorts them.
	# Then reverse them back to real words.
	set rbtvals {}
	foreach i $btvals {
		lappend rbtvals [reverse $i]
	}
	set rbtvals [lsort -increasing $rbtvals]
	set newbtvals {}
	foreach i $rbtvals {
		lappend newbtvals [reverse $i]
	}
	set btvals $newbtvals
}

# Check function for test093; keys and data are identical
proc test093_check { key data } {
	global btvalsck

	error_check_good "key/data mismatch" $data [reverse $key]
	lappend btvalsck $key
}

# Check function for test093 big keys;
proc test093_checkbig { key data } {
	source ./include.tcl
	global btvalsck

	set fid [open $data r]
	fconfigure $fid -translation binary
	set cont [read $fid]
	close $fid
	error_check_good "key/data mismatch" $key $data$cont
	lappend btvalsck $key
}