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 */
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_mask( be->be_private, type, &indexmask );
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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
149 at_cn = at_canonical_name( at_find( type ) );
151 attr_normalize( type );
152 at_cn = at_canonical_name( type );
155 if ( at_cn == NULL ) {
156 Debug( LDAP_DEBUG_ANY,
157 "<= index_read no canonical name for type \"%s\"\n",
158 type != NULL ? type : "(NULL)", 0, 0 );
162 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
164 Debug( LDAP_DEBUG_ANY,
165 "<= index_read NULL (could not open %s%s)\n",
166 at_cn, LDBM_SUFFIX, 0 );
172 if ( prefix != UNKNOWN_PREFIX ) {
173 unsigned int len = strlen( val );
175 if ( (len + 2) < sizeof(buf) ) {
178 /* value + prefix + null */
179 tmpval = (char *) ch_malloc( len + 2 );
184 strcpy( &realval[1], val );
188 key.dsize = strlen( realval ) + 1;
190 idl = idl_fetch( be, db, key );
191 if ( tmpval != NULL ) {
195 ldbm_cache_close( be, db );
197 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
198 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
202 /* Add or remove stuff from index files */
212 int (*idl_func)(Backend *, DBCache *, Datum, ID)
221 char prefix = index2prefix( indextype );
223 ldbm_datum_init( key );
225 Debug( LDAP_DEBUG_TRACE,
226 "=> change_value( \"%c%s\", op=%s )\n",
227 prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
229 if ( prefix != UNKNOWN_PREFIX ) {
230 unsigned int len = strlen( val );
232 if ( (len + 2) < sizeof(buf) ) {
235 /* value + prefix + null */
236 tmpval = (char *) ch_malloc( len + 2 );
240 strcpy( &realval[1], val );
244 key.dsize = strlen( realval ) + 1;
246 rc = idl_func( be, db, key, id );
248 if ( tmpval != NULL ) {
252 ldap_pvt_thread_yield();
254 Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
258 }/* static int change_value() */
265 struct berval **vals,
270 char *val, *p, *code, *w;
272 int indexmask, syntax;
273 char buf[SUBLEN + 1];
278 int (*idl_funct)(Backend *,
281 char *at_cn; /* Attribute canonical name */
285 Debug( LDAP_DEBUG_TRACE,
286 "=> index_change_values( %s, NULL, %ld, op=%s )\n",
287 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
291 Debug( LDAP_DEBUG_TRACE,
292 "=> index_change_values( \"%s\", %ld, op=%s )\n",
293 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
296 if (op == SLAP_INDEX_ADD_OP) {
300 idl_funct = idl_insert_key;
307 idl_funct = idl_delete_key;
312 attr_normalize(type);
313 attr_mask( be->be_private, type, &indexmask );
315 if ( indexmask == 0 ) {
319 #ifdef SLAPD_SCHEMA_NOT_COMPAT
320 at_cn = at_canonical_name( at_find( type ) );
322 syntax = attr_syntax( type );
323 at_cn = at_canonical_name( type );
326 if ( at_cn == NULL ) {
327 Debug( LDAP_DEBUG_ANY,
328 "<= index_change_values no canonical name for type \"%s\"\n",
329 type != NULL ? type : "(NULL)", 0, 0 );
333 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
335 Debug( LDAP_DEBUG_ANY,
336 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
338 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
343 #ifdef SLAPD_SCHEMA_NOT_COMPAT
344 /* not yet implemented */
346 for ( i = 0; vals[i] != NULL; i++ ) {
348 * presence index entry
350 if ( indexmask & SLAP_INDEX_PRESENCE ) {
352 change_value( be, db, at_cn, SLAP_INDEX_PRESENCE,
353 "*", id, idl_funct );
357 Debug( LDAP_DEBUG_TRACE,
358 "index_change_values syntax 0x%x syntax bin 0x%x\n",
359 syntax, SYNTAX_BIN, 0 );
361 if ( syntax & SYNTAX_BIN ) {
363 ldbm_cache_close( be, db );
369 len = vals[i]->bv_len;
372 if ( len + 2 > sizeof(vbuf) ) {
373 bigbuf = (char *) ch_malloc( len + 1 );
378 (void) memcpy( val, vals[i]->bv_val, len );
381 value_normalize( val, syntax );
383 /* value_normalize could change the length of val */
387 * equality index entry
389 if ( indexmask & SLAP_INDEX_EQUALITY ) {
391 change_value( be, db, at_cn, SLAP_INDEX_EQUALITY,
397 * approximate index entry
399 if ( indexmask & SLAP_INDEX_APPROX ) {
400 for ( w = first_word( val ); w != NULL;
401 w = next_word( w ) ) {
402 if ( (code = phonetic( w )) != NULL ) {
416 * substrings index entry
418 if ( indexmask & SLAP_INDEX_SUB ) {
419 /* leading and trailing */
420 if ( len > SUBLEN - 2 ) {
422 for ( j = 0; j < SUBLEN - 1; j++ ) {
427 change_value( be, db, at_cn, SLAP_INDEX_SUB,
428 buf, id, idl_funct );
430 p = val + len - SUBLEN + 1;
431 for ( j = 0; j < SUBLEN - 1; j++ ) {
434 buf[SUBLEN - 1] = '$';
437 change_value( be, db, at_cn, SLAP_INDEX_SUB,
438 buf, id, idl_funct );
442 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
443 for ( j = 0; j < SUBLEN; j++ ) {
448 change_value( be, db, at_cn, SLAP_INDEX_SUB,
449 buf, id, idl_funct );
453 if ( bigbuf != NULL ) {
459 ldbm_cache_close( be, db );
465 index2prefix( int indextype )
469 switch ( indextype ) {
470 case SLAP_INDEX_EQUALITY:
473 case SLAP_INDEX_APPROX:
474 prefix = APPROX_PREFIX;
480 prefix = UNKNOWN_PREFIX;