1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: test062.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test062
8# TEST	Test of partial puts (using DB_CURRENT) onto duplicate pages.
9# TEST	Insert the first 200 words into the dictionary 200 times each with
10# TEST	self as key and <random letter>:self as data.  Use partial puts to
11# TEST	append self again to data;  verify correctness.
12proc test062 { method {nentries 200} {ndups 200} {tnum "062"} args } {
13	global alphabet
14	global rand_init
15	source ./include.tcl
16
17	berkdb srand $rand_init
18
19	set args [convert_args $method $args]
20	set omethod [convert_method $method]
21
22	if { [is_record_based $method] == 1 || [is_rbtree $method] == 1 } {
23		puts "Test$tnum skipping for method $omethod"
24		return
25	}
26	# Create the database and open the dictionary
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/test$tnum.db
34		set env NULL
35	} else {
36		set testfile test$tnum.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 == 200 } {
47				set nentries 100
48			}
49			reduce_dups nentries ndups
50		}
51		set testdir [get_home $env]
52	}
53	cleanup $testdir $env
54
55	puts "Test$tnum:\
56	    $method ($args) $nentries Partial puts and $ndups duplicates."
57	set db [eval {berkdb_open -create -mode 0644 \
58	    $omethod -dup} $args {$testfile} ]
59	error_check_good dbopen [is_valid_db $db] TRUE
60	set did [open $dict]
61
62	set pflags ""
63	set gflags ""
64	set txn ""
65	set count 0
66
67	# Here is the loop where we put each key/data pair
68	puts "\tTest$tnum.a: Put loop (initialize database)"
69	while { [gets $did str] != -1 && $count < $nentries } {
70		for { set i 1 } { $i <= $ndups } { incr i } {
71			set pref \
72			    [string index $alphabet [berkdb random_int 0 25]]
73			set datastr $pref:$str
74			if { $txnenv == 1 } {
75				set t [$env txn]
76				error_check_good txn [is_valid_txn $t $env] TRUE
77				set txn "-txn $t"
78			}
79			set ret [eval {$db put} \
80			    $txn $pflags {$str [chop_data $method $datastr]}]
81			error_check_good put $ret 0
82			if { $txnenv == 1 } {
83				error_check_good txn [$t commit] 0
84			}
85		}
86		set keys($count) $str
87
88		incr count
89	}
90	close $did
91
92	puts "\tTest$tnum.b: Partial puts."
93	if { $txnenv == 1 } {
94		set t [$env txn]
95		error_check_good txn [is_valid_txn $t $env] TRUE
96		set txn "-txn $t"
97	}
98	set dbc [eval {$db cursor} $txn]
99	error_check_good cursor_open [is_substr $dbc $db] 1
100
101	# Do a partial write to extend each datum in
102	# the regular db by the corresponding dictionary word.
103	# We have to go through each key's dup set using -set
104	# because cursors are not stable in the hash AM and we
105	# want to make sure we hit all the keys.
106	for { set i 0 } { $i < $count } { incr i } {
107		set key $keys($i)
108		for {set ret [$dbc get -set $key]}  \
109		    {[llength $ret] != 0} \
110		    {set ret [$dbc get -nextdup]} {
111
112			set k [lindex [lindex $ret 0] 0]
113			set orig_d [lindex [lindex $ret 0] 1]
114			set d [string range $orig_d 2 end]
115			set doff [expr [string length $d] + 2]
116			set dlen 0
117			error_check_good data_and_key_sanity $d $k
118
119			set ret [$dbc get -current]
120			error_check_good before_sanity \
121			    [lindex [lindex $ret 0] 0] \
122			    [string range [lindex [lindex $ret 0] 1] 2 end]
123
124			error_check_good partial_put [eval {$dbc put -current \
125			    -partial [list $doff $dlen] $d}] 0
126
127			set ret [$dbc get -current]
128			error_check_good partial_put_correct \
129			    [lindex [lindex $ret 0] 1] $orig_d$d
130		}
131	}
132
133	puts "\tTest$tnum.c: Double-checking get loop."
134	# Double-check that each datum in the regular db has
135	# been appropriately modified.
136
137	for {set ret [$dbc get -first]} \
138	    {[llength $ret] != 0} \
139	    {set ret [$dbc get -next]} {
140
141		set k [lindex [lindex $ret 0] 0]
142		set d [lindex [lindex $ret 0] 1]
143		error_check_good modification_correct \
144		    [string range $d 2 end] [repeat $k 2]
145	}
146
147	error_check_good dbc_close [$dbc close] 0
148	if { $txnenv == 1 } {
149		error_check_good txn [$t commit] 0
150	}
151	error_check_good db_close [$db close] 0
152}
153