8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / db / btree / bt_curadj.c
blob0e8fb00aef6432cf19a73605a7d8056b05ebe412
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #pragma ident "%Z%%M% %I% %E% SMI"
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "@(#)bt_curadj.c 10.69 (Sleepycat) 12/2/98";
14 #endif /* not lint */
16 #ifndef NO_SYSTEM_INCLUDES
17 #include <sys/types.h>
19 #include <stdlib.h>
20 #endif
22 #include "db_int.h"
23 #include "db_page.h"
24 #include "btree.h"
26 #ifdef DEBUG
28 * __bam_cprint --
29 * Display the current cursor list.
31 * PUBLIC: int __bam_cprint __P((DB *));
33 int
34 __bam_cprint(dbp)
35 DB *dbp;
37 CURSOR *cp;
38 DBC *dbc;
40 DB_THREAD_LOCK(dbp);
41 for (dbc = TAILQ_FIRST(&dbp->active_queue);
42 dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
43 cp = (CURSOR *)dbc->internal;
44 fprintf(stderr,
45 "%#0x->%#0x: page: %lu index: %lu dpage %lu dindex: %lu recno: %lu",
46 (u_int)dbc, (u_int)cp, (u_long)cp->pgno, (u_long)cp->indx,
47 (u_long)cp->dpgno, (u_long)cp->dindx, (u_long)cp->recno);
48 if (F_ISSET(cp, C_DELETED))
49 fprintf(stderr, " (deleted)");
50 fprintf(stderr, "\n");
52 DB_THREAD_UNLOCK(dbp);
54 return (0);
56 #endif /* DEBUG */
59 * __bam_ca_delete --
60 * Update the cursors when items are deleted and when already deleted
61 * items are overwritten. Return the number of relevant cursors found.
63 * PUBLIC: int __bam_ca_delete __P((DB *, db_pgno_t, u_int32_t, int));
65 int
66 __bam_ca_delete(dbp, pgno, indx, delete)
67 DB *dbp;
68 db_pgno_t pgno;
69 u_int32_t indx;
70 int delete;
72 DBC *dbc;
73 CURSOR *cp;
74 int count; /* !!!: Has to contain max number of cursors. */
76 /* Recno is responsible for its own adjustments. */
77 if (dbp->type == DB_RECNO)
78 return (0);
81 * Adjust the cursors. We don't have to review the cursors for any
82 * thread of control other than the current one, because we have the
83 * page write locked at this point, and any other thread of control
84 * had better be using a different locker ID, meaning only cursors in
85 * our thread of control can be on the page.
87 * It's possible for multiple cursors within the thread to have write
88 * locks on the same page, but, cursors within a thread must be single
89 * threaded, so all we're locking here is the cursor linked list.
91 DB_THREAD_LOCK(dbp);
92 for (count = 0, dbc = TAILQ_FIRST(&dbp->active_queue);
93 dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
94 cp = (CURSOR *)dbc->internal;
96 if ((cp->pgno == pgno && cp->indx == indx) ||
97 (cp->dpgno == pgno && cp->dindx == indx)) {
98 if (delete)
99 F_SET(cp, C_DELETED);
100 else
101 F_CLR(cp, C_DELETED);
102 ++count;
105 DB_THREAD_UNLOCK(dbp);
107 return (count);
111 * __bam_ca_di --
112 * Adjust the cursors during a delete or insert.
114 * PUBLIC: void __bam_ca_di __P((DB *, db_pgno_t, u_int32_t, int));
116 void
117 __bam_ca_di(dbp, pgno, indx, adjust)
118 DB *dbp;
119 db_pgno_t pgno;
120 u_int32_t indx;
121 int adjust;
123 CURSOR *cp;
124 DBC *dbc;
126 /* Recno is responsible for its own adjustments. */
127 if (dbp->type == DB_RECNO)
128 return;
131 * Adjust the cursors. See the comment in __bam_ca_delete().
133 DB_THREAD_LOCK(dbp);
134 for (dbc = TAILQ_FIRST(&dbp->active_queue);
135 dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
136 cp = (CURSOR *)dbc->internal;
137 if (cp->pgno == pgno && cp->indx >= indx)
138 cp->indx += adjust;
139 if (cp->dpgno == pgno && cp->dindx >= indx)
140 cp->dindx += adjust;
142 DB_THREAD_UNLOCK(dbp);
146 * __bam_ca_dup --
147 * Adjust the cursors when moving items from a leaf page to a duplicates
148 * page.
150 * PUBLIC: void __bam_ca_dup __P((DB *,
151 * PUBLIC: db_pgno_t, u_int32_t, u_int32_t, db_pgno_t, u_int32_t));
153 void
154 __bam_ca_dup(dbp, fpgno, first, fi, tpgno, ti)
155 DB *dbp;
156 db_pgno_t fpgno, tpgno;
157 u_int32_t first, fi, ti;
159 CURSOR *cp;
160 DBC *dbc;
162 /* Recno is responsible for its own adjustments. */
163 if (dbp->type == DB_RECNO)
164 return;
167 * Adjust the cursors. See the comment in __bam_ca_delete().
169 DB_THREAD_LOCK(dbp);
170 for (dbc = TAILQ_FIRST(&dbp->active_queue);
171 dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
172 cp = (CURSOR *)dbc->internal;
174 * Ignore matching entries that have already been moved,
175 * we move from the same location on the leaf page more
176 * than once.
178 if (cp->dpgno == PGNO_INVALID &&
179 cp->pgno == fpgno && cp->indx == fi) {
180 cp->indx = first;
181 cp->dpgno = tpgno;
182 cp->dindx = ti;
185 DB_THREAD_UNLOCK(dbp);
189 * __bam_ca_rsplit --
190 * Adjust the cursors when doing reverse splits.
192 * PUBLIC: void __bam_ca_rsplit __P((DB *, db_pgno_t, db_pgno_t));
194 void
195 __bam_ca_rsplit(dbp, fpgno, tpgno)
196 DB *dbp;
197 db_pgno_t fpgno, tpgno;
199 CURSOR *cp;
200 DBC *dbc;
202 /* Recno is responsible for its own adjustments. */
203 if (dbp->type == DB_RECNO)
204 return;
207 * Adjust the cursors. See the comment in __bam_ca_delete().
209 DB_THREAD_LOCK(dbp);
210 for (dbc = TAILQ_FIRST(&dbp->active_queue);
211 dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
212 cp = (CURSOR *)dbc->internal;
213 if (cp->pgno == fpgno)
214 cp->pgno = tpgno;
216 DB_THREAD_UNLOCK(dbp);
220 * __bam_ca_split --
221 * Adjust the cursors when splitting a page.
223 * PUBLIC: void __bam_ca_split __P((DB *,
224 * PUBLIC: db_pgno_t, db_pgno_t, db_pgno_t, u_int32_t, int));
226 void
227 __bam_ca_split(dbp, ppgno, lpgno, rpgno, split_indx, cleft)
228 DB *dbp;
229 db_pgno_t ppgno, lpgno, rpgno;
230 u_int32_t split_indx;
231 int cleft;
233 DBC *dbc;
234 CURSOR *cp;
236 /* Recno is responsible for its own adjustments. */
237 if (dbp->type == DB_RECNO)
238 return;
241 * Adjust the cursors. See the comment in __bam_ca_delete().
243 * If splitting the page that a cursor was on, the cursor has to be
244 * adjusted to point to the same record as before the split. Most
245 * of the time we don't adjust pointers to the left page, because
246 * we're going to copy its contents back over the original page. If
247 * the cursor is on the right page, it is decremented by the number of
248 * records split to the left page.
250 DB_THREAD_LOCK(dbp);
251 for (dbc = TAILQ_FIRST(&dbp->active_queue);
252 dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
253 cp = (CURSOR *)dbc->internal;
254 if (cp->pgno == ppgno)
255 if (cp->indx < split_indx) {
256 if (cleft)
257 cp->pgno = lpgno;
258 } else {
259 cp->pgno = rpgno;
260 cp->indx -= split_indx;
262 if (cp->dpgno == ppgno)
263 if (cp->dindx < split_indx) {
264 if (cleft)
265 cp->dpgno = lpgno;
266 } else {
267 cp->dpgno = rpgno;
268 cp->dindx -= split_indx;
271 DB_THREAD_UNLOCK(dbp);