1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	test029
8# TEST	Test the Btree and Record number renumbering.
9proc test029 { method {nentries 10000} args} {
10	source ./include.tcl
11
12	set do_renumber [is_rrecno $method]
13	set args [convert_args $method $args]
14	set omethod [convert_method $method]
15
16	puts "Test029: $method ($args)"
17
18	if { [string compare $omethod "-hash"] == 0 } {
19		puts "Test029 skipping for method HASH"
20		return
21	}
22	# Btree with compression does not support -recnum.
23	if { [is_compressed $args] == 1 } {
24		puts "Test029 skipping for compressed btree with -recnum."
25		return
26	}
27	if { [is_partitioned $args] } {
28		puts "Test029 skipping for partitioned $omethod"
29		return
30	}
31	if { [is_record_based $method] == 1 && $do_renumber != 1 } {
32		puts "Test029 skipping for method RECNO (w/out renumbering)"
33		return
34	}
35
36	# Create the database and open the dictionary
37	set txnenv 0
38	set eindex [lsearch -exact $args "-env"]
39	#
40	# If we are using an env, then testfile should just be the db name.
41	# Otherwise it is the test directory and the name.
42	if { $eindex == -1 } {
43		set testfile $testdir/test029.db
44		set env NULL
45	} else {
46		set testfile test029.db
47		incr eindex
48		set env [lindex $args $eindex]
49		set txnenv [is_txnenv $env]
50		if { $txnenv == 1 } {
51			append args " -auto_commit "
52			#
53			# If we are using txns and running with the
54			# default, set the default down a bit.
55			#
56			if { $nentries == 10000 } {
57				# Do not set nentries down to 100 until we
58				# fix SR #5958.
59				set nentries 1000
60			}
61		}
62		set testdir [get_home $env]
63	}
64	cleanup $testdir $env
65
66	# Read the first nentries dictionary elements and reverse them.
67	# Keep a list of these (these will be the keys).
68	puts "\tTest029.a: initialization"
69	set keys ""
70	set did [open $dict]
71	set count 0
72	while { [gets $did str] != -1 && $count < $nentries } {
73		lappend keys [reverse $str]
74		incr count
75	}
76	close $did
77
78	# Generate sorted order for the keys
79	set sorted_keys [lsort $keys]
80
81	# Save the first and last keys
82	set last_key [lindex $sorted_keys end]
83	set last_keynum [llength $sorted_keys]
84
85	set first_key [lindex $sorted_keys 0]
86	set first_keynum 1
87
88	# Create the database
89	if { [string compare $omethod "-btree"] == 0 } {
90		set db [eval {berkdb_open -create \
91			-mode 0644 -recnum} $args {$omethod $testfile}]
92	   error_check_good dbopen [is_valid_db $db] TRUE
93	} else {
94		set db [eval {berkdb_open -create \
95			-mode 0644} $args {$omethod $testfile}]
96	   error_check_good dbopen [is_valid_db $db] TRUE
97	}
98
99	set pflags ""
100	set gflags ""
101	set txn ""
102
103	if { [is_record_based $method] == 1 } {
104		append gflags " -recno"
105	}
106
107	puts "\tTest029.b: put/get loop"
108	foreach k $keys {
109		if { [is_record_based $method] == 1 } {
110			set key [lsearch $sorted_keys $k]
111			incr key
112		} else {
113			set key $k
114		}
115		if { $txnenv == 1 } {
116			set t [$env txn]
117			error_check_good txn [is_valid_txn $t $env] TRUE
118			set txn "-txn $t"
119		}
120		set ret [eval {$db put} \
121		    $txn $pflags {$key [chop_data $method $k]}]
122		error_check_good dbput $ret 0
123
124		set ret [eval {$db get} $txn $gflags {$key}]
125		error_check_good dbget [lindex [lindex $ret 0] 1] $k
126		if { $txnenv == 1 } {
127			error_check_good txn [$t commit] 0
128		}
129	}
130
131	# Now delete the first key in the database
132	puts "\tTest029.c: delete and verify renumber"
133
134	# Delete the first key in the file
135	if { [is_record_based $method] == 1 } {
136		set key $first_keynum
137	} else {
138		set key $first_key
139	}
140
141	if { $txnenv == 1 } {
142		set t [$env txn]
143		error_check_good txn [is_valid_txn $t $env] TRUE
144		set txn "-txn $t"
145	}
146	set ret [eval {$db del} $txn {$key}]
147	error_check_good db_del $ret 0
148	if { $txnenv == 1 } {
149		error_check_good txn [$t commit] 0
150	}
151
152	# Now we are ready to retrieve records based on
153	# record number
154	if { [string compare $omethod "-btree"] == 0 } {
155		append gflags " -recno"
156	}
157
158	# First try to get the old last key (shouldn't exist)
159	if { $txnenv == 1 } {
160		set t [$env txn]
161		error_check_good txn [is_valid_txn $t $env] TRUE
162		set txn "-txn $t"
163	}
164	set ret [eval {$db get} $txn $gflags {$last_keynum}]
165	error_check_good get_after_del $ret [list]
166	if { $txnenv == 1 } {
167		error_check_good txn [$t commit] 0
168	}
169
170	# Now try to get what we think should be the last key
171	if { $txnenv == 1 } {
172		set t [$env txn]
173		error_check_good txn [is_valid_txn $t $env] TRUE
174		set txn "-txn $t"
175	}
176	set ret [eval {$db get} $txn $gflags {[expr $last_keynum - 1]}]
177	error_check_good \
178	    getn_last_after_del [lindex [lindex $ret 0] 1] $last_key
179	if { $txnenv == 1 } {
180		error_check_good txn [$t commit] 0
181	}
182
183	# Create a cursor; we need it for the next test and we
184	# need it for recno here.
185	if { $txnenv == 1 } {
186		set t [$env txn]
187		error_check_good txn [is_valid_txn $t $env] TRUE
188		set txn "-txn $t"
189	}
190	set dbc [eval {$db cursor} $txn]
191	error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE
192
193	# OK, now re-put the first key and make sure that we
194	# renumber the last key appropriately.
195	if { [string compare $omethod "-btree"] == 0 } {
196		set ret [eval {$db put} $txn \
197		    {$key [chop_data $method $first_key]}]
198		error_check_good db_put $ret 0
199	} else {
200		# Recno
201		set ret [$dbc get -first]
202		set ret [eval {$dbc put} $pflags {-before $first_key}]
203		error_check_bad dbc_put:DB_BEFORE $ret 0
204	}
205
206	# Now check that the last record matches the last record number
207	set ret [eval {$db get} $txn $gflags {$last_keynum}]
208	error_check_good \
209	    getn_last_after_put [lindex [lindex $ret 0] 1] $last_key
210
211	# Now delete the first key in the database using a cursor
212	puts "\tTest029.d: delete with cursor and verify renumber"
213
214	set ret [$dbc get -first]
215	error_check_good dbc_first $ret [list [list $key $first_key]]
216
217	# Now delete at the cursor
218	set ret [$dbc del]
219	error_check_good dbc_del $ret 0
220
221	# Now check the record numbers of the last keys again.
222	# First try to get the old last key (shouldn't exist)
223	set ret [eval {$db get} $txn $gflags {$last_keynum}]
224	error_check_good get_last_after_cursor_del:$ret $ret [list]
225
226	# Now try to get what we think should be the last key
227	set ret [eval {$db get} $txn $gflags {[expr $last_keynum - 1]}]
228	error_check_good \
229	    getn_after_cursor_del [lindex [lindex $ret 0] 1] $last_key
230
231	# Re-put the first key and make sure that we renumber the last
232	# key appropriately.  We can't do a c_put -current, so do
233	# a db put instead.
234	if { [string compare $omethod "-btree"] == 0 } {
235		puts "\tTest029.e: put (non-cursor) and verify renumber"
236		set ret [eval {$db put} $txn \
237		    {$key [chop_data $method $first_key]}]
238		error_check_good db_put $ret 0
239	} else {
240		puts "\tTest029.e: put with cursor and verify renumber"
241		set ret [eval {$dbc put} $pflags {-before $first_key}]
242		error_check_bad dbc_put:DB_BEFORE $ret 0
243	}
244
245	# Now check that the last record matches the last record number
246	set ret [eval {$db get} $txn $gflags {$last_keynum}]
247	error_check_good \
248	    get_after_cursor_reput [lindex [lindex $ret 0] 1] $last_key
249
250	error_check_good dbc_close [$dbc close] 0
251	if { $txnenv == 1 } {
252		error_check_good txn [$t commit] 0
253	}
254	error_check_good db_close [$db close] 0
255}
256