Asterisk - The Open Source Telephony Project  18.5.0
Functions
rec_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 "recno.h"
Include dependency graph for rec_put.c:

Go to the source code of this file.

Functions

int __rec_iput (BTREE *t, recno_t nrec, const DBT *data, u_int flags)
 
int __rec_put (DB *dbp, DBT *key, const DBT *data, u_int flags) const
 

Function Documentation

◆ __rec_iput()

int __rec_iput ( BTREE t,
recno_t  nrec,
const DBT data,
u_int  flags 
)

Definition at line 190 of file rec_put.c.

References __bt_split(), __ovfl_put(), __rec_dleaf(), __rec_search(), B_MODIFIED, _btree::bt_mp, _btree::bt_nrecs, _btree::bt_ovflsize, DBT::data, db, F_SET, _epg::index, _page::linp, _page::lower, MPOOL_DIRTY, mpool_put(), NEXTINDEX, NOVFLSIZE, NRLEAFDBT, NULL, P_BIGDATA, _epg::page, R_IAFTER, R_IBEFORE, RET_ERROR, RET_SUCCESS, SEARCH, SINSERT, DBT::size, status, _page::upper, and WR_RLEAF.

Referenced by __rec_fmap(), __rec_fpipe(), __rec_put(), __rec_vmap(), and __rec_vpipe().

195 {
196  DBT tdata;
197  EPG *e;
198  PAGE *h;
199  indx_t idx, nxtindex;
200  pgno_t pg;
201  u_int32_t nbytes;
202  int dflags, status;
203  char *dest, db[NOVFLSIZE];
204 
205  /*
206  * If the data won't fit on a page, store it on indirect pages.
207  *
208  * XXX
209  * If the insert fails later on, these pages aren't recovered.
210  */
211  if (data->size > t->bt_ovflsize) {
212  if (__ovfl_put(t, data, &pg) == RET_ERROR)
213  return (RET_ERROR);
214  tdata.data = db;
215  tdata.size = NOVFLSIZE;
216  *(pgno_t *)db = pg;
217  *(u_int32_t *)(db + sizeof(pgno_t)) = data->size;
218  dflags = P_BIGDATA;
219  data = &tdata;
220  } else
221  dflags = 0;
222 
223  /* __rec_search pins the returned page. */
224  if ((e = __rec_search(t, nrec,
225  nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
226  SINSERT : SEARCH)) == NULL)
227  return (RET_ERROR);
228 
229  h = e->page;
230  idx = e->index;
231 
232  /*
233  * Add the specified key/data pair to the tree. The R_IAFTER and
234  * R_IBEFORE flags insert the key after/before the specified key.
235  *
236  * Pages are split as required.
237  */
238  switch (flags) {
239  case R_IAFTER:
240  ++idx;
241  break;
242  case R_IBEFORE:
243  break;
244  default:
245  if (nrec < t->bt_nrecs &&
246  __rec_dleaf(t, h, idx) == RET_ERROR) {
247  mpool_put(t->bt_mp, h, 0);
248  return (RET_ERROR);
249  }
250  break;
251  }
252 
253  /*
254  * If not enough room, split the page. The split code will insert
255  * the key and data and unpin the current page. If inserting into
256  * the offset array, shift the pointers up.
257  */
258  nbytes = NRLEAFDBT(data->size);
259  if ((u_int32_t) (h->upper - h->lower) < nbytes + sizeof(indx_t)) {
260  status = __bt_split(t, h, NULL, data, dflags, nbytes, idx);
261  if (status == RET_SUCCESS)
262  ++t->bt_nrecs;
263  return (status);
264  }
265 
266  if (idx < (nxtindex = NEXTINDEX(h)))
267  memmove(h->linp + idx + 1, h->linp + idx,
268  (nxtindex - idx) * sizeof(indx_t));
269  h->lower += sizeof(indx_t);
270 
271  h->linp[idx] = h->upper -= nbytes;
272  dest = (char *)h + h->upper;
273  WR_RLEAF(dest, data, dflags);
274 
275  ++t->bt_nrecs;
276  F_SET(t, B_MODIFIED);
277  mpool_put(t->bt_mp, h, MPOOL_DIRTY);
278 
279  return (RET_SUCCESS);
280 }
Definition: btree.h:75
#define NRLEAFDBT(dsize)
Definition: btree.h:227
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 WR_RLEAF(p, data, flags)
Definition: btree.h:231
#define R_IBEFORE
Definition: db.h:95
#define F_SET(p, f)
Definition: btree.h:40
#define B_MODIFIED
Definition: btree.h:370
Definition: recno.h:36
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
static sqlite3 * db
dflags
Definition: app_dictate.c:63
Definition: btree.h:254
Definition: db.h:85
indx_t lower
Definition: btree.h:89
#define NULL
Definition: resample.c:96
u_int16_t indx_t
Definition: db.h:80
EPG * __rec_search(BTREE *t, recno_t recno, enum SRCHOP op)
Definition: rec_search.c:64
indx_t upper
Definition: btree.h:90
recno_t bt_nrecs
Definition: btree.h:360
#define NOVFLSIZE
Definition: btree.h:117
#define MPOOL_DIRTY
Definition: mpool.h:61
#define RET_SUCCESS
Definition: db.h:52
PAGE * page
Definition: btree.h:255
indx_t linp[1]
Definition: btree.h:91
MPOOL * bt_mp
Definition: btree.h:313
int __rec_dleaf(BTREE *t, PAGE *h, u_int32_t idx)
Definition: rec_delete.c:154
u_int32_t pgno_t
Definition: db.h:78
indx_t index
Definition: btree.h:256
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
Definition: recno.h:36
unsigned int u_int32_t
indx_t bt_ovflsize
Definition: btree.h:339
#define P_BIGDATA
Definition: btree.h:131
#define R_IAFTER
Definition: db.h:94
jack_status_t status
Definition: app_jack.c:146
int __ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg)
Definition: bt_overflow.c:139

◆ __rec_put()

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

Definition at line 62 of file rec_put.c.

References __rec_iput(), __rec_ret(), _btree::bt_bval, _btree::bt_cursor, _btree::bt_mp, _btree::bt_nrecs, _btree::bt_pinned, _btree::bt_rdata, _btree::bt_reclen, CURS_INIT, DBT::data, errno, F_ISSET, F_SET, free(), __db::internal, malloc(), mpool_put(), NULL, R_CURSOR, R_EOF, R_FIXLEN, R_IAFTER, R_IBEFORE, R_INMEM, R_MODIFIED, R_NOOVERWRITE, R_SETCURSOR, _cursor::rcursor, realloc, RET_ERROR, RET_SPECIAL, RET_SUCCESS, DBT::size, and status.

Referenced by __rec_open().

67 {
68  BTREE *t;
69  DBT fdata, tdata;
70  recno_t nrec;
71  int status;
72 
73  t = dbp->internal;
74 
75  /* Toss any page pinned across calls. */
76  if (t->bt_pinned != NULL) {
77  mpool_put(t->bt_mp, t->bt_pinned, 0);
78  t->bt_pinned = NULL;
79  }
80 
81  /*
82  * If using fixed-length records, and the record is long, return
83  * EINVAL. If it's short, pad it out. Use the record data return
84  * memory, it's only short-term.
85  */
86  if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
87  if (data->size > t->bt_reclen)
88  goto einval;
89 
90  if (t->bt_rdata.size < t->bt_reclen) {
91  t->bt_rdata.data = t->bt_rdata.data == NULL ?
92  malloc(t->bt_reclen) :
93  realloc(t->bt_rdata.data, t->bt_reclen);
94  if (t->bt_rdata.data == NULL)
95  return (RET_ERROR);
96  t->bt_rdata.size = t->bt_reclen;
97  }
98  memmove(t->bt_rdata.data, data->data, data->size);
99  memset((char *)t->bt_rdata.data + data->size,
100  t->bt_bval, t->bt_reclen - data->size);
101  fdata.data = t->bt_rdata.data;
102  fdata.size = t->bt_reclen;
103  } else {
104  fdata.data = data->data;
105  fdata.size = data->size;
106  }
107 
108  switch (flags) {
109  case R_CURSOR:
110  if (!F_ISSET(&t->bt_cursor, CURS_INIT))
111  goto einval;
112  nrec = t->bt_cursor.rcursor;
113  break;
114  case R_SETCURSOR:
115  if ((nrec = *(recno_t *)key->data) == 0)
116  goto einval;
117  break;
118  case R_IAFTER:
119  if ((nrec = *(recno_t *)key->data) == 0) {
120  nrec = 1;
121  flags = R_IBEFORE;
122  }
123  break;
124  case 0:
125  case R_IBEFORE:
126  if ((nrec = *(recno_t *)key->data) == 0)
127  goto einval;
128  break;
129  case R_NOOVERWRITE:
130  if ((nrec = *(recno_t *)key->data) == 0)
131  goto einval;
132  if (nrec <= t->bt_nrecs)
133  return (RET_SPECIAL);
134  break;
135  default:
136 einval: errno = EINVAL;
137  return (RET_ERROR);
138  }
139 
140  /*
141  * Make sure that records up to and including the put record are
142  * already in the database. If skipping records, create empty ones.
143  */
144  if (nrec > t->bt_nrecs) {
145  if (!F_ISSET(t, R_EOF | R_INMEM) &&
146  t->bt_irec(t, nrec) == RET_ERROR)
147  return (RET_ERROR);
148  if (nrec > t->bt_nrecs + 1) {
149  if (F_ISSET(t, R_FIXLEN)) {
150  if ((tdata.data =
151  (void *)malloc(t->bt_reclen)) == NULL)
152  return (RET_ERROR);
153  tdata.size = t->bt_reclen;
154  memset(tdata.data, t->bt_bval, tdata.size);
155  } else {
156  tdata.data = NULL;
157  tdata.size = 0;
158  }
159  while (nrec > t->bt_nrecs + 1)
160  if (__rec_iput(t,
161  t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
162  return (RET_ERROR);
163  if (F_ISSET(t, R_FIXLEN))
164  free(tdata.data);
165  }
166  }
167 
168  if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
169  return (status);
170 
171  if (flags == R_SETCURSOR)
172  t->bt_cursor.rcursor = nrec;
173 
174  F_SET(t, R_MODIFIED);
175  return (__rec_ret(t, NULL, nrec, key, NULL));
176 }
#define R_CURSOR
Definition: db.h:91
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
#define RET_ERROR
Definition: db.h:51
#define R_IBEFORE
Definition: db.h:95
#define realloc(a, b)
Definition: astmm.h:163
#define F_SET(p, f)
Definition: btree.h:40
#define F_ISSET(p, f)
Definition: btree.h:42
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
CURSOR bt_cursor
Definition: btree.h:320
recno_t rcursor
Definition: btree.h:286
void * internal
Definition: db.h:137
Definition: db.h:85
#define NULL
Definition: resample.c:96
recno_t bt_nrecs
Definition: btree.h:360
Definition: btree.h:312
#define R_MODIFIED
Definition: btree.h:382
char * malloc()
int __rec_ret(BTREE *t, EPG *e, recno_t nrec, DBT *key, DBT *data)
Definition: rec_utils.c:62
u_char bt_bval
Definition: btree.h:362
void free()
#define RET_SUCCESS
Definition: db.h:52
PAGE * bt_pinned
Definition: btree.h:318
DBT bt_rdata
Definition: btree.h:333
#define CURS_INIT
Definition: btree.h:291
MPOOL * bt_mp
Definition: btree.h:313
#define R_FIXLEN
Definition: btree.h:379
int errno
#define R_INMEM
Definition: btree.h:381
#define R_EOF
Definition: btree.h:378
size_t bt_reclen
Definition: btree.h:361
#define R_SETCURSOR
Definition: db.h:100
#define R_NOOVERWRITE
Definition: db.h:98
#define RET_SPECIAL
Definition: db.h:53
u_int32_t recno_t
Definition: db.h:82
#define R_IAFTER
Definition: db.h:94
int __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
Definition: rec_put.c:190
jack_status_t status
Definition: app_jack.c:146