1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999-2009 Oracle.  All rights reserved.
4#
5# $Id$
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
27	# Btree with compression does not support unsorted duplicates.
28	if { [is_compressed $args] == 1 } {
29		puts "Test$tnum skipping for btree with compression."
30		return
31	}
32
33	# Create the database and open the dictionary
34	set txnenv 0
35	set eindex [lsearch -exact $args "-env"]
36	#
37	# If we are using an env, then testfile should just be the db name.
38	# Otherwise it is the test directory and the name.
39	if { $eindex == -1 } {
40		set testfile $testdir/test$tnum.db
41		set env NULL
42	} else {
43		set testfile test$tnum.db
44		incr eindex
45		set env [lindex $args $eindex]
46		set txnenv [is_txnenv $env]
47		if { $txnenv == 1 } {
48			append args " -auto_commit "
49			#
50			# If we are using txns and running with the
51			# default, set the default down a bit.
52			#
53			if { $nentries == 200 } {
54				set nentries 100
55			}
56			reduce_dups nentries ndups
57		}
58		set testdir [get_home $env]
59	}
60	cleanup $testdir $env
61
62	puts "Test$tnum:\
63	    $method ($args) $nentries Partial puts and $ndups duplicates."
64	set db [eval {berkdb_open -create -mode 0644 \
65	    $omethod -dup} $args {$testfile} ]
66	error_check_good dbopen [is_valid_db $db] TRUE
67	set did [open $dict]
68
69	set pflags ""
70	set gflags ""
71	set txn ""
72	set count 0
73
74	# Here is the loop where we put each key/data pair
75	puts "\tTest$tnum.a: Put loop (initialize database)"
76	while { [gets $did str] != -1 && $count < $nentries } {
77		for { set i 1 } { $i <= $ndups } { incr i } {
78			set pref \
79			    [string index $alphabet [berkdb random_int 0 25]]
80			set datastr $pref:$str
81			if { $txnenv == 1 } {
82				set t [$env txn]
83				error_check_good txn [is_valid_txn $t $env] TRUE
84				set txn "-txn $t"
85			}
86			set ret [eval {$db put} \
87			    $txn $pflags {$str [chop_data $method $datastr]}]
88			error_check_good put $ret 0
89			if { $txnenv == 1 } {
90				error_check_good txn [$t commit] 0
91			}
92		}
93		set keys($count) $str
94
95		incr count
96	}
97	close $did
98
99	puts "\tTest$tnum.b: Partial puts."
100	if { $txnenv == 1 } {
101		set t [$env txn]
102		error_check_good txn [is_valid_txn $t $env] TRUE
103		set txn "-txn $t"
104	}
105	set dbc [eval {$db cursor} $txn]
106	error_check_good cursor_open [is_substr $dbc $db] 1
107
108	# Do a partial write to extend each datum in
109	# the regular db by the corresponding dictionary word.
110	# We have to go through each key's dup set using -set
111	# because cursors are not stable in the hash AM and we
112	# want to make sure we hit all the keys.
113	for { set i 0 } { $i < $count } { incr i } {
114		set key $keys($i)
115		for {set ret [$dbc get -set $key]}  \
116		    {[llength $ret] != 0} \
117		    {set ret [$dbc get -nextdup]} {
118
119			set k [lindex [lindex $ret 0] 0]
120			set orig_d [lindex [lindex $ret 0] 1]
121			set d [string range $orig_d 2 end]
122			set doff [expr [string length $d] + 2]
123			set dlen 0
124			error_check_good data_and_key_sanity $d $k
125
126			set ret [$dbc get -current]
127			error_check_good before_sanity \
128			    [lindex [lindex $ret 0] 0] \
129			    [string range [lindex [lindex $ret 0] 1] 2 end]
130
131			error_check_good partial_put [eval {$dbc put -current \
132			    -partial [list $doff $dlen] $d}] 0
133
134			set ret [$dbc get -current]
135			error_check_good partial_put_correct \
136			    [lindex [lindex $ret 0] 1] $orig_d$d
137		}
138	}
139
140	puts "\tTest$tnum.c: Double-checking get loop."
141	# Double-check that each datum in the regular db has
142	# been appropriately modified.
143
144	for {set ret [$dbc get -first]} \
145	    {[llength $ret] != 0} \
146	    {set ret [$dbc get -next]} {
147
148		set k [lindex [lindex $ret 0] 0]
149		set d [lindex [lindex $ret 0] 1]
150		error_check_good modification_correct \
151		    [string range $d 2 end] [repeat $k 2]
152	}
153
154	error_check_good dbc_close [$dbc close] 0
155	if { $txnenv == 1 } {
156		error_check_good txn [$t commit] 0
157	}
158	error_check_good db_close [$db close] 0
159}
160