1 /* index.c - routines for dealing with attribute indexes */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
18 static int change_value(Backend *be,
25 (*idl_func)(Backend *, DBCache *, Datum, ID));
26 static int index2prefix(int indextype);
36 struct berval *bvals[2];
38 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
42 * dn index entry - make it look like an attribute so it works
43 * with index_change_values() call
46 bv.bv_val = ch_strdup( e->e_ndn );
47 bv.bv_len = strlen( bv.bv_val );
51 /* add the dn to the indexes */
53 char *dn = ch_strdup("dn");
54 index_change_values( be, dn, bvals, e->e_id, SLAP_INDEX_ADD_OP );
60 /* add each attribute to the indexes */
61 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
62 index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
66 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
80 for ( ; ml != NULL; ml = ml->ml_next ) {
81 LDAPMod *mod = &ml->ml_mod;
83 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
84 case LDAP_MOD_REPLACE:
85 /* XXX: Delete old index data==>problem when this
86 * gets called we lost values already!
89 rc = index_change_values( be,
96 rc = index_change_values( be,
100 SLAP_INDEX_DELETE_OP );
102 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there */
126 int indexmask, syntax;
128 char *realval, *tmpval;
133 ldbm_datum_init( key );
135 prefix = index2prefix( indextype );
136 Debug( LDAP_DEBUG_TRACE, "=> index_read(\"%c%s\"->\"%s\")\n",
139 attr_masks( be->be_private, type, &indexmask, &syntax );
140 if ( ! (indextype & indexmask) ) {
141 idl = idl_allids( be );
142 Debug( LDAP_DEBUG_TRACE,
143 "<= index_read %ld candidates (allids - not indexed)\n",
144 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
148 attr_normalize( type );
149 at_cn = at_canonical_name( type );
151 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
153 Debug( LDAP_DEBUG_ANY,
154 "<= index_read NULL (could not open %s%s)\n", at_cn,
161 if ( prefix != UNKNOWN_PREFIX ) {
162 unsigned int len = strlen( val );
164 if ( (len + 2) < sizeof(buf) ) {
167 /* value + prefix + null */
168 tmpval = (char *) ch_malloc( len + 2 );
173 strcpy( &realval[1], val );
177 key.dsize = strlen( realval ) + 1;
179 idl = idl_fetch( be, db, key );
180 if ( tmpval != NULL ) {
184 ldbm_cache_close( be, db );
186 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
187 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
191 /* Add or remove stuff from index files */
201 int (*idl_func)(Backend *, DBCache *, Datum, ID)
210 char prefix = index2prefix( indextype );
212 ldbm_datum_init( key );
214 Debug( LDAP_DEBUG_TRACE,
215 "=> change_value( \"%c%s\", op=%s )\n",
216 prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
218 if ( prefix != UNKNOWN_PREFIX ) {
219 unsigned int len = strlen( val );
221 if ( (len + 2) < sizeof(buf) ) {
224 /* value + prefix + null */
225 tmpval = (char *) ch_malloc( len + 2 );
229 strcpy( &realval[1], val );
233 key.dsize = strlen( realval ) + 1;
235 rc = idl_func( be, db, key, id );
237 if ( tmpval != NULL ) {
241 ldap_pvt_thread_yield();
243 Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
247 }/* static int change_value() */
254 struct berval **vals,
259 char *val, *p, *code, *w;
261 int indexmask, syntax;
262 char buf[SUBLEN + 1];
267 int (*idl_funct)(Backend *,
270 char *at_cn; /* Attribute canonical name */
274 Debug( LDAP_DEBUG_TRACE,
275 "=> index_change_values( %s, NULL, %ld, op=%s )\n",
276 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
280 Debug( LDAP_DEBUG_TRACE,
281 "=> index_change_values( \"%s\", %ld, op=%s )\n",
282 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
285 if (op == SLAP_INDEX_ADD_OP) {
289 idl_funct = idl_insert_key;
296 idl_funct = idl_delete_key;
301 attr_normalize(type);
302 attr_masks( be->be_private, type, &indexmask, &syntax );
304 if ( indexmask == 0 ) {
308 at_cn = at_canonical_name( type );
310 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
312 Debug( LDAP_DEBUG_ANY,
313 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
316 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
321 for ( i = 0; vals[i] != NULL; i++ ) {
323 * presence index entry
325 if ( indexmask & INDEX_PRESENCE ) {
327 change_value( be, db, at_cn, INDEX_PRESENCE,
328 "*", id, idl_funct );
332 Debug( LDAP_DEBUG_TRACE,
333 "index_change_values syntax 0x%x syntax bin 0x%x\n",
334 syntax, SYNTAX_BIN, 0 );
336 if ( syntax & SYNTAX_BIN ) {
338 ldbm_cache_close( be, db );
344 len = vals[i]->bv_len;
347 if ( len + 2 > sizeof(vbuf) ) {
348 bigbuf = (char *) ch_malloc( len + 1 );
353 (void) memcpy( val, vals[i]->bv_val, len );
356 value_normalize( val, syntax );
358 /* value_normalize could change the length of val */
362 * equality index entry
364 if ( indexmask & INDEX_EQUALITY ) {
366 change_value( be, db, at_cn, INDEX_EQUALITY,
372 * approximate index entry
374 if ( indexmask & INDEX_APPROX ) {
375 for ( w = first_word( val ); w != NULL;
376 w = next_word( w ) ) {
377 if ( (code = phonetic( w )) != NULL ) {
391 * substrings index entry
393 if ( indexmask & INDEX_SUB ) {
394 /* leading and trailing */
395 if ( len > SUBLEN - 2 ) {
397 for ( j = 0; j < SUBLEN - 1; j++ ) {
402 change_value( be, db, at_cn, INDEX_SUB,
403 buf, id, idl_funct );
405 p = val + len - SUBLEN + 1;
406 for ( j = 0; j < SUBLEN - 1; j++ ) {
409 buf[SUBLEN - 1] = '$';
412 change_value( be, db, at_cn, INDEX_SUB,
413 buf, id, idl_funct );
417 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
418 for ( j = 0; j < SUBLEN; j++ ) {
423 change_value( be, db, at_cn, INDEX_SUB,
424 buf, id, idl_funct );
428 if ( bigbuf != NULL ) {
433 ldbm_cache_close( be, db );
440 index2prefix( int indextype )
444 switch ( indextype ) {
449 prefix = APPROX_PREFIX;
455 prefix = UNKNOWN_PREFIX;