1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	rsrc003
8# TEST	Recno backing file test.  Try different patterns of adding
9# TEST	records and making sure that the corresponding file matches.
10proc rsrc003 { } {
11	source ./include.tcl
12	global fixed_len
13
14	puts "Rsrc003: Basic recno backing file writeback tests fixed length"
15
16	# We run this test essentially twice, once with a db file
17	# and once without (an in-memory database).
18	#
19	# Then run with big fixed-length records
20	set rec1 "This is record 1"
21	set rec2 "This is record 2"
22	set rec3 "This is record 3"
23	set bigrec1 [replicate "This is record 1 " 512]
24	set bigrec2 [replicate "This is record 2 " 512]
25	set bigrec3 [replicate "This is record 3 " 512]
26
27	set orig_fixed_len $fixed_len
28	set rlist {
29	{{$rec1 $rec2 $rec3} "small records" }
30	{{$bigrec1 $bigrec2 $bigrec3} "large records" }}
31
32	foreach testfile { "$testdir/rsrc003.db" "" } {
33
34		foreach rec $rlist {
35			cleanup $testdir NULL
36
37			set recs [lindex $rec 0]
38			set msg [lindex $rec 1]
39
40			# Create the starting files
41			# Note that for the rest of the test, we are going
42			# to append a LF when we 'put' via DB to maintain
43			# file structure and allow us to use 'gets'.
44			set oid1 [open $testdir/rsrc.txt w]
45			set oid2 [open $testdir/check.txt w]
46			fconfigure $oid1 -translation binary
47			fconfigure $oid2 -translation binary
48			foreach record $recs {
49				set r [subst $record]
50				set fixed_len [string length $r]
51				puts $oid1 $r
52				puts $oid2 $r
53			}
54			close $oid1
55			close $oid2
56
57			set reclen [expr $fixed_len + 1]
58			if { $reclen > [string length $rec1] } {
59				set repl 512
60			} else {
61				set repl 2
62			}
63			if { $testfile == "" } {
64				puts \
65"Rsrc003: Testing with in-memory database with $msg."
66			} else {
67				puts \
68"Rsrc003: Testing with disk-backed database with $msg."
69			}
70
71			puts -nonewline \
72			    "\tRsrc003.a: Read file, rewrite last record;"
73			puts " write it out and diff"
74			set db [eval {berkdb_open -create -mode 0644 -recno \
75			    -len $reclen -source $testdir/rsrc.txt} $testfile]
76			error_check_good dbopen [is_valid_db $db] TRUE
77
78			# Read the last record; replace it (don't change it).
79			# Then close the file and diff the two files.
80			set txn ""
81			set dbc [eval {$db cursor} $txn]
82			error_check_good db_cursor \
83			    [is_valid_cursor $dbc $db] TRUE
84
85			set rec [$dbc get -last]
86			error_check_good get_last [llength [lindex $rec 0]] 2
87			set key [lindex [lindex $rec 0] 0]
88			set data [lindex [lindex $rec 0] 1]
89
90			# Get the last record from the text file
91			set oid [open $testdir/rsrc.txt]
92			fconfigure $oid -translation binary
93			set laststr ""
94			while { [gets $oid str] != -1 } {
95				append str \12
96				set laststr $str
97			}
98			close $oid
99			error_check_good getlast $data $laststr
100
101			set ret [eval {$db put} $txn {$key $data}]
102			error_check_good replace_last $ret 0
103
104			error_check_good curs_close [$dbc close] 0
105			error_check_good db_sync [$db sync] 0
106			error_check_good db_sync [$db sync] 0
107			error_check_good \
108			    diff1($testdir/rsrc.txt,$testdir/check.txt) \
109			    [filecmp $testdir/rsrc.txt $testdir/check.txt] 0
110
111			puts -nonewline "\tRsrc003.b: "
112			puts "Append some records in tree and verify in file."
113			set oid [open $testdir/check.txt a]
114			fconfigure $oid -translation binary
115			for {set i 1} {$i < 10} {incr i} {
116				set rec [chop_data -frecno [replicate \
117				    "This is New Record $i" $repl]]
118				puts $oid $rec
119				append rec \12
120				incr key
121				set ret [eval {$db put} $txn {-append $rec}]
122				error_check_good put_append $ret $key
123			}
124			error_check_good db_sync [$db sync] 0
125			error_check_good db_sync [$db sync] 0
126			close $oid
127			set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
128			error_check_good \
129			    diff2($testdir/{rsrc.txt,check.txt}) $ret 0
130
131			puts "\tRsrc003.c: Append by record number"
132			set oid [open $testdir/check.txt a]
133			fconfigure $oid -translation binary
134			for {set i 1} {$i < 10} {incr i} {
135				set rec [chop_data -frecno [replicate \
136				    "New Record (set 2) $i" $repl]]
137				puts $oid $rec
138				append rec \12
139				incr key
140				set ret [eval {$db put} $txn {$key $rec}]
141				error_check_good put_byno $ret 0
142			}
143
144			error_check_good db_sync [$db sync] 0
145			error_check_good db_sync [$db sync] 0
146			close $oid
147			set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
148			error_check_good \
149			    diff3($testdir/{rsrc.txt,check.txt}) $ret 0
150
151			puts \
152"\tRsrc003.d: Verify proper syncing of changes on close."
153			error_check_good Rsrc003:db_close [$db close] 0
154			set db [eval {berkdb_open -create -mode 0644 -recno \
155			    -len $reclen -source $testdir/rsrc.txt} $testfile]
156			set oid [open $testdir/check.txt a]
157			fconfigure $oid -translation binary
158			for {set i 1} {$i < 10} {incr i} {
159				set rec [chop_data -frecno [replicate \
160				    "New Record (set 3) $i" $repl]]
161				puts $oid $rec
162				append rec \12
163				set ret [eval {$db put} $txn {-append $rec}]
164				# Don't bother checking return;
165				# we don't know what
166				# the key number is, and we'll pick up a failure
167				# when we compare.
168			}
169			error_check_good Rsrc003:db_close [$db close] 0
170			close $oid
171			set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
172			error_check_good \
173			    diff5($testdir/{rsrc,check}.txt) $ret 0
174		}
175	}
176	set fixed_len $orig_fixed_len
177	return
178}
179