]> git.ipfire.org Git - thirdparty/glibc.git/blame - db2/db/db_dispatch.c
Update.
[thirdparty/glibc.git] / db2 / db / db_dispatch.c
CommitLineData
92f1da4d
UD
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 */
7/*
8 * Copyright (c) 1995, 1996
9 * The President and Fellows of Harvard University. All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * Margo Seltzer.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43#include "config.h"
44
45#ifndef lint
af69217f 46static const char sccsid[] = "@(#)db_dispatch.c 10.9 (Sleepycat) 1/17/98";
92f1da4d
UD
47#endif /* not lint */
48
49#ifndef NO_SYSTEM_INCLUDES
50#include <sys/types.h>
51
52#include <errno.h>
53#include <fcntl.h>
54#include <stddef.h>
55#include <stdlib.h>
56#include <string.h>
57#endif
58
59#include "db_int.h"
60#include "db_page.h"
61#include "db_dispatch.h"
62#include "db_am.h"
63#include "common_ext.h"
af69217f 64#include "log_auto.h"
92f1da4d
UD
65
66/*
67 * Data structures to manage the DB dispatch table. The dispatch table
68 * is a dynamically allocated array of pointers to dispatch functions.
69 * The dispatch_size is the number of entries possible in the current
70 * dispatch table and the dispatch_valid is the number of valid entries
71 * in the dispatch table.
72 */
73static int (**dispatch_table) __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
74static u_int32_t dispatch_size = 0;
75
76/*
77 * __db_dispatch --
78 *
79 * This is the transaction dispatch function used by the db access methods.
80 * It is designed to handle the record format used by all the access
81 * methods (the one automatically generated by the db_{h,log,read}.sh
82 * scripts in the tools directory). An application using a different
83 * recovery paradigm will supply a different dispatch function to txn_open.
84 *
85 * PUBLIC: int __db_dispatch __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
86 */
87int
88__db_dispatch(logp, db, lsnp, redo, info)
89 DB_LOG *logp; /* The log file. */
90 DBT *db; /* The log record upon which to dispatch. */
91 DB_LSN *lsnp; /* The lsn of the record being dispatched. */
92 int redo; /* Redo this op (or undo it). */
93 void *info;
94{
95 u_int32_t rectype, txnid;
96
97 memcpy(&rectype, db->data, sizeof(rectype));
98 memcpy(&txnid, (u_int8_t *)db->data + sizeof(rectype), sizeof(txnid));
99
100 switch (redo) {
101 case TXN_REDO:
102 case TXN_UNDO:
103 return ((dispatch_table[rectype])(logp, db, lsnp, redo, info));
104 case TXN_OPENFILES:
105 if (rectype < DB_txn_BEGIN )
106 return ((dispatch_table[rectype])(logp,
107 db, lsnp, redo, info));
108 break;
109 case TXN_BACKWARD_ROLL:
110 /*
111 * Running full recovery in the backward pass. If we've
112 * seen this txnid before and added to it our commit list,
113 * then we do nothing during this pass. If we've never
114 * seen it, then we call the appropriate recovery routine
115 * in "abort mode".
116 */
af69217f
UD
117 if (rectype == DB_log_register ||
118 __db_txnlist_find(info, txnid) == DB_NOTFOUND)
92f1da4d
UD
119 return ((dispatch_table[rectype])(logp,
120 db, lsnp, TXN_UNDO, info));
121 break;
122 case TXN_FORWARD_ROLL:
123 /*
124 * In the forward pass, if we haven't seen the transaction,
125 * do nothing, else recovery it.
126 */
af69217f
UD
127 if (rectype == DB_log_register ||
128 __db_txnlist_find(info, txnid) != DB_NOTFOUND)
92f1da4d
UD
129 return ((dispatch_table[rectype])(logp,
130 db, lsnp, TXN_REDO, info));
131 break;
132 default:
133 abort();
134 }
135 return (0);
136}
137
138/*
139 * __db_add_recovery --
140 *
141 * PUBLIC: int __db_add_recovery __P((DB_ENV *,
142 * PUBLIC: int (*)(DB_LOG *, DBT *, DB_LSN *, int, void *), u_int32_t));
143 */
144int
145__db_add_recovery(dbenv, func, ndx)
146 DB_ENV *dbenv;
147 int (*func) __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
148 u_int32_t ndx;
149{
150 u_int32_t i;
151
152 /* Check if function is already registered. */
153 if (dispatch_table && ndx < dispatch_size &&
154 dispatch_table[ndx] != 0 && dispatch_table[ndx] != func)
155 return (DB_REGISTERED);
156
157 /* Check if we have to grow the table. */
158 if (ndx >= dispatch_size) {
159 if (dispatch_table == NULL)
160 dispatch_table = (int (**)
cc3fa755
UD
161 __P((DB_LOG *, DBT *, DB_LSN *, int, void *)))
162 __db_malloc(DB_user_BEGIN * sizeof(dispatch_table[0]));
92f1da4d
UD
163 else
164 dispatch_table = (int (**)
165 __P((DB_LOG *, DBT *, DB_LSN *, int, void *)))
cc3fa755 166 __db_realloc(dispatch_table, (DB_user_BEGIN +
92f1da4d
UD
167 dispatch_size) * sizeof(dispatch_table[0]));
168 if (dispatch_table == NULL) {
169 __db_err(dbenv, "%s", strerror(ENOMEM));
170 return (ENOMEM);
171 }
172 for (i = dispatch_size,
173 dispatch_size += DB_user_BEGIN; i < dispatch_size; ++i)
174 dispatch_table[i] = NULL;
175 }
176
177 dispatch_table[ndx] = func;
178 return (0);
179}
180
181/*
182 * __db_txnlist_init --
183 * Initialize transaction linked list.
184 *
185 * PUBLIC: int __db_txnlist_init __P((void *));
186 */
187int
188__db_txnlist_init(retp)
189 void *retp;
190{
191 __db_txnhead *headp;
192
cc3fa755
UD
193 if ((headp = (struct __db_txnhead *)
194 __db_malloc(sizeof(struct __db_txnhead))) == NULL)
92f1da4d
UD
195 return (ENOMEM);
196
197 LIST_INIT(&headp->head);
198 headp->maxid = 0;
199
200 *(void **)retp = headp;
201 return (0);
202}
203
204/*
205 * __db_txnlist_add --
206 * Add an element to our transaction linked list.
207 *
208 * PUBLIC: int __db_txnlist_add __P((void *, u_int32_t));
209 */
210int
211__db_txnlist_add(listp, txnid)
212 void *listp;
213 u_int32_t txnid;
214{
215 __db_txnhead *hp;
216 __db_txnlist *elp;
217
cc3fa755 218 if ((elp = (__db_txnlist *)__db_malloc(sizeof(__db_txnlist))) == NULL)
92f1da4d
UD
219 return (ENOMEM);
220
221 elp->txnid = txnid;
222 hp = (struct __db_txnhead *)listp;
223 LIST_INSERT_HEAD(&hp->head, elp, links);
224 if (txnid > hp->maxid)
225 hp->maxid = txnid;
226
227 return (0);
228}
229
230/*
231 * __db_txnlist_find --
232 * Checks to see if txnid is in the txnid list, returns 1 if found,
233 * 0 if not found.
234 *
235 * PUBLIC: int __db_txnlist_find __P((void *, u_int32_t));
236 */
237int
238__db_txnlist_find(listp, txnid)
239 void *listp;
240 u_int32_t txnid;
241{
92f1da4d 242 __db_txnhead *hp;
04be94a8 243 __db_txnlist *p;
92f1da4d
UD
244
245 if ((hp = (struct __db_txnhead *)listp) == NULL)
246 return (DB_NOTFOUND);
247
248 if (hp->maxid < txnid) {
249 hp->maxid = txnid;
250 return (DB_NOTFOUND);
251 }
252
253 for (p = hp->head.lh_first; p != NULL; p = p->links.le_next)
254 if (p->txnid == txnid)
255 return (0);
256
257 return (DB_NOTFOUND);
258}
259
260#ifdef DEBUG
04be94a8
UD
261/*
262 * __db_txnlist_print --
263 * Print out the transaction list.
af69217f
UD
264 *
265 * PUBLIC: void __db_txnlist_print __P((void *));
04be94a8 266 */
92f1da4d
UD
267void
268__db_txnlist_print(listp)
269 void *listp;
270{
92f1da4d 271 __db_txnhead *hp;
04be94a8 272 __db_txnlist *p;
92f1da4d
UD
273
274 hp = (struct __db_txnhead *)listp;
275 printf("Maxid: %lu\n", (u_long)hp->maxid);
276 for (p = hp->head.lh_first; p != NULL; p = p->links.le_next)
277 printf("TXNID: %lu\n", (u_long)p->txnid);
278}
279#endif
04be94a8
UD
280
281/*
282 * __db_txnlist_end --
283 * Discard transaction linked list.
284 *
285 * PUBLIC: void __db_txnlist_end __P((void *));
286 */
287void
288__db_txnlist_end(listp)
289 void *listp;
290{
291 __db_txnhead *hp;
292 __db_txnlist *p;
293
294 hp = (struct __db_txnhead *)listp;
295 while ((p = LIST_FIRST(&hp->head)) != LIST_END(&hp->head)) {
296 LIST_REMOVE(p, links);
297 __db_free(p);
298 }
299 __db_free(listp);
300}