]>
Commit | Line | Data |
---|---|---|
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 | |
46 | static const char sccsid[] = "@(#)db_dispatch.c 10.5 (Sleepycat) 7/2/97"; | |
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" | |
64 | ||
65 | /* | |
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. | |
71 | */ | |
72 | static int (**dispatch_table) __P((DB_LOG *, DBT *, DB_LSN *, int, void *)); | |
73 | static u_int32_t dispatch_size = 0; | |
74 | ||
75 | /* | |
76 | * __db_dispatch -- | |
77 | * | |
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. | |
83 | * | |
84 | * PUBLIC: int __db_dispatch __P((DB_LOG *, DBT *, DB_LSN *, int, void *)); | |
85 | */ | |
86 | int | |
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). */ | |
92 | void *info; | |
93 | { | |
94 | u_int32_t rectype, txnid; | |
95 | ||
96 | memcpy(&rectype, db->data, sizeof(rectype)); | |
97 | memcpy(&txnid, (u_int8_t *)db->data + sizeof(rectype), sizeof(txnid)); | |
98 | ||
99 | switch (redo) { | |
100 | case TXN_REDO: | |
101 | case TXN_UNDO: | |
102 | return ((dispatch_table[rectype])(logp, db, lsnp, redo, info)); | |
103 | case TXN_OPENFILES: | |
104 | if (rectype < DB_txn_BEGIN ) | |
105 | return ((dispatch_table[rectype])(logp, | |
106 | db, lsnp, redo, info)); | |
107 | break; | |
108 | case TXN_BACKWARD_ROLL: | |
109 | /* | |
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 | |
114 | * in "abort mode". | |
115 | */ | |
116 | if (__db_txnlist_find(info, txnid) == DB_NOTFOUND) | |
117 | return ((dispatch_table[rectype])(logp, | |
118 | db, lsnp, TXN_UNDO, info)); | |
119 | break; | |
120 | case TXN_FORWARD_ROLL: | |
121 | /* | |
122 | * In the forward pass, if we haven't seen the transaction, | |
123 | * do nothing, else recovery it. | |
124 | */ | |
125 | if (__db_txnlist_find(info, txnid) != DB_NOTFOUND) | |
126 | return ((dispatch_table[rectype])(logp, | |
127 | db, lsnp, TXN_REDO, info)); | |
128 | break; | |
129 | default: | |
130 | abort(); | |
131 | } | |
132 | return (0); | |
133 | } | |
134 | ||
135 | /* | |
136 | * __db_add_recovery -- | |
137 | * | |
138 | * PUBLIC: int __db_add_recovery __P((DB_ENV *, | |
139 | * PUBLIC: int (*)(DB_LOG *, DBT *, DB_LSN *, int, void *), u_int32_t)); | |
140 | */ | |
141 | int | |
142 | __db_add_recovery(dbenv, func, ndx) | |
143 | DB_ENV *dbenv; | |
144 | int (*func) __P((DB_LOG *, DBT *, DB_LSN *, int, void *)); | |
145 | u_int32_t ndx; | |
146 | { | |
147 | u_int32_t i; | |
148 | ||
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); | |
153 | ||
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])); | |
160 | else | |
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)); | |
167 | return (ENOMEM); | |
168 | } | |
169 | for (i = dispatch_size, | |
170 | dispatch_size += DB_user_BEGIN; i < dispatch_size; ++i) | |
171 | dispatch_table[i] = NULL; | |
172 | } | |
173 | ||
174 | dispatch_table[ndx] = func; | |
175 | return (0); | |
176 | } | |
177 | ||
178 | /* | |
179 | * __db_txnlist_init -- | |
180 | * Initialize transaction linked list. | |
181 | * | |
182 | * PUBLIC: int __db_txnlist_init __P((void *)); | |
183 | */ | |
184 | int | |
185 | __db_txnlist_init(retp) | |
186 | void *retp; | |
187 | { | |
188 | __db_txnhead *headp; | |
189 | ||
190 | if ((headp = | |
191 | (struct __db_txnhead *)malloc(sizeof(struct __db_txnhead))) == NULL) | |
192 | return (ENOMEM); | |
193 | ||
194 | LIST_INIT(&headp->head); | |
195 | headp->maxid = 0; | |
196 | ||
197 | *(void **)retp = headp; | |
198 | return (0); | |
199 | } | |
200 | ||
201 | /* | |
202 | * __db_txnlist_add -- | |
203 | * Add an element to our transaction linked list. | |
204 | * | |
205 | * PUBLIC: int __db_txnlist_add __P((void *, u_int32_t)); | |
206 | */ | |
207 | int | |
208 | __db_txnlist_add(listp, txnid) | |
209 | void *listp; | |
210 | u_int32_t txnid; | |
211 | { | |
212 | __db_txnhead *hp; | |
213 | __db_txnlist *elp; | |
214 | ||
215 | if ((elp = (__db_txnlist *)malloc(sizeof(__db_txnlist))) == NULL) | |
216 | return (ENOMEM); | |
217 | ||
218 | elp->txnid = txnid; | |
219 | hp = (struct __db_txnhead *)listp; | |
220 | LIST_INSERT_HEAD(&hp->head, elp, links); | |
221 | if (txnid > hp->maxid) | |
222 | hp->maxid = txnid; | |
223 | ||
224 | return (0); | |
225 | } | |
226 | ||
227 | /* | |
228 | * __db_txnlist_find -- | |
229 | * Checks to see if txnid is in the txnid list, returns 1 if found, | |
230 | * 0 if not found. | |
231 | * | |
232 | * PUBLIC: int __db_txnlist_find __P((void *, u_int32_t)); | |
233 | */ | |
234 | int | |
235 | __db_txnlist_find(listp, txnid) | |
236 | void *listp; | |
237 | u_int32_t txnid; | |
238 | { | |
239 | __db_txnlist *p; | |
240 | __db_txnhead *hp; | |
241 | ||
242 | if ((hp = (struct __db_txnhead *)listp) == NULL) | |
243 | return (DB_NOTFOUND); | |
244 | ||
245 | if (hp->maxid < txnid) { | |
246 | hp->maxid = txnid; | |
247 | return (DB_NOTFOUND); | |
248 | } | |
249 | ||
250 | for (p = hp->head.lh_first; p != NULL; p = p->links.le_next) | |
251 | if (p->txnid == txnid) | |
252 | return (0); | |
253 | ||
254 | return (DB_NOTFOUND); | |
255 | } | |
256 | ||
257 | #ifdef DEBUG | |
258 | void | |
259 | __db_txnlist_print(listp) | |
260 | void *listp; | |
261 | { | |
262 | __db_txnlist *p; | |
263 | __db_txnhead *hp; | |
264 | ||
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); | |
269 | } | |
270 | #endif |