150995Sbostic /*- 250995Sbostic * Copyright (c) 1990 The Regents of the University of California. 350995Sbostic * All rights reserved. 450995Sbostic * 550995Sbostic * %sccs.include.redist.c% 650995Sbostic */ 750995Sbostic 850995Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*60053Sbostic static char sccsid[] = "@(#)rec_put.c 5.13 (Berkeley) 05/16/93"; 1050995Sbostic #endif /* LIBC_SCCS and not lint */ 1150995Sbostic 1250995Sbostic #include <sys/types.h> 1356758Sbostic 1450995Sbostic #include <errno.h> 1550995Sbostic #include <stdio.h> 1650995Sbostic #include <stdlib.h> 1750995Sbostic #include <string.h> 1856758Sbostic 1957933Sbostic #include <db.h> 2051089Sbostic #include "recno.h" 2150995Sbostic 2250995Sbostic /* 2350995Sbostic * __REC_PUT -- Add a recno item to the tree. 2450995Sbostic * 2550995Sbostic * Parameters: 2650995Sbostic * dbp: pointer to access method 2750995Sbostic * key: key 2850995Sbostic * data: data 2959865Sbostic * flag: R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE 3050995Sbostic * 3150995Sbostic * Returns: 3259865Sbostic * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is 3359865Sbostic * already in the tree and R_NOOVERWRITE specified. 3450995Sbostic */ 3550995Sbostic int 3650995Sbostic __rec_put(dbp, key, data, flags) 3750995Sbostic const DB *dbp; 3856758Sbostic DBT *key; 3956758Sbostic const DBT *data; 4050995Sbostic u_int flags; 4150995Sbostic { 4250995Sbostic BTREE *t; 4350995Sbostic DBT tdata; 4450995Sbostic recno_t nrec; 4550995Sbostic int status; 4650995Sbostic 4751089Sbostic t = dbp->internal; 4851089Sbostic 4951089Sbostic switch (flags) { 5051089Sbostic case R_CURSOR: 51*60053Sbostic if (!ISSET(t, B_SEQINIT)) 5251089Sbostic goto einval; 5351089Sbostic nrec = t->bt_rcursor; 5451089Sbostic break; 5556758Sbostic case R_SETCURSOR: 5656758Sbostic if ((nrec = *(recno_t *)key->data) == 0) 5756758Sbostic goto einval; 5856758Sbostic break; 5954282Sbostic case R_IAFTER: 6054282Sbostic if ((nrec = *(recno_t *)key->data) == 0) { 6154282Sbostic nrec = 1; 6254282Sbostic flags = R_IBEFORE; 6354282Sbostic } 6454282Sbostic break; 6551089Sbostic case 0: 6651089Sbostic case R_IBEFORE: 6751089Sbostic if ((nrec = *(recno_t *)key->data) == 0) 6851089Sbostic goto einval; 6951089Sbostic break; 7051089Sbostic case R_NOOVERWRITE: 7151089Sbostic if ((nrec = *(recno_t *)key->data) == 0) 7251089Sbostic goto einval; 7351089Sbostic if (nrec <= t->bt_nrecs) 7451089Sbostic return (RET_SPECIAL); 7551089Sbostic break; 7651089Sbostic default: 7751089Sbostic einval: errno = EINVAL; 7850995Sbostic return (RET_ERROR); 7950995Sbostic } 8050995Sbostic 8150995Sbostic /* 8254282Sbostic * Make sure that records up to and including the put record are 8354282Sbostic * already in the database. If skipping records, create empty ones. 8450995Sbostic */ 8550995Sbostic if (nrec > t->bt_nrecs) { 86*60053Sbostic if (!ISSET(t, R_EOF | R_INMEM) && 8758750Sbostic t->bt_irec(t, nrec) == RET_ERROR) 8851089Sbostic return (RET_ERROR); 8951089Sbostic if (nrec > t->bt_nrecs + 1) { 9051089Sbostic tdata.data = NULL; 9151089Sbostic tdata.size = 0; 9256041Sbostic while (nrec > t->bt_nrecs + 1) 9354282Sbostic if (__rec_iput(t, 9456041Sbostic t->bt_nrecs, &tdata, 0) != RET_SUCCESS) 9551089Sbostic return (RET_ERROR); 9650995Sbostic } 9750995Sbostic } 9856758Sbostic 9956758Sbostic if ((status = __rec_iput(t, nrec - 1, data, flags)) != RET_SUCCESS) 10056758Sbostic return (status); 10156758Sbostic 10259865Sbostic if (flags == R_SETCURSOR) 10356758Sbostic t->bt_rcursor = nrec; 10456758Sbostic 105*60053Sbostic SET(t, R_MODIFIED); 10656758Sbostic return (__rec_ret(t, NULL, nrec, key, NULL)); 10750995Sbostic } 10850995Sbostic 10950995Sbostic /* 11050995Sbostic * __REC_IPUT -- Add a recno item to the tree. 11150995Sbostic * 11250995Sbostic * Parameters: 11350995Sbostic * t: tree 11450995Sbostic * nrec: record number 11550995Sbostic * data: data 11650995Sbostic * 11750995Sbostic * Returns: 11851089Sbostic * RET_ERROR, RET_SUCCESS 11950995Sbostic */ 12050995Sbostic int 12150995Sbostic __rec_iput(t, nrec, data, flags) 12250995Sbostic BTREE *t; 12350995Sbostic recno_t nrec; 12450995Sbostic const DBT *data; 12550995Sbostic u_int flags; 12650995Sbostic { 12750995Sbostic DBT tdata; 12850995Sbostic EPG *e; 12950995Sbostic PAGE *h; 13057988Sbostic indx_t index, nxtindex; 13150995Sbostic pgno_t pg; 13250995Sbostic size_t nbytes; 13351089Sbostic int dflags, status; 13450995Sbostic char *dest, db[NOVFLSIZE]; 13550995Sbostic 13650995Sbostic /* 13750995Sbostic * If the data won't fit on a page, store it on indirect pages. 13850995Sbostic * 13950995Sbostic * XXX 14050995Sbostic * If the insert fails later on, these pages aren't recovered. 14150995Sbostic */ 14251089Sbostic if (data->size > t->bt_ovflsize) { 14350995Sbostic if (__ovfl_put(t, data, &pg) == RET_ERROR) 14450995Sbostic return (RET_ERROR); 14550995Sbostic tdata.data = db; 14650995Sbostic tdata.size = NOVFLSIZE; 14750995Sbostic *(pgno_t *)db = pg; 14850995Sbostic *(size_t *)(db + sizeof(pgno_t)) = data->size; 14950995Sbostic dflags = P_BIGDATA; 15050995Sbostic data = &tdata; 15150995Sbostic } else 15250995Sbostic dflags = 0; 15350995Sbostic 15450995Sbostic /* __rec_search pins the returned page. */ 15556041Sbostic if ((e = __rec_search(t, nrec, 15656997Sbostic nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ? 15756997Sbostic SINSERT : SEARCH)) == NULL) 15850995Sbostic return (RET_ERROR); 15950995Sbostic 16050995Sbostic h = e->page; 16150995Sbostic index = e->index; 16250995Sbostic 16350995Sbostic /* 16451089Sbostic * Add the specified key/data pair to the tree. The R_IAFTER and 16551089Sbostic * R_IBEFORE flags insert the key after/before the specified key. 16650995Sbostic * 16750995Sbostic * Pages are split as required. 16850995Sbostic */ 16950995Sbostic switch (flags) { 17050995Sbostic case R_IAFTER: 17150995Sbostic ++index; 17250995Sbostic break; 17350995Sbostic case R_IBEFORE: 17450995Sbostic break; 17550995Sbostic default: 17651089Sbostic if (nrec < t->bt_nrecs && 17751089Sbostic __rec_dleaf(t, h, index) == RET_ERROR) { 17850995Sbostic mpool_put(t->bt_mp, h, 0); 17950995Sbostic return (RET_ERROR); 18050995Sbostic } 18150995Sbostic break; 18250995Sbostic } 18350995Sbostic 18450995Sbostic /* 18550995Sbostic * If not enough room, split the page. The split code will insert 18650995Sbostic * the key and data and unpin the current page. If inserting into 18750995Sbostic * the offset array, shift the pointers up. 18850995Sbostic */ 18950995Sbostic nbytes = NRLEAFDBT(data->size); 19057988Sbostic if (h->upper - h->lower < nbytes + sizeof(indx_t)) { 19151089Sbostic status = __bt_split(t, h, NULL, data, dflags, nbytes, index); 19251089Sbostic if (status == RET_SUCCESS) 19351089Sbostic ++t->bt_nrecs; 19451089Sbostic return (status); 19551089Sbostic } 19650995Sbostic 19750995Sbostic if (index < (nxtindex = NEXTINDEX(h))) 19858015Sbostic memmove(h->linp + index + 1, h->linp + index, 19957988Sbostic (nxtindex - index) * sizeof(indx_t)); 20057988Sbostic h->lower += sizeof(indx_t); 20150995Sbostic 20250995Sbostic h->linp[index] = h->upper -= nbytes; 20350995Sbostic dest = (char *)h + h->upper; 20450995Sbostic WR_RLEAF(dest, data, dflags); 20550995Sbostic 206*60053Sbostic ++t->bt_nrecs; 207*60053Sbostic SET(t, B_MODIFIED); 20850995Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 209*60053Sbostic 21050995Sbostic return (RET_SUCCESS); 21150995Sbostic } 212