1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999, 2013 Oracle and/or its affiliates.  All rights reserved.
4#
5# $Id$
6#
7# TEST	test061
8# TEST	Test of txn abort and commit for in-memory databases.
9# TEST	a) Put + abort: verify absence of data
10# TEST	b) Put + commit: verify presence of data
11# TEST	c) Overwrite + abort: verify that data is unchanged
12# TEST	d) Overwrite + commit: verify that data has changed
13# TEST	e) Delete + abort: verify that data is still present
14# TEST	f) Delete + commit: verify that data has been deleted
15proc test061 { method args } {
16	global alphabet
17	global encrypt
18	global errorCode
19	global passwd
20	source ./include.tcl
21
22	#
23	# If we are using an env, then skip this test.  It needs its own.
24	set eindex [lsearch -exact $args "-env"]
25	if { $eindex != -1 } {
26		incr eindex
27		set env [lindex $args $eindex]
28		puts "Test061 skipping for env $env"
29		return
30	}
31	if { [is_partitioned $args] == 1 } {
32		puts "Test061 skipping for partitioned $method"
33		return
34	}
35	set args [convert_args $method $args]
36	set omethod [convert_method $method]
37	if { [is_queueext $method] == 1} {
38		puts "Test061 skipping for method $method"
39		return
40	}
41	puts "Test061: Transaction abort and commit test for in-memory data."
42	puts "Test061: $method $args"
43
44	set encargs ""
45	set args [split_encargs $args encargs]
46	set pageargs ""
47	split_pageargs $args pageargs
48
49	set key "key"
50	set data "data"
51	set otherdata "otherdata"
52	set txn ""
53	set flags ""
54	set gflags ""
55
56	if { [is_record_based $method] == 1} {
57		set key 1
58		set gflags " -recno"
59	}
60
61	puts "\tTest061: Create environment and $method database."
62	env_cleanup $testdir
63
64	# create environment
65	set eflags "-create -txn $encargs -home $testdir"
66	set dbenv [eval {berkdb_env} $eflags $pageargs ]
67	error_check_good dbenv [is_valid_env $dbenv] TRUE
68
69	# db open -- no file specified, in-memory database
70	set flags "-auto_commit -create $args $omethod"
71	set db [eval {berkdb_open -env} $dbenv $flags]
72	error_check_good dbopen [is_valid_db $db] TRUE
73
74	# Here we go with the six test cases.  Since we need to verify
75	# a different thing each time, and since we can't just reuse
76	# the same data if we're to test overwrite, we just
77	# plow through rather than writing some impenetrable loop code;
78	# each of the cases is only a few lines long, anyway.
79
80	puts "\tTest061.a: put/abort"
81
82	# txn_begin
83	set txn [$dbenv txn]
84	error_check_good txn_begin [is_valid_txn $txn $dbenv] TRUE
85
86	# put a key
87	set ret [eval {$db put} -txn $txn {$key [chop_data $method $data]}]
88	error_check_good db_put $ret 0
89
90	# check for existence
91	set ret [eval {$db get} -txn $txn $gflags {$key}]
92	error_check_good get $ret [list [list $key [pad_data $method $data]]]
93
94	# abort
95	error_check_good txn_abort [$txn abort] 0
96
97	# check for *non*-existence
98	set ret [eval {$db get} $gflags {$key}]
99	error_check_good get $ret {}
100
101	puts "\tTest061.b: put/commit"
102
103	# txn_begin
104	set txn [$dbenv txn]
105	error_check_good txn_begin [is_valid_txn $txn $dbenv] TRUE
106
107	# put a key
108	set ret [eval {$db put} -txn $txn {$key [chop_data $method $data]}]
109	error_check_good db_put $ret 0
110
111	# check for existence
112	set ret [eval {$db get} -txn $txn $gflags {$key}]
113	error_check_good get $ret [list [list $key [pad_data $method $data]]]
114
115	# commit
116	error_check_good txn_commit [$txn commit] 0
117
118	# check again for existence
119	set ret [eval {$db get} $gflags {$key}]
120	error_check_good get $ret [list [list $key [pad_data $method $data]]]
121
122	puts "\tTest061.c: overwrite/abort"
123
124	# txn_begin
125	set txn [$dbenv txn]
126	error_check_good txn_begin [is_valid_txn $txn $dbenv] TRUE
127
128	# overwrite {key,data} with {key,otherdata}
129	set ret [eval {$db put} -txn $txn {$key [chop_data $method $otherdata]}]
130	error_check_good db_put $ret 0
131
132	# check for existence
133	set ret [eval {$db get} -txn $txn $gflags {$key}]
134	error_check_good get $ret \
135	    [list [list $key [pad_data $method $otherdata]]]
136
137	# abort
138	error_check_good txn_abort [$txn abort] 0
139
140	# check that data is unchanged ($data not $otherdata)
141	set ret [eval {$db get} $gflags {$key}]
142	error_check_good get $ret [list [list $key [pad_data $method $data]]]
143
144	puts "\tTest061.d: overwrite/commit"
145
146	# txn_begin
147	set txn [$dbenv txn]
148	error_check_good txn_begin [is_valid_txn $txn $dbenv] TRUE
149
150	# overwrite {key,data} with {key,otherdata}
151	set ret [eval {$db put} -txn $txn {$key [chop_data $method $otherdata]}]
152	error_check_good db_put $ret 0
153
154	# check for existence
155	set ret [eval {$db get} -txn $txn $gflags {$key}]
156	error_check_good get $ret \
157	    [list [list $key [pad_data $method $otherdata]]]
158
159	# commit
160	error_check_good txn_commit [$txn commit] 0
161
162	# check that data has changed ($otherdata not $data)
163	set ret [eval {$db get} $gflags {$key}]
164	error_check_good get $ret \
165	    [list [list $key [pad_data $method $otherdata]]]
166
167	puts "\tTest061.e: delete/abort"
168
169	# txn_begin
170	set txn [$dbenv txn]
171	error_check_good txn_begin [is_valid_txn $txn $dbenv] TRUE
172
173	# delete
174	set ret [eval {$db del} -txn $txn {$key}]
175	error_check_good db_put $ret 0
176
177	# check for nonexistence
178	set ret [eval {$db get} -txn $txn $gflags {$key}]
179	error_check_good get $ret {}
180
181	# abort
182	error_check_good txn_abort [$txn abort] 0
183
184	# check for existence
185	set ret [eval {$db get} $gflags {$key}]
186	error_check_good get $ret \
187	    [list [list $key [pad_data $method $otherdata]]]
188
189	puts "\tTest061.f: delete/commit"
190
191	# txn_begin
192	set txn [$dbenv txn]
193	error_check_good txn_begin [is_valid_txn $txn $dbenv] TRUE
194
195	# put a key
196	set ret [eval {$db del} -txn $txn {$key}]
197	error_check_good db_put $ret 0
198
199	# check for nonexistence
200	set ret [eval {$db get} -txn $txn $gflags {$key}]
201	error_check_good get $ret {}
202
203	# commit
204	error_check_good txn_commit [$txn commit] 0
205
206	# check for continued nonexistence
207	set ret [eval {$db get} $gflags {$key}]
208	error_check_good get $ret {}
209
210	# We're done; clean up.
211	error_check_good db_close [eval {$db close}] 0
212	error_check_good env_close [eval {$dbenv close}] 0
213
214	# Now run db_recover and ensure that it runs cleanly.
215	set utilflag ""
216	if { $encrypt != 0 } {
217		set utilflag "-P $passwd"
218	}
219	puts "\tTest061.g: Running db_recover -h"
220	set ret [catch {eval {exec} $util_path/db_recover -h $testdir \
221	    $utilflag} res]
222	if { $ret != 0 } {
223		puts "FAIL: db_recover outputted $res"
224	}
225	error_check_good db_recover $ret 0
226
227	puts "\tTest061.h: Running db_recover -c -h"
228	set ret [catch {eval {exec} $util_path/db_recover -c -h $testdir \
229	    $utilflag} res]
230	error_check_good db_recover-c $ret 0
231}
232