Asterisk - The Open Source Telephony Project  18.5.0
Functions
bt_split.c File Reference
#include <sys/types.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/db.h"
#include "btree.h"
Include dependency graph for bt_split.c:

Go to the source code of this file.

Functions

int __bt_split (BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags, size_t ilen, u_int32_t argskip)
 
static int bt_broot __P ((BTREE *, PAGE *, PAGE *, PAGE *))
 
static PAGE *bt_page __P ((BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t))
 
static int bt_preserve __P ((BTREE *, pgno_t))
 
static PAGE *bt_psplit __P ((BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t))
 
static recno_t rec_total __P ((PAGE *))
 
static int bt_broot (BTREE *t, PAGE *h, PAGE *l, PAGE *r)
 
static PAGEbt_page (BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
 
static int bt_preserve (BTREE *t, pgno_t pg)
 
static PAGEbt_psplit (BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
 
static PAGEbt_root (BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
 
static int bt_rroot (BTREE *t, PAGE *h, PAGE *l, PAGE *r)
 
static recno_t rec_total (PAGE *h)
 

Function Documentation

◆ __bt_split()

int __bt_split ( BTREE t,
PAGE sp,
const DBT key,
const DBT data,
int  flags,
size_t  ilen,
u_int32_t  argskip 
)

Definition at line 82 of file bt_split.c.

References __dbpanic(), a, b, bt_broot(), _btree::bt_dbp, _btree::bt_mp, bt_page(), BT_POP, bt_preserve(), bt_root(), bt_rroot(), _bleaf::bytes, DBT::data, F_ISSET, _page::flags, _bleaf::flags, GETBINTERNAL, GETBLEAF, if(), _epgno::index, _binternal::ksize, _bleaf::ksize, _page::linp, _page::lower, MPOOL_DIRTY, mpool_get(), mpool_put(), NBINTERNAL, NEXTINDEX, NRINTERNAL, NULL, P_BIGKEY, P_BINTERNAL, P_BLEAF, P_INVALID, P_RINTERNAL, P_RLEAF, P_ROOT, P_TYPE, _page::pgno, _epgno::pgno, _page::prevpg, R_RECNO, rec_total(), RET_ERROR, RET_SUCCESS, DBT::size, _page::upper, WR_BINTERNAL, WR_BLEAF, and WR_RLEAF.

Referenced by __bt_put(), and __rec_iput().

89 {
90  BINTERNAL *bi = 0;
91  BLEAF *bl = 0, *tbl;
92  DBT a, b;
93  EPGNO *parent;
94  PAGE *h, *l, *r, *lchild, *rchild;
95  indx_t nxtindex;
96  u_int16_t skip;
97  u_int32_t n, nbytes, nksize = 0;
98  int parentsplit;
99  char *dest;
100 
101  /*
102  * Split the page into two pages, l and r. The split routines return
103  * a pointer to the page into which the key should be inserted and with
104  * skip set to the offset which should be used. Additionally, l and r
105  * are pinned.
106  */
107  skip = argskip;
108  h = sp->pgno == P_ROOT ?
109  bt_root(t, sp, &l, &r, &skip, ilen) :
110  bt_page(t, sp, &l, &r, &skip, ilen);
111  if (h == NULL)
112  return (RET_ERROR);
113 
114  /*
115  * Insert the new key/data pair into the leaf page. (Key inserts
116  * always cause a leaf page to split first.)
117  */
118  h->linp[skip] = h->upper -= ilen;
119  dest = (char *)h + h->upper;
120  if (F_ISSET(t, R_RECNO))
121  WR_RLEAF(dest, data, flags)
122  else
123  WR_BLEAF(dest, key, data, flags)
124 
125  /* If the root page was split, make it look right. */
126  if (sp->pgno == P_ROOT &&
127  (F_ISSET(t, R_RECNO) ?
128  bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
129  goto err2;
130 
131  /*
132  * Now we walk the parent page stack -- a LIFO stack of the pages that
133  * were traversed when we searched for the page that split. Each stack
134  * entry is a page number and a page index offset. The offset is for
135  * the page traversed on the search. We've just split a page, so we
136  * have to insert a new key into the parent page.
137  *
138  * If the insert into the parent page causes it to split, may have to
139  * continue splitting all the way up the tree. We stop if the root
140  * splits or the page inserted into didn't have to split to hold the
141  * new key. Some algorithms replace the key for the old page as well
142  * as the new page. We don't, as there's no reason to believe that the
143  * first key on the old page is any better than the key we have, and,
144  * in the case of a key being placed at index 0 causing the split, the
145  * key is unavailable.
146  *
147  * There are a maximum of 5 pages pinned at any time. We keep the left
148  * and right pages pinned while working on the parent. The 5 are the
149  * two children, left parent and right parent (when the parent splits)
150  * and the root page or the overflow key page when calling bt_preserve.
151  * This code must make sure that all pins are released other than the
152  * root page or overflow page which is unlocked elsewhere.
153  */
154  while ((parent = BT_POP(t)) != NULL) {
155  lchild = l;
156  rchild = r;
157 
158  /* Get the parent page. */
159  if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
160  goto err2;
161 
162  /*
163  * The new key goes ONE AFTER the index, because the split
164  * was to the right.
165  */
166  skip = parent->index + 1;
167 
168  /*
169  * Calculate the space needed on the parent page.
170  *
171  * Prefix trees: space hack when inserting into BINTERNAL
172  * pages. Retain only what's needed to distinguish between
173  * the new entry and the LAST entry on the page to its left.
174  * If the keys compare equal, retain the entire key. Note,
175  * we don't touch overflow keys, and the entire key must be
176  * retained for the next-to-left most key on the leftmost
177  * page of each level, or the search will fail. Applicable
178  * ONLY to internal pages that have leaf pages as children.
179  * Further reduction of the key between pairs of internal
180  * pages loses too much information.
181  */
182  switch (rchild->flags & P_TYPE) {
183  case P_BINTERNAL:
184  bi = GETBINTERNAL(rchild, 0);
185  nbytes = NBINTERNAL(bi->ksize);
186  break;
187  case P_BLEAF:
188  bl = GETBLEAF(rchild, 0);
189  nbytes = NBINTERNAL(bl->ksize);
190  if (t->bt_pfx && !(bl->flags & P_BIGKEY) &&
191  (h->prevpg != P_INVALID || skip > 1)) {
192  tbl = GETBLEAF(lchild, NEXTINDEX(lchild) - 1);
193  a.size = tbl->ksize;
194  a.data = tbl->bytes;
195  b.size = bl->ksize;
196  b.data = bl->bytes;
197  nksize = t->bt_pfx(&a, &b);
198  n = NBINTERNAL(nksize);
199  if (n < nbytes) {
200 #ifdef STATISTICS
201  bt_pfxsaved += nbytes - n;
202 #endif
203  nbytes = n;
204  } else
205  nksize = 0;
206  } else
207  nksize = 0;
208  break;
209  case P_RINTERNAL:
210  case P_RLEAF:
211  nbytes = NRINTERNAL;
212  break;
213  default:
214  abort();
215  }
216 
217  /* Split the parent page if necessary or shift the indices. */
218  if ((u_int32_t) (h->upper - h->lower)
219  < nbytes + sizeof(indx_t)) {
220  sp = h;
221  h = h->pgno == P_ROOT ?
222  bt_root(t, h, &l, &r, &skip, nbytes) :
223  bt_page(t, h, &l, &r, &skip, nbytes);
224  if (h == NULL)
225  goto err1;
226  parentsplit = 1;
227  } else {
228  if (skip < (nxtindex = NEXTINDEX(h)))
229  memmove(h->linp + skip + 1, h->linp + skip,
230  (nxtindex - skip) * sizeof(indx_t));
231  h->lower += sizeof(indx_t);
232  parentsplit = 0;
233  }
234 
235  /* Insert the key into the parent page. */
236  switch (rchild->flags & P_TYPE) {
237  case P_BINTERNAL:
238  h->linp[skip] = h->upper -= nbytes;
239  dest = (char *)h + h->linp[skip];
240  memmove(dest, bi, nbytes);
241  ((BINTERNAL *)dest)->pgno = rchild->pgno;
242  break;
243  case P_BLEAF:
244  h->linp[skip] = h->upper -= nbytes;
245  dest = (char *)h + h->linp[skip];
246  WR_BINTERNAL(dest, nksize ? nksize : bl->ksize,
247  rchild->pgno, bl->flags & P_BIGKEY);
248  memmove(dest, bl->bytes, nksize ? nksize : bl->ksize);
249  if (bl->flags & P_BIGKEY &&
250  bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
251  goto err1;
252  break;
253  case P_RINTERNAL:
254  /*
255  * Update the left page count. If split
256  * added at index 0, fix the correct page.
257  */
258  if (skip > 0)
259  dest = (char *)h + h->linp[skip - 1];
260  else
261  dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
262  ((RINTERNAL *)dest)->nrecs = rec_total(lchild);
263  ((RINTERNAL *)dest)->pgno = lchild->pgno;
264 
265  /* Update the right page count. */
266  h->linp[skip] = h->upper -= nbytes;
267  dest = (char *)h + h->linp[skip];
268  ((RINTERNAL *)dest)->nrecs = rec_total(rchild);
269  ((RINTERNAL *)dest)->pgno = rchild->pgno;
270  break;
271  case P_RLEAF:
272  /*
273  * Update the left page count. If split
274  * added at index 0, fix the correct page.
275  */
276  if (skip > 0)
277  dest = (char *)h + h->linp[skip - 1];
278  else
279  dest = (char *)l + l->linp[NEXTINDEX(l) - 1];
280  ((RINTERNAL *)dest)->nrecs = NEXTINDEX(lchild);
281  ((RINTERNAL *)dest)->pgno = lchild->pgno;
282 
283  /* Update the right page count. */
284  h->linp[skip] = h->upper -= nbytes;
285  dest = (char *)h + h->linp[skip];
286  ((RINTERNAL *)dest)->nrecs = NEXTINDEX(rchild);
287  ((RINTERNAL *)dest)->pgno = rchild->pgno;
288  break;
289  default:
290  abort();
291  }
292 
293  /* Unpin the held pages. */
294  if (!parentsplit) {
295  mpool_put(t->bt_mp, h, MPOOL_DIRTY);
296  break;
297  }
298 
299  /* If the root page was split, make it look right. */
300  if (sp->pgno == P_ROOT &&
301  (F_ISSET(t, R_RECNO) ?
302  bt_rroot(t, sp, l, r) : bt_broot(t, sp, l, r)) == RET_ERROR)
303  goto err1;
304 
305  mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
306  mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
307  }
308 
309  /* Unpin the held pages. */
310  mpool_put(t->bt_mp, l, MPOOL_DIRTY);
311  mpool_put(t->bt_mp, r, MPOOL_DIRTY);
312 
313  /* Clear any pages left on the stack. */
314  return (RET_SUCCESS);
315 
316  /*
317  * If something fails in the above loop we were already walking back
318  * up the tree and the tree is now inconsistent. Nothing much we can
319  * do about it but release any memory we're holding.
320  */
321 err1: mpool_put(t->bt_mp, lchild, MPOOL_DIRTY);
322  mpool_put(t->bt_mp, rchild, MPOOL_DIRTY);
323 
324 err2: mpool_put(t->bt_mp, l, 0);
325  mpool_put(t->bt_mp, r, 0);
326  __dbpanic(t->bt_dbp);
327  return (RET_ERROR);
328 }
Definition: btree.h:75
void * data
Definition: db.h:86
#define NEXTINDEX(p)
Definition: btree.h:98
size_t size
Definition: db.h:87
#define RET_ERROR
Definition: db.h:51
#define GETBLEAF(pg, indx)
Definition: btree.h:188
#define NBINTERNAL(len)
Definition: btree.h:142
#define WR_RLEAF(p, data, flags)
Definition: btree.h:231
pgno_t pgno
Definition: btree.h:76
#define F_ISSET(p, f)
Definition: btree.h:42
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
static int bt_rroot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
Definition: bt_split.c:497
u_char flags
Definition: btree.h:183
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
#define NRINTERNAL
Definition: btree.h:169
indx_t index
Definition: btree.h:251
Definition: db.h:85
indx_t lower
Definition: btree.h:89
#define BT_POP(t)
Definition: btree.h:327
#define P_TYPE
Definition: btree.h:85
#define NULL
Definition: resample.c:96
u_int16_t indx_t
Definition: db.h:80
indx_t upper
Definition: btree.h:90
#define GETBINTERNAL(pg, indx)
Definition: btree.h:138
Definition: btree.h:249
static int bt_preserve(BTREE *t, pgno_t pg)
Definition: bt_split.c:792
#define WR_BINTERNAL(p, size, pgno, flags)
Definition: btree.h:146
#define P_RLEAF
Definition: btree.h:84
#define MPOOL_DIRTY
Definition: mpool.h:61
void * mpool_get(MPOOL *mp, pgno_t pgno, u_int flags)
Definition: mpool.c:165
#define P_RINTERNAL
Definition: btree.h:83
u_int32_t flags
Definition: btree.h:87
#define RET_SUCCESS
Definition: db.h:52
#define P_BIGKEY
Definition: btree.h:132
static PAGE * bt_page(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
Definition: bt_split.c:345
indx_t linp[1]
Definition: btree.h:91
MPOOL * bt_mp
Definition: btree.h:313
u_int32_t pgno_t
Definition: db.h:78
void __dbpanic(DB *dbp)
char bytes[1]
Definition: btree.h:184
pgno_t prevpg
Definition: btree.h:77
static int bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
Definition: bt_split.c:537
unsigned short u_int16_t
#define P_ROOT
Definition: btree.h:65
unsigned int u_int32_t
pgno_t pgno
Definition: btree.h:250
Definition: btree.h:180
#define P_INVALID
Definition: btree.h:63
DB * bt_dbp
Definition: btree.h:315
u_int32_t ksize
Definition: btree.h:181
static struct test_val b
static recno_t rec_total(PAGE *h)
Definition: bt_split.c:820
#define R_RECNO
Definition: btree.h:375
#define P_BINTERNAL
Definition: btree.h:80
#define WR_BLEAF(p, key, data, flags)
Definition: btree.h:200
#define P_BLEAF
Definition: btree.h:81
u_int32_t ksize
Definition: btree.h:129
static PAGE * bt_root(BTREE *t, PAGE *h, PAGE **lp, PAGE **rp, indx_t *skip, size_t ilen)
Definition: bt_split.c:450
static struct test_val a

◆ __P() [1/5]

static int bt_rroot __P ( (BTREE *, PAGE *, PAGE *, PAGE *)  )
static

◆ __P() [2/5]

static PAGE *bt_root __P ( (BTREE *, PAGE *, PAGE **, PAGE **, indx_t *, size_t)  )
static

◆ __P() [3/5]

static int bt_preserve __P ( (BTREE *, pgno_t )
static

◆ __P() [4/5]

static PAGE* bt_psplit __P ( (BTREE *, PAGE *, PAGE *, PAGE *, indx_t *, size_t)  )
static

◆ __P() [5/5]

static recno_t rec_total __P ( (PAGE *)  )
static

◆ bt_broot()

static int bt_broot ( BTREE t,
PAGE h,
PAGE l,
PAGE r 
)
static

Definition at line 537 of file bt_split.c.

References _btree::bt_mp, bt_preserve(), _btree::bt_psize, BTDATAOFF, _bleaf::bytes, _page::flags, _bleaf::flags, GETBINTERNAL, GETBLEAF, _binternal::ksize, _bleaf::ksize, _page::linp, _page::lower, MPOOL_DIRTY, mpool_put(), NBINTERNAL, P_BIGKEY, P_BINTERNAL, P_BLEAF, P_TYPE, _page::pgno, RET_ERROR, RET_SUCCESS, _page::upper, and WR_BINTERNAL.

Referenced by __bt_split().

540 {
541  BINTERNAL *bi;
542  BLEAF *bl;
543  u_int32_t nbytes;
544  char *dest;
545 
546  /*
547  * If the root page was a leaf page, change it into an internal page.
548  * We copy the key we split on (but not the key's data, in the case of
549  * a leaf page) to the new root page.
550  *
551  * The btree comparison code guarantees that the left-most key on any
552  * level of the tree is never used, so it doesn't need to be filled in.
553  */
554  nbytes = NBINTERNAL(0);
555  h->linp[0] = h->upper = t->bt_psize - nbytes;
556  dest = (char *)h + h->upper;
557  WR_BINTERNAL(dest, 0, l->pgno, 0);
558 
559  switch (h->flags & P_TYPE) {
560  case P_BLEAF:
561  bl = GETBLEAF(r, 0);
562  nbytes = NBINTERNAL(bl->ksize);
563  h->linp[1] = h->upper -= nbytes;
564  dest = (char *)h + h->upper;
565  WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
566  memmove(dest, bl->bytes, bl->ksize);
567 
568  /*
569  * If the key is on an overflow page, mark the overflow chain
570  * so it isn't deleted when the leaf copy of the key is deleted.
571  */
572  if (bl->flags & P_BIGKEY &&
573  bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
574  return (RET_ERROR);
575  break;
576  case P_BINTERNAL:
577  bi = GETBINTERNAL(r, 0);
578  nbytes = NBINTERNAL(bi->ksize);
579  h->linp[1] = h->upper -= nbytes;
580  dest = (char *)h + h->upper;
581  memmove(dest, bi, nbytes);
582  ((BINTERNAL *)dest)->pgno = r->pgno;
583  break;
584  default:
585  abort();
586  }
587 
588  /* There are two keys on the page. */
589  h->lower = BTDATAOFF + 2 * sizeof(indx_t);
590 
591  /* Unpin the root page, set to btree internal page. */
592  h->flags &= ~P_TYPE;
593  h->flags |= P_BINTERNAL;
594  mpool_put(t->bt_mp, h, MPOOL_DIRTY);
595 
596  return (RET_SUCCESS);
597 }
#define RET_ERROR
Definition: db.h:51
#define GETBLEAF(pg, indx)
Definition: btree.h:188
#define NBINTERNAL(len)
Definition: btree.h:142
pgno_t pgno
Definition: btree.h:76
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
u_char flags
Definition: btree.h:183
indx_t lower
Definition: btree.h:89
#define P_TYPE
Definition: btree.h:85
u_int16_t indx_t
Definition: db.h:80
indx_t upper
Definition: btree.h:90
#define GETBINTERNAL(pg, indx)
Definition: btree.h:138
static int bt_preserve(BTREE *t, pgno_t pg)
Definition: bt_split.c:792
#define WR_BINTERNAL(p, size, pgno, flags)
Definition: btree.h:146
#define MPOOL_DIRTY
Definition: mpool.h:61
u_int32_t flags
Definition: btree.h:87
#define RET_SUCCESS
Definition: db.h:52
#define P_BIGKEY
Definition: btree.h:132
indx_t linp[1]
Definition: btree.h:91
MPOOL * bt_mp
Definition: btree.h:313
u_int32_t pgno_t
Definition: db.h:78
char bytes[1]
Definition: btree.h:184
u_int32_t bt_psize
Definition: btree.h:338
unsigned int u_int32_t
Definition: btree.h:180
#define BTDATAOFF
Definition: btree.h:95
u_int32_t ksize
Definition: btree.h:181
#define P_BINTERNAL
Definition: btree.h:80
#define P_BLEAF
Definition: btree.h:81
u_int32_t ksize
Definition: btree.h:129

◆ bt_page()

static PAGE* bt_page ( BTREE t,
PAGE h,
PAGE **  lp,
PAGE **  rp,
indx_t skip,
size_t  ilen 
)
static

Definition at line 345 of file bt_split.c.

References __bt_new(), _btree::bt_mp, _btree::bt_psize, bt_psplit(), BTDATAOFF, _page::flags, free(), _page::lower, malloc(), MPOOL_DIRTY, mpool_get(), mpool_put(), NEXTINDEX, _page::nextpg, NULL, P_INVALID, P_TYPE, _page::pgno, _page::prevpg, and _page::upper.

Referenced by __bt_split().

350 {
351  PAGE *l, *r, *tp;
352  pgno_t npg;
353 
354 #ifdef STATISTICS
355  ++bt_split;
356 #endif
357  /* Put the new right page for the split into place. */
358  if ((r = __bt_new(t, &npg)) == NULL)
359  return (NULL);
360  r->pgno = npg;
361  r->lower = BTDATAOFF;
362  r->upper = t->bt_psize;
363  r->nextpg = h->nextpg;
364  r->prevpg = h->pgno;
365  r->flags = h->flags & P_TYPE;
366 
367  /*
368  * If we're splitting the last page on a level because we're appending
369  * a key to it (skip is NEXTINDEX()), it's likely that the data is
370  * sorted. Adding an empty page on the side of the level is less work
371  * and can push the fill factor much higher than normal. If we're
372  * wrong it's no big deal, we'll just do the split the right way next
373  * time. It may look like it's equally easy to do a similar hack for
374  * reverse sorted data, that is, split the tree left, but it's not.
375  * Don't even try.
376  */
377  if (h->nextpg == P_INVALID && *skip == NEXTINDEX(h)) {
378 #ifdef STATISTICS
379  ++bt_sortsplit;
380 #endif
381  h->nextpg = r->pgno;
382  r->lower = BTDATAOFF + sizeof(indx_t);
383  *skip = 0;
384  *lp = h;
385  *rp = r;
386  return (r);
387  }
388 
389  /* Put the new left page for the split into place. */
390  if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) {
391  mpool_put(t->bt_mp, r, 0);
392  return (NULL);
393  }
394 #ifdef PURIFY
395  memset(l, 0xff, t->bt_psize);
396 #endif
397  l->pgno = h->pgno;
398  l->nextpg = r->pgno;
399  l->prevpg = h->prevpg;
400  l->lower = BTDATAOFF;
401  l->upper = t->bt_psize;
402  l->flags = h->flags & P_TYPE;
403 
404  /* Fix up the previous pointer of the page after the split page. */
405  if (h->nextpg != P_INVALID) {
406  if ((tp = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL) {
407  free(l);
408  /* XXX mpool_free(t->bt_mp, r->pgno); */
409  return (NULL);
410  }
411  tp->prevpg = r->pgno;
412  mpool_put(t->bt_mp, tp, MPOOL_DIRTY);
413  }
414 
415  /*
416  * Split right. The key/data pairs aren't sorted in the btree page so
417  * it's simpler to copy the data from the split page onto two new pages
418  * instead of copying half the data to the right page and compacting
419  * the left page in place. Since the left page can't change, we have
420  * to swap the original and the allocated left page after the split.
421  */
422  tp = bt_psplit(t, h, l, r, skip, ilen);
423 
424  /* Move the new left page onto the old left page. */
425  memmove(h, l, t->bt_psize);
426  if (tp == l)
427  tp = h;
428  free(l);
429 
430  *lp = h;
431  *rp = r;
432  return (tp);
433 }
Definition: btree.h:75
static PAGE * bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
Definition: bt_split.c:614
#define NEXTINDEX(p)
Definition: btree.h:98
pgno_t pgno
Definition: btree.h:76
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
indx_t lower
Definition: btree.h:89
#define P_TYPE
Definition: btree.h:85
#define NULL
Definition: resample.c:96
u_int16_t indx_t
Definition: db.h:80
indx_t upper
Definition: btree.h:90
#define MPOOL_DIRTY
Definition: mpool.h:61
char * malloc()
void * mpool_get(MPOOL *mp, pgno_t pgno, u_int flags)
Definition: mpool.c:165
void free()
u_int32_t flags
Definition: btree.h:87
MPOOL * bt_mp
Definition: btree.h:313
u_int32_t pgno_t
Definition: db.h:78
pgno_t nextpg
Definition: btree.h:78
pgno_t prevpg
Definition: btree.h:77
u_int32_t bt_psize
Definition: btree.h:338
#define P_INVALID
Definition: btree.h:63
#define BTDATAOFF
Definition: btree.h:95
PAGE * __bt_new(BTREE *t, pgno_t *npg)
Definition: bt_page.c:86

◆ bt_preserve()

static int bt_preserve ( BTREE t,
pgno_t  pg 
)
static

Definition at line 792 of file bt_split.c.

References _btree::bt_mp, _page::flags, MPOOL_DIRTY, mpool_get(), mpool_put(), NULL, P_PRESERVE, RET_ERROR, and RET_SUCCESS.

Referenced by __bt_split(), and bt_broot().

795 {
796  PAGE *h;
797 
798  if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
799  return (RET_ERROR);
800  h->flags |= P_PRESERVE;
801  mpool_put(t->bt_mp, h, MPOOL_DIRTY);
802  return (RET_SUCCESS);
803 }
Definition: btree.h:75
#define P_PRESERVE
Definition: btree.h:86
#define RET_ERROR
Definition: db.h:51
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
#define NULL
Definition: resample.c:96
#define MPOOL_DIRTY
Definition: mpool.h:61
void * mpool_get(MPOOL *mp, pgno_t pgno, u_int flags)
Definition: mpool.c:165
u_int32_t flags
Definition: btree.h:87
#define RET_SUCCESS
Definition: db.h:52
MPOOL * bt_mp
Definition: btree.h:313

◆ bt_psplit()

static PAGE* bt_psplit ( BTREE t,
PAGE h,
PAGE l,
PAGE r,
indx_t pskip,
size_t  ilen 
)
static

Definition at line 614 of file bt_split.c.

References _btree::bt_cursor, _btree::bt_psize, BTDATAOFF, c, CURS_INIT, F_ISSET, _page::flags, _binternal::flags, _bleaf::flags, GETBINTERNAL, GETBLEAF, GETRINTERNAL, GETRLEAF, _epgno::index, _binternal::ksize, _page::linp, _page::lower, NBINTERNAL, NBLEAF, NEXTINDEX, NRINTERNAL, NRLEAF, P_BIGKEY, P_BINTERNAL, P_BLEAF, P_RINTERNAL, P_RLEAF, P_TYPE, _cursor::pg, _page::pgno, _epgno::pgno, and _page::upper.

Referenced by bt_page(), and bt_root().

619 {
620  BINTERNAL *bi;
621  BLEAF *bl;
622  CURSOR *c;
623  RLEAF *rl;
624  PAGE *rval;
625  void *src = 0;
626  indx_t full, half, nxt, off, skip, top, used;
627  u_int32_t nbytes;
628  int bigkeycnt, isbigkey;
629 
630  /*
631  * Split the data to the left and right pages. Leave the skip index
632  * open. Additionally, make some effort not to split on an overflow
633  * key. This makes internal page processing faster and can save
634  * space as overflow keys used by internal pages are never deleted.
635  */
636  bigkeycnt = 0;
637  skip = *pskip;
638  full = t->bt_psize - BTDATAOFF;
639  half = full / 2;
640  used = 0;
641  for (nxt = off = 0, top = NEXTINDEX(h); nxt < top; ++off) {
642  if (skip == off) {
643  nbytes = ilen;
644  isbigkey = 0; /* XXX: not really known. */
645  } else
646  switch (h->flags & P_TYPE) {
647  case P_BINTERNAL:
648  src = bi = GETBINTERNAL(h, nxt);
649  nbytes = NBINTERNAL(bi->ksize);
650  isbigkey = bi->flags & P_BIGKEY;
651  break;
652  case P_BLEAF:
653  src = bl = GETBLEAF(h, nxt);
654  nbytes = NBLEAF(bl);
655  isbigkey = bl->flags & P_BIGKEY;
656  break;
657  case P_RINTERNAL:
658  src = GETRINTERNAL(h, nxt);
659  nbytes = NRINTERNAL;
660  isbigkey = 0;
661  break;
662  case P_RLEAF:
663  src = rl = GETRLEAF(h, nxt);
664  nbytes = NRLEAF(rl);
665  isbigkey = 0;
666  break;
667  default:
668  abort();
669  }
670 
671  /*
672  * If the key/data pairs are substantial fractions of the max
673  * possible size for the page, it's possible to get situations
674  * where we decide to try and copy too much onto the left page.
675  * Make sure that doesn't happen.
676  */
677  if ((skip <= off && used + nbytes + sizeof(indx_t) >= full)
678  || nxt == top - 1) {
679  --off;
680  break;
681  }
682 
683  /* Copy the key/data pair, if not the skipped index. */
684  if (skip != off) {
685  ++nxt;
686 
687  l->linp[off] = l->upper -= nbytes;
688  memmove((char *)l + l->upper, src, nbytes);
689  }
690 
691  used += nbytes + sizeof(indx_t);
692  if (used >= half) {
693  if (!isbigkey || bigkeycnt == 3)
694  break;
695  else
696  ++bigkeycnt;
697  }
698  }
699 
700  /*
701  * Off is the last offset that's valid for the left page.
702  * Nxt is the first offset to be placed on the right page.
703  */
704  l->lower += (off + 1) * sizeof(indx_t);
705 
706  /*
707  * If splitting the page that the cursor was on, the cursor has to be
708  * adjusted to point to the same record as before the split. If the
709  * cursor is at or past the skipped slot, the cursor is incremented by
710  * one. If the cursor is on the right page, it is decremented by the
711  * number of records split to the left page.
712  */
713  c = &t->bt_cursor;
714  if (F_ISSET(c, CURS_INIT) && c->pg.pgno == h->pgno) {
715  if (c->pg.index >= skip)
716  ++c->pg.index;
717  if (c->pg.index < nxt) /* Left page. */
718  c->pg.pgno = l->pgno;
719  else { /* Right page. */
720  c->pg.pgno = r->pgno;
721  c->pg.index -= nxt;
722  }
723  }
724 
725  /*
726  * If the skipped index was on the left page, just return that page.
727  * Otherwise, adjust the skip index to reflect the new position on
728  * the right page.
729  */
730  if (skip <= off) {
731  skip = 0;
732  rval = l;
733  } else {
734  rval = r;
735  *pskip -= nxt;
736  }
737 
738  for (off = 0; nxt < top; ++off) {
739  if (skip == nxt) {
740  ++off;
741  skip = 0;
742  }
743  switch (h->flags & P_TYPE) {
744  case P_BINTERNAL:
745  src = bi = GETBINTERNAL(h, nxt);
746  nbytes = NBINTERNAL(bi->ksize);
747  break;
748  case P_BLEAF:
749  src = bl = GETBLEAF(h, nxt);
750  nbytes = NBLEAF(bl);
751  break;
752  case P_RINTERNAL:
753  src = GETRINTERNAL(h, nxt);
754  nbytes = NRINTERNAL;
755  break;
756  case P_RLEAF:
757  src = rl = GETRLEAF(h, nxt);
758  nbytes = NRLEAF(rl);
759  break;
760  default:
761  abort();
762  }
763  ++nxt;
764  r->linp[off] = r->upper -= nbytes;
765  memmove((char *)r + r->upper, src, nbytes);
766  }
767  r->lower += off * sizeof(indx_t);
768 
769  /* If the key is being appended to the page, adjust the index. */
770  if (skip == top)
771  r->lower += sizeof(indx_t);
772 
773  return (rval);
774 }
Definition: btree.h:75
EPGNO pg
Definition: btree.h:284
#define NEXTINDEX(p)
Definition: btree.h:98
#define GETBLEAF(pg, indx)
Definition: btree.h:188
#define NBINTERNAL(len)
Definition: btree.h:142
pgno_t pgno
Definition: btree.h:76
#define GETRLEAF(pg, indx)
Definition: btree.h:220
#define F_ISSET(p, f)
Definition: btree.h:42
Definition: btree.h:213
CURSOR bt_cursor
Definition: btree.h:320
u_char flags
Definition: btree.h:183
#define NRINTERNAL
Definition: btree.h:169
indx_t index
Definition: btree.h:251
indx_t lower
Definition: btree.h:89
static struct test_val c
#define P_TYPE
Definition: btree.h:85
u_int16_t indx_t
Definition: db.h:80
indx_t upper
Definition: btree.h:90
#define GETBINTERNAL(pg, indx)
Definition: btree.h:138
#define GETRINTERNAL(pg, indx)
Definition: btree.h:165
#define P_RLEAF
Definition: btree.h:84
#define P_RINTERNAL
Definition: btree.h:83
#define NRLEAF(p)
Definition: btree.h:224
u_int32_t flags
Definition: btree.h:87
u_char flags
Definition: btree.h:133
#define P_BIGKEY
Definition: btree.h:132
indx_t linp[1]
Definition: btree.h:91
#define CURS_INIT
Definition: btree.h:291
Definition: btree.h:283
u_int32_t bt_psize
Definition: btree.h:338
unsigned int u_int32_t
pgno_t pgno
Definition: btree.h:250
Definition: btree.h:180
#define BTDATAOFF
Definition: btree.h:95
#define NBLEAF(p)
Definition: btree.h:192
#define P_BINTERNAL
Definition: btree.h:80
#define P_BLEAF
Definition: btree.h:81
u_int32_t ksize
Definition: btree.h:129

◆ bt_root()

static PAGE* bt_root ( BTREE t,
PAGE h,
PAGE **  lp,
PAGE **  rp,
indx_t skip,
size_t  ilen 
)
static

Definition at line 450 of file bt_split.c.

References __bt_new(), _btree::bt_psize, bt_psplit(), BTDATAOFF, _page::flags, _page::lower, _page::nextpg, NULL, P_INVALID, P_TYPE, _page::pgno, _page::prevpg, and _page::upper.

Referenced by __bt_split().

455 {
456  PAGE *l, *r, *tp;
457  pgno_t lnpg, rnpg;
458 
459 #ifdef STATISTICS
460  ++bt_split;
461  ++bt_rootsplit;
462 #endif
463  /* Put the new left and right pages for the split into place. */
464  if ((l = __bt_new(t, &lnpg)) == NULL ||
465  (r = __bt_new(t, &rnpg)) == NULL)
466  return (NULL);
467  l->pgno = lnpg;
468  r->pgno = rnpg;
469  l->nextpg = r->pgno;
470  r->prevpg = l->pgno;
471  l->prevpg = r->nextpg = P_INVALID;
472  l->lower = r->lower = BTDATAOFF;
473  l->upper = r->upper = t->bt_psize;
474  l->flags = r->flags = h->flags & P_TYPE;
475 
476  /* Split the root page. */
477  tp = bt_psplit(t, h, l, r, skip, ilen);
478 
479  *lp = l;
480  *rp = r;
481  return (tp);
482 }
Definition: btree.h:75
static PAGE * bt_psplit(BTREE *t, PAGE *h, PAGE *l, PAGE *r, indx_t *pskip, size_t ilen)
Definition: bt_split.c:614
pgno_t pgno
Definition: btree.h:76
indx_t lower
Definition: btree.h:89
#define P_TYPE
Definition: btree.h:85
#define NULL
Definition: resample.c:96
indx_t upper
Definition: btree.h:90
u_int32_t flags
Definition: btree.h:87
u_int32_t pgno_t
Definition: db.h:78
pgno_t nextpg
Definition: btree.h:78
pgno_t prevpg
Definition: btree.h:77
u_int32_t bt_psize
Definition: btree.h:338
#define P_INVALID
Definition: btree.h:63
#define BTDATAOFF
Definition: btree.h:95
PAGE * __bt_new(BTREE *t, pgno_t *npg)
Definition: bt_page.c:86

◆ bt_rroot()

static int bt_rroot ( BTREE t,
PAGE h,
PAGE l,
PAGE r 
)
static

Definition at line 497 of file bt_split.c.

References _btree::bt_mp, _btree::bt_psize, BTDATAOFF, _page::flags, _page::linp, _page::lower, MPOOL_DIRTY, mpool_put(), NEXTINDEX, NRINTERNAL, P_RINTERNAL, P_RLEAF, P_TYPE, _page::pgno, rec_total(), RET_SUCCESS, _page::upper, and WR_RINTERNAL.

Referenced by __bt_split().

500 {
501  char *dest;
502 
503  /* Insert the left and right keys, set the header information. */
504  h->linp[0] = h->upper = t->bt_psize - NRINTERNAL;
505  dest = (char *)h + h->upper;
506  WR_RINTERNAL(dest,
507  l->flags & P_RLEAF ? NEXTINDEX(l) : rec_total(l), l->pgno);
508 
509  h->linp[1] = h->upper -= NRINTERNAL;
510  dest = (char *)h + h->upper;
511  WR_RINTERNAL(dest,
512  r->flags & P_RLEAF ? NEXTINDEX(r) : rec_total(r), r->pgno);
513 
514  h->lower = BTDATAOFF + 2 * sizeof(indx_t);
515 
516  /* Unpin the root page, set to recno internal page. */
517  h->flags &= ~P_TYPE;
518  h->flags |= P_RINTERNAL;
519  mpool_put(t->bt_mp, h, MPOOL_DIRTY);
520 
521  return (RET_SUCCESS);
522 }
#define NEXTINDEX(p)
Definition: btree.h:98
pgno_t pgno
Definition: btree.h:76
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
#define NRINTERNAL
Definition: btree.h:169
indx_t lower
Definition: btree.h:89
#define P_TYPE
Definition: btree.h:85
u_int16_t indx_t
Definition: db.h:80
indx_t upper
Definition: btree.h:90
#define P_RLEAF
Definition: btree.h:84
#define MPOOL_DIRTY
Definition: mpool.h:61
#define P_RINTERNAL
Definition: btree.h:83
u_int32_t flags
Definition: btree.h:87
#define RET_SUCCESS
Definition: db.h:52
indx_t linp[1]
Definition: btree.h:91
MPOOL * bt_mp
Definition: btree.h:313
#define WR_RINTERNAL(p, nrecs, pgno)
Definition: btree.h:173
u_int32_t bt_psize
Definition: btree.h:338
#define BTDATAOFF
Definition: btree.h:95
static recno_t rec_total(PAGE *h)
Definition: bt_split.c:820

◆ rec_total()

static recno_t rec_total ( PAGE h)
static

Definition at line 820 of file bt_split.c.

References GETRINTERNAL, and NEXTINDEX.

Referenced by __bt_split(), and bt_rroot().

822 {
823  recno_t recs;
824  indx_t nxt, top;
825 
826  for (recs = 0, nxt = 0, top = NEXTINDEX(h); nxt < top; ++nxt)
827  recs += GETRINTERNAL(h, nxt)->nrecs;
828  return (recs);
829 }
#define NEXTINDEX(p)
Definition: btree.h:98
u_int16_t indx_t
Definition: db.h:80
#define GETRINTERNAL(pg, indx)
Definition: btree.h:165
u_int32_t recno_t
Definition: db.h:82