bt_overflow.c 5.8 KB

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