1 /* index.c - routines for dealing with attribute indexes */
11 #include "back-ldbm.h"
13 static int add_value(Backend *be, struct dbcache *db, char *type, int indextype, char *val, ID id);
14 static int index2prefix(int indextype);
25 struct berval *bvals[2];
27 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
31 * dn index entry - make it look like an attribute so it works
32 * with index_add_values() call
35 bv.bv_val = ch_strdup( e->e_dn );
36 bv.bv_len = strlen( bv.bv_val );
37 (void) dn_normalize_case( bv.bv_val );
41 /* add the dn to the indexes */
42 index_add_values( be, "dn", bvals, e->e_id );
46 /* add each attribute to the indexes */
47 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
48 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
51 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
65 for ( ; ml != NULL; ml = ml->ml_next ) {
66 LDAPMod *mod = &ml->ml_mod;
68 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
70 case LDAP_MOD_REPLACE:
71 rc = index_add_values( be, mod->mod_type,
72 mod->mod_bvalues, id );
99 int indexmask, syntax;
101 char *realval, *tmpval;
104 #ifdef HAVE_BERKELEY_DB2
105 memset( &key, 0, sizeof( key ) );
108 prefix = index2prefix( indextype );
109 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
112 attr_masks( be->be_private, type, &indexmask, &syntax );
113 if ( ! (indextype & indexmask) ) {
114 idl = idl_allids( be );
115 Debug( LDAP_DEBUG_TRACE,
116 "<= index_read %lu candidates (allids - not indexed)\n",
117 idl ? idl->b_nids : 0, 0, 0 );
121 attr_normalize( type );
122 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
124 Debug( LDAP_DEBUG_ANY,
125 "<= index_read NULL (could not open %s%s)\n", type,
132 if ( prefix != '\0' ) {
133 unsigned int len = strlen( val );
135 if ( (len + 2) < sizeof(buf) ) {
138 /* value + prefix + null */
139 tmpval = (char *) ch_malloc( len + 2 );
143 strcpy( &realval[1], val );
147 key.dsize = strlen( realval ) + 1;
149 idl = idl_fetch( be, db, key );
150 if ( tmpval != NULL ) {
154 ldbm_cache_close( be, db );
156 Debug( LDAP_DEBUG_TRACE, "<= index_read %lu candidates\n",
157 idl ? idl->b_nids : 0, 0, 0 );
175 char *realval, *tmpval, *s;
178 #ifdef HAVE_BERKELEY_DB2
179 memset( &key, 0, sizeof( key ) );
182 prefix = index2prefix( indextype );
183 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
188 if ( prefix != '\0' ) {
189 unsigned int len = strlen( val );
191 if ( (len + 2) < sizeof(buf) ) {
194 /* value + prefix + null */
195 tmpval = (char *) ch_malloc( len + 2 );
199 strcpy( &realval[1], val );
203 key.dsize = strlen( realval ) + 1;
205 rc = idl_insert_key( be, db, key, id );
207 if ( tmpval != NULL ) {
214 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
222 struct berval **vals,
226 char *val, *p, *code, *w;
228 int indexmask, syntax;
229 char buf[SUBLEN + 1];
234 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
237 attr_masks( be->be_private, type, &indexmask, &syntax );
238 if ( indexmask == 0 ) {
242 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
244 Debug( LDAP_DEBUG_ANY,
245 "<= index_add_values -1 (could not open/create %s%s)\n",
246 type, LDBM_SUFFIX, 0 );
251 for ( i = 0; vals[i] != NULL; i++ ) {
253 * presence index entry
255 if ( indexmask & INDEX_PRESENCE ) {
256 add_value( be, db, type, INDEX_PRESENCE, "*", id );
259 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
260 syntax, SYNTAX_BIN, 0 );
261 if ( syntax & SYNTAX_BIN ) {
262 ldbm_cache_close( be, db );
267 len = vals[i]->bv_len;
270 if ( len + 2 > sizeof(vbuf) ) {
271 bigbuf = (char *) ch_malloc( len + 1 );
276 (void) memcpy( val, vals[i]->bv_val, len );
279 value_normalize( val, syntax );
282 * equality index entry
284 if ( indexmask & INDEX_EQUALITY ) {
285 add_value( be, db, type, INDEX_EQUALITY, val, id );
289 * approximate index entry
291 if ( indexmask & INDEX_APPROX ) {
292 for ( w = first_word( val ); w != NULL;
293 w = next_word( w ) ) {
294 if ( (code = phonetic( w )) != NULL ) {
295 add_value( be, db, type, INDEX_APPROX,
303 * substrings index entry
305 if ( indexmask & INDEX_SUB ) {
306 /* leading and trailing */
307 if ( len > SUBLEN - 2 ) {
309 for ( j = 0; j < SUBLEN - 1; j++ ) {
314 add_value( be, db, type, INDEX_SUB, buf, id );
316 p = val + len - SUBLEN + 1;
317 for ( j = 0; j < SUBLEN - 1; j++ ) {
320 buf[SUBLEN - 1] = '$';
323 add_value( be, db, type, INDEX_SUB, buf, id );
327 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
328 for ( j = 0; j < SUBLEN; j++ ) {
333 add_value( be, db, type, INDEX_SUB, buf, id );
337 if ( bigbuf != NULL ) {
341 ldbm_cache_close( be, db );
347 index2prefix( int indextype )
351 switch ( indextype ) {
356 prefix = APPROX_PREFIX;