1 /* index.c - routines for dealing with attribute indexes */
11 #include "back-bdb2.h"
13 static int add_value(BackendDB *be, struct dbcache *db, char *type, int indextype, char *val, ID id);
14 static int index2prefix(int indextype);
17 bdb2i_index_add_entry(
25 struct berval *bvals[2];
27 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
31 * dn index entry - make it look like an attribute so it works
32 * with bdb2i_index_add_values() call
35 bv.bv_val = ch_strdup( e->e_dn );
36 bv.bv_len = strlen( bv.bv_val );
37 (void) dn_normalize_case( bv.bv_val );
41 /* add the dn to the indexes */
42 bdb2i_index_add_values( be, "dn", bvals, e->e_id );
46 /* add each attribute to the indexes */
47 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
48 bdb2i_index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
51 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
65 for ( ; ml != NULL; ml = ml->ml_next ) {
66 LDAPMod *mod = &ml->ml_mod;
68 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
70 case LDAP_MOD_REPLACE:
71 rc = bdb2i_index_add_values( be, mod->mod_type,
72 mod->mod_bvalues, id );
99 int indexmask, syntax;
101 char *realval, *tmpval;
104 ldbm_datum_init( key );
106 prefix = index2prefix( indextype );
107 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_read( \"%s\" \"%c\" \"%s\" )\n",
110 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
111 if ( ! (indextype & indexmask) ) {
112 idl = bdb2i_idl_allids( be );
113 Debug( LDAP_DEBUG_TRACE,
114 "<= bdb2i_index_read %lu candidates (allids - not indexed)\n",
115 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
119 attr_normalize( type );
120 if ( (db = bdb2i_cache_open( be, type, BDB2_SUFFIX, LDBM_WRCREAT ))
122 Debug( LDAP_DEBUG_ANY,
123 "<= bdb2i_index_read NULL (could not open %s%s)\n", type,
130 if ( prefix != UNKNOWN_PREFIX ) {
131 unsigned int len = strlen( val );
133 if ( (len + 2) < sizeof(buf) ) {
136 /* value + prefix + null */
137 tmpval = (char *) ch_malloc( len + 2 );
141 strcpy( &realval[1], val );
145 key.dsize = strlen( realval ) + 1;
147 idl = bdb2i_idl_fetch( be, db, key );
148 if ( tmpval != NULL ) {
152 bdb2i_cache_close( be, db );
154 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_index_read %lu candidates\n",
155 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
171 ID_BLOCK *idl = NULL;
176 char prefix = index2prefix( indextype );
178 ldbm_datum_init( key );
180 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
182 if ( prefix != UNKNOWN_PREFIX ) {
183 unsigned int len = strlen( val );
185 if ( (len + 2) < sizeof(buf) ) {
188 /* value + prefix + null */
189 tmpval = (char *) ch_malloc( len + 2 );
193 strcpy( &realval[1], val );
197 key.dsize = strlen( realval ) + 1;
199 rc = bdb2i_idl_insert_key( be, db, key, id );
201 if ( tmpval != NULL ) {
206 bdb2i_idl_free( idl );
209 ldap_pvt_thread_yield();
211 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
216 bdb2i_index_add_values(
219 struct berval **vals,
223 char *val, *p, *code, *w;
225 int indexmask, syntax;
226 char buf[SUBLEN + 1];
231 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_add_values( \"%s\", %ld )\n", type,
234 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
235 if ( indexmask == 0 ) {
239 if ( (db = bdb2i_cache_open( be, type, BDB2_SUFFIX, LDBM_WRCREAT ))
241 Debug( LDAP_DEBUG_ANY,
242 "<= bdb2i_index_add_values -1 (could not open/create %s%s)\n",
243 type, BDB2_SUFFIX, 0 );
247 for ( i = 0; vals[i] != NULL; i++ ) {
249 * presence index entry
251 if ( indexmask & INDEX_PRESENCE ) {
252 add_value( be, db, type, INDEX_PRESENCE, "*", id );
255 Debug( LDAP_DEBUG_TRACE, "*** bdb2i_index_add_values syntax 0x%x syntax bin 0x%x\n",
256 syntax, SYNTAX_BIN, 0 );
257 if ( syntax & SYNTAX_BIN ) {
258 bdb2i_cache_close( be, db );
263 len = vals[i]->bv_len;
266 if ( len + 2 > sizeof(vbuf) ) {
267 bigbuf = (char *) ch_malloc( len + 1 );
272 (void) memcpy( val, vals[i]->bv_val, len );
275 value_normalize( val, syntax );
278 * equality index entry
280 if ( indexmask & INDEX_EQUALITY ) {
281 add_value( be, db, type, INDEX_EQUALITY, val, id );
285 * approximate index entry
287 if ( indexmask & INDEX_APPROX ) {
288 for ( w = first_word( val ); w != NULL;
289 w = next_word( w ) ) {
290 if ( (code = phonetic( w )) != NULL ) {
291 add_value( be, db, type, INDEX_APPROX,
299 * substrings index entry
301 if ( indexmask & INDEX_SUB ) {
302 /* leading and trailing */
303 if ( len > SUBLEN - 2 ) {
305 for ( j = 0; j < SUBLEN - 1; j++ ) {
310 add_value( be, db, type, INDEX_SUB, buf, id );
312 p = val + len - SUBLEN + 1;
313 for ( j = 0; j < SUBLEN - 1; j++ ) {
316 buf[SUBLEN - 1] = '$';
319 add_value( be, db, type, INDEX_SUB, buf, id );
323 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
324 for ( j = 0; j < SUBLEN; j++ ) {
329 add_value( be, db, type, INDEX_SUB, buf, id );
333 if ( bigbuf != NULL ) {
337 bdb2i_cache_close( be, db );
343 index2prefix( int indextype )
347 switch ( indextype ) {
352 prefix = APPROX_PREFIX;
358 prefix = UNKNOWN_PREFIX;