2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
8 * Copyright (c) 1995, 1996
9 * The President and Fellows of Harvard University. All rights reserved.
11 * This code is derived from software contributed to Berkeley by
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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
46 static const char sccsid[] = "@(#)db_dispatch.c 10.5 (Sleepycat) 7/2/97";
49 #ifndef NO_SYSTEM_INCLUDES
50 #include <sys/types.h>
61 #include "db_dispatch.h"
63 #include "common_ext.h"
66 * Data structures to manage the DB dispatch table. The dispatch table
67 * is a dynamically allocated array of pointers to dispatch functions.
68 * The dispatch_size is the number of entries possible in the current
69 * dispatch table and the dispatch_valid is the number of valid entries
70 * in the dispatch table.
72 static int (**dispatch_table) __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
73 static u_int32_t dispatch_size = 0;
78 * This is the transaction dispatch function used by the db access methods.
79 * It is designed to handle the record format used by all the access
80 * methods (the one automatically generated by the db_{h,log,read}.sh
81 * scripts in the tools directory). An application using a different
82 * recovery paradigm will supply a different dispatch function to txn_open.
84 * PUBLIC: int __db_dispatch __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
87 __db_dispatch(logp, db, lsnp, redo, info)
88 DB_LOG *logp; /* The log file. */
89 DBT *db; /* The log record upon which to dispatch. */
90 DB_LSN *lsnp; /* The lsn of the record being dispatched. */
91 int redo; /* Redo this op (or undo it). */
94 u_int32_t rectype, txnid;
96 memcpy(&rectype, db->data, sizeof(rectype));
97 memcpy(&txnid, (u_int8_t *)db->data + sizeof(rectype), sizeof(txnid));
102 return ((dispatch_table[rectype])(logp, db, lsnp, redo, info));
104 if (rectype < DB_txn_BEGIN )
105 return ((dispatch_table[rectype])(logp,
106 db, lsnp, redo, info));
108 case TXN_BACKWARD_ROLL:
110 * Running full recovery in the backward pass. If we've
111 * seen this txnid before and added to it our commit list,
112 * then we do nothing during this pass. If we've never
113 * seen it, then we call the appropriate recovery routine
116 if (__db_txnlist_find(info, txnid) == DB_NOTFOUND)
117 return ((dispatch_table[rectype])(logp,
118 db, lsnp, TXN_UNDO, info));
120 case TXN_FORWARD_ROLL:
122 * In the forward pass, if we haven't seen the transaction,
123 * do nothing, else recovery it.
125 if (__db_txnlist_find(info, txnid) != DB_NOTFOUND)
126 return ((dispatch_table[rectype])(logp,
127 db, lsnp, TXN_REDO, info));
136 * __db_add_recovery --
138 * PUBLIC: int __db_add_recovery __P((DB_ENV *,
139 * PUBLIC: int (*)(DB_LOG *, DBT *, DB_LSN *, int, void *), u_int32_t));
142 __db_add_recovery(dbenv, func, ndx)
144 int (*func) __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
149 /* Check if function is already registered. */
150 if (dispatch_table && ndx < dispatch_size &&
151 dispatch_table[ndx] != 0 && dispatch_table[ndx] != func)
152 return (DB_REGISTERED);
154 /* Check if we have to grow the table. */
155 if (ndx >= dispatch_size) {
156 if (dispatch_table == NULL)
157 dispatch_table = (int (**)
158 __P((DB_LOG *, DBT *, DB_LSN *, int, void *)))
159 malloc(DB_user_BEGIN * sizeof(dispatch_table[0]));
161 dispatch_table = (int (**)
162 __P((DB_LOG *, DBT *, DB_LSN *, int, void *)))
163 realloc(dispatch_table, (DB_user_BEGIN +
164 dispatch_size) * sizeof(dispatch_table[0]));
165 if (dispatch_table == NULL) {
166 __db_err(dbenv, "%s", strerror(ENOMEM));
169 for (i = dispatch_size,
170 dispatch_size += DB_user_BEGIN; i < dispatch_size; ++i)
171 dispatch_table[i] = NULL;
174 dispatch_table[ndx] = func;
179 * __db_txnlist_init --
180 * Initialize transaction linked list.
182 * PUBLIC: int __db_txnlist_init __P((void *));
185 __db_txnlist_init(retp)
191 (struct __db_txnhead *)malloc(sizeof(struct __db_txnhead))) == NULL)
194 LIST_INIT(&headp->head);
197 *(void **)retp = headp;
202 * __db_txnlist_add --
203 * Add an element to our transaction linked list.
205 * PUBLIC: int __db_txnlist_add __P((void *, u_int32_t));
208 __db_txnlist_add(listp, txnid)
215 if ((elp = (__db_txnlist *)malloc(sizeof(__db_txnlist))) == NULL)
219 hp = (struct __db_txnhead *)listp;
220 LIST_INSERT_HEAD(&hp->head, elp, links);
221 if (txnid > hp->maxid)
228 * __db_txnlist_find --
229 * Checks to see if txnid is in the txnid list, returns 1 if found,
232 * PUBLIC: int __db_txnlist_find __P((void *, u_int32_t));
235 __db_txnlist_find(listp, txnid)
242 if ((hp = (struct __db_txnhead *)listp) == NULL)
243 return (DB_NOTFOUND);
245 if (hp->maxid < txnid) {
247 return (DB_NOTFOUND);
250 for (p = hp->head.lh_first; p != NULL; p = p->links.le_next)
251 if (p->txnid == txnid)
254 return (DB_NOTFOUND);
259 __db_txnlist_print(listp)
265 hp = (struct __db_txnhead *)listp;
266 printf("Maxid: %lu\n", (u_long)hp->maxid);
267 for (p = hp->head.lh_first; p != NULL; p = p->links.le_next)
268 printf("TXNID: %lu\n", (u_long)p->txnid);