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

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

Definition at line 153 of file bt_utils.c.

References __ovfl_get(), _binternal::bytes, _bleaf::bytes, DBT::data, _binternal::flags, _bleaf::flags, _page::flags, GETBINTERNAL, GETBLEAF, h, _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().

00157 {
00158    BINTERNAL *bi;
00159    BLEAF *bl;
00160    DBT k2;
00161    PAGE *h;
00162    void *bigkey;
00163 
00164    /*
00165     * The left-most key on internal pages, at any level of the tree, is
00166     * guaranteed by the following code to be less than any user key.
00167     * This saves us from having to update the leftmost key on an internal
00168     * page when the user inserts a new key in the tree smaller than
00169     * anything we've yet seen.
00170     */
00171    h = e->page;
00172    if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
00173       return (1);
00174 
00175    bigkey = NULL;
00176    if (h->flags & P_BLEAF) {
00177       bl = GETBLEAF(h, e->index);
00178       if (bl->flags & P_BIGKEY)
00179          bigkey = bl->bytes;
00180       else {
00181          k2.data = bl->bytes;
00182          k2.size = bl->ksize;
00183       }
00184    } else {
00185       bi = GETBINTERNAL(h, e->index);
00186       if (bi->flags & P_BIGKEY)
00187          bigkey = bi->bytes;
00188       else {
00189          k2.data = bi->bytes;
00190          k2.size = bi->ksize;
00191       }
00192    }
00193 
00194    if (bigkey) {
00195       if (__ovfl_get(t, bigkey,
00196           &k2.size, &t->bt_rdata.data, &t->bt_rdata.size))
00197          return (RET_ERROR);
00198       k2.data = t->bt_rdata.data;
00199    }
00200    return ((*t->bt_cmp)(k1, &k2));
00201 }

int __bt_defcmp ( DBT a,
DBT b 
) const

Definition at line 216 of file bt_utils.c.

References len(), and MIN.

Referenced by __bt_open().

00218 {
00219    register size_t len;
00220    register u_char *p1, *p2;
00221 
00222    /*
00223     * XXX
00224     * If a size_t doesn't fit in an int, this routine can lose.
00225     * What we need is a integral type which is guaranteed to be
00226     * larger than a size_t, and there is no such thing.
00227     */
00228    len = MIN(a->size, b->size);
00229    for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
00230       if (*p1 != *p2)
00231          return ((int)*p1 - (int)*p2);
00232    return ((int)a->size - (int)b->size);
00233 }

size_t __bt_defpfx ( DBT a,
DBT b 
) const

Definition at line 246 of file bt_utils.c.

References len(), and MIN.

Referenced by __bt_open().

00248 {
00249    register u_char *p1, *p2;
00250    register size_t cnt, len;
00251 
00252    cnt = 1;
00253    len = MIN(a->size, b->size);
00254    for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
00255       if (*p1 != *p2)
00256          return (cnt);
00257 
00258    /* a->size must be <= b->size, or they wouldn't be in this order. */
00259    return (a->size < b->size ? a->size + 1 : a->size);
00260 }

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, 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().

00072 {
00073    BLEAF *bl;
00074    void *p;
00075 
00076    bl = GETBLEAF(e->page, e->index);
00077 
00078    /*
00079     * We must copy big keys/data to make them contiguous.  Otherwise,
00080     * leave the page pinned and don't copy unless the user specified
00081     * concurrent access.
00082     */
00083    if (key == NULL)
00084       goto dataonly;
00085 
00086    if (bl->flags & P_BIGKEY) {
00087       if (__ovfl_get(t, bl->bytes,
00088           &key->size, &rkey->data, &rkey->size))
00089          return (RET_ERROR);
00090       key->data = rkey->data;
00091    } else if (copy || F_ISSET(t, B_DB_LOCK)) {
00092       if (bl->ksize > rkey->size) {
00093          p = (void *)(rkey->data == NULL ?
00094              malloc(bl->ksize) : realloc(rkey->data, bl->ksize));
00095          if (p == NULL)
00096             return (RET_ERROR);
00097          rkey->data = p;
00098          rkey->size = bl->ksize;
00099       }
00100       memmove(rkey->data, bl->bytes, bl->ksize);
00101       key->size = bl->ksize;
00102       key->data = rkey->data;
00103    } else {
00104       key->size = bl->ksize;
00105       key->data = bl->bytes;
00106    }
00107 
00108 dataonly:
00109    if (data == NULL)
00110       return (RET_SUCCESS);
00111 
00112    if (bl->flags & P_BIGDATA) {
00113       if (__ovfl_get(t, bl->bytes + bl->ksize,
00114           &data->size, &rdata->data, &rdata->size))
00115          return (RET_ERROR);
00116       data->data = rdata->data;
00117    } else if (copy || F_ISSET(t, B_DB_LOCK)) {
00118       /* Use +1 in case the first record retrieved is 0 length. */
00119       if (bl->dsize + 1 > rdata->size) {
00120          p = (void *)(rdata->data == NULL ?
00121              malloc(bl->dsize + 1) :
00122              realloc(rdata->data, bl->dsize + 1));
00123          if (p == NULL)
00124             return (RET_ERROR);
00125          rdata->data = p;
00126          rdata->size = bl->dsize + 1;
00127       }
00128       memmove(rdata->data, bl->bytes + bl->ksize, bl->dsize);
00129       data->size = bl->dsize;
00130       data->data = rdata->data;
00131    } else {
00132       data->size = bl->dsize;
00133       data->data = bl->bytes + bl->ksize;
00134    }
00135 
00136    return (RET_SUCCESS);
00137 }


Generated on Thu Apr 16 06:29:16 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6