summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/rsrc001.tcl
blob: 40cc3fda4b49d0e81d2b1cc673dbbf20b3cf1bf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1996-2009 Oracle.  All rights reserved.
#
# $Id$
#
# TEST	rsrc001
# TEST	Recno backing file test.  Try different patterns of adding
# TEST	records and making sure that the corresponding file matches.
proc rsrc001 { } {
	source ./include.tcl

	puts "Rsrc001: Basic recno backing file writeback tests"

	# We run this test essentially twice, once with a db file
	# and once without (an in-memory database).
	set rec1 "This is record 1"
	set rec2 "This is record 2 This is record 2"
	set rec3 "This is record 3 This is record 3 This is record 3"
	set rec4 [replicate "This is record 4 " 512]

	foreach testfile { "$testdir/rsrc001.db" "" } {

		cleanup $testdir NULL

		if { $testfile == "" } {
			puts "Rsrc001: Testing with in-memory database."
		} else {
			puts "Rsrc001: Testing with disk-backed database."
		}

		# Create backing file for the empty-file test.
		set oid1 [open $testdir/rsrc.txt w]
		fconfigure $oid1 -translation binary
		close $oid1

		puts "\tRsrc001.a: Put to empty file."
		set db [eval {berkdb_open -create -mode 0644\
		    -recno -source $testdir/rsrc.txt} $testfile]
		error_check_good dbopen [is_valid_db $db] TRUE
		set txn ""

		set ret [eval {$db put} $txn {1 $rec1}]
		error_check_good put_to_empty $ret 0
		error_check_good db_close [$db close] 0

		# Now fill out the backing file and create the check file.
		set oid1 [open $testdir/rsrc.txt a]
		set oid2 [open $testdir/check.txt w]
		fconfigure $oid1 -translation binary 
		fconfigure $oid2 -translation binary 

		# This one was already put into rsrc.txt.
		puts $oid2 $rec1

		# These weren't.
		puts $oid1 $rec2
		puts $oid2 $rec2
		puts $oid1 $rec3
		puts $oid2 $rec3
		puts $oid1 $rec4
		puts $oid2 $rec4
		close $oid1
		close $oid2

		puts -nonewline "\tRsrc001.b: Read file, rewrite last record;"
		puts " write it out and diff"
		set db [eval {berkdb_open -create -mode 0644\
		    -recno -source $testdir/rsrc.txt} $testfile]
		error_check_good dbopen [is_valid_db $db] TRUE

		# Read the last record; replace it (but we won't change it).
		# Then close the file and diff the two files.
		set dbc [eval {$db cursor} $txn]
		error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE

		set rec [$dbc get -last]
		error_check_good get_last [llength [lindex $rec 0]] 2
		set key [lindex [lindex $rec 0] 0]
		set data [lindex [lindex $rec 0] 1]

		# Get the last record from the text file
		set oid [open $testdir/rsrc.txt]
		fconfigure $oid -translation binary 
		set laststr ""
		while { [gets $oid str] != -1 } {
			set laststr $str
		}
		close $oid
		error_check_good getlast $data $laststr

		set ret [eval {$db put} $txn {$key $data}]
		error_check_good replace_last $ret 0

		error_check_good curs_close [$dbc close] 0
		error_check_good db_sync [$db sync] 0
		error_check_good db_sync [$db sync] 0
		error_check_good \
		    Rsrc001:diff($testdir/rsrc.txt,$testdir/check.txt) \
		    [filecmp $testdir/rsrc.txt $testdir/check.txt] 0

		puts -nonewline "\tRsrc001.c: "
		puts "Append some records in tree and verify in file."
		set oid [open $testdir/check.txt a]
		fconfigure $oid -translation binary
		for {set i 1} {$i < 10} {incr i} {
			set rec [replicate "New Record $i" $i]
			puts $oid $rec
			incr key
			set ret [eval {$db put} $txn {-append $rec}]
			error_check_good put_append $ret $key
		}
		error_check_good db_sync [$db sync] 0
		error_check_good db_sync [$db sync] 0
		close $oid
		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
		error_check_good \
		    Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0

		puts "\tRsrc001.d: Append by record number"
		set oid [open $testdir/check.txt a]
		fconfigure $oid -translation binary
		for {set i 1} {$i < 10} {incr i} {
			set rec [replicate "New Record (set 2) $i" $i]
			puts $oid $rec
			incr key
			set ret [eval {$db put} $txn {$key $rec}]
			error_check_good put_byno $ret 0
		}

		error_check_good db_sync [$db sync] 0
		error_check_good db_sync [$db sync] 0
		close $oid
		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
		error_check_good \
		    Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0

		puts "\tRsrc001.e: Put beyond end of file."
		set oid [open $testdir/check.txt a]
		fconfigure $oid -translation binary
		for {set i 1} {$i < 10} {incr i} {
			puts $oid ""
			incr key
		}
		set rec "Last Record"
		puts $oid $rec
		incr key

		set ret [eval {$db put} $txn {$key $rec}]
		error_check_good put_byno $ret 0

		puts "\tRsrc001.f: Put beyond end of file, after reopen."

		error_check_good db_close [$db close] 0
		set db [eval {berkdb_open -create -mode 0644\
		    -recno -source $testdir/rsrc.txt} $testfile]
		error_check_good dbopen [is_valid_db $db] TRUE

		set rec "Last record with reopen"
		puts $oid $rec

		incr key
		set ret [eval {$db put} $txn {$key $rec}]
		error_check_good put_byno_with_reopen $ret 0

		puts "\tRsrc001.g:\
		    Put several beyond end of file, after reopen with snapshot."
		error_check_good db_close [$db close] 0
		set db [eval {berkdb_open -create -mode 0644\
		    -snapshot -recno -source $testdir/rsrc.txt} $testfile]
		error_check_good dbopen [is_valid_db $db] TRUE

		set rec "Really really last record with reopen"
		puts $oid ""
		puts $oid ""
		puts $oid ""
		puts $oid $rec

		incr key
		incr key
		incr key
		incr key

		set ret [eval {$db put} $txn {$key $rec}]
		error_check_good put_byno_with_reopen $ret 0

		error_check_good db_sync [$db sync] 0
		error_check_good db_sync [$db sync] 0

		close $oid
		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
		error_check_good \
		    Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0

		puts "\tRsrc001.h: Verify proper syncing of changes on close."
		error_check_good Rsrc001:db_close [$db close] 0
		set db [eval {berkdb_open -create -mode 0644 -recno \
		    -source $testdir/rsrc.txt} $testfile]
		set oid [open $testdir/check.txt a]
		fconfigure $oid -translation binary 
		for {set i 1} {$i < 10} {incr i} {
			set rec [replicate "New Record $i" $i]
			puts $oid $rec
			set ret [eval {$db put} $txn {-append $rec}]
			# Don't bother checking return;  we don't know what
			# the key number is, and we'll pick up a failure
			# when we compare.
		}
		error_check_good Rsrc001:db_close [$db close] 0
		close $oid
		set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
		error_check_good Rsrc001:diff($testdir/{rsrc,check}.txt) $ret 0
	}
}