Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / tcl / log001.tcl
1 # See the file LICENSE for redistribution information.
2 #
3 # Copyright (c) 1996, 2012 Oracle and/or its affiliates.  All rights reserved.
4 #
5 # $Id$
6 #
7
8 # TEST  log001
9 # TEST  Read/write log records.
10 # TEST  Test with and without fixed-length, in-memory logging,
11 # TEST  and encryption.
12 proc log001 { } {
13         global passwd
14         global has_crypto
15         global rand_init
16
17         berkdb srand $rand_init
18         set iter 1000
19
20         set max [expr 1024 * 128]
21         foreach fixedlength { 0 1 } {
22                 foreach inmem { 1 0 } {
23                         log001_body $max $iter $fixedlength $inmem
24                         log001_body $max [expr $iter * 15] $fixedlength $inmem
25
26                         # Skip encrypted tests if not supported.
27                         if { $has_crypto == 0 } {
28                                 continue
29                         }
30                         log001_body $max\
31                             $iter $fixedlength $inmem "-encryptaes $passwd"
32                         log001_body $max\
33                             [expr $iter * 15] $fixedlength $inmem "-encryptaes $passwd"
34                 }
35         }
36 }
37
38 proc log001_body { max nrecs fixedlength inmem {encargs ""} } {
39         source ./include.tcl
40
41         puts -nonewline "Log001: Basic put/get log records: "
42         if { $fixedlength == 1 } {
43                 puts -nonewline "fixed-length ($encargs)"
44         } else {
45                 puts -nonewline "variable-length ($encargs)"
46         }
47
48         # In-memory logging requires a large enough log buffer that
49         # any active transaction can be aborted.
50         if { $inmem == 1 } {
51                 set lbuf [expr 8 * [expr 1024 * 1024]]
52                 puts " with in-memory logging."
53         } else {
54                 puts " with on-disk logging."
55         }
56
57         env_cleanup $testdir
58
59         set logargs ""
60         if { $inmem == 1 } {
61                 set logargs "-log_inmemory -log_buffer $lbuf"
62         }
63         set env [eval {berkdb_env -log -create -home $testdir -mode 0644} \
64             $encargs $logargs -log_max $max]
65         error_check_good envopen [is_valid_env $env] TRUE
66
67         # We will write records to the log and make sure we can
68         # read them back correctly.  We'll use a standard pattern
69         # repeated some number of times for each record.
70         set lsn_list {}
71         set rec_list {}
72         puts "\tLog001.a: Writing $nrecs log records"
73         for { set i 0 } { $i < $nrecs } { incr i } {
74                 set rec ""
75                 for { set j 0 } { $j < [expr $i % 10 + 1] } {incr j} {
76                         set rec $rec$i:logrec:$i
77                 }
78                 if { $fixedlength != 1 } {
79                         set rec $rec:[random_data 237 0 0]
80                 }
81                 set lsn [$env log_put $rec]
82                 error_check_bad log_put [is_substr $lsn log_cmd] 1
83                 lappend lsn_list $lsn
84                 lappend rec_list $rec
85         }
86
87         # Open a log cursor.
88         set logc [$env log_cursor]
89         error_check_good logc [is_valid_logc $logc $env] TRUE
90
91         puts "\tLog001.b: Retrieving log records sequentially (forward)"
92         set i 0
93         for { set grec [$logc get -first] } { [llength $grec] != 0 } {
94                 set grec [$logc get -next]} {
95                 error_check_good log_get:seq [lindex $grec 1] \
96                                                  [lindex $rec_list $i]
97                 incr i
98         }
99
100         puts "\tLog001.c: Retrieving log records sequentially (backward)"
101         set i [llength $rec_list]
102         for { set grec [$logc get -last] } { [llength $grec] != 0 } {
103             set grec [$logc get -prev] } {
104                 incr i -1
105                 error_check_good \
106                     log_get:seq [lindex $grec 1] [lindex $rec_list $i]
107         }
108
109         puts "\tLog001.d: Retrieving log records sequentially by LSN"
110         set i 0
111         foreach lsn $lsn_list {
112                 set grec [$logc get -set $lsn]
113                 error_check_good \
114                     log_get:seq [lindex $grec 1] [lindex $rec_list $i]
115                 incr i
116         }
117
118         puts "\tLog001.e: Retrieving log records randomly by LSN"
119         set m [expr [llength $lsn_list] - 1]
120         for { set i 0 } { $i < $nrecs } { incr i } {
121                 set recno [berkdb random_int 0 $m ]
122                 set lsn [lindex $lsn_list $recno]
123                 set grec [$logc get -set $lsn]
124                 error_check_good \
125                     log_get:seq [lindex $grec 1] [lindex $rec_list $recno]
126         }
127
128         puts "\tLog001.f: Retrieving first/current, last/current log record"
129         set grec [$logc get -first]
130         error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list 0]
131         set grec [$logc get -current]
132         error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list 0]
133         set i [expr [llength $rec_list] - 1]
134         set grec [$logc get -last]
135         error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list $i]
136         set grec [$logc get -current]
137         error_check_good log_get:seq [lindex $grec 1] [lindex $rec_list $i]
138
139         # Close and unlink the file
140         error_check_good log_cursor:close:$logc [$logc close] 0
141         error_check_good env:close [$env close] 0
142         error_check_good envremove [berkdb envremove -home $testdir] 0
143 }