Asterisk - The Open Source Telephony Project GIT-master-f3e88d3
Functions
bt_put.c File Reference
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/db.h"
#include "btree.h"
Include dependency graph for bt_put.c:

Go to the source code of this file.

Functions

int __bt_put (DB *dbp, DBT *key, const DBT *data, u_int flags) const
 
static EPG *bt_fast __P ((BTREE *, const DBT *, const DBT *, int *))
 
static EPGbt_fast (BTREE *t, const DBT *key, const DBT *data, int *exactp)
 

Function Documentation

◆ __bt_put()

int __bt_put ( DB dbp,
DBT key,
const DBT data,
u_int  flags 
) const

Definition at line 67 of file bt_put.c.

72{
73 BTREE *t;
74 DBT tkey, tdata;
75 EPG *e = 0;
76 PAGE *h;
77 indx_t idx, nxtindex;
78 pgno_t pg;
79 u_int32_t nbytes;
80 int dflags, exact, status;
81 char *dest, db[NOVFLSIZE], kb[NOVFLSIZE];
82
83 t = dbp->internal;
84
85 /* Toss any page pinned across calls. */
86 if (t->bt_pinned != NULL) {
87 mpool_put(t->bt_mp, t->bt_pinned, 0);
88 t->bt_pinned = NULL;
89 }
90
91 /* Check for change to a read-only tree. */
92 if (F_ISSET(t, B_RDONLY)) {
93 errno = EPERM;
94 return (RET_ERROR);
95 }
96
97 switch (flags) {
98 case 0:
99 case R_NOOVERWRITE:
100 break;
101 case R_CURSOR:
102 /*
103 * If flags is R_CURSOR, put the cursor. Must already
104 * have started a scan and not have already deleted it.
105 */
106 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
107 !F_ISSET(&t->bt_cursor,
109 break;
110 /* FALLTHROUGH */
111 default:
112 errno = EINVAL;
113 return (RET_ERROR);
114 }
115
116 /*
117 * If the key/data pair won't fit on a page, store it on overflow
118 * pages. Only put the key on the overflow page if the pair are
119 * still too big after moving the data to an overflow page.
120 *
121 * XXX
122 * If the insert fails later on, the overflow pages aren't recovered.
123 */
124 dflags = 0;
125 if (key->size + data->size > t->bt_ovflsize) {
126 if (key->size > t->bt_ovflsize) {
127storekey: if (__ovfl_put(t, key, &pg) == RET_ERROR)
128 return (RET_ERROR);
129 tkey.data = kb;
130 tkey.size = NOVFLSIZE;
131 memmove(kb, &pg, sizeof(pgno_t));
132 memmove(kb + sizeof(pgno_t),
133 &key->size, sizeof(u_int32_t));
134 dflags |= P_BIGKEY;
135 key = &tkey;
136 }
137 if (key->size + data->size > t->bt_ovflsize) {
138 if (__ovfl_put(t, data, &pg) == RET_ERROR)
139 return (RET_ERROR);
140 tdata.data = db;
141 tdata.size = NOVFLSIZE;
142 memmove(db, &pg, sizeof(pgno_t));
143 memmove(db + sizeof(pgno_t),
144 &data->size, sizeof(u_int32_t));
145 dflags |= P_BIGDATA;
146 data = &tdata;
147 }
148 if (key->size + data->size > t->bt_ovflsize)
149 goto storekey;
150 }
151
152 /* Replace the cursor. */
153 if (flags == R_CURSOR) {
154 if ((h = mpool_get(t->bt_mp, t->bt_cursor.pg.pgno, 0)) == NULL)
155 return (RET_ERROR);
156 idx = t->bt_cursor.pg.index;
157 goto delete;
158 }
159
160 /*
161 * Find the key to delete, or, the location at which to insert.
162 * Bt_fast and __bt_search both pin the returned page.
163 */
164 if (t->bt_order == NOT || (e = bt_fast(t, key, data, &exact)) == NULL)
165 if ((e = __bt_search(t, key, &exact)) == NULL)
166 return (RET_ERROR);
167 h = e->page;
168 idx = e->index;
169
170 /*
171 * Add the key/data pair to the tree. If an identical key is already
172 * in the tree, and R_NOOVERWRITE is set, an error is returned. If
173 * R_NOOVERWRITE is not set, the key is either added (if duplicates are
174 * permitted) or an error is returned.
175 */
176 switch (flags) {
177 case R_NOOVERWRITE:
178 if (!exact)
179 break;
180 mpool_put(t->bt_mp, h, 0);
181 return (RET_SPECIAL);
182 default:
183 if (!exact || !F_ISSET(t, B_NODUPS))
184 break;
185 /*
186 * !!!
187 * Note, the delete may empty the page, so we need to put a
188 * new entry into the page immediately.
189 */
190delete: if (__bt_dleaf(t, key, h, idx) == RET_ERROR) {
191 mpool_put(t->bt_mp, h, 0);
192 return (RET_ERROR);
193 }
194 break;
195 }
196
197 /*
198 * If not enough room, or the user has put a ceiling on the number of
199 * keys permitted in the page, split the page. The split code will
200 * insert the key and data and unpin the current page. If inserting
201 * into the offset array, shift the pointers up.
202 */
203 nbytes = NBLEAFDBT(key->size, data->size);
204 if ((u_int32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) {
205 if ((status = __bt_split(t, h, key,
206 data, dflags, nbytes, idx)) != RET_SUCCESS)
207 return (status);
208 goto success;
209 }
210
211 if (idx < (nxtindex = NEXTINDEX(h)))
212 memmove(h->linp + idx + 1, h->linp + idx,
213 (nxtindex - idx) * sizeof(indx_t));
214 h->lower += sizeof(indx_t);
215
216 h->linp[idx] = h->upper -= nbytes;
217 dest = (char *)h + h->upper;
218 WR_BLEAF(dest, key, data, dflags);
219
220 /* If the cursor is on this page, adjust it as necessary. */
221 if (F_ISSET(&t->bt_cursor, CURS_INIT) &&
223 t->bt_cursor.pg.pgno == h->pgno && t->bt_cursor.pg.index >= idx)
224 ++t->bt_cursor.pg.index;
225
226 if (t->bt_order == NOT) {
227 if (h->nextpg == P_INVALID) {
228 if (idx == NEXTINDEX(h) - 1) {
229 t->bt_order = FORWARD;
230 t->bt_last.index = idx;
231 t->bt_last.pgno = h->pgno;
232 }
233 } else if (h->prevpg == P_INVALID) {
234 if (idx == 0) {
235 t->bt_order = BACK;
236 t->bt_last.index = 0;
237 t->bt_last.pgno = h->pgno;
238 }
239 }
240 }
241
243
244success:
245 if (flags == R_SETCURSOR)
246 __bt_setcur(t, e->page->pgno, e->index);
247
248 F_SET(t, B_MODIFIED);
249 return (RET_SUCCESS);
250}
dflags
Definition: app_dictate.c:63
jack_status_t status
Definition: app_jack.c:146
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
int __bt_dleaf(BTREE *t, const DBT *key, PAGE *h, u_int idx)
Definition: bt_delete.c:474
int __ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg)
Definition: bt_overflow.c:139
static EPG * bt_fast(BTREE *t, const DBT *key, const DBT *data, int *exactp)
Definition: bt_put.c:267
EPG * __bt_search(BTREE *t, const DBT *key, int *exactp)
Definition: bt_search.c:66
void __bt_setcur(BTREE *t, pgno_t pgno, u_int index)
Definition: bt_seq.c:443
int __bt_split(BTREE *t, PAGE *sp, const DBT *key, const DBT *data, int flags, size_t ilen, u_int32_t argskip)
Definition: bt_split.c:82
#define F_ISSET(p, f)
Definition: btree.h:42
#define CURS_AFTER
Definition: btree.h:289
#define B_MODIFIED
Definition: btree.h:370
#define B_NODUPS
Definition: btree.h:374
#define NEXTINDEX(p)
Definition: btree.h:98
#define NOVFLSIZE
Definition: btree.h:117
#define WR_BLEAF(p, key, data, flags)
Definition: btree.h:200
#define P_BIGDATA
Definition: btree.h:131
#define P_BIGKEY
Definition: btree.h:132
#define B_RDONLY
Definition: btree.h:372
#define CURS_INIT
Definition: btree.h:291
#define CURS_ACQUIRE
Definition: btree.h:288
#define F_SET(p, f)
Definition: btree.h:40
#define P_INVALID
Definition: btree.h:63
#define NBLEAFDBT(ksize, dsize)
Definition: btree.h:195
#define CURS_BEFORE
Definition: btree.h:290
static sqlite3 * db
u_int16_t indx_t
Definition: db.h:80
#define R_NOOVERWRITE
Definition: db.h:98
#define R_CURSOR
Definition: db.h:91
#define RET_SUCCESS
Definition: db.h:52
#define R_SETCURSOR
Definition: db.h:100
#define RET_ERROR
Definition: db.h:51
#define RET_SPECIAL
Definition: db.h:53
u_int32_t pgno_t
Definition: db.h:78
static DB * dbp
Definition: hsearch.c:49
unsigned int u_int32_t
int errno
void * mpool_get(MPOOL *mp, pgno_t pgno, u_int flags)
Definition: mpool.c:165
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
#define MPOOL_DIRTY
Definition: mpool.h:61
#define NULL
Definition: resample.c:96
Definition: db.h:85
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
void * internal
Definition: db.h:137
Definition: btree.h:312
CURSOR bt_cursor
Definition: btree.h:320
MPOOL * bt_mp
Definition: btree.h:313
PAGE * bt_pinned
Definition: btree.h:318
indx_t bt_ovflsize
Definition: btree.h:339
enum _btree::@506 bt_order
EPGNO bt_last
Definition: btree.h:343
EPGNO pg
Definition: btree.h:284
Definition: btree.h:254
indx_t index
Definition: btree.h:256
PAGE * page
Definition: btree.h:255
pgno_t pgno
Definition: btree.h:250
indx_t index
Definition: btree.h:251
Definition: btree.h:75
pgno_t prevpg
Definition: btree.h:77
indx_t lower
Definition: btree.h:89
indx_t upper
Definition: btree.h:90
pgno_t pgno
Definition: btree.h:76
indx_t linp[1]
Definition: btree.h:91
pgno_t nextpg
Definition: btree.h:78

References __bt_dleaf(), __bt_search(), __bt_setcur(), __bt_split(), __ovfl_put(), B_MODIFIED, B_NODUPS, B_RDONLY, _btree::bt_cursor, bt_fast(), _btree::bt_last, _btree::bt_mp, _btree::bt_order, _btree::bt_ovflsize, _btree::bt_pinned, CURS_ACQUIRE, CURS_AFTER, CURS_BEFORE, CURS_INIT, DBT::data, db, dbp, errno, F_ISSET, F_SET, if(), _epgno::index, _epg::index, __db::internal, _page::linp, _page::lower, MPOOL_DIRTY, mpool_get(), mpool_put(), NBLEAFDBT, NEXTINDEX, _page::nextpg, NOVFLSIZE, NULL, P_BIGDATA, P_BIGKEY, P_INVALID, _epg::page, _cursor::pg, _page::pgno, _epgno::pgno, _page::prevpg, R_CURSOR, R_NOOVERWRITE, R_SETCURSOR, RET_ERROR, RET_SPECIAL, RET_SUCCESS, DBT::size, status, _page::upper, and WR_BLEAF.

Referenced by __bt_open().

◆ __P()

static EPG *bt_fast __P ( (BTREE *, const DBT *, const DBT *, int *)  )
static

◆ bt_fast()

static EPG * bt_fast ( BTREE t,
const DBT key,
const DBT data,
int *  exactp 
)
static

Definition at line 267 of file bt_put.c.

271{
272 PAGE *h;
273 u_int32_t nbytes;
274 int cmp;
275
276 if ((h = mpool_get(t->bt_mp, t->bt_last.pgno, 0)) == NULL) {
277 t->bt_order = NOT;
278 return (NULL);
279 }
280 t->bt_cur.page = h;
281 t->bt_cur.index = t->bt_last.index;
282
283 /*
284 * If won't fit in this page or have too many keys in this page,
285 * have to search to get split stack.
286 */
287 nbytes = NBLEAFDBT(key->size, data->size);
288 if ((u_int32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t))
289 goto miss;
290
291 if (t->bt_order == FORWARD) {
292 if (t->bt_cur.page->nextpg != P_INVALID)
293 goto miss;
294 if (t->bt_cur.index != NEXTINDEX(h) - 1)
295 goto miss;
296 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) < 0)
297 goto miss;
298 t->bt_last.index = cmp ? ++t->bt_cur.index : t->bt_cur.index;
299 } else {
300 if (t->bt_cur.page->prevpg != P_INVALID)
301 goto miss;
302 if (t->bt_cur.index != 0)
303 goto miss;
304 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) > 0)
305 goto miss;
306 t->bt_last.index = 0;
307 }
308 *exactp = cmp == 0;
309#ifdef STATISTICS
310 ++bt_cache_hit;
311#endif
312 return (&t->bt_cur);
313
314miss:
315#ifdef STATISTICS
316 ++bt_cache_miss;
317#endif
318 t->bt_order = NOT;
319 mpool_put(t->bt_mp, h, 0);
320 return (NULL);
321}
int __bt_cmp(BTREE *t, const DBT *k1, EPG *e)
Definition: bt_utils.c:153
EPG bt_cur
Definition: btree.h:317

References __bt_cmp(), _btree::bt_cur, _btree::bt_last, _btree::bt_mp, _btree::bt_order, _epgno::index, _epg::index, _page::lower, mpool_get(), mpool_put(), NBLEAFDBT, NEXTINDEX, _page::nextpg, NULL, P_INVALID, _epg::page, _epgno::pgno, _page::prevpg, DBT::size, and _page::upper.

Referenced by __bt_put().