]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_overflow.c
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid
[] = "@(#)bt_overflow.c 8.5 (Berkeley) 7/16/94";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
64 #include <sys/param.h>
76 * Big key and data entries are stored on linked lists of pages. The initial
77 * reference is byte string stored with the key or data and is the page number
78 * and size. The actual record is stored in a chain of pages linked by the
79 * nextpg field of the PAGE header.
81 * The first page of the chain has a special property. If the record is used
82 * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
86 * A single DBT is written to each chain, so a lot of space on the last page
87 * is wasted. This is a fairly major bug for some data sets.
91 * __OVFL_GET -- Get an overflow key/data item.
95 * p: pointer to { pgno_t, u_int32_t }
96 * buf: storage address
100 * RET_ERROR, RET_SUCCESS
103 __ovfl_get(t
, p
, ssz
, buf
, bufsz
)
115 memmove(&pg
, p
, sizeof(pgno_t
));
116 memmove(&sz
, (char *)p
+ sizeof(pgno_t
), sizeof(u_int32_t
));
120 if (pg
== P_INVALID
|| sz
== 0)
123 /* Make the buffer bigger as necessary. */
125 *buf
= (char *)(*buf
== NULL
? malloc(sz
) : reallocf(*buf
, sz
));
132 * Step through the linked list of pages, copying the data on each one
133 * into the buffer. Never copy more than the data's length.
135 plen
= t
->bt_psize
- BTDATAOFF
;
136 for (p
= *buf
;; p
= (char *)p
+ nb
, pg
= h
->nextpg
) {
137 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
141 memmove(p
, (char *)h
+ BTDATAOFF
, nb
);
142 mpool_put(t
->bt_mp
, h
, 0);
147 return (RET_SUCCESS
);
151 * __OVFL_PUT -- Store an overflow key/data item.
156 * pgno: storage page number
159 * RET_ERROR, RET_SUCCESS
162 __ovfl_put(t
, dbt
, pg
)
174 * Allocate pages and copy the key/data record into them. Store the
175 * number of the first page in the chain.
177 plen
= t
->bt_psize
- BTDATAOFF
;
178 for (last
= NULL
, p
= dbt
->data
, sz
= dbt
->size
;;
179 p
= (char *)p
+ plen
, last
= h
) {
180 if ((h
= __bt_new(t
, &npg
)) == NULL
)
184 h
->nextpg
= h
->prevpg
= P_INVALID
;
185 h
->flags
= P_OVERFLOW
;
186 h
->lower
= h
->upper
= 0;
189 memmove((char *)h
+ BTDATAOFF
, p
, nb
);
192 last
->nextpg
= h
->pgno
;
193 mpool_put(t
->bt_mp
, last
, MPOOL_DIRTY
);
197 if ((sz
-= nb
) == 0) {
198 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
202 return (RET_SUCCESS
);
206 * __OVFL_DELETE -- Delete an overflow chain.
210 * p: pointer to { pgno_t, u_int32_t }
213 * RET_ERROR, RET_SUCCESS
225 memmove(&pg
, p
, sizeof(pgno_t
));
226 memmove(&sz
, (char *)p
+ sizeof(pgno_t
), sizeof(u_int32_t
));
229 if (pg
== P_INVALID
|| sz
== 0)
232 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
235 /* Don't delete chains used by internal pages. */
236 if (h
->flags
& P_PRESERVE
) {
237 mpool_put(t
->bt_mp
, h
, 0);
238 return (RET_SUCCESS
);
241 /* Step through the chain, calling the free routine for each page. */
242 for (plen
= t
->bt_psize
- BTDATAOFF
;; sz
-= plen
) {
247 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
250 return (RET_SUCCESS
);