grrr...fix reverse chronological order
[dragonfly.git] / lib / libcr / db / btree / bt_overflow.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
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.
23  *
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.
35  *
36  * @(#)bt_overflow.c    8.5 (Berkeley) 7/16/94
37  */
38
39 #include <sys/param.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <db.h>
46 #include "btree.h"
47
48 /*
49  * Big key/data code.
50  *
51  * Big key and data entries are stored on linked lists of pages.  The initial
52  * reference is byte string stored with the key or data and is the page number
53  * and size.  The actual record is stored in a chain of pages linked by the
54  * nextpg field of the PAGE header.
55  *
56  * The first page of the chain has a special property.  If the record is used
57  * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
58  * in the header.
59  *
60  * XXX
61  * A single DBT is written to each chain, so a lot of space on the last page
62  * is wasted.  This is a fairly major bug for some data sets.
63  */
64
65 /*
66  * __OVFL_GET -- Get an overflow key/data item.
67  *
68  * Parameters:
69  *      t:      tree
70  *      p:      pointer to { pgno_t, u_int32_t }
71  *      buf:    storage address
72  *      bufsz:  storage size
73  *
74  * Returns:
75  *      RET_ERROR, RET_SUCCESS
76  */
77 int
78 __ovfl_get(t, p, ssz, buf, bufsz)
79         BTREE *t;
80         void *p;
81         size_t *ssz;
82         void **buf;
83         size_t *bufsz;
84 {
85         PAGE *h;
86         pgno_t pg;
87         size_t nb, plen;
88         u_int32_t sz;
89
90         memmove(&pg, p, sizeof(pgno_t));
91         memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));
92         *ssz = sz;
93
94 #ifdef DEBUG
95         if (pg == P_INVALID || sz == 0)
96                 abort();
97 #endif
98         /* Make the buffer bigger as necessary. */
99         if (*bufsz < sz) {
100                 *buf = (char *)(*buf == NULL ? malloc(sz) : reallocf(*buf, sz));
101                 if (*buf == NULL)
102                         return (RET_ERROR);
103                 *bufsz = sz;
104         }
105
106         /*
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.
109          */
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);
114
115                 nb = MIN(sz, plen);
116                 memmove(p, (char *)h + BTDATAOFF, nb);
117                 mpool_put(t->bt_mp, h, 0);
118
119                 if ((sz -= nb) == 0)
120                         break;
121         }
122         return (RET_SUCCESS);
123 }
124
125 /*
126  * __OVFL_PUT -- Store an overflow key/data item.
127  *
128  * Parameters:
129  *      t:      tree
130  *      data:   DBT to store
131  *      pgno:   storage page number
132  *
133  * Returns:
134  *      RET_ERROR, RET_SUCCESS
135  */
136 int
137 __ovfl_put(t, dbt, pg)
138         BTREE *t;
139         const DBT *dbt;
140         pgno_t *pg;
141 {
142         PAGE *h, *last;
143         void *p;
144         pgno_t npg;
145         size_t nb, plen;
146         u_int32_t sz;
147
148         /*
149          * Allocate pages and copy the key/data record into them.  Store the
150          * number of the first page in the chain.
151          */
152         plen = t->bt_psize - BTDATAOFF;
153         for (last = NULL, p = dbt->data, sz = dbt->size;;
154             p = (char *)p + plen, last = h) {
155                 if ((h = __bt_new(t, &npg)) == NULL)
156                         return (RET_ERROR);
157
158                 h->pgno = npg;
159                 h->nextpg = h->prevpg = P_INVALID;
160                 h->flags = P_OVERFLOW;
161                 h->lower = h->upper = 0;
162
163                 nb = MIN(sz, plen);
164                 memmove((char *)h + BTDATAOFF, p, nb);
165
166                 if (last) {
167                         last->nextpg = h->pgno;
168                         mpool_put(t->bt_mp, last, MPOOL_DIRTY);
169                 } else
170                         *pg = h->pgno;
171
172                 if ((sz -= nb) == 0) {
173                         mpool_put(t->bt_mp, h, MPOOL_DIRTY);
174                         break;
175                 }
176         }
177         return (RET_SUCCESS);
178 }
179
180 /*
181  * __OVFL_DELETE -- Delete an overflow chain.
182  *
183  * Parameters:
184  *      t:      tree
185  *      p:      pointer to { pgno_t, u_int32_t }
186  *
187  * Returns:
188  *      RET_ERROR, RET_SUCCESS
189  */
190 int
191 __ovfl_delete(t, p)
192         BTREE *t;
193         void *p;
194 {
195         PAGE *h;
196         pgno_t pg;
197         size_t plen;
198         u_int32_t sz;
199
200         memmove(&pg, p, sizeof(pgno_t));
201         memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));
202
203 #ifdef DEBUG
204         if (pg == P_INVALID || sz == 0)
205                 abort();
206 #endif
207         if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
208                 return (RET_ERROR);
209
210         /* Don't delete chains used by internal pages. */
211         if (h->flags & P_PRESERVE) {
212                 mpool_put(t->bt_mp, h, 0);
213                 return (RET_SUCCESS);
214         }
215
216         /* Step through the chain, calling the free routine for each page. */
217         for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {
218                 pg = h->nextpg;
219                 __bt_free(t, h);
220                 if (sz <= plen)
221                         break;
222                 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
223                         return (RET_ERROR);
224         }
225         return (RET_SUCCESS);
226 }