summaryrefslogtreecommitdiff
path: root/test/tcl/test135.tcl
blob: c9a1769a229874a1474f3ef546e3e84314c4cdce (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
# See the file LICENSE for redistribution information.
#
# Copyright (c) 2011, 2012 Oracle and/or its affiliates.  All rights reserved.
#
# $Id$
#
# TEST	test135
# TEST  Test operations on similar overflow records. [#20329]
# TEST  Open 3 kinds of databases: one is not dup, and one is dup-unsorted
# TEST    while another one is dup-sorted.
# TEST  Then we put some similar but not identical overflow records into 
# TEST    these databases. 
# TEST  Also, we try to remove some records from these databases.
# TEST  We'll verify the records after put and after deletion.
# TEST  Here is how we define the 'similar overflow' in this test:
# TEST    * Both the key.size and data.size are not small than DB's pagesize:
# TEST      The key.size is around 2*pagesize and data.size equals to pagesize.
# TEST    * The keys are of same length, and they only differ in a small piece.
# TEST      The location of difference could be in the start/middle/end.
# TEST    * For dup databases, the dup datas have same rule as the key rule
# TEST      mentioned above.

proc test135 {method {keycnt 10} {datacnt 10} {subdb 0} {tnum "135"} args} {
	source ./include.tcl

	# We are only testing key-based methods.
	if {[is_record_based $method]} {
		puts "Skipping test$tnum for $method test."
		return
	}

	set sub_msg ""
	# Check if we use sub database.
	if { $subdb } {
		if {[is_partitioned $args]} {
			puts "Skipping test$tnum with sub database\
		       	    for partitioned $method test."
			    return
		}
		set sub_msg "using sub databases"
	}

	# Skip for specified pagesizes. This test uses a very small
	# pagesize to make it run fast.
	set pgindex [lsearch -exact $args "-pagesize"]
	if { $pgindex != -1 } {
		puts "Test$tnum: Skipping for specific pagesizes"
		return
	}

	set pgsize 512

	# 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 $args "-env"]
	set txnenv 0
	if { $eindex == -1 } {
		if {$subdb} {
			puts "Skipping test$tnum $sub_msg for non-env test."
			return
		}
		set basename $testdir/test$tnum
		set env NULL
	} else {
		set basename test$tnum
		incr eindex
		set env [lindex $args $eindex]
		set txnenv [is_txnenv $env]
		if { $txnenv == 1 } {
			append args " -auto_commit "
		}
		set testdir [get_home $env]
	}

	cleanup $testdir $env
	set args [convert_args $method $args]

	puts "Test$tnum: $method ($args)\
	    Similar Overflow Records Operation Test $sub_msg."

	set i 0
	set dupargs [list "" "-dup" "-dup -dupsort"]
	if {[is_rbtree $method]} {
		set dupargs [list ""]
	} elseif {[is_compressed $args]} {
		set dupargs [list "" "-dup -dupsort"]
	}
	foreach duparg $dupargs {
		# kloc specifies where the keys differ
		foreach kloc [list "start" "middle" "end"] {
			# dloc specifies where the dup datas differ
			foreach dloc [list "start" "middle" "end"] {
				test135_sub \
				    "\tTest$tnum.$i ($duparg $kloc $dloc)" \
				    $duparg $kloc $dloc $basename $subdb \
				    $method $i $args
				incr i
			}
		}
	}
}

proc test135_sub { prefix duparg kloc dloc basename use_subdb 
    method indx dboargs } {
	global alphabet
	upvar txnenv txnenv
	upvar env env
	upvar keycnt keycnt
	upvar datacnt datacnt
	upvar pgsize pgsize

	if { $use_subdb } {
		set testfile $basename.db
		set subname "db$indx"
	} else {
		set testfile $basename-$indx.db
		set subname ""
	}

	set isdup [is_substr $duparg "-dup"]

	puts "$prefix.a: Open the database."
	set omethod [convert_method $method]
	set db [eval {berkdb_open_noerr -create -mode 0644} \
	    -pagesize $pgsize $dboargs $duparg $omethod $testfile $subname]
	error_check_good dbopen [is_valid_db $db] TRUE

	set txn ""
	if { $txnenv == 1 } {
		set t [$env txn]
		error_check_good txn [is_valid_txn $t $env] TRUE
		set txn "-txn $t"
	}

	set app_data(0) 0
	set alpha_len [string length $alphabet]
	set kbits [test135_countbits $alpha_len $keycnt]
	set dbits [test135_countbits $alpha_len $datacnt]

	# The key and data are composed of characters from 'a' to 'z' and they
	# have the format of: 
	#     $prefix$str$suffix
	# Both prefix and suffix are strings with all characters set to 'a'.
	# Here is an example to compose the str:
	# Assume we have 100 keys, and as 100 > 26('a'-'z', 26 chars),
	# we need at least two characters to represent 100 different strs.
	# These 100 strs should be: aa, ab, ..., az, ...., bz, ...., dv
	puts "$prefix.b: Putting records into the database."
	for {set klen [expr 2 * $pgsize - 50]} \
	    {$klen < [expr 2 * $pgsize + 50]} {incr klen 10} {
		set kpos [test135_getdiffpos $klen $kloc $kbits 10]
		set dpos [test135_getdiffpos $pgsize $dloc $dbits $alpha_len]
		set kprefix [repeat "a" $kpos]
		set ksuffix [repeat "a" \
		    [expr $klen - $kbits - $kpos]]
		set dprefix [repeat "a" $dpos]
		set dsuffix [repeat "a" \
		    [expr $pgsize - $dbits - $dpos]]
		for {set i 0} {$i < $keycnt} {incr i} {
			set k [test135_tostr $alphabet $alpha_len $kbits $i]
			if {$isdup} {
				for {set j 0} {$j < $datacnt} {incr j} {
					set d [test135_tostr $alphabet \
					    $alpha_len $dbits $j]
					set ret [eval $db put $txn \
					    $kprefix$k$ksuffix \
					    $dprefix$d$dsuffix]
					error_check_good db_put $ret 0
				}
			} else {
				set d [test135_tostr $alphabet \
				     $alpha_len $dbits 1]
				set ret [eval $db put $txn $kprefix$k$ksuffix \
				    $dprefix$d$dsuffix]
				error_check_good db_put $ret 0
			}
		}
		set app_data($klen) [list $kprefix $ksuffix \
		    $dprefix $dsuffix]
	}

	puts "$prefix.c: Verify records after putting."
	for {set klen [expr 2 * $pgsize - 50]} \
	    {$klen < [expr 2 * $pgsize + 50]} {incr klen 10} {
		set applist $app_data($klen)
		set kprefix [lindex $applist 0]
		set ksuffix [lindex $applist 1]
		set dprefix [lindex $applist 2]
		set dsuffix [lindex $applist 3]
		for {set i 0} {$i < $keycnt} {incr i} {
			set k [test135_tostr $alphabet $alpha_len $kbits $i]
			if {$isdup} {
				for {set j 0} {$j < $datacnt} {incr j} {
					set d [test135_tostr $alphabet \
					    $alpha_len $dbits $j]
					set ret [eval $db get -get_both $txn \
					    $kprefix$k$ksuffix \
					    $dprefix$d$dsuffix]
					error_check_good db_get [llength $ret] 1
				}
			} else {
				set d [test135_tostr $alphabet \
				     $alpha_len $dbits 1]
				set ret [eval $db get -get_both $txn \
				    $kprefix$k$ksuffix $dprefix$d$dsuffix]
				error_check_good db_get [llength $ret] 1
			}
		}
	}

	puts "$prefix.d: Delete some records from the database."
	for {set klen [expr 2 * $pgsize - 50]} \
	    {$klen < [expr 2 * $pgsize + 50]} {incr klen 10} {
		set applist $app_data($klen)
		set kprefix [lindex $applist 0]
		set ksuffix [lindex $applist 1]
		for {set i 0} {$i < $keycnt} {incr i 2} {
			set k [test135_tostr $alphabet $alpha_len $kbits $i]
			set ret [eval $db del $txn $kprefix$k$ksuffix]
			error_check_good db_del $ret 0
		}
	}

	puts "$prefix.e: Verify records after deleting."
	for {set klen [expr 2 * $pgsize - 50]} \
	    {$klen < [expr 2 * $pgsize + 50]} {incr klen 10} {
		set applist $app_data($klen)
		set kprefix [lindex $applist 0]
		set ksuffix [lindex $applist 1]
		set dprefix [lindex $applist 2]
		set dsuffix [lindex $applist 3]
		for {set i 0} {$i < $keycnt} {incr i 2} {
			set k [test135_tostr $alphabet $alpha_len $kbits $i]
			set ret [eval $db get $txn $kprefix$k$ksuffix]
			error_check_good db_get [llength $ret] 0
		}
		for {set i 1} {$i < $keycnt} {incr i 2} {
			set k [test135_tostr $alphabet $alpha_len $kbits $i]
			if {$isdup} {
				for {set j 0} {$j < $datacnt} {incr j} {
					set d [test135_tostr $alphabet \
					    $alpha_len $dbits $j]
					set ret [eval $db get -get_both $txn \
					    $kprefix$k$ksuffix \
					    $dprefix$d$dsuffix]
					error_check_good db_get [llength $ret] 1
				}
			} else {
				set d [test135_tostr $alphabet \
				     $alpha_len $dbits 1]
				set ret [eval $db get -get_both $txn \
				    $kprefix$k$ksuffix $dprefix$d$dsuffix]
				error_check_good db_get [llength $ret] 1
			}
		}
	}

	if { $txnenv == 1 } {
		error_check_good txn_commit [$t commit] 0
	}
	error_check_good db_close [$db close] 0
}

# Determines how many bits we need to represent a scope of values from 0.
# For example, if we want to represent values from 0-99, we need
# at least two decimal bits.
proc test135_countbits {radix maxval} {
	set cnt 1
	while {$maxval > 0} {
		set maxval [expr $maxval / $radix]
		if {!$maxval} { 
			break
		} else {
			incr cnt
		}
	}
	return $cnt
}

# Return the string representation of a value.
# For example, if we want to represent 21 in 3 decimal bits,
# the returned value should be "021"
proc test135_tostr {src radix cnt val} {
	set str ""
	for {set i 0} {$i < $cnt} {incr i} {
		set indx [expr $val % $radix]
		set val [expr $val / $radix]
		set str [string index $src $indx]$str
	}
	return $str
}

# Return the location where the difference begins.
proc test135_getdiffpos {sz loc bits mbits} {
	set lower 0
	set upper [expr $sz - $bits]
	switch -exact $loc {
		"start" {
			set upper [expr $mbits - $bits]
		}
		"middle" {
			set upper [expr $sz / 2 - $mbits / 2 - $bits]
			set lower [expr $upper - $mbits + 1]
		}
		"end" {
			set lower [expr $sz - $mbits]
		}
	}
	return [berkdb random_int $lower $upper]
}