xref: /original-bsd/lib/libc/db/recno/rec_put.c (revision 7eb91141)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)rec_put.c	5.2 (Berkeley) 09/11/91";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <errno.h>
14 #include <db.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "recno.h"
19 
20 /*
21  * __REC_PUT -- Add a recno item to the tree.
22  *
23  * Parameters:
24  *	dbp:	pointer to access method
25  *	key:	key
26  *	data:	data
27  *	flag:	R_APPEND, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
28  *
29  * Returns:
30  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is already in the
31  *	tree and R_NOOVERWRITE specified.
32  */
33 int
34 __rec_put(dbp, key, data, flags)
35 	const DB *dbp;
36 	const DBT *key, *data;
37 	u_int flags;
38 {
39 	BTREE *t;
40 	DBT tdata;
41 	recno_t nrec;
42 	int status;
43 
44 	t = dbp->internal;
45 
46 	switch (flags) {
47 	case R_APPEND:
48 		nrec = t->bt_nrecs + 1;
49 		break;
50 	case R_CURSOR:
51 		if (ISSET(t, BTF_DELCRSR))
52 			goto einval;
53 		nrec = t->bt_rcursor;
54 		break;
55 	case 0:
56 	case R_IAFTER:
57 	case R_IBEFORE:
58 		if ((nrec = *(recno_t *)key->data) == 0)
59 			goto einval;
60 		break;
61 	case R_NOOVERWRITE:
62 		if ((nrec = *(recno_t *)key->data) == 0)
63 			goto einval;
64 		if (nrec <= t->bt_nrecs)
65 			return (RET_SPECIAL);
66 		break;
67 	default:
68 einval:		errno = EINVAL;
69 		return (RET_ERROR);
70 	}
71 
72 	/*
73 	 * Make sure that records up to and including the put record are already
74  	 * in the database.  If skipping records, create empty ones.
75 	 */
76 	if (nrec > t->bt_nrecs) {
77 		if (t->bt_irec(t, nrec) == RET_ERROR)
78 			return (RET_ERROR);
79 		if (nrec > t->bt_nrecs + 1) {
80 			tdata.data = NULL;
81 			tdata.size = 0;
82 			while (nrec > t->bt_nrecs)
83 				if (__rec_iput(t, nrec, &tdata, 0)
84 				    != RET_SUCCESS)
85 					return (RET_ERROR);
86 		}
87 	}
88 	--nrec;
89 	if ((status = __rec_iput(t, nrec, data, flags)) == RET_SUCCESS)
90 		SET(t, BTF_MODIFIED);
91 	return (status);
92 }
93 
94 /*
95  * __REC_IPUT -- Add a recno item to the tree.
96  *
97  * Parameters:
98  *	t:	tree
99  *	nrec:	record number
100  *	data:	data
101  *
102  * Returns:
103  *	RET_ERROR, RET_SUCCESS
104  */
105 int
106 __rec_iput(t, nrec, data, flags)
107 	BTREE *t;
108 	recno_t nrec;
109 	const DBT *data;
110 	u_int flags;
111 {
112 	DBT tdata;
113 	EPG *e;
114 	PAGE *h;
115 	index_t index, nxtindex;
116 	pgno_t pg;
117 	size_t nbytes;
118 	int dflags, status;
119 	char *dest, db[NOVFLSIZE];
120 
121 	/*
122 	 * If the data won't fit on a page, store it on indirect pages.
123 	 *
124 	 * XXX
125 	 * If the insert fails later on, these pages aren't recovered.
126 	 */
127 	if (data->size > t->bt_ovflsize) {
128 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
129 			return (RET_ERROR);
130 		tdata.data = db;
131 		tdata.size = NOVFLSIZE;
132 		*(pgno_t *)db = pg;
133 		*(size_t *)(db + sizeof(pgno_t)) = data->size;
134 		dflags = P_BIGDATA;
135 		data = &tdata;
136 	} else
137 		dflags = 0;
138 
139 	/* __rec_search pins the returned page. */
140 	if ((e = __rec_search(t, nrec, SINSERT)) == NULL)
141 		return (RET_ERROR);
142 
143 	h = e->page;
144 	index = e->index;
145 
146 	/*
147 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
148 	 * R_IBEFORE flags insert the key after/before the specified key.
149 	 *
150 	 * Pages are split as required.
151 	 */
152 	switch (flags) {
153 	case R_IAFTER:
154 		++index;
155 		break;
156 	case R_IBEFORE:
157 		break;
158 	default:
159 		if (nrec < t->bt_nrecs &&
160 		    __rec_dleaf(t, h, index) == RET_ERROR) {
161 			BT_CLR(t);
162 			mpool_put(t->bt_mp, h, 0);
163 			return (RET_ERROR);
164 		}
165 		break;
166 	}
167 
168 	/*
169 	 * If not enough room, split the page.  The split code will insert
170 	 * the key and data and unpin the current page.  If inserting into
171 	 * the offset array, shift the pointers up.
172 	 */
173 	nbytes = NRLEAFDBT(data->size);
174 	if (h->upper - h->lower < nbytes + sizeof(index_t)) {
175 		status = __bt_split(t, h, NULL, data, dflags, nbytes, index);
176 		if (status == RET_SUCCESS)
177 			++t->bt_nrecs;
178 		return (status);
179 	}
180 
181 	if (index < (nxtindex = NEXTINDEX(h)))
182 		bcopy(h->linp + index, h->linp + index + 1,
183 		    (nxtindex - index) * sizeof(index_t));
184 	h->lower += sizeof(index_t);
185 
186 	h->linp[index] = h->upper -= nbytes;
187 	dest = (char *)h + h->upper;
188 	WR_RLEAF(dest, data, dflags);
189 
190 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
191 	++t->bt_nrecs;
192 	return (RET_SUCCESS);
193 }
194