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 */
269 Debug( LDAP_DEBUG_TRACE,
270 "=> index_change_values( %s, NULL, %ld, op=%s )\n",
271 type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
275 Debug( LDAP_DEBUG_TRACE,
276 "=> index_change_values( \"%s\", %ld, op=%s )\n",
277 type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
280 if (op == __INDEX_ADD_OP) {
284 idl_funct = idl_insert_key;
291 idl_funct = idl_delete_key;
296 attr_normalize(type);
297 attr_masks( be->be_private, type, &indexmask, &syntax );
299 if ( indexmask == 0 ) {
303 at_cn = at_canonical_name( type );
305 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
307 Debug( LDAP_DEBUG_ANY,
308 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
311 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
316 for ( i = 0; vals[i] != NULL; i++ ) {
318 * presence index entry
320 if ( indexmask & INDEX_PRESENCE ) {
322 change_value( be, db, at_cn, INDEX_PRESENCE,
323 "*", id, idl_funct );
327 Debug( LDAP_DEBUG_TRACE,
328 "index_change_values syntax 0x%x syntax bin 0x%x\n",
329 syntax, SYNTAX_BIN, 0 );
331 if ( syntax & SYNTAX_BIN ) {
333 ldbm_cache_close( be, db );
339 len = vals[i]->bv_len;
342 if ( len + 2 > sizeof(vbuf) ) {
343 bigbuf = (char *) ch_malloc( len + 1 );
348 (void) memcpy( val, vals[i]->bv_val, len );
351 value_normalize( val, syntax );
353 /* value_normalize could change the length of val */
357 * equality index entry
359 if ( indexmask & INDEX_EQUALITY ) {
361 change_value( be, db, at_cn, INDEX_EQUALITY,
367 * approximate index entry
369 if ( indexmask & INDEX_APPROX ) {
370 for ( w = first_word( val ); w != NULL;
371 w = next_word( w ) ) {
372 if ( (code = phonetic( w )) != NULL ) {
386 * substrings index entry
388 if ( indexmask & INDEX_SUB ) {
389 /* leading and trailing */
390 if ( len > SUBLEN - 2 ) {
392 for ( j = 0; j < SUBLEN - 1; j++ ) {
397 change_value( be, db, at_cn, INDEX_SUB,
398 buf, id, idl_funct );
400 p = val + len - SUBLEN + 1;
401 for ( j = 0; j < SUBLEN - 1; j++ ) {
404 buf[SUBLEN - 1] = '$';
407 change_value( be, db, at_cn, INDEX_SUB,
408 buf, id, idl_funct );
412 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
413 for ( j = 0; j < SUBLEN; j++ ) {
418 change_value( be, db, at_cn, INDEX_SUB,
419 buf, id, idl_funct );
423 if ( bigbuf != NULL ) {
428 ldbm_cache_close( be, db );
432 }/* int index_change_values() */
435 index2prefix( int indextype )
439 switch ( indextype ) {
444 prefix = APPROX_PREFIX;
450 prefix = UNKNOWN_PREFIX;