1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2005,2008 Oracle.  All rights reserved.
4#
5# $Id: env013.tcl,v 1.11 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	env013
8# TEST	Test of basic functionality of fileid_reset.
9# TEST
10# TEST	Create a database in an env.  Copy it to a new file within
11# TEST	the same env.  Reset the file id and make sure it has changed.
12proc env013 { } {
13	source ./include.tcl
14	global util_path
15
16	puts "Env013: Test fileid_reset."
17
18	set testfile A.db
19	set dupfile B.db
20	set nentries 500
21	set filenames "A B C D E"
22
23	foreach lorder { 1234 4321 } {
24		puts "\tEnv013.a: Creating env."
25		env_cleanup $testdir
26		set env [berkdb_env -create -home $testdir -txn]
27		error_check_good dbenv [is_valid_env $env] TRUE
28
29		# Open database A, populate and close.
30		puts "\tEnv013.b: Creating database with lorder $lorder."
31		foreach filename $filenames {
32			set db [eval {berkdb_open \
33			    -pagesize 8192 -env $env -auto_commit \
34			    -btree -create -mode 0644 $testfile $filename} ]
35			error_check_good dbopen [is_valid_db $db] TRUE
36			set t [$env txn]
37			error_check_good txn [is_valid_txn $t $env] TRUE
38			for { set i 0 } { $i < $nentries } { incr i } {
39				set key KEY.$i
40				set data DATA.$i
41				error_check_good\
42				    db_put [$db put -txn $t $key $data] 0
43			}
44			error_check_good txn_commit [$t commit] 0
45			error_check_good db_close [$db close] 0
46		}
47
48		# Copy database file A to database file B for fileid testing.
49		puts "\tEnv013.c: Copy database."
50		file copy -force $testdir/$testfile $testdir/$dupfile
51
52		# Reset B's fileid and confirm the ID has changed.
53		puts "\tEnv013.d: Resetting file id for copied database."
54		error_check_good fileid_reset [$env id_reset $dupfile] 0
55		set orig_id [getfileid $testdir/$testfile]
56		puts "\tEnv013.d: orig: $orig_id"
57		set new_id [getfileid $testdir/$dupfile]
58		puts "\tEnv013.d: new: $new_id"
59		error_check_bad id_changed $orig_id $new_id
60
61		# Verify and open B.
62		puts "\tEnv013.e: Verify and open database copy."
63	 	error_check_good verify [verify_dir $testdir "\tEnv013.e: "] 0
64		set db [eval {berkdb_open} \
65		    -env $env -auto_commit -btree -mode 0644 -rdonly $dupfile]
66		error_check_good dup_open [is_valid_db $db] TRUE
67
68		# Clean up.
69		error_check_good db_close [$db close] 0
70		error_check_good env_close [$env close] 0
71	}
72}
73
74# Get file id number, identified as "uid" in db_stat.
75proc getfileid { db } {
76	global util_path
77
78	set ret [exec $util_path/db_dump -da $db]
79	set uidstart [string first "uid:" $ret]
80	set uidend [string first "\tminkey:" $ret]
81	set uid [string range $ret $uidstart $uidend]
82	set uid [string trimright $uid]
83	return $uid
84}
85