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

Go to the source code of this file.

Functions

EPG__bt_search (BTREE *t, const DBT *key, int *exactp)
 
static int __bt_snext (BTREE *t, PAGE *h, const DBT *key, int *exactp)
 
static int __bt_sprev (BTREE *t, PAGE *h, const DBT *key, int *exactp)
 
static int __bt_snext __P ((BTREE *, PAGE *, const DBT *, int *))
 

Function Documentation

◆ __bt_search()

EPG * __bt_search ( BTREE t,
const DBT key,
int *  exactp 
)

Definition at line 66 of file bt_search.c.

70{
71 PAGE *h;
72 indx_t base, index, lim;
73 pgno_t pg;
74 int cmp;
75
76 BT_CLR(t);
77 for (pg = P_ROOT;;) {
78 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
79 return (NULL);
80
81 /* Do a binary search on the current page. */
82 t->bt_cur.page = h;
83 for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
84 t->bt_cur.index = index = base + (lim >> 1);
85 if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) {
86 if (h->flags & P_BLEAF) {
87 *exactp = 1;
88 return (&t->bt_cur);
89 }
90 goto next;
91 }
92 if (cmp > 0) {
93 base = index + 1;
94 --lim;
95 }
96 }
97
98 /*
99 * If it's a leaf page, we're almost done. If no duplicates
100 * are allowed, or we have an exact match, we're done. Else,
101 * it's possible that there were matching keys on this page,
102 * which later deleted, and we're on a page with no matches
103 * while there are matches on other pages. If at the start or
104 * end of a page, check the adjacent page.
105 */
106 if (h->flags & P_BLEAF) {
107 if (!F_ISSET(t, B_NODUPS)) {
108 if (base == 0 &&
109 h->prevpg != P_INVALID &&
110 __bt_sprev(t, h, key, exactp))
111 return (&t->bt_cur);
112 if (base == NEXTINDEX(h) &&
113 h->nextpg != P_INVALID &&
114 __bt_snext(t, h, key, exactp))
115 return (&t->bt_cur);
116 }
117 *exactp = 0;
118 t->bt_cur.index = base;
119 return (&t->bt_cur);
120 }
121
122 /*
123 * No match found. Base is the smallest index greater than
124 * key and may be zero or a last + 1 index. If it's non-zero,
125 * decrement by one, and record the internal page which should
126 * be a parent page for the key. If a split later occurs, the
127 * inserted page will be to the right of the saved page.
128 */
129 index = base ? base - 1 : base;
130
131next: BT_PUSH(t, h->pgno, index);
132 pg = GETBINTERNAL(h, index)->pgno;
133 mpool_put(t->bt_mp, h, 0);
134 }
135}
static int __bt_sprev(BTREE *t, PAGE *h, const DBT *key, int *exactp)
Definition: bt_search.c:190
static int __bt_snext(BTREE *t, PAGE *h, const DBT *key, int *exactp)
Definition: bt_search.c:151
int __bt_cmp(BTREE *t, const DBT *k1, EPG *e)
Definition: bt_utils.c:153
#define F_ISSET(p, f)
Definition: btree.h:42
#define BT_CLR(t)
Definition: btree.h:328
#define B_NODUPS
Definition: btree.h:374
#define NEXTINDEX(p)
Definition: btree.h:98
#define GETBINTERNAL(pg, indx)
Definition: btree.h:138
#define P_INVALID
Definition: btree.h:63
#define P_BLEAF
Definition: btree.h:81
#define BT_PUSH(t, p, i)
Definition: btree.h:322
#define P_ROOT
Definition: btree.h:65
u_int16_t indx_t
Definition: db.h:80
u_int32_t pgno_t
Definition: db.h:78
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 NULL
Definition: resample.c:96
MPOOL * bt_mp
Definition: btree.h:313
EPG bt_cur
Definition: btree.h:317
indx_t index
Definition: btree.h:256
PAGE * page
Definition: btree.h:255
Definition: btree.h:75
pgno_t prevpg
Definition: btree.h:77
u_int32_t flags
Definition: btree.h:87
pgno_t pgno
Definition: btree.h:76
pgno_t nextpg
Definition: btree.h:78

References __bt_cmp(), __bt_snext(), __bt_sprev(), B_NODUPS, BT_CLR, _btree::bt_cur, _btree::bt_mp, BT_PUSH, F_ISSET, _page::flags, GETBINTERNAL, _epg::index, mpool_get(), mpool_put(), NEXTINDEX, _page::nextpg, NULL, P_BLEAF, P_INVALID, P_ROOT, _epg::page, _page::pgno, and _page::prevpg.

Referenced by __bt_bdelete(), __bt_first(), __bt_get(), __bt_put(), and __bt_stkacq().

◆ __bt_snext()

static int __bt_snext ( BTREE t,
PAGE h,
const DBT key,
int *  exactp 
)
static

Definition at line 151 of file bt_search.c.

156{
157 EPG e;
158
159 /*
160 * Get the next page. The key is either an exact
161 * match, or not as good as the one we already have.
162 */
163 if ((e.page = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
164 return (0);
165 e.index = 0;
166 if (__bt_cmp(t, key, &e) == 0) {
167 mpool_put(t->bt_mp, h, 0);
168 t->bt_cur = e;
169 *exactp = 1;
170 return (1);
171 }
172 mpool_put(t->bt_mp, e.page, 0);
173 return (0);
174}
Definition: btree.h:254

References __bt_cmp(), _btree::bt_cur, _btree::bt_mp, _epg::index, mpool_get(), mpool_put(), _page::nextpg, NULL, and _epg::page.

Referenced by __bt_search().

◆ __bt_sprev()

static int __bt_sprev ( BTREE t,
PAGE h,
const DBT key,
int *  exactp 
)
static

Definition at line 190 of file bt_search.c.

195{
196 EPG e;
197
198 /*
199 * Get the previous page. The key is either an exact
200 * match, or not as good as the one we already have.
201 */
202 if ((e.page = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
203 return (0);
204 e.index = NEXTINDEX(e.page) - 1;
205 if (__bt_cmp(t, key, &e) == 0) {
206 mpool_put(t->bt_mp, h, 0);
207 t->bt_cur = e;
208 *exactp = 1;
209 return (1);
210 }
211 mpool_put(t->bt_mp, e.page, 0);
212 return (0);
213}

References __bt_cmp(), _btree::bt_cur, _btree::bt_mp, _epg::index, mpool_get(), mpool_put(), NEXTINDEX, NULL, _epg::page, and _page::prevpg.

Referenced by __bt_search().

◆ __P()

static int __bt_sprev __P ( (BTREE *, PAGE *, const DBT *, int *)  )
static