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"
19 static int change_value(Backend *be,
26 (*idl_func)(Backend *, DBCache *, Datum, ID));
27 static int index2prefix(int indextype);
37 struct berval *bvals[2];
39 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
43 * dn index entry - make it look like an attribute so it works
44 * with index_change_values() call
47 bv.bv_val = ch_strdup( e->e_ndn );
48 bv.bv_len = strlen( bv.bv_val );
52 /* add the dn to the indexes */
54 char *dn = ch_strdup("dn");
55 #ifdef SLAPD_SCHEMA_NOT_COMPAT
56 /* not yet implemented */
58 index_change_values( be, dn, bvals, e->e_id, SLAP_INDEX_ADD_OP );
65 /* add each attribute to the indexes */
66 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
67 #ifdef SLAPD_SCHEMA_NOT_COMPAT
68 /* index_change_values( be, SLAP_INDEX_ADD_OP, e->e_id, ap ); */
70 index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
75 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
89 for ( ; ml != NULL; ml = ml->ml_next ) {
90 LDAPMod *mod = &ml->ml_mod;
92 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
93 case LDAP_MOD_REPLACE:
94 /* XXX: Delete old index data==>problem when this
95 * gets called we lost values already!
98 rc = index_change_values( be,
104 case LDAP_MOD_DELETE:
105 rc = index_change_values( be,
109 SLAP_INDEX_DELETE_OP );
111 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there */
137 char *realval, *tmpval;
142 ldbm_datum_init( key );
144 prefix = index2prefix( indextype );
145 Debug( LDAP_DEBUG_TRACE, "=> index_read(\"%c%s\"->\"%s\")\n",
148 attr_mask( be->be_private, type, &indexmask );
149 if ( ! (indextype & indexmask) ) {
150 idl = idl_allids( be );
151 Debug( LDAP_DEBUG_TRACE,
152 "<= index_read %ld candidates (allids - not indexed)\n",
153 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
157 #ifdef SLAPD_SCHEMA_NOT_COMPAT
158 at_cn = at_canonical_name( at_find( type ) );
160 attr_normalize( type );
161 at_cn = at_canonical_name( type );
164 if ( at_cn == NULL ) {
165 Debug( LDAP_DEBUG_ANY,
166 "<= index_read no canonical name for type \"%s\"\n",
167 type != NULL ? type : "(NULL)", 0, 0 );
171 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
173 Debug( LDAP_DEBUG_ANY,
174 "<= index_read NULL (could not open %s%s)\n",
175 at_cn, LDBM_SUFFIX, 0 );
181 if ( prefix != UNKNOWN_PREFIX ) {
182 unsigned int len = strlen( val );
184 if ( (len + 2) < sizeof(buf) ) {
187 /* value + prefix + null */
188 tmpval = (char *) ch_malloc( len + 2 );
193 strcpy( &realval[1], val );
197 key.dsize = strlen( realval ) + 1;
199 idl = idl_fetch( be, db, key );
200 if ( tmpval != NULL ) {
204 ldbm_cache_close( be, db );
206 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
207 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
211 /* Add or remove stuff from index files */
221 int (*idl_func)(Backend *, DBCache *, Datum, ID)
230 char prefix = index2prefix( indextype );
232 ldbm_datum_init( key );
234 Debug( LDAP_DEBUG_TRACE,
235 "=> change_value( \"%c%s\", op=%s )\n",
236 prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
238 if ( prefix != UNKNOWN_PREFIX ) {
239 unsigned int len = strlen( val );
241 if ( (len + 2) < sizeof(buf) ) {
244 /* value + prefix + null */
245 tmpval = (char *) ch_malloc( len + 2 );
249 strcpy( &realval[1], val );
253 key.dsize = strlen( realval ) + 1;
255 rc = idl_func( be, db, key, id );
257 if ( tmpval != NULL ) {
261 ldap_pvt_thread_yield();
263 Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
267 }/* static int change_value() */
274 struct berval **vals,
279 char *val, *p, *code, *w;
281 int indexmask, syntax;
282 char buf[SUBLEN + 1];
287 int (*idl_funct)(Backend *,
290 char *at_cn; /* Attribute canonical name */
294 Debug( LDAP_DEBUG_TRACE,
295 "=> index_change_values( %s, NULL, %ld, op=%s )\n",
296 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
300 Debug( LDAP_DEBUG_TRACE,
301 "=> index_change_values( \"%s\", %ld, op=%s )\n",
302 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
305 if (op == SLAP_INDEX_ADD_OP) {
309 idl_funct = idl_insert_key;
316 idl_funct = idl_delete_key;
321 attr_normalize(type);
322 attr_mask( be->be_private, type, &indexmask );
324 if ( indexmask == 0 ) {
328 #ifdef SLAPD_SCHEMA_NOT_COMPAT
329 at_cn = at_canonical_name( at_find( type ) );
331 syntax = attr_syntax( type );
332 at_cn = at_canonical_name( type );
335 if ( at_cn == NULL ) {
336 Debug( LDAP_DEBUG_ANY,
337 "<= index_change_values no canonical name for type \"%s\"\n",
338 type != NULL ? type : "(NULL)", 0, 0 );
342 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
344 Debug( LDAP_DEBUG_ANY,
345 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
347 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
352 #ifdef SLAPD_SCHEMA_NOT_COMPAT
353 /* not yet implemented */
355 for ( i = 0; vals[i] != NULL; i++ ) {
357 * presence index entry
359 if ( indexmask & SLAP_INDEX_PRESENCE ) {
361 change_value( be, db, at_cn, SLAP_INDEX_PRESENCE,
362 "*", id, idl_funct );
366 Debug( LDAP_DEBUG_TRACE,
367 "index_change_values syntax 0x%x syntax bin 0x%x\n",
368 syntax, SYNTAX_BIN, 0 );
370 if ( syntax & SYNTAX_BIN ) {
372 ldbm_cache_close( be, db );
378 len = vals[i]->bv_len;
381 if ( len + 2 > sizeof(vbuf) ) {
382 bigbuf = (char *) ch_malloc( len + 1 );
387 (void) memcpy( val, vals[i]->bv_val, len );
390 value_normalize( val, syntax );
392 /* value_normalize could change the length of val */
396 * equality index entry
398 if ( indexmask & SLAP_INDEX_EQUALITY ) {
400 change_value( be, db, at_cn, SLAP_INDEX_EQUALITY,
406 * approximate index entry
408 if ( indexmask & SLAP_INDEX_APPROX ) {
409 for ( w = first_word( val ); w != NULL;
410 w = next_word( w ) ) {
411 if ( (code = phonetic( w )) != NULL ) {
425 * substrings index entry
427 if ( indexmask & SLAP_INDEX_SUB ) {
428 /* leading and trailing */
429 if ( len > SUBLEN - 2 ) {
431 for ( j = 0; j < SUBLEN - 1; j++ ) {
436 change_value( be, db, at_cn, SLAP_INDEX_SUB,
437 buf, id, idl_funct );
439 p = val + len - SUBLEN + 1;
440 for ( j = 0; j < SUBLEN - 1; j++ ) {
443 buf[SUBLEN - 1] = '$';
446 change_value( be, db, at_cn, SLAP_INDEX_SUB,
447 buf, id, idl_funct );
451 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
452 for ( j = 0; j < SUBLEN; j++ ) {
457 change_value( be, db, at_cn, SLAP_INDEX_SUB,
458 buf, id, idl_funct );
462 if ( bigbuf != NULL ) {
468 ldbm_cache_close( be, db );
474 index2prefix( int indextype )
478 switch ( indextype ) {
479 case SLAP_INDEX_EQUALITY:
482 case SLAP_INDEX_APPROX:
483 prefix = APPROX_PREFIX;
489 prefix = UNKNOWN_PREFIX;