1# Copyright (c) 2000, 2020 Oracle and/or its affiliates.  All rights reserved.
2#
3# See the file LICENSE for license information.
4#
5# $Id$
6#
7# TEST	sdb010
8# TEST	Test DB->remove() method and DB->truncate() for subdbs
9proc sdb010 { method args } {
10	global errorCode
11	source ./include.tcl
12
13	set args [convert_args $method $args]
14	set omethod [convert_method $method]
15
16	puts "Subdb010: Test of DB->remove() and DB->truncate"
17
18       if { [is_queue $method] == 1 || [is_heap $method] == 1 } {
19		puts "\tSubdb010: Skipping for method $method."
20		return
21	}
22
23	set txnenv 0
24	set envargs ""
25	set eindex [lsearch -exact $args "-env"]
26	#
27	# If we are not given an env, create one.
28	if { $eindex == -1 } {
29		set env [berkdb_env -create -home $testdir -mode 0644]
30		error_check_good env_open [is_valid_env $env] TRUE
31	} else {
32		incr eindex
33		set env [lindex $args $eindex]
34	}
35	set testfile subdb010.db
36	set envargs " -env $env "
37	set txnenv [is_txnenv $env]
38	if { $txnenv == 1 } {
39		append args " -auto_commit "
40		append envargs " -auto_commit "
41	}
42	set testdir [get_home $env]
43	set tfpath $testdir/$testfile
44
45	cleanup $testdir $env
46
47	set txn ""
48	set testdb DATABASE
49	set testdb2 DATABASE2
50
51	set db [eval {berkdb_open -create -mode 0644} $omethod \
52	    $args $envargs $testfile $testdb]
53	error_check_good db_open [is_valid_db $db] TRUE
54	error_check_good db_close [$db close] 0
55
56	puts "\tSubdb010.a: Test of DB->remove()"
57	error_check_good file_exists_before [file exists $tfpath] 1
58	error_check_good db_remove [eval {berkdb dbremove} $envargs \
59	    $testfile $testdb] 0
60
61	# File should still exist.
62	error_check_good file_exists_after [file exists $tfpath] 1
63
64	# But database should not.
65	set ret [catch {eval berkdb_open $omethod \
66	    $args $envargs $testfile $testdb} res]
67	error_check_bad open_failed ret 0
68	error_check_good open_failed_ret [is_substr $errorCode ENOENT] 1
69
70	puts "\tSubdb010.b: Setup for DB->truncate()"
71	# The nature of the key and data are unimportant; use numeric key
72	# so record-based methods don't need special treatment.
73	set key1 1
74	set key2 2
75	set data1 [pad_data $method data1]
76	set data2 [pad_data $method data2]
77
78	set db [eval {berkdb_open -create -mode 0644} $omethod \
79	    $args $envargs {$testfile $testdb}]
80	error_check_good db_open [is_valid_db $db] TRUE
81	if { $txnenv == 1 } {
82		set t [$env txn]
83		error_check_good txn [is_valid_txn $t $env] TRUE
84		set txn "-txn $t"
85	}
86	error_check_good dbput [eval {$db put} $txn {$key1 $data1}] 0
87	if { $txnenv == 1 } {
88		error_check_good txn [$t commit] 0
89	}
90
91	set db2 [eval {berkdb_open -create -mode 0644} $omethod \
92	    $args $envargs $testfile $testdb2]
93	error_check_good db_open [is_valid_db $db2] TRUE
94	if { $txnenv == 1 } {
95		set t [$env txn]
96		error_check_good txn [is_valid_txn $t $env] TRUE
97		set txn "-txn $t"
98	}
99	error_check_good dbput [eval {$db2 put} $txn {$key2 $data2}] 0
100	if { $txnenv == 1 } {
101		error_check_good txn [$t commit] 0
102	}
103
104	error_check_good db_close [$db close] 0
105	error_check_good db_close [$db2 close] 0
106
107	puts "\tSubdb010.c: truncate"
108	#
109	# Return value should be 1, the count of how many items were
110	# destroyed when we truncated.
111	set db [eval {berkdb_open -create -mode 0644} $omethod \
112	    $args $envargs $testfile $testdb]
113	error_check_good db_open [is_valid_db $db] TRUE
114	if { $txnenv == 1 } {
115		set t [$env txn]
116		error_check_good txn [is_valid_txn $t $env] TRUE
117		set txn "-txn $t"
118	}
119	error_check_good trunc_subdb [eval {$db truncate} $txn] 1
120	if { $txnenv == 1 } {
121		error_check_good txn [$t commit] 0
122	}
123	error_check_good db_close [$db close] 0
124
125	puts "\tSubdb010.d: check"
126	set db [eval {berkdb_open} $args $envargs {$testfile $testdb}]
127	error_check_good db_open [is_valid_db $db] TRUE
128	if { $txnenv == 1 } {
129		set t [$env txn]
130		error_check_good txn [is_valid_txn $t $env] TRUE
131		set txn "-txn $t"
132	}
133	set dbc [eval {$db cursor} $txn]
134	error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE
135	set kd [$dbc get -first]
136	error_check_good trunc_dbcget [llength $kd] 0
137	error_check_good dbcclose [$dbc close] 0
138	if { $txnenv == 1 } {
139		error_check_good txn [$t commit] 0
140	}
141
142	set db2 [eval {berkdb_open} $args $envargs {$testfile $testdb2}]
143	error_check_good db_open [is_valid_db $db2] TRUE
144	if { $txnenv == 1 } {
145		set t [$env txn]
146		error_check_good txn [is_valid_txn $t $env] TRUE
147		set txn "-txn $t"
148	}
149	set dbc [eval {$db2 cursor} $txn]
150	error_check_good db_cursor [is_valid_cursor $dbc $db2] TRUE
151	set kd [$dbc get -first]
152	error_check_bad notrunc_dbcget1 [llength $kd] 0
153	set db2kd [list [list $key2 $data2]]
154	error_check_good key2 $kd $db2kd
155	set kd [$dbc get -next]
156	error_check_good notrunc_dbget2 [llength $kd] 0
157	error_check_good dbcclose [$dbc close] 0
158	if { $txnenv == 1 } {
159		error_check_good txn [$t commit] 0
160	}
161
162	error_check_good db_close [$db close] 0
163	error_check_good db_close [$db2 close] 0
164
165	# If we created our env, close it.
166	if { $eindex == -1 } {
167		error_check_good env_close [$env close] 0
168	}
169}
170