1 /* index.c - routines for dealing with attribute indexes */
9 extern char *first_word();
10 extern char *next_word();
11 extern char *phonetic();
12 extern IDList *idl_fetch();
13 extern IDList *idl_allids();
14 extern struct dbcache *ldbm_cache_open();
16 int index_add_values();
18 static int add_value();
19 static int index2prefix();
30 struct berval *bvals[2];
32 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
36 * dn index entry - make it look like an attribute so it works
37 * with index_add_values() call
40 bv.bv_val = strdup( e->e_dn );
41 bv.bv_len = strlen( bv.bv_val );
42 (void) dn_normalize_case( bv.bv_val );
46 /* add the dn to the indexes */
47 index_add_values( be, "dn", bvals, e->e_id );
51 /* add each attribute to the indexes */
52 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
53 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
56 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
70 for ( ; mods != NULL; mods = mods->mod_next ) {
71 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
73 case LDAP_MOD_REPLACE:
74 rc = index_add_values( be, mods->mod_type,
75 mods->mod_bvalues, id );
102 int indexmask, syntax;
104 char *realval, *tmpval;
107 prefix = index2prefix( indextype );
108 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
111 attr_masks( be->be_private, type, &indexmask, &syntax );
112 if ( ! (indextype & indexmask) ) {
113 idl = idl_allids( be );
114 Debug( LDAP_DEBUG_TRACE,
115 "<= index_read %d candidates (allids - not indexed)\n",
116 idl ? idl->b_nids : 0, 0, 0 );
120 attr_normalize( type );
121 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
123 Debug( LDAP_DEBUG_ANY,
124 "<= index_read NULL (could not open %s%s)\n", type,
131 if ( prefix != '\0' ) {
132 int len = strlen( val );
134 if ( (len + 2) < sizeof(buf) ) {
137 /* value + prefix + null */
138 tmpval = (char *) ch_malloc( len + 2 );
142 strcpy( &realval[1], val );
146 key.dsize = strlen( realval ) + 1;
148 idl = idl_fetch( be, db, key );
149 if ( tmpval != NULL ) {
153 ldbm_cache_close( be, db );
155 Debug( LDAP_DEBUG_TRACE, "<= index_read %d candidates\n",
156 idl ? idl->b_nids : 0, 0, 0 );
174 char *realval, *tmpval, *s;
177 prefix = index2prefix( indextype );
178 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
183 if ( prefix != '\0' ) {
184 int len = strlen( val );
186 if ( (len + 2) < sizeof(buf) ) {
189 /* value + prefix + null */
190 tmpval = (char *) ch_malloc( len + 2 );
194 strcpy( &realval[1], val );
198 key.dsize = strlen( realval ) + 1;
200 rc = idl_insert_key( be, db, key, id );
202 if ( tmpval != NULL ) {
209 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
217 struct berval **vals,
221 char *val, *p, *code, *w;
223 int indexmask, syntax;
224 char buf[SUBLEN + 1];
229 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
232 attr_masks( be->be_private, type, &indexmask, &syntax );
233 if ( indexmask == 0 ) {
237 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
239 Debug( LDAP_DEBUG_ANY,
240 "<= index_add_values -1 (could not open/create %s%s)\n",
241 type, LDBM_SUFFIX, 0 );
246 for ( i = 0; vals[i] != NULL; i++ ) {
248 * presence index entry
250 if ( indexmask & INDEX_PRESENCE ) {
251 add_value( be, db, type, INDEX_PRESENCE, "*", id );
254 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
255 syntax, SYNTAX_BIN, 0 );
256 if ( syntax & SYNTAX_BIN ) {
257 ldbm_cache_close( be, db );
262 len = vals[i]->bv_len;
265 if ( len + 2 > sizeof(vbuf) ) {
266 bigbuf = (char *) ch_malloc( len + 1 );
271 (void) memcpy( val, vals[i]->bv_val, len );
274 value_normalize( val, syntax );
277 * equality index entry
279 if ( indexmask & INDEX_EQUALITY ) {
280 add_value( be, db, type, INDEX_EQUALITY, val, id );
284 * approximate index entry
286 if ( indexmask & INDEX_APPROX ) {
287 for ( w = first_word( val ); w != NULL;
288 w = next_word( w ) ) {
289 if ( (code = phonetic( w )) != NULL ) {
290 add_value( be, db, type, INDEX_APPROX,
298 * substrings index entry
300 if ( indexmask & INDEX_SUB ) {
301 /* leading and trailing */
302 if ( len > SUBLEN - 2 ) {
304 for ( j = 0; j < SUBLEN - 1; j++ ) {
309 add_value( be, db, type, INDEX_SUB, buf, id );
311 p = val + len - SUBLEN + 1;
312 for ( j = 0; j < SUBLEN - 1; j++ ) {
315 buf[SUBLEN - 1] = '$';
318 add_value( be, db, type, INDEX_SUB, buf, id );
322 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
323 for ( j = 0; j < SUBLEN; j++ ) {
328 add_value( be, db, type, INDEX_SUB, buf, id );
332 if ( bigbuf != NULL ) {
336 ldbm_cache_close( be, db );
342 index2prefix( int indextype )
346 switch ( indextype ) {
351 prefix = APPROX_PREFIX;