summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/test050.tcl
blob: 74b509a8b05f9f75cfb256bdae6d05ca3f88029e (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
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1999-2009 Oracle.  All rights reserved.
#
# $Id$
#
# TEST	test050
# TEST	Overwrite test of small/big key/data with cursor checks for Recno.
proc test050 { method args } {
	global alphabet
	global errorInfo
	global errorCode
	source ./include.tcl

	set tstn 050

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

	if { [is_rrecno $method] != 1 } {
		puts "Test$tstn skipping for method $method."
		return
	}

	puts "\tTest$tstn:\
	    Overwrite test with cursor and small/big key/data ($method)."

	set data "data"
	set txn ""
	set flags ""

	puts "\tTest$tstn: Create $method database."
	set txnenv 0
	set eindex [lsearch -exact $args "-env"]
	#
	# If we are using an env, then testfile should just be the db name.
	# Otherwise it is the test directory and the name.
	if { $eindex == -1 } {
		set testfile $testdir/test0$tstn.db
		set env NULL
	} else {
		set testfile test0$tstn.db
		incr eindex
		set env [lindex $args $eindex]
		set txnenv [is_txnenv $env]
		if { $txnenv == 1 } {
			append args " -auto_commit "
		}
		set testdir [get_home $env]
	}
	set t1 $testdir/t1
	cleanup $testdir $env

	set oflags "-create -mode 0644 $args $omethod"
	set db [eval {berkdb_open_noerr} $oflags $testfile]
	error_check_good dbopen [is_valid_db $db] TRUE

	# keep nkeys even
	set nkeys 20

	# Fill page w/ small key/data pairs
	#
	puts "\tTest$tstn: Fill page with $nkeys small key/data pairs."
	for { set i 1 } { $i <= $nkeys } { incr i } {
		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 {$i [chop_data $method $data$i]}]
		error_check_good dbput $ret 0
		if { $txnenv == 1 } {
			error_check_good txn [$t commit] 0
		}
	}

	if { $txnenv == 1 } {
		set t [$env txn]
		error_check_good txn [is_valid_txn $t $env] TRUE
		set txn "-txn $t"
	}
	# open curs to db
	set dbc [eval {$db cursor} $txn]
	error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE

	# get db order of keys
	for {set i 0; set ret [$dbc get -first]} { [llength $ret] != 0} { \
			set ret [$dbc get -next]} {
		set key_set($i) [lindex [lindex $ret 0] 0]
		set data_set($i) [lindex [lindex $ret 0] 1]
		incr i
	}

	# verify ordering: should be unnecessary, but hey, why take chances?
	#	key_set is zero indexed but keys start at 1
	for {set i 0} { $i < $nkeys } {incr i} {
		error_check_good \
		    verify_order:$i $key_set($i) [pad_data $method [expr $i+1]]
	}

	puts "\tTest$tstn.a: Inserts before/after by cursor."
	puts "\t\tTest$tstn.a.1:\
	    Insert with uninitialized cursor (should fail)."
	error_check_good dbc_close [$dbc close] 0
	if { $txnenv == 1 } {
		error_check_good txn [$t commit] 0
	}
	if { $txnenv == 1 } {
		set t [$env txn]
		error_check_good txn [is_valid_txn $t $env] TRUE
		set txn "-txn $t"
	}
	set dbc [eval {$db cursor} $txn]
	error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE
	catch {$dbc put -before DATA1} ret
	error_check_good dbc_put:before:uninit [is_substr $errorCode EINVAL] 1

	catch {$dbc put -after DATA2} ret
	error_check_good dbc_put:after:uninit [is_substr $errorCode EINVAL] 1

	puts "\t\tTest$tstn.a.2: Insert with deleted cursor (should succeed)."
	set ret [$dbc get -first]
	error_check_bad dbc_get:first [llength $ret] 0
	error_check_good dbc_del [$dbc del] 0
	set ret [$dbc put -current DATAOVER1]
	error_check_good dbc_put:current:deleted $ret 0

	puts "\t\tTest$tstn.a.3: Insert by cursor before cursor (DB_BEFORE)."
	set currecno [lindex [lindex [$dbc get -current] 0] 0]
	set ret [$dbc put -before DATAPUTBEFORE]
	error_check_good dbc_put:before $ret $currecno
	set old1 [$dbc get -next]
	error_check_bad dbc_get:next [llength $old1] 0
	error_check_good \
	    dbc_get:next(compare) [lindex [lindex $old1 0] 1] DATAOVER1

	puts "\t\tTest$tstn.a.4: Insert by cursor after cursor (DB_AFTER)."
	set ret [$dbc get -first]
	error_check_bad dbc_get:first [llength $ret] 0
	error_check_good dbc_get:first [lindex [lindex $ret 0] 1] DATAPUTBEFORE
	set currecno [lindex [lindex [$dbc get -current] 0] 0]
	set ret [$dbc put -after DATAPUTAFTER]
	error_check_good dbc_put:after $ret [expr $currecno + 1]
	set ret [$dbc get -prev]
	error_check_bad dbc_get:prev [llength $ret] 0
	error_check_good \
	    dbc_get:prev [lindex [lindex $ret 0] 1] DATAPUTBEFORE

	puts "\t\tTest$tstn.a.5: Verify that all keys have been renumbered."
	# should be $nkeys + 2 keys, starting at 1
	for {set i 1; set ret [$dbc get -first]} { \
			$i <= $nkeys && [llength $ret] != 0 } {\
			incr i; set ret [$dbc get -next]} {
		error_check_good check_renumber $i [lindex [lindex $ret 0] 0]
	}

	# tested above

	puts "\tTest$tstn.b: Overwrite tests (cursor and key)."
	# For the next part of the test, we need a db with no dups to test
	# overwrites
	#
	# we should have ($nkeys + 2) keys, ordered:
	#	DATAPUTBEFORE, DATAPUTAFTER, DATAOVER1, data1, ..., data$nkeys
	#
	# Prepare cursor on item
	#
	set ret [$dbc get -first]
	error_check_bad dbc_get:first [llength $ret] 0

	# Prepare unique big/small values for an initial
	# and an overwrite set of data
	set databig DATA_BIG_[repeat alphabet 250]
	set datasmall DATA_SMALL

	# Now, we want to overwrite data:
	#  by key and by cursor
	#  1. small by small
	#	2. small by big
	#	3. big by small
	#	4. big by big
	#
	set i 0
	# Do all overwrites for key and cursor
	foreach type { by_key by_cursor } {
		incr i
		puts "\tTest$tstn.b.$i: Overwrites $type."
		foreach pair { {small small} \
		    {small big} {big small} {big big} } {
			# put in initial type
			set data $data[lindex $pair 0]
			set ret [$dbc put -current $data]
			error_check_good dbc_put:curr:init:($pair) $ret 0

			# Now, try to overwrite: dups not supported in this db
			if { [string compare $type by_key] == 0 } {
				puts "\t\tTest$tstn.b.$i:\
				    Overwrite:($pair):$type"
				set ret [eval {$db put} $txn \
				    1 {OVER$pair$data[lindex $pair 1]}]
				error_check_good dbput:over:($pair) $ret 0
			} else {
				# This is a cursor overwrite
				puts "\t\tTest$tstn.b.$i:\
				    Overwrite:($pair) by cursor."
				set ret [$dbc put \
				    -current OVER$pair$data[lindex $pair 1]]
				error_check_good dbcput:over:($pair) $ret 0
			}
		} ;# foreach pair
	} ;# foreach type key/cursor

	puts "\tTest$tstn.c: Cleanup and close cursor."
	error_check_good dbc_close [$dbc close] 0
	if { $txnenv == 1 } {
		error_check_good txn [$t commit] 0
	}
	error_check_good db_close [$db close] 0

}