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(
24 struct berval *bvals[2];
26 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
30 * dn index entry - make it look like an attribute so it works
31 * with bdb2i_index_add_values() call
34 bv.bv_val = ch_strdup( e->e_ndn );
35 bv.bv_len = strlen( bv.bv_val );
39 /* add the dn to the indexes */
41 char *dn = ch_strdup( "dn" );
42 bdb2i_index_add_values( be, dn, bvals, e->e_id );
48 /* add each attribute to the indexes */
49 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
50 bdb2i_index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
53 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
67 for ( ; ml != NULL; ml = ml->ml_next ) {
68 LDAPMod *mod = &ml->ml_mod;
70 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
72 case LDAP_MOD_REPLACE:
73 rc = bdb2i_index_add_values( be, mod->mod_type,
74 mod->mod_bvalues, id );
76 case LDAP_MOD_SOFTADD:
101 int indexmask, syntax;
103 char *realval, *tmpval;
108 ldbm_datum_init( key );
110 prefix = index2prefix( indextype );
111 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_read( \"%s\" \"%c\" \"%s\" )\n",
114 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
115 if ( ! (indextype & indexmask) ) {
116 idl = bdb2i_idl_allids( be );
117 Debug( LDAP_DEBUG_TRACE,
118 "<= bdb2i_index_read %ld candidates (allids - not indexed)\n",
119 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
123 attr_normalize( type );
124 at_cn = at_canonical_name(type);
126 if ( (db = bdb2i_cache_open( be, at_cn, BDB2_SUFFIX, LDBM_WRCREAT ))
128 Debug( LDAP_DEBUG_ANY,
129 "<= bdb2i_index_read NULL (could not open %s%s)\n", at_cn,
136 if ( prefix != UNKNOWN_PREFIX ) {
137 unsigned int len = strlen( val );
139 if ( (len + 2) < sizeof(buf) ) {
142 /* value + prefix + null */
143 tmpval = (char *) ch_malloc( len + 2 );
147 strcpy( &realval[1], val );
151 key.dsize = strlen( realval ) + 1;
153 idl = bdb2i_idl_fetch( be, db, key );
154 if ( tmpval != NULL ) {
158 bdb2i_cache_close( be, db );
160 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_index_read %ld candidates\n",
161 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
181 char prefix = index2prefix( indextype );
183 ldbm_datum_init( key );
185 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
187 if ( prefix != UNKNOWN_PREFIX ) {
188 unsigned int len = strlen( val );
190 if ( (len + 2) < sizeof(buf) ) {
193 /* value + prefix + null */
194 tmpval = (char *) ch_malloc( len + 2 );
198 strcpy( &realval[1], val );
202 key.dsize = strlen( realval ) + 1;
204 rc = bdb2i_idl_insert_key( be, db, key, id );
206 if ( tmpval != NULL ) {
210 ldap_pvt_thread_yield();
212 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
217 bdb2i_index_add_values(
220 struct berval **vals,
224 char *val, *p, *code, *w;
226 int indexmask, syntax;
227 char buf[SUBLEN + 1];
235 Debug( LDAP_DEBUG_TRACE,
236 "=> bdb2i_index_add_values( \"%s\", NULL, %ld )\n",
241 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_add_values( \"%s\", %ld )\n", type,
243 attr_normalize( type );
244 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
245 if ( indexmask == 0 ) {
248 at_cn = at_canonical_name(type);
250 if ( (db = bdb2i_cache_open( be, at_cn, BDB2_SUFFIX, LDBM_WRCREAT ))
252 Debug( LDAP_DEBUG_ANY,
253 "<= bdb2i_index_add_values -1 (could not open/create %s%s)\n",
254 at_cn, BDB2_SUFFIX, 0 );
258 for ( i = 0; vals[i] != NULL; i++ ) {
260 * presence index entry
262 if ( indexmask & INDEX_PRESENCE ) {
263 add_value( be, db, at_cn, INDEX_PRESENCE, "*", id );
266 Debug( LDAP_DEBUG_TRACE, "*** bdb2i_index_add_values syntax 0x%x syntax bin 0x%x\n",
267 syntax, SYNTAX_BIN, 0 );
268 if ( syntax & SYNTAX_BIN ) {
269 bdb2i_cache_close( be, db );
274 len = vals[i]->bv_len;
277 if ( len + 2 > sizeof(vbuf) ) {
278 bigbuf = (char *) ch_malloc( len + 1 );
283 (void) memcpy( val, vals[i]->bv_val, len );
286 value_normalize( val, syntax );
288 /* value_normalize could change the length of val */
292 * equality index entry
294 if ( indexmask & INDEX_EQUALITY ) {
295 add_value( be, db, at_cn, INDEX_EQUALITY, val, id );
299 * approximate index entry
301 if ( indexmask & INDEX_APPROX ) {
302 for ( w = first_word( val ); w != NULL;
303 w = next_word( w ) ) {
304 if ( (code = phonetic( w )) != NULL ) {
305 add_value( be, db, at_cn, INDEX_APPROX,
313 * substrings index entry
315 if ( indexmask & INDEX_SUB ) {
316 /* leading and trailing */
317 if ( len > SUBLEN - 2 ) {
319 for ( j = 0; j < SUBLEN - 1; j++ ) {
324 add_value( be, db, at_cn, INDEX_SUB, buf, id );
326 p = val + len - SUBLEN + 1;
327 for ( j = 0; j < SUBLEN - 1; j++ ) {
330 buf[SUBLEN - 1] = '$';
333 add_value( be, db, at_cn, INDEX_SUB, buf, id );
337 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
338 for ( j = 0; j < SUBLEN; j++ ) {
343 add_value( be, db, at_cn, INDEX_SUB, buf, id );
347 if ( bigbuf != NULL ) {
351 bdb2i_cache_close( be, db );
357 index2prefix( int indextype )
361 switch ( indextype ) {
366 prefix = APPROX_PREFIX;
372 prefix = UNKNOWN_PREFIX;