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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
<!--Id: auto.so,v 10.3 2002/03/15 16:19:10 bostic Exp -->
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
<!--All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Automatically generated functions</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
</head>
<body bgcolor=white>
<table width="100%"><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Application Specific Logging and Recovery</dl></h3></td>
<td align=right><a href="../../ref/apprec/def.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/apprec/config.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h1 align=center>Automatically generated functions</h1>
<p>The XXX.src file is processed using the gen_rec.awk script included in
the dist directory of the Berkeley DB distribution. This is an awk script
that is executed from with the following command line:
<p><blockquote><pre>awk -f gen_rec.awk \
-v source_file=<i>C_FILE</i> \
-v header_file=<i>H_FILE</i> \
-v template_file=<i>TMP_FILE</i> < XXX.src</pre></blockquote>
<p>where <i>C_FILE</i> is the name of the file into which to place the
automatically generated C code, <i>H_FILE</i> is the name of the
file into which to place the automatically generated data structures
and declarations, and <i>TMP_FILE</i> is the name of the file into
which to place a template for the recovery routines.
<p>Because the gen_rec.awk script uses sources files located relative to
the Berkeley DB dist directory, it must be run from the dist directory. For
example, in building the Berkeley DB logging and recovery routines for
ex_apprec, the following script is used to rebuild the automatically
generated files:
<p><blockquote><pre>E=../examples_c/ex_apprec
<p>
cd ../../dist
awk -f gen_rec.awk \
-v source_file=$E/ex_apprec_auto.c \
-v header_file=$E/ex_apprec_auto.h \
-v template_file=$E/ex_apprec_template < $E/ex_apprec.src</pre></blockquote>
<p>For each log record description found in the XXX.src file, the following
structure declarations and #defines will be created in the file
<i>header_file</i>:
<p><blockquote><pre>#define DB_PREFIX_RECORD_TYPE /* Integer ID number */
<p>
typedef struct _PREFIX_RECORD_TYPE_args {
/*
* These three fields are generated for every record.
*/
u_int32_t type; /* Record type used for dispatch. */
<p>
/*
* Transaction handle that identifies the transaction on whose
* behalf the record is being logged.
*/
DB_TXN *txnid;
<p>
/*
* The log sequence number returned by the previous call to log_put
* for this transaction.
*/
DB_LSN *prev_lsn;
<p>
/*
* The rest of the structure contains one field for each of
* the entries in the record statement.
*/
};</pre></blockquote>
<p>Thus, the auto-generated ex_apprec_mkdir_args structure looks as follows:
<p><blockquote><pre>typedef struct _ex_apprec_mkdir_args {
u_int32_t type;
DB_TXN *txnid;
DB_LSN prev_lsn;
DBT dirname;
} ex_apprec_mkdir_args;</pre></blockquote>
<p>The template_file will contain a template for a recovery function. The
recovery function is called on each record read from the log during
system recovery, transaction abort, or the application of log records
on a replication client, and is expected to redo or undo the operations
described by that record. The details of the recovery function will be
specific to the record being logged and need to be written manually,
but the template provides a good starting point. (Note that the
template assumes that the record is manipulating the internals of a
Berkeley DB database and sets up database handles, page structures, and such
for convenience. Many application-specific log records will not need
these, and may simply delete much of the template. See
ex_apprec_template and ex_apprec_rec.c for an example.)
<p>The template file should be copied to a source file in the application
(but not the automatically generated source_file, as that will get
overwritten each time gen_rec.awk is run) and fully developed there.
The recovery function takes the following parameters:
<p><blockquote><p><dl compact>
<p><dt>dbenv<dd>The environment in which recovery is running.
<p><dt>rec<dd>The record being recovered.
<p><dt>lsn<dd>The log sequence number of the record being recovered. The
prev_lsn field, automatically included in every auto-generated log
record, should be returned through this argument. The prev_lsn field
is used to chain log records together to allow transaction aborts;
because the recovery function is the only place that a log record gets
parsed, the responsibility for returning this value lies with the
recovery function writer.
<p><dt>op<dd>A parameter of type db_recops, which indicates what operation is being
run (<a href="../../api_c/env_set_app_dispatch.html#DB_TXN_ABORT">DB_TXN_ABORT</a>, <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_APPLY">DB_TXN_APPLY</a>, <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_BACKWARD_ROLL">DB_TXN_BACKWARD_ROLL</a>,
<a href="../../api_c/env_set_app_dispatch.html#DB_TXN_FORWARD_ROLL">DB_TXN_FORWARD_ROLL</a> or <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_PRINT">DB_TXN_PRINT</a>).
<p><dt>info<dd>A structure passed by the dispatch function. It is used to contain a
list of committed transactions and information about files that may have
been deleted. Application-specific log records can usually simply
ignore this field.
</dl></blockquote>
<p>In addition to the header_file and template_file, a source_file is
created, containing a log, read, recovery, and print function for each
record type.
<p>The log function marshalls the parameters into a buffer, and calls
<a href="../../api_c/log_put.html">DB_ENV->log_put</a> on that buffer returning 0 on success and non-zero on
failure. The log function takes the following parameters:
<p><blockquote><p><dl compact>
<p><dt>dbenv<dd>The environment in which recovery is running.
<p><dt>txnid<dd>The transaction identifier for the transaction handle returned by
<a href="../../api_c/txn_begin.html">DB_ENV->txn_begin</a>.
<p><dt>lsnp<dd>A pointer to storage for a log sequence number into which the log
sequence number of the new log record will be returned.
<p><dt>syncflag<dd>A flag indicating whether the record must be written synchronously.
Valid values are 0 and <a href="../../api_c/log_put.html#DB_FLUSH">DB_FLUSH</a>.
<p><dt>args<dd>The remaining parameters to the log message are the fields described
in the XXX.src file, in order.
</dl></blockquote>
<p>The read function takes a buffer and unmarshalls its contents into a
structure of the appropriate type. It returns 0 on success and non-zero
on error. After the fields of the structure have been used, the pointer
returned from the read function should be freed. The read function
takes the following parameters:
<p><blockquote><p><dl compact>
<p><dt>dbenv<dd>The environment in which recovery is running.
<p><dt>recbuf<dd>A buffer.
<p><dt>argp<dd>A pointer to a structure of the appropriate type.
</dl></blockquote>
<p>The print function displays the contents of the record. The print
function takes the same parameters as the recovery function described
previously. Although some of the parameters are unused by the print
function, taking the same parameters allows a single dispatch loop to
dispatch to a variety of functions. The print function takes the
following parameters:
<p><blockquote><p><dl compact>
<p><dt>dbenv<dd>The environment in which recovery is running.
<p><dt>rec<dd>The record being recovered.
<p><dt>lsn<dd>The log sequence number of the record being recovered.
<p><dt>op<dd>Unused.
<p><dt>info<dd>Unused.
</dl></blockquote>
<p>Finally, the source file will contain a function (named XXX_init_print,
where XXX is replaced by the prefix) which should be added to the
initialization part of the standalone <a href="../../utility/db_printlog.html">db_printlog</a> utility code
so that utility can be used to display application-specific log records.
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/apprec/def.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/apprec/config.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>
</body>
</html>
|