1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2006,2008 Oracle.  All rights reserved.
4#
5# $Id: test121.tcl,v 1.11 2008/03/26 14:43:20 carol Exp $
6#
7# TEST	test121
8# TEST	Tests of multi-version concurrency control.
9# TEST
10# TEST	MVCC and cursor adjustment.
11# TEST	Set up a -snapshot cursor and position it in the middle
12# TEST 	of a database.
13# TEST  Write to the database, both before and after the cursor,
14# TEST	and verify that it stays on the same position.
15
16proc test121 { method {tnum "121"} args } {
17	source ./include.tcl
18
19	# This test needs its own env.
20	set eindex [lsearch -exact $args "-env"]
21	if { $eindex != -1 } {
22		incr eindex
23		set env [lindex $args $eindex]
24		puts "Test$tnum skipping for env $env"
25		return
26	}
27
28	# MVCC is not allowed with queue methods.
29	if { [is_queue $method] == 1 } {
30		puts "Test$tnum skipping for method $method"
31		return
32	}
33
34	puts "\tTest$tnum ($method): MVCC and cursor adjustment."
35
36	set args [convert_args $method $args]
37	set omethod [convert_method $method]
38	set encargs ""
39	set args [split_encargs $args encargs]
40	set filename "test.db"
41
42	# Create transactional env.  Specifying -multiversion makes
43	# all databases opened within the env -multiversion.
44
45	env_cleanup $testdir
46	puts "\tTest$tnum.a: Creating txn env."
47
48	# Raise cachesize so this test focuses on cursor adjustment
49	# and not on small cache issues.
50	set cachesize [expr 2 * 1024 * 1024]
51	set max_locks 2000
52	set max_objects 2000
53	set env [eval {berkdb_env -create -cachesize "0 $cachesize 1"}\
54	    -lock_max_locks $max_locks -lock_max_objects $max_objects\
55	    -txn -multiversion $encargs -home $testdir]
56	error_check_good env_open [is_valid_env $env] TRUE
57
58	# Open database.
59	puts "\tTest$tnum.b: Creating -multiversion db."
60	set db [eval {berkdb_open} \
61	    -create -auto_commit -env $env $omethod $args $filename]
62	error_check_good db_open [is_valid_db $db] TRUE
63
64	# Start transactions.
65	puts "\tTest$tnum.c: Start txns with -snapshot."
66	set t1 [$env txn -snapshot]
67	set txn1 "-txn $t1"
68
69	# Enter some data using txn1.  Leave holes, by using keys
70	# 2, 4, 6 ....
71	set niter 10000
72	set data DATA
73	for { set i 1 } { $i <= $niter } { incr i } {
74		set key [expr $i * 2]
75		error_check_good t1_put [eval {$db put} $txn1 $key $data.$key] 0
76	}
77	error_check_good t1_commit [$t1 commit] 0
78
79	# Open a read-only cursor.
80	set t2 [$env txn -snapshot]
81	set txn2 "-txn $t2"
82	set cursor [eval {$db cursor} $txn2]
83	error_check_good db_cursor [is_valid_cursor $cursor $db] TRUE
84
85	# Walk the cursor halfway through the database.
86	set i 1
87	set halfway [expr $niter / 2]
88	for { set ret [$cursor get -first] } \
89	    { $i <= $halfway } \
90	    { set ret [$cursor get -next] } {
91		incr i
92	}
93
94	set currentkey [lindex [lindex $ret 0] 0]
95	set currentdata [lindex [lindex $ret 0] 1]
96
97	# Start a new transaction and use it to enter more data.
98	# Verify that the cursor is not changed.
99	puts "\tTest$tnum.c: Enter more data."
100	set t1 [$env txn -snapshot]
101	set txn1 "-txn $t1"
102
103	# Enter more data, filling in the holes from the first
104	# time around by using keys 1, 3, 5 ....  Cursor should
105	# stay on the same item.
106	for { set i 1 } { $i <= $niter } { incr i } {
107		set key [expr [expr $i * 2] - 1]
108		error_check_good t1_put [eval {$db put} $txn1 $key $data.$key] 0
109		set ret [$cursor get -current]
110		set k [lindex [lindex $ret 0] 0]
111		set d [lindex [lindex $ret 0] 1]
112		error_check_good current_key $k $currentkey
113		error_check_good current_data $d $currentdata
114	}
115
116	error_check_good t1_commit [$t1 commit] 0
117	error_check_good cursor_close [$cursor close] 0
118	error_check_good t2_commit [$t2 commit] 0
119
120	# Clean up.
121	error_check_good db_close [$db close] 0
122	error_check_good env_close [$env close] 0
123}
124