]>
git.saurik.com Git - apple/libc.git/blob - db/recno/rec_delete.c
126cd7f11c840011311403a4ea9b860de9402228
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/types.h>
68 static int rec_rdelete
__P((BTREE
*, recno_t
));
71 * __REC_DELETE -- Delete the item(s) referenced by a key.
74 * dbp: pointer to access method
76 * flags: R_CURSOR if deleting what the cursor references
79 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
82 __rec_delete(dbp
, key
, flags
)
93 /* Toss any page pinned across calls. */
94 if (t
->bt_pinned
!= NULL
) {
95 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
101 if ((nrec
= *(recno_t
*)key
->data
) == 0)
103 if (nrec
> t
->bt_nrecs
)
104 return (RET_SPECIAL
);
106 status
= rec_rdelete(t
, nrec
);
109 if (!ISSET(t
, B_SEQINIT
))
111 if (t
->bt_nrecs
== 0)
112 return (RET_SPECIAL
);
113 status
= rec_rdelete(t
, t
->bt_rcursor
- 1);
114 if (status
== RET_SUCCESS
)
118 einval
: errno
= EINVAL
;
122 if (status
== RET_SUCCESS
)
123 SET(t
, B_MODIFIED
| R_MODIFIED
);
128 * REC_RDELETE -- Delete the data matching the specified key.
132 * nrec: record to delete
135 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
146 /* Find the record; __rec_search pins the page. */
147 if ((e
= __rec_search(t
, nrec
, SDELETE
)) == NULL
)
150 /* Delete the record. */
152 status
= __rec_dleaf(t
, h
, e
->index
);
153 if (status
!= RET_SUCCESS
) {
154 mpool_put(t
->bt_mp
, h
, 0);
157 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
158 return (RET_SUCCESS
);
162 * __REC_DLEAF -- Delete a single record from a recno leaf page.
166 * index: index on current page to delete
169 * RET_SUCCESS, RET_ERROR.
172 __rec_dleaf(t
, h
, index
)
178 register indx_t
*ip
, cnt
, offset
;
179 register size_t nbytes
;
184 * Delete a record from a recno leaf page. Internal records are never
185 * deleted from internal pages, regardless of the records that caused
186 * them to be added being deleted. Pages made empty by deletion are
187 * not reclaimed. They are, however, made available for reuse.
189 * Pack the remaining entries at the end of the page, shift the indices
190 * down, overwriting the deleted record and its index. If the record
191 * uses overflow pages, make them available for reuse.
193 to
= rl
= GETRLEAF(h
, index
);
194 if (rl
->flags
& P_BIGDATA
&& __ovfl_delete(t
, rl
->bytes
) == RET_ERROR
)
199 * Compress the key/data pairs. Compress and adjust the [BR]LEAF
200 * offsets. Reset the headers.
202 from
= (char *)h
+ h
->upper
;
203 memmove(from
+ nbytes
, from
, (char *)to
- from
);
206 offset
= h
->linp
[index
];
207 for (cnt
= &h
->linp
[index
] - (ip
= &h
->linp
[0]); cnt
--; ++ip
)
210 for (cnt
= &h
->linp
[NEXTINDEX(h
)] - ip
; --cnt
; ++ip
)
211 ip
[0] = ip
[1] < offset
? ip
[1] + nbytes
: ip
[1];
212 h
->lower
-= sizeof(indx_t
);
214 return (RET_SUCCESS
);