1 /* index.c - routines for dealing with attribute indexes */
11 #include "back-ldbm.h"
13 static int change_value(Backend *be,
20 (*idl_func)(Backend *, DBCache *, Datum, ID));
21 static int index2prefix(int indextype);
31 struct berval *bvals[2];
33 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
37 * dn index entry - make it look like an attribute so it works
38 * with index_change_values() call
41 bv.bv_val = ch_strdup( e->e_ndn );
42 bv.bv_len = strlen( bv.bv_val );
46 /* add the dn to the indexes */
48 char *dn = ch_strdup("dn");
49 index_change_values( be, dn, bvals, e->e_id, __INDEX_ADD_OP );
55 /* add each attribute to the indexes */
56 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
58 index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
62 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
76 for ( ; ml != NULL; ml = ml->ml_next ) {
77 LDAPMod *mod = &ml->ml_mod;
79 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
80 case LDAP_MOD_REPLACE:
81 /* XXX: Delete old index data==>problem when this
82 * gets called we lost values already!
85 rc = index_change_values( be,
92 rc = index_change_values( be,
98 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there */
122 int indexmask, syntax;
124 char *realval, *tmpval;
129 ldbm_datum_init( key );
131 prefix = index2prefix( indextype );
132 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
135 attr_masks( be->be_private, type, &indexmask, &syntax );
136 if ( ! (indextype & indexmask) ) {
137 idl = idl_allids( be );
138 Debug( LDAP_DEBUG_TRACE,
139 "<= index_read %ld candidates (allids - not indexed)\n",
140 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
144 attr_normalize( type );
145 at_cn = at_canonical_name( type );
147 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
149 Debug( LDAP_DEBUG_ANY,
150 "<= index_read NULL (could not open %s%s)\n", at_cn,
157 if ( prefix != UNKNOWN_PREFIX ) {
158 unsigned int len = strlen( val );
160 if ( (len + 2) < sizeof(buf) ) {
163 /* value + prefix + null */
164 tmpval = (char *) ch_malloc( len + 2 );
168 strcpy( &realval[1], val );
172 key.dsize = strlen( realval ) + 1;
174 idl = idl_fetch( be, db, key );
175 if ( tmpval != NULL ) {
179 ldbm_cache_close( be, db );
181 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
182 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
186 /* Add or remove stuff from index files */
196 int (*idl_func)(Backend *, DBCache *, Datum, ID)
205 char prefix = index2prefix( indextype );
207 ldbm_datum_init( key );
209 Debug( LDAP_DEBUG_TRACE,
210 "=> change_value( \"%c%s\", op=%s )\n",
211 prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
213 if ( prefix != UNKNOWN_PREFIX ) {
214 unsigned int len = strlen( val );
216 if ( (len + 2) < sizeof(buf) ) {
219 /* value + prefix + null */
220 tmpval = (char *) ch_malloc( len + 2 );
224 strcpy( &realval[1], val );
228 key.dsize = strlen( realval ) + 1;
230 rc = idl_func( be, db, key, id );
232 if ( tmpval != NULL ) {
236 ldap_pvt_thread_yield();
238 Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
242 }/* static int change_value() */
249 struct berval **vals,
254 char *val, *p, *code, *w;
256 int indexmask, syntax;
257 char buf[SUBLEN + 1];
262 int (*idl_funct)(Backend *,
265 char *at_cn; /* Attribute canonical name */
268 Debug( LDAP_DEBUG_TRACE,
269 "=> index_change_values( \"%s\", %ld, op=%s )\n",
270 type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
273 if (op == __INDEX_ADD_OP) {
277 idl_funct = idl_insert_key;
284 idl_funct = idl_delete_key;
289 attr_normalize(type);
290 attr_masks( be->be_private, type, &indexmask, &syntax );
292 if ( indexmask == 0 ) {
296 at_cn = at_canonical_name( type );
298 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
300 Debug( LDAP_DEBUG_ANY,
301 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
304 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
309 if ( vals != NULL ) {
310 for ( i = 0; vals[i] != NULL; i++ ) {
312 * presence index entry
314 if ( indexmask & INDEX_PRESENCE ) {
316 change_value( be, db, at_cn, INDEX_PRESENCE,
317 "*", id, idl_funct );
321 Debug( LDAP_DEBUG_TRACE,
322 "index_change_values syntax 0x%x syntax bin 0x%x\n",
323 syntax, SYNTAX_BIN, 0 );
325 if ( syntax & SYNTAX_BIN ) {
327 ldbm_cache_close( be, db );
333 len = vals[i]->bv_len;
336 if ( len + 2 > sizeof(vbuf) ) {
337 bigbuf = (char *) ch_malloc( len + 1 );
342 (void) memcpy( val, vals[i]->bv_val, len );
345 value_normalize( val, syntax );
347 /* value_normalize could change the length of val */
351 * equality index entry
353 if ( indexmask & INDEX_EQUALITY ) {
355 change_value( be, db, at_cn, INDEX_EQUALITY,
361 * approximate index entry
363 if ( indexmask & INDEX_APPROX ) {
364 for ( w = first_word( val ); w != NULL;
365 w = next_word( w ) ) {
366 if ( (code = phonetic( w )) != NULL ) {
380 * substrings index entry
382 if ( indexmask & INDEX_SUB ) {
383 /* leading and trailing */
384 if ( len > SUBLEN - 2 ) {
386 for ( j = 0; j < SUBLEN - 1; j++ ) {
391 change_value( be, db, at_cn, INDEX_SUB,
392 buf, id, idl_funct );
394 p = val + len - SUBLEN + 1;
395 for ( j = 0; j < SUBLEN - 1; j++ ) {
398 buf[SUBLEN - 1] = '$';
401 change_value( be, db, at_cn, INDEX_SUB,
402 buf, id, idl_funct );
406 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
407 for ( j = 0; j < SUBLEN; j++ ) {
412 change_value( be, db, at_cn, INDEX_SUB,
413 buf, id, idl_funct );
417 if ( bigbuf != NULL ) {
423 ldbm_cache_close( be, db );
427 }/* int index_change_values() */
430 index2prefix( int indextype )
434 switch ( indextype ) {
439 prefix = APPROX_PREFIX;
445 prefix = UNKNOWN_PREFIX;