]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_overflow.c
489290e2d81abe88dd9f4db27480e4a4f3970bd8
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid
[] = "@(#)bt_overflow.c 8.5 (Berkeley) 7/16/94";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
66 #include <sys/param.h>
78 * Big key and data entries are stored on linked lists of pages. The initial
79 * reference is byte string stored with the key or data and is the page number
80 * and size. The actual record is stored in a chain of pages linked by the
81 * nextpg field of the PAGE header.
83 * The first page of the chain has a special property. If the record is used
84 * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
88 * A single DBT is written to each chain, so a lot of space on the last page
89 * is wasted. This is a fairly major bug for some data sets.
93 * __OVFL_GET -- Get an overflow key/data item.
97 * p: pointer to { pgno_t, u_int32_t }
98 * buf: storage address
102 * RET_ERROR, RET_SUCCESS
105 __ovfl_get(t
, p
, ssz
, buf
, bufsz
)
117 memmove(&pg
, p
, sizeof(pgno_t
));
118 memmove(&sz
, (char *)p
+ sizeof(pgno_t
), sizeof(u_int32_t
));
122 if (pg
== P_INVALID
|| sz
== 0)
125 /* Make the buffer bigger as necessary. */
127 *buf
= (char *)(*buf
== NULL
? malloc(sz
) : reallocf(*buf
, sz
));
134 * Step through the linked list of pages, copying the data on each one
135 * into the buffer. Never copy more than the data's length.
137 plen
= t
->bt_psize
- BTDATAOFF
;
138 for (p
= *buf
;; p
= (char *)p
+ nb
, pg
= h
->nextpg
) {
139 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
143 memmove(p
, (char *)h
+ BTDATAOFF
, nb
);
144 mpool_put(t
->bt_mp
, h
, 0);
149 return (RET_SUCCESS
);
153 * __OVFL_PUT -- Store an overflow key/data item.
158 * pgno: storage page number
161 * RET_ERROR, RET_SUCCESS
164 __ovfl_put(t
, dbt
, pg
)
176 * Allocate pages and copy the key/data record into them. Store the
177 * number of the first page in the chain.
179 plen
= t
->bt_psize
- BTDATAOFF
;
180 for (last
= NULL
, p
= dbt
->data
, sz
= dbt
->size
;;
181 p
= (char *)p
+ plen
, last
= h
) {
182 if ((h
= __bt_new(t
, &npg
)) == NULL
)
186 h
->nextpg
= h
->prevpg
= P_INVALID
;
187 h
->flags
= P_OVERFLOW
;
188 h
->lower
= h
->upper
= 0;
191 memmove((char *)h
+ BTDATAOFF
, p
, nb
);
194 last
->nextpg
= h
->pgno
;
195 mpool_put(t
->bt_mp
, last
, MPOOL_DIRTY
);
199 if ((sz
-= nb
) == 0) {
200 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
204 return (RET_SUCCESS
);
208 * __OVFL_DELETE -- Delete an overflow chain.
212 * p: pointer to { pgno_t, u_int32_t }
215 * RET_ERROR, RET_SUCCESS
227 memmove(&pg
, p
, sizeof(pgno_t
));
228 memmove(&sz
, (char *)p
+ sizeof(pgno_t
), sizeof(u_int32_t
));
231 if (pg
== P_INVALID
|| sz
== 0)
234 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
237 /* Don't delete chains used by internal pages. */
238 if (h
->flags
& P_PRESERVE
) {
239 mpool_put(t
->bt_mp
, h
, 0);
240 return (RET_SUCCESS
);
243 /* Step through the chain, calling the free routine for each page. */
244 for (plen
= t
->bt_psize
- BTDATAOFF
;; sz
-= plen
) {
249 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
252 return (RET_SUCCESS
);