1 # See the file LICENSE for redistribution information.
3 # Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.
8 # TEST Recno backing file test. Try different patterns of adding
9 # TEST records and making sure that the corresponding file matches.
13 puts "Rsrc001: Basic recno backing file writeback tests"
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]
22 foreach testfile { "$testdir/rsrc001.db" "" } {
26 if { $testfile == "" } {
27 puts "Rsrc001: Testing with in-memory database."
29 puts "Rsrc001: Testing with disk-backed database."
32 # Create backing file for the empty-file test.
33 set oid1 [open $testdir/rsrc.txt w]
34 fconfigure $oid1 -translation binary
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
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
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
53 # This one was already put into rsrc.txt.
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
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
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]
82 # Get the last record from the text file
83 set oid [open $testdir/rsrc.txt]
84 fconfigure $oid -translation binary
86 while { [gets $oid str] != -1 } {
90 error_check_good getlast $data $laststr
92 set ret [eval {$db put} $txn {$key $data}]
93 error_check_good replace_last $ret 0
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
99 Rsrc001:diff($testdir/rsrc.txt,$testdir/check.txt) \
100 [filecmp $testdir/rsrc.txt $testdir/check.txt] 0
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]
110 set ret [eval {$db put} $txn {-append $rec}]
111 error_check_good put_append $ret $key
113 error_check_good db_sync [$db sync] 0
114 error_check_good db_sync [$db sync] 0
116 set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
118 Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
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]
127 set ret [eval {$db put} $txn {$key $rec}]
128 error_check_good put_byno $ret 0
131 error_check_good db_sync [$db sync] 0
132 error_check_good db_sync [$db sync] 0
134 set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
136 Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
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} {
145 set rec "Last Record"
149 set ret [eval {$db put} $txn {$key $rec}]
150 error_check_good put_byno $ret 0
152 puts "\tRsrc001.f: Put beyond end of file, after reopen."
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
159 set rec "Last record with reopen"
163 set ret [eval {$db put} $txn {$key $rec}]
164 error_check_good put_byno_with_reopen $ret 0
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
173 set rec "Really really last record with reopen"
184 set ret [eval {$db put} $txn {$key $rec}]
185 error_check_good put_byno_with_reopen $ret 0
187 error_check_good db_sync [$db sync] 0
188 error_check_good db_sync [$db sync] 0
191 set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
193 Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
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]
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
209 error_check_good Rsrc001:db_close [$db close] 0
211 set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
212 error_check_good Rsrc001:diff($testdir/{rsrc,check}.txt) $ret 0