1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	rsrc001
8# TEST	Recno backing file test.  Try different patterns of adding
9# TEST	records and making sure that the corresponding file matches.
10proc rsrc001 { } {
11	source ./include.tcl
12
13	puts "Rsrc001: Basic recno backing file writeback tests"
14
15	# We run this test essentially twice, once with a db file
16	# and once without (an in-memory database).
17	set rec1 "This is record 1"
18	set rec2 "This is record 2 This is record 2"
19	set rec3 "This is record 3 This is record 3 This is record 3"
20	set rec4 [replicate "This is record 4 " 512]
21
22	foreach testfile { "$testdir/rsrc001.db" "" } {
23
24		cleanup $testdir NULL
25
26		if { $testfile == "" } {
27			puts "Rsrc001: Testing with in-memory database."
28		} else {
29			puts "Rsrc001: Testing with disk-backed database."
30		}
31
32		# Create backing file for the empty-file test.
33		set oid1 [open $testdir/rsrc.txt w]
34		fconfigure $oid1 -translation binary
35		close $oid1
36
37		puts "\tRsrc001.a: Put to empty file."
38		set db [eval {berkdb_open -create -mode 0644\
39		    -recno -source $testdir/rsrc.txt} $testfile]
40		error_check_good dbopen [is_valid_db $db] TRUE
41		set txn ""
42
43		set ret [eval {$db put} $txn {1 $rec1}]
44		error_check_good put_to_empty $ret 0
45		error_check_good db_close [$db close] 0
46
47		# Now fill out the backing file and create the check file.
48		set oid1 [open $testdir/rsrc.txt a]
49		set oid2 [open $testdir/check.txt w]
50		fconfigure $oid1 -translation binary
51		fconfigure $oid2 -translation binary
52
53		# This one was already put into rsrc.txt.
54		puts $oid2 $rec1
55
56		# These weren't.
57		puts $oid1 $rec2
58		puts $oid2 $rec2
59		puts $oid1 $rec3
60		puts $oid2 $rec3
61		puts $oid1 $rec4
62		puts $oid2 $rec4
63		close $oid1
64		close $oid2
65
66		puts -nonewline "\tRsrc001.b: Read file, rewrite last record;"
67		puts " write it out and diff"
68		set db [eval {berkdb_open -create -mode 0644\
69		    -recno -source $testdir/rsrc.txt} $testfile]
70		error_check_good dbopen [is_valid_db $db] TRUE
71
72		# Read the last record; replace it (but we won't change it).
73		# Then close the file and diff the two files.
74		set dbc [eval {$db cursor} $txn]
75		error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE
76
77		set rec [$dbc get -last]
78		error_check_good get_last [llength [lindex $rec 0]] 2
79		set key [lindex [lindex $rec 0] 0]
80		set data [lindex [lindex $rec 0] 1]
81
82		# Get the last record from the text file
83		set oid [open $testdir/rsrc.txt]
84		fconfigure $oid -translation binary
85		set laststr ""
86		while { [gets $oid str] != -1 } {
87			set laststr $str
88		}
89		close $oid
90		error_check_good getlast $data $laststr
91
92		set ret [eval {$db put} $txn {$key $data}]
93		error_check_good replace_last $ret 0
94
95		error_check_good curs_close [$dbc close] 0
96		error_check_good db_sync [$db sync] 0
97		error_check_good db_sync [$db sync] 0
98		error_check_good \
99		    Rsrc001:diff($testdir/rsrc.txt,$testdir/check.txt) \
100		    [filecmp $testdir/rsrc.txt $testdir/check.txt] 0
101
102		puts -nonewline "\tRsrc001.c: "
103		puts "Append some records in tree and verify in file."
104		set oid [open $testdir/check.txt a]
105		fconfigure $oid -translation binary
106		for {set i 1} {$i < 10} {incr i} {
107			set rec [replicate "New Record $i" $i]
108			puts $oid $rec
109			incr key
110			set ret [eval {$db put} $txn {-append $rec}]
111			error_check_good put_append $ret $key
112		}
113		error_check_good db_sync [$db sync] 0
114		error_check_good db_sync [$db sync] 0
115		close $oid
116		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
117		error_check_good \
118		    Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
119
120		puts "\tRsrc001.d: Append by record number"
121		set oid [open $testdir/check.txt a]
122		fconfigure $oid -translation binary
123		for {set i 1} {$i < 10} {incr i} {
124			set rec [replicate "New Record (set 2) $i" $i]
125			puts $oid $rec
126			incr key
127			set ret [eval {$db put} $txn {$key $rec}]
128			error_check_good put_byno $ret 0
129		}
130
131		error_check_good db_sync [$db sync] 0
132		error_check_good db_sync [$db sync] 0
133		close $oid
134		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
135		error_check_good \
136		    Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
137
138		puts "\tRsrc001.e: Put beyond end of file."
139		set oid [open $testdir/check.txt a]
140		fconfigure $oid -translation binary
141		for {set i 1} {$i < 10} {incr i} {
142			puts $oid ""
143			incr key
144		}
145		set rec "Last Record"
146		puts $oid $rec
147		incr key
148
149		set ret [eval {$db put} $txn {$key $rec}]
150		error_check_good put_byno $ret 0
151
152		puts "\tRsrc001.f: Put beyond end of file, after reopen."
153
154		error_check_good db_close [$db close] 0
155		set db [eval {berkdb_open -create -mode 0644\
156		    -recno -source $testdir/rsrc.txt} $testfile]
157		error_check_good dbopen [is_valid_db $db] TRUE
158
159		set rec "Last record with reopen"
160		puts $oid $rec
161
162		incr key
163		set ret [eval {$db put} $txn {$key $rec}]
164		error_check_good put_byno_with_reopen $ret 0
165
166		puts "\tRsrc001.g:\
167		    Put several beyond end of file, after reopen with snapshot."
168		error_check_good db_close [$db close] 0
169		set db [eval {berkdb_open -create -mode 0644\
170		    -snapshot -recno -source $testdir/rsrc.txt} $testfile]
171		error_check_good dbopen [is_valid_db $db] TRUE
172
173		set rec "Really really last record with reopen"
174		puts $oid ""
175		puts $oid ""
176		puts $oid ""
177		puts $oid $rec
178
179		incr key
180		incr key
181		incr key
182		incr key
183
184		set ret [eval {$db put} $txn {$key $rec}]
185		error_check_good put_byno_with_reopen $ret 0
186
187		error_check_good db_sync [$db sync] 0
188		error_check_good db_sync [$db sync] 0
189
190		close $oid
191		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
192		error_check_good \
193		    Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
194
195		puts "\tRsrc001.h: Verify proper syncing of changes on close."
196		error_check_good Rsrc001:db_close [$db close] 0
197		set db [eval {berkdb_open -create -mode 0644 -recno \
198		    -source $testdir/rsrc.txt} $testfile]
199		set oid [open $testdir/check.txt a]
200		fconfigure $oid -translation binary
201		for {set i 1} {$i < 10} {incr i} {
202			set rec [replicate "New Record $i" $i]
203			puts $oid $rec
204			set ret [eval {$db put} $txn {-append $rec}]
205			# Don't bother checking return;  we don't know what
206			# the key number is, and we'll pick up a failure
207			# when we compare.
208		}
209		error_check_good Rsrc001:db_close [$db close] 0
210		close $oid
211		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
212		error_check_good Rsrc001:diff($testdir/{rsrc,check}.txt) $ret 0
213	}
214}
215
216