2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)updutils.c 5.2 (Berkeley) 2/22/91";
39 #endif /* LIBC_SCCS and not lint */
41 #include <sys/types.h>
48 * _BT_FIXSCAN -- Adjust a scan to cope with a change in tree structure.
50 * If the user has an active scan on the database, and we delete an
51 * item from the page the cursor is pointing at, we need to figure
52 * out what to do about it. Basically, the solution is to point
53 * "between" keys in the tree, using the CRSR_BEFORE flag. The
54 * requirement is that the user should not miss any data in the
55 * tree during a scan, just because he happened to do some deletions
56 * or insertions while it was active.
58 * In order to guarantee that the scan progresses properly, we need
59 * to save the key of any deleted item we were pointing at, so that
60 * we can check later insertions against it.
64 * index -- index of item at which change was made
65 * newd -- new datum (for insertions only)
66 * op -- operation (DELETE or INSERT) causing change
69 * RET_SUCCESS, RET_ERROR (errno is set).
76 _bt_fixscan(t
, index
, newd
, op
)
88 if (index
< c
->c_index
)
90 else if (index
== c
->c_index
) {
91 if (!(c
->c_flags
& CRSR_BEFORE
)) {
92 if (_bt_crsrkey(t
) == RET_ERROR
)
94 c
->c_flags
|= CRSR_BEFORE
;
99 * If we previously deleted the object at this location,
100 * and now we're inserting a new one, we need to do the
101 * right thing -- the cursor should come either before
102 * or after the new item, depending on the key that was
103 * here, and the new one.
106 if (c
->c_flags
& CRSR_BEFORE
) {
107 if (index
<= c
->c_index
) {
113 d
= (DATUM
*) GETDATUM(t
->bt_curpage
, index
);
114 if (d
->d_flags
& D_BIGKEY
) {
115 bcopy(&(newd
->d_bytes
[0]),
118 if (_bt_getbig(t
, chain
, &tmp
, &itmp
)
122 tmp
= &(newd
->d_bytes
[0]);
124 r
= (*(t
->bt_compare
))(tmp
, c
->c_key
);
128 if (d
->d_flags
& D_BIGKEY
)
131 } else if (index
<= c
->c_index
)
134 return (RET_SUCCESS
);
138 * _BT_CRSRKEY -- Save a copy of the key of the record that the cursor
139 * is pointing to. The record is about to be deleted.
145 * RET_SUCCESS, RET_ERROR.
148 * Allocates memory for the copy which should be released when
149 * it is no longer needed.
162 d
= (DATUM
*) GETDATUM(t
->bt_curpage
, c
->c_index
);
164 if (d
->d_flags
& D_BIGKEY
) {
165 bcopy(&(d
->d_bytes
[0]), (char *) &pgno
, sizeof(pgno
));
166 return (_bt_getbig(t
, pgno
, &(c
->c_key
), &ignore
));
168 if ((c
->c_key
= (char *) malloc(d
->d_ksize
)) == (char *) NULL
)
171 bcopy(&(d
->d_bytes
[0]), c
->c_key
, (size_t) (d
->d_ksize
));
173 return (RET_SUCCESS
);