Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / DB / btree / bt_overflow.c
blobf6b8da1821cc02a97e37208973a5a6529e99d77e
1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
34 * SUCH DAMAGE.
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)bt_overflow.c 5.8 (Berkeley) 2/16/93";
39 #endif /* LIBC_SCCS and not lint */
41 #include <sys/param.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include <db.h>
48 #include "btree.h"
51 * Big key/data code.
53 * Big key and data entries are stored on linked lists of pages. The initial
54 * reference is byte string stored with the key or data and is the page number
55 * and size. The actual record is stored in a chain of pages linked by the
56 * nextpg field of the PAGE header.
58 * The first page of the chain has a special property. If the record is used
59 * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
60 * in the header.
62 * XXX
63 * A single DBT is written to each chain, so a lot of space on the last page
64 * is wasted. This is a fairly major bug for some data sets.
68 * __OVFL_GET -- Get an overflow key/data item.
70 * Parameters:
71 * t: tree
72 * p: pointer to { pgno_t, size_t }
73 * buf: storage address
74 * bufsz: storage size
76 * Returns:
77 * RET_ERROR, RET_SUCCESS
79 int
80 __ovfl_get(t, p, ssz, buf, bufsz)
81 BTREE *t;
82 void *p;
83 size_t *ssz;
84 char **buf;
85 size_t *bufsz;
87 PAGE *h;
88 pgno_t pg;
89 size_t nb, plen, sz;
91 memmove(&pg, p, sizeof(pgno_t));
92 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(size_t));
93 *ssz = sz;
95 #ifdef DEBUG
96 if (pg == P_INVALID || sz == 0)
97 abort();
98 #endif
99 /* Make the buffer bigger as necessary. */
100 if (*bufsz < sz) {
101 if ((*buf = realloc(*buf, sz)) == NULL)
102 return (RET_ERROR);
103 *bufsz = sz;
107 * Step through the linked list of pages, copying the data on each one
108 * into the buffer. Never copy more than the data's length.
110 plen = t->bt_psize - BTDATAOFF;
111 for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
112 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
113 return (RET_ERROR);
115 nb = MIN(sz, plen);
116 memmove(p, (char *)h + BTDATAOFF, nb);
117 mpool_put(t->bt_mp, h, 0);
119 if ((sz -= nb) == 0)
120 break;
122 return (RET_SUCCESS);
126 * __OVFL_PUT -- Store an overflow key/data item.
128 * Parameters:
129 * t: tree
130 * data: DBT to store
131 * pgno: storage page number
133 * Returns:
134 * RET_ERROR, RET_SUCCESS
137 __ovfl_put(t, dbt, pg)
138 BTREE *t;
139 const DBT *dbt;
140 pgno_t *pg;
142 PAGE *h, *last;
143 void *p;
144 pgno_t npg;
145 size_t nb, plen, sz;
148 * Allocate pages and copy the key/data record into them. Store the
149 * number of the first page in the chain.
151 plen = t->bt_psize - BTDATAOFF;
152 for (last = NULL, p = dbt->data, sz = dbt->size;;
153 p = (char *)p + plen, last = h) {
154 if ((h = __bt_new(t, &npg)) == NULL)
155 return (RET_ERROR);
157 h->pgno = npg;
158 h->nextpg = h->prevpg = P_INVALID;
159 h->flags = P_OVERFLOW;
160 h->lower = h->upper = 0;
162 nb = MIN(sz, plen);
163 memmove((char *)h + BTDATAOFF, p, nb);
165 if (last) {
166 last->nextpg = h->pgno;
167 mpool_put(t->bt_mp, last, MPOOL_DIRTY);
168 } else
169 *pg = h->pgno;
171 if ((sz -= nb) == 0) {
172 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
173 break;
176 return (RET_SUCCESS);
180 * __OVFL_DELETE -- Delete an overflow chain.
182 * Parameters:
183 * t: tree
184 * p: pointer to { pgno_t, size_t }
186 * Returns:
187 * RET_ERROR, RET_SUCCESS
190 __ovfl_delete(t, p)
191 BTREE *t;
192 void *p;
194 PAGE *h;
195 pgno_t pg;
196 size_t plen, sz;
198 memmove(&pg, p, sizeof(pgno_t));
199 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(size_t));
201 #ifdef DEBUG
202 if (pg == P_INVALID || sz == 0)
203 abort();
204 #endif
205 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
206 return (RET_ERROR);
208 /* Don't delete chains used by internal pages. */
209 if (h->flags & P_PRESERVE) {
210 mpool_put(t->bt_mp, h, 0);
211 return (RET_SUCCESS);
214 /* Step through the chain, calling the free routine for each page. */
215 for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {
216 pg = h->nextpg;
217 __bt_free(t, h);
218 if (sz <= plen)
219 break;
220 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
221 return (RET_ERROR);
223 return (RET_SUCCESS);