summaryrefslogtreecommitdiff
path: root/test/log001.tcl
blob: 2fb66f5c583cd898bab33a4b7595741b4d3323b9 (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
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1996-2009 Oracle.  All rights reserved.
#
# $Id$
#

# TEST	log001
# TEST	Read/write log records.
# TEST	Test with and without fixed-length, in-memory logging,
# TEST	and encryption.
proc log001 { } {
	global passwd
	global has_crypto
	global rand_init

	berkdb srand $rand_init
	set iter 1000

	set max [expr 1024 * 128]
	foreach fixedlength { 0 1 } {
		foreach inmem { 1 0 } {
			log001_body $max $iter $fixedlength $inmem
			log001_body $max [expr $iter * 15] $fixedlength $inmem

			# Skip encrypted tests if not supported.
			if { $has_crypto == 0 } {
				continue
			}
			log001_body $max\
			    $iter $fixedlength $inmem "-encryptaes $passwd"
			log001_body $max\
			    [expr $iter * 15] $fixedlength $inmem "-encryptaes $passwd"
		}
	}
}

proc log001_body { max nrecs fixedlength inmem {encargs ""} } {
	source ./include.tcl

	puts -nonewline "Log001: Basic put/get log records: "
	if { $fixedlength == 1 } {
		puts -nonewline "fixed-length ($encargs)"
	} else {
		puts -nonewline "variable-length ($encargs)"
	}

	# In-memory logging requires a large enough log buffer that
	# any active transaction can be aborted.
	if { $inmem == 1 } {
		set lbuf [expr 8 * [expr 1024 * 1024]]
		puts " with in-memory logging."
	} else {
		puts " with on-disk logging."
	}

	env_cleanup $testdir

	set logargs ""
	if { $inmem == 1 } {
		set logargs "-log_inmemory -log_buffer $lbuf"
	}
	set env [eval {berkdb_env -log -create -home $testdir -mode 0644} \
	    $encargs $logargs -log_max $max]
	error_check_good envopen [is_valid_env $env] TRUE

	# We will write records to the log and make sure we can
	# read them back correctly.  We'll use a standard pattern
	# repeated some number of times for each record.
	set lsn_list {}
	set rec_list {}
	puts "\tLog001.a: Writing $nrecs log records"
	for { set i 0 } { $i < $nrecs } { incr i } {
		set rec ""
		for { set j 0 } { $j < [expr $i % 10 + 1] } {incr j} {
			set rec $rec$i:logrec:$i
		}
		if { $fixedlength != 1 } {
			set rec $rec:[random_data 237 0 0]
		}
		set lsn [$env log_put $rec]
		error_check_bad log_put [is_substr $lsn log_cmd] 1
		lappend lsn_list $lsn
		lappend rec_list $rec
	}

	# Open a log cursor.
	set logc [$env log_cursor]
	error_check_good logc [is_valid_logc $logc $env] TRUE

	puts "\tLog001.b: Retrieving log records sequentially (forward)"
	set i 0
	for { set grec [$logc get -first] } { [llength $grec] != 0 } {
		set grec [$logc get -next]} {
		error_check_good log_get:seq [lindex $grec 1] \
						 [lindex $rec_list $i]
		incr i
	}

	puts "\tLog001.c: Retrieving log records sequentially (backward)"
	set i [llength $rec_list]
	for { set grec [$logc get -last] } { [llength $grec] != 0 } {
	    set grec [$logc get -prev] } {
		incr i -1
		error_check_good \
		    log_get:seq [lindex $grec 1] [lindex $rec_list $i]
	}

	puts "\tLog001.d: Retrieving log records sequentially by LSN"
	set i 0
	foreach lsn $lsn_list {
		set grec [$logc get -set $lsn]
		error_check_good \
		    log_get:seq [lindex $grec 1] [lindex $rec_list $i]
		incr i
	}

	puts "\tLog001.e: Retrieving log records randomly by LSN"
	set m [expr [llength $lsn_list] - 1]
	for { set i 0 } { $i < $nrecs } { incr i } {
		set recno [berkdb random_int 0 $m ]
		set lsn [lindex $lsn_list $recno]
		set grec [$logc get -set $lsn]
		error_check_good \
		    log_get:seq [lindex $grec 1] [lindex $rec_list $recno]
	}

	puts "\tLog001.f: Retrieving first/current, last/current log record"
	set grec [$logc get -first]
	error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list 0]
	set grec [$logc get -current]
	error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list 0]
	set i [expr [llength $rec_list] - 1]
	set grec [$logc get -last]
	error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list $i]
	set grec [$logc get -current]
	error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list $i]

	# Close and unlink the file
	error_check_good log_cursor:close:$logc [$logc close] 0
	error_check_good env:close [$env close] 0
	error_check_good envremove [berkdb envremove -home $testdir] 0
}