Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / vi_rec.c
blobaad113f53400a5c69212cefe4811a836ad5718ec
1 /* $NetBSD$ */
3 #include "db_config.h"
5 #ifndef NO_SYSTEM_INCLUDES
6 #include <sys/types.h>
8 #include <string.h>
9 #endif
11 #include "common.h"
13 #include "db_int.h"
14 #include "db_page.h"
15 #include <log.h>
16 #include "hash.h"
17 #include "btree.h"
19 #define LOG_CURSOR_HIT -1000
22 * PUBLIC: #ifdef USE_DB4_LOGGING
25 * __vi_marker_recover --
26 * Recovery function for marker.
28 * PUBLIC: int __vi_marker_recover
29 * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
31 int
32 __vi_marker_recover(dbenv, dbtp, lsnp, op, info)
33 DB_ENV *dbenv;
34 DBT *dbtp;
35 DB_LSN *lsnp;
36 db_recops op;
37 void *info;
39 __vi_marker_args *argp;
40 int ret;
42 REC_PRINT(__vi_marker_print);
43 REC_NOOP_INTRO(__vi_marker_read);
45 *lsnp = argp->prev_lsn;
46 ret = 0;
48 REC_NOOP_CLOSE;
52 * __vi_cursor_recover --
53 * Recovery function for cursor.
55 * PUBLIC: int __vi_cursor_recover
56 * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
58 int
59 __vi_cursor_recover(dbenv, dbtp, lsnp, op, info)
60 DB_ENV *dbenv;
61 DBT *dbtp;
62 DB_LSN *lsnp;
63 db_recops op;
64 void *info;
66 __vi_cursor_args *argp;
67 int ret;
68 SCR *sp;
70 REC_PRINT(__vi_cursor_print);
71 REC_NOOP_INTRO(__vi_cursor_read);
73 sp = (SCR *)dbenv->app_private;
75 *lsnp = argp->prev_lsn;
76 if (sp->state.undo == UNDO_SETLINE) {
77 /* Why the check for ep->l_cur ? (copied from log.c)
79 ret = (argp->lno != sp->lno ||
80 (argp->opcode == LOG_CURSOR_INIT && sp->ep->l_cur == 1))
81 ? LOG_CURSOR_HIT : 0;
83 else {
84 ret = argp->opcode ==
85 (DB_UNDO(op) ? LOG_CURSOR_INIT : LOG_CURSOR_END)
86 ? LOG_CURSOR_HIT : 0;
87 if (ret) {
88 sp->state.pos.lno = argp->lno;
89 sp->state.pos.cno = argp->cno;
93 REC_NOOP_CLOSE;
97 * __vi_mark_recover --
98 * Recovery function for mark.
100 * PUBLIC: int __vi_mark_recover
101 * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
104 __vi_mark_recover(dbenv, dbtp, lsnp, op, info)
105 DB_ENV *dbenv;
106 DBT *dbtp;
107 DB_LSN *lsnp;
108 db_recops op;
109 void *info;
111 __vi_mark_args *argp;
112 int ret;
113 MARK m;
114 SCR *sp;
116 REC_PRINT(__vi_mark_print);
117 REC_NOOP_INTRO(__vi_mark_read);
119 sp = (SCR *)dbenv->app_private;
120 *lsnp = argp->prev_lsn;
121 m.lno = argp->lmp.lno;
122 m.cno = argp->lmp.cno;
123 ret = mark_set(sp, argp->lmp.name, &m, 0);
125 REC_NOOP_CLOSE;
129 * __vi_change_recover --
130 * Recovery function for change.
132 * PUBLIC: int __vi_change_recover
133 * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
136 __vi_change_recover(dbenv, dbtp, lsnp, op, info)
137 DB_ENV *dbenv;
138 DBT *dbtp;
139 DB_LSN *lsnp;
140 db_recops op;
141 void *info;
143 __vi_change_args *argp;
144 int ret;
145 SCR *sp;
147 REC_PRINT(__vi_change_print);
148 REC_NOOP_INTRO(__vi_change_read);
150 ret = 0;
152 sp = (SCR *)dbenv->app_private;
153 if (DB_UNDO(op) != (argp->opcode & 1))
154 switch (argp->opcode) {
155 case LOG_LINE_RESET_B:
156 case LOG_LINE_RESET_F:
157 ret = line_insdel(sp, LINE_RESET, argp->lno);
158 update_cache(sp, LINE_RESET, argp->lno);
159 ret = scr_update(sp, argp->lno, LINE_RESET, 1) || ret;
160 break;
161 case LOG_LINE_APPEND_B:
162 case LOG_LINE_DELETE_F:
163 ret = line_insdel(sp, LINE_DELETE, argp->lno);
164 update_cache(sp, LINE_DELETE, argp->lno);
165 ret = scr_update(sp, argp->lno, LINE_DELETE, 1) || ret;
166 break;
167 case LOG_LINE_DELETE_B:
168 case LOG_LINE_APPEND_F:
169 ret = line_insdel(sp, LINE_INSERT, argp->lno);
170 update_cache(sp, LINE_INSERT, argp->lno);
171 ret = scr_update(sp, argp->lno, LINE_INSERT, 1) || ret;
172 break;
175 *lsnp = argp->prev_lsn;
177 REC_NOOP_CLOSE;
182 * PUBLIC: int __vi_log_truncate __P((EXF *ep));
185 __vi_log_truncate(EXF *ep)
187 DB_LSN ckplsn;
189 ZERO_LSN(ckplsn);
190 return __log_vtruncate(ep->env, &ep->lsn_cur, &ckplsn);
191 /*return __log_vtruncate(ep->env, &ep->lsn_cur, &ep->lsn_first);*/
196 * PUBLIC: int __vi_log_dispatch __P((DB_ENV *dbenv, DBT *data, DB_LSN *lsn, db_recops ops));
199 __vi_log_dispatch(DB_ENV *dbenv, DBT *data, DB_LSN *lsn, db_recops ops)
201 u_int32_t rectype;
202 char s[100];
204 memcpy(&rectype, data->data, sizeof(rectype));
205 snprintf(s,100,"%d\n", rectype);
206 return dbenv->dtab[rectype](dbenv, data, lsn, ops, NULL);
209 static int
210 vi_log_get(SCR *sp, DB_LOGC *logc, DBT *data, u_int32_t which)
212 size_t nlen;
213 EXF *ep;
215 ep = sp->ep;
217 nlen = 1024;
218 retry:
219 BINC_GOTO(sp, sp->wp->l_lp, sp->wp->l_len, nlen);
220 memset(data, 0, sizeof(*data));
221 data->data = sp->wp->l_lp;
222 data->ulen = sp->wp->l_len;
223 data->flags = DB_DBT_USERMEM;
224 switch ((sp->db_error = logc->get(logc, &ep->lsn_cur, data, which))) {
225 case ENOMEM:
226 nlen = data->size;
227 goto retry;
228 default:
229 alloc_err:
230 msgq(sp, M_DBERR, "logc->get");
231 F_SET(ep, F_NOLOG);
232 return (1);
233 case 0:
236 return 0;
241 * PUBLIC: int __vi_log_traverse __P((SCR *sp, undo_t undo, MARK *));
244 __vi_log_traverse(SCR *sp, undo_t undo, MARK *rp)
246 DB_LOGC *logc;
247 DBT data;
248 EXF *ep;
249 int ret;
250 DB_LSN lsn;
251 u_int32_t which;
252 db_recops ops;
254 ep = sp->ep;
256 F_SET(ep, F_NOLOG); /* Turn off logging. */
258 sp->state.undo = undo;
259 ep->env->app_private = sp;
260 if ((sp->db_error = ep->env->log_cursor(ep->env, &logc, 0))
261 != 0) {
262 msgq(sp, M_DBERR, "env->log_cursor");
263 return (1);
265 if (vi_log_get(sp, logc, &data, DB_SET))
266 return 1;
267 if (undo == UNDO_FORWARD) {
268 ops = DB_TXN_FORWARD_ROLL;
269 which = DB_NEXT;
270 if (vi_log_get(sp, logc, &data, DB_NEXT))
271 return 1;
272 } else {
273 ops = DB_TXN_BACKWARD_ROLL;
274 which = DB_PREV;
277 for (;;) {
278 MEMCPY(&lsn, &ep->lsn_cur, 1);
279 ret = __vi_log_dispatch(ep->env, &data, &lsn, ops);
280 if (ret != 0) {
281 if (ret == LOG_CURSOR_HIT)
282 break;
285 if (vi_log_get(sp, logc, &data, which))
286 return 1;
287 if (undo == UNDO_SETLINE &&
288 log_compare(&ep->lsn_cur, &ep->lsn_first) <= 0) {
289 /* Move to previous record without dispatching. */
290 undo = UNDO_BACKWARD;
291 break;
294 if (undo == UNDO_BACKWARD)
295 if (vi_log_get(sp, logc, &data, DB_PREV))
296 return 1;
298 logc->close(logc, 0);
300 ep->env->app_private = NULL;
302 MEMMOVE(rp, &sp->state.pos, 1);
304 F_CLR(ep, F_NOLOG);
306 return 0;
310 vi_db_init_recover(DB_ENV *dbenv)
312 int ret;
314 if ((ret = __db_init_recover(dbenv)) != 0)
315 return (ret);
316 if ((ret = __bam_init_recover(dbenv)) != 0)
317 return (ret);
319 return 0;
322 * PUBLIC: #endif