]>
git.saurik.com Git - apple/libc.git/blob - db.subproj/btree.subproj/bt_overflow.c
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/param.h>
71 * Big key and data entries are stored on linked lists of pages. The initial
72 * reference is byte string stored with the key or data and is the page number
73 * and size. The actual record is stored in a chain of pages linked by the
74 * nextpg field of the PAGE header.
76 * The first page of the chain has a special property. If the record is used
77 * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
81 * A single DBT is written to each chain, so a lot of space on the last page
82 * is wasted. This is a fairly major bug for some data sets.
86 * __OVFL_GET -- Get an overflow key/data item.
90 * p: pointer to { pgno_t, size_t }
91 * buf: storage address
95 * RET_ERROR, RET_SUCCESS
98 __ovfl_get(t
, p
, ssz
, buf
, bufsz
)
109 memmove(&pg
, p
, sizeof(pgno_t
));
110 memmove(&sz
, (char *)p
+ sizeof(pgno_t
), sizeof(size_t));
114 if (pg
== P_INVALID
|| sz
== 0)
117 /* Make the buffer bigger as necessary. */
119 if ((*buf
= (char *)realloc(*buf
, sz
)) == NULL
)
125 * Step through the linked list of pages, copying the data on each one
126 * into the buffer. Never copy more than the data's length.
128 plen
= t
->bt_psize
- BTDATAOFF
;
129 for (p
= *buf
;; p
= (char *)p
+ nb
, pg
= h
->nextpg
) {
130 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
134 memmove(p
, (char *)h
+ BTDATAOFF
, nb
);
135 mpool_put(t
->bt_mp
, h
, 0);
140 return (RET_SUCCESS
);
144 * __OVFL_PUT -- Store an overflow key/data item.
149 * pgno: storage page number
152 * RET_ERROR, RET_SUCCESS
155 __ovfl_put(t
, dbt
, pg
)
166 * Allocate pages and copy the key/data record into them. Store the
167 * number of the first page in the chain.
169 plen
= t
->bt_psize
- BTDATAOFF
;
170 for (last
= NULL
, p
= dbt
->data
, sz
= dbt
->size
;;
171 p
= (char *)p
+ plen
, last
= h
) {
172 if ((h
= __bt_new(t
, &npg
)) == NULL
)
176 h
->nextpg
= h
->prevpg
= P_INVALID
;
177 h
->flags
= P_OVERFLOW
;
178 h
->lower
= h
->upper
= 0;
181 memmove((char *)h
+ BTDATAOFF
, p
, nb
);
184 last
->nextpg
= h
->pgno
;
185 mpool_put(t
->bt_mp
, last
, MPOOL_DIRTY
);
189 if ((sz
-= nb
) == 0) {
190 mpool_put(t
->bt_mp
, h
, MPOOL_DIRTY
);
194 return (RET_SUCCESS
);
198 * __OVFL_DELETE -- Delete an overflow chain.
202 * p: pointer to { pgno_t, size_t }
205 * RET_ERROR, RET_SUCCESS
216 memmove(&pg
, p
, sizeof(pgno_t
));
217 memmove(&sz
, (char *)p
+ sizeof(pgno_t
), sizeof(size_t));
220 if (pg
== P_INVALID
|| sz
== 0)
223 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
226 /* Don't delete chains used by internal pages. */
227 if (h
->flags
& P_PRESERVE
) {
228 mpool_put(t
->bt_mp
, h
, 0);
229 return (RET_SUCCESS
);
232 /* Step through the chain, calling the free routine for each page. */
233 for (plen
= t
->bt_psize
- BTDATAOFF
;; sz
-= plen
) {
238 if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
)
241 return (RET_SUCCESS
);