1 /* index.c - routines for dealing with attribute indexes */
11 #include "back-ldbm.h"
13 static int add_value(Backend *be, struct dbcache *db, char *type, int indextype, char *val, ID id);
14 static int index2prefix(int indextype);
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 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 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 ) {
51 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
54 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
68 for ( ; ml != NULL; ml = ml->ml_next ) {
69 LDAPMod *mod = &ml->ml_mod;
71 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
73 case LDAP_MOD_REPLACE:
74 rc = index_add_values( be, mod->mod_type,
75 mod->mod_bvalues, id );
77 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there */
102 int indexmask, syntax;
104 char *realval, *tmpval;
109 ldbm_datum_init( key );
111 prefix = index2prefix( indextype );
112 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
115 attr_masks( be->be_private, type, &indexmask, &syntax );
116 if ( ! (indextype & indexmask) ) {
117 idl = idl_allids( be );
118 Debug( LDAP_DEBUG_TRACE,
119 "<= index_read %ld candidates (allids - not indexed)\n",
120 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
124 attr_normalize( type );
125 at_on = at_official_name( type );
127 if ( (db = ldbm_cache_open( be, at_on, LDBM_SUFFIX, LDBM_WRCREAT ))
129 Debug( LDAP_DEBUG_ANY,
130 "<= index_read NULL (could not open %s%s)\n", at_on,
137 if ( prefix != UNKNOWN_PREFIX ) {
138 unsigned int len = strlen( val );
140 if ( (len + 2) < sizeof(buf) ) {
143 /* value + prefix + null */
144 tmpval = (char *) ch_malloc( len + 2 );
148 strcpy( &realval[1], val );
152 key.dsize = strlen( realval ) + 1;
154 idl = idl_fetch( be, db, key );
155 if ( tmpval != NULL ) {
159 ldbm_cache_close( be, db );
161 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
162 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
178 /* XXX do we need idl ??? */
179 ID_BLOCK *idl = NULL;
184 char prefix = index2prefix( indextype );
186 ldbm_datum_init( key );
188 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
190 if ( prefix != UNKNOWN_PREFIX ) {
191 unsigned int len = strlen( val );
193 if ( (len + 2) < sizeof(buf) ) {
196 /* value + prefix + null */
197 tmpval = (char *) ch_malloc( len + 2 );
201 strcpy( &realval[1], val );
205 key.dsize = strlen( realval ) + 1;
207 rc = idl_insert_key( be, db, key, id );
209 if ( tmpval != NULL ) {
217 ldap_pvt_thread_yield();
219 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
227 struct berval **vals,
231 char *val, *p, *code, *w;
233 int indexmask, syntax;
234 char buf[SUBLEN + 1];
239 char *at_on; /* Attribute official name */
241 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
243 attr_normalize(type);
244 attr_masks( be->be_private, type, &indexmask, &syntax );
245 if ( indexmask == 0 ) {
248 at_on = at_official_name( type );
249 if ( (db = ldbm_cache_open( be, at_on, LDBM_SUFFIX, LDBM_WRCREAT ))
251 Debug( LDAP_DEBUG_ANY,
252 "<= index_add_values -1 (could not open/create %s%s)\n",
253 at_on, LDBM_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_on, INDEX_PRESENCE, "*", id );
266 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
267 syntax, SYNTAX_BIN, 0 );
268 if ( syntax & SYNTAX_BIN ) {
269 ldbm_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_on, 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_on, 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_on, 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_on, 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_on, INDEX_SUB, buf, id );
347 if ( bigbuf != NULL ) {
351 ldbm_cache_close( be, db );
357 index2prefix( int indextype )
361 switch ( indextype ) {
366 prefix = APPROX_PREFIX;
372 prefix = UNKNOWN_PREFIX;