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

Go to the source code of this file.

Functions

int __bt_cmp (BTREE *t, const DBT *k1, EPG *e)
 
int __bt_defcmp (DBT *a, DBT *b) const
 
size_t __bt_defpfx (DBT *a, DBT *b) const
 
int __bt_ret (BTREE *t, EPG *e, DBT *key, DBT *rkey, DBT *data, DBT *rdata, int copy)
 

Function Documentation

◆ __bt_cmp()

int __bt_cmp ( BTREE t,
const DBT k1,
EPG e 
)

Definition at line 153 of file bt_utils.c.

References __ovfl_get(), _btree::bt_rdata, _binternal::bytes, _bleaf::bytes, DBT::data, _page::flags, _binternal::flags, _bleaf::flags, GETBINTERNAL, GETBLEAF, _epg::index, _binternal::ksize, _bleaf::ksize, NULL, P_BIGKEY, P_BLEAF, P_INVALID, _epg::page, _page::prevpg, RET_ERROR, and DBT::size.

Referenced by __bt_bdelete(), __bt_curdel(), __bt_first(), __bt_search(), __bt_snext(), __bt_sprev(), and bt_fast().

157 {
158  BINTERNAL *bi;
159  BLEAF *bl;
160  DBT k2;
161  PAGE *h;
162  void *bigkey;
163 
164  /*
165  * The left-most key on internal pages, at any level of the tree, is
166  * guaranteed by the following code to be less than any user key.
167  * This saves us from having to update the leftmost key on an internal
168  * page when the user inserts a new key in the tree smaller than
169  * anything we've yet seen.
170  */
171  h = e->page;
172  if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
173  return (1);
174 
175  bigkey = NULL;
176  if (h->flags & P_BLEAF) {
177  bl = GETBLEAF(h, e->index);
178  if (bl->flags & P_BIGKEY)
179  bigkey = bl->bytes;
180  else {
181  k2.data = bl->bytes;
182  k2.size = bl->ksize;
183  }
184  } else {
185  bi = GETBINTERNAL(h, e->index);
186  if (bi->flags & P_BIGKEY)
187  bigkey = bi->bytes;
188  else {
189  k2.data = bi->bytes;
190  k2.size = bi->ksize;
191  }
192  }
193 
194  if (bigkey) {
195  if (__ovfl_get(t, bigkey,
196  &k2.size, &t->bt_rdata.data, &t->bt_rdata.size))
197  return (RET_ERROR);
198  k2.data = t->bt_rdata.data;
199  }
200  return ((*t->bt_cmp)(k1, &k2));
201 }
Definition: btree.h:75
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
#define RET_ERROR
Definition: db.h:51
#define GETBLEAF(pg, indx)
Definition: btree.h:188
int __ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz)
Definition: bt_overflow.c:80
u_char flags
Definition: btree.h:183
Definition: db.h:85
#define NULL
Definition: resample.c:96
#define GETBINTERNAL(pg, indx)
Definition: btree.h:138
u_int32_t flags
Definition: btree.h:87
u_char flags
Definition: btree.h:133
#define P_BIGKEY
Definition: btree.h:132
DBT bt_rdata
Definition: btree.h:333
PAGE * page
Definition: btree.h:255
indx_t index
Definition: btree.h:256
char bytes[1]
Definition: btree.h:184
pgno_t prevpg
Definition: btree.h:77
Definition: btree.h:180
#define P_INVALID
Definition: btree.h:63
u_int32_t ksize
Definition: btree.h:181
#define P_BLEAF
Definition: btree.h:81
u_int32_t ksize
Definition: btree.h:129
char bytes[1]
Definition: btree.h:134

◆ __bt_defcmp()

int __bt_defcmp ( DBT a,
DBT b 
) const

Definition at line 216 of file bt_utils.c.

References DBT::data, len(), MIN, and DBT::size.

Referenced by __bt_open().

218 {
219  register size_t len;
220  register u_char *p1, *p2;
221 
222  /*
223  * XXX
224  * If a size_t doesn't fit in an int, this routine can lose.
225  * What we need is a integral type which is guaranteed to be
226  * larger than a size_t, and there is no such thing.
227  */
228  len = MIN(a->size, b->size);
229  for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
230  if (*p1 != *p2)
231  return ((int)*p1 - (int)*p2);
232  return ((int)a->size - (int)b->size);
233 }
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
#define MIN(a, b)
Definition: utils.h:226
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

◆ __bt_defpfx()

size_t __bt_defpfx ( DBT a,
DBT b 
) const

Definition at line 246 of file bt_utils.c.

References DBT::data, len(), MIN, and DBT::size.

Referenced by __bt_open().

248 {
249  register u_char *p1, *p2;
250  register size_t cnt, len;
251 
252  cnt = 1;
253  len = MIN(a->size, b->size);
254  for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
255  if (*p1 != *p2)
256  return (cnt);
257 
258  /* a->size must be <= b->size, or they wouldn't be in this order. */
259  return (a->size < b->size ? a->size + 1 : a->size);
260 }
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
#define MIN(a, b)
Definition: utils.h:226
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

◆ __bt_ret()

int __bt_ret ( BTREE t,
EPG e,
DBT key,
DBT rkey,
DBT data,
DBT rdata,
int  copy 
)

Definition at line 67 of file bt_utils.c.

References __ovfl_get(), B_DB_LOCK, _bleaf::bytes, copy(), DBT::data, _bleaf::dsize, F_ISSET, _bleaf::flags, GETBLEAF, _epg::index, _bleaf::ksize, malloc(), NULL, P_BIGDATA, P_BIGKEY, _epg::page, realloc, RET_ERROR, RET_SUCCESS, and DBT::size.

Referenced by __bt_curdel(), __bt_get(), and __bt_seq().

72 {
73  BLEAF *bl;
74  void *p;
75 
76  bl = GETBLEAF(e->page, e->index);
77 
78  /*
79  * We must copy big keys/data to make them contiguous. Otherwise,
80  * leave the page pinned and don't copy unless the user specified
81  * concurrent access.
82  */
83  if (key == NULL)
84  goto dataonly;
85 
86  if (bl->flags & P_BIGKEY) {
87  if (__ovfl_get(t, bl->bytes,
88  &key->size, &rkey->data, &rkey->size))
89  return (RET_ERROR);
90  key->data = rkey->data;
91  } else if (copy || F_ISSET(t, B_DB_LOCK)) {
92  if (bl->ksize > rkey->size) {
93  p = (void *)(rkey->data == NULL ?
94  malloc(bl->ksize) : realloc(rkey->data, bl->ksize));
95  if (p == NULL)
96  return (RET_ERROR);
97  rkey->data = p;
98  rkey->size = bl->ksize;
99  }
100  memmove(rkey->data, bl->bytes, bl->ksize);
101  key->size = bl->ksize;
102  key->data = rkey->data;
103  } else {
104  key->size = bl->ksize;
105  key->data = bl->bytes;
106  }
107 
108 dataonly:
109  if (data == NULL)
110  return (RET_SUCCESS);
111 
112  if (bl->flags & P_BIGDATA) {
113  if (__ovfl_get(t, bl->bytes + bl->ksize,
114  &data->size, &rdata->data, &rdata->size))
115  return (RET_ERROR);
116  data->data = rdata->data;
117  } else if (copy || F_ISSET(t, B_DB_LOCK)) {
118  /* Use +1 in case the first record retrieved is 0 length. */
119  if (bl->dsize + 1 > rdata->size) {
120  p = (void *)(rdata->data == NULL ?
121  malloc(bl->dsize + 1) :
122  realloc(rdata->data, bl->dsize + 1));
123  if (p == NULL)
124  return (RET_ERROR);
125  rdata->data = p;
126  rdata->size = bl->dsize + 1;
127  }
128  memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize);
129  data->size = bl->dsize;
130  data->data = rdata->data;
131  } else {
132  data->size = bl->dsize;
133  data->data = bl->bytes + bl->ksize;
134  }
135 
136  return (RET_SUCCESS);
137 }
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
#define RET_ERROR
Definition: db.h:51
#define GETBLEAF(pg, indx)
Definition: btree.h:188
#define realloc(a, b)
Definition: astmm.h:163
#define F_ISSET(p, f)
Definition: btree.h:42
u_int32_t dsize
Definition: btree.h:182
int __ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz)
Definition: bt_overflow.c:80
u_char flags
Definition: btree.h:183
static int copy(char *infile, char *outfile)
Utility function to copy a file.
#define NULL
Definition: resample.c:96
char * malloc()
#define RET_SUCCESS
Definition: db.h:52
#define P_BIGKEY
Definition: btree.h:132
PAGE * page
Definition: btree.h:255
indx_t index
Definition: btree.h:256
char bytes[1]
Definition: btree.h:184
Definition: btree.h:180
u_int32_t ksize
Definition: btree.h:181
#define P_BIGDATA
Definition: btree.h:131
#define B_DB_LOCK
Definition: btree.h:385