1 /* index.c - routines for dealing with attribute indexes */
11 #include "back-ldbm.h"
13 extern char *first_word();
14 extern char *next_word();
15 extern char *phonetic();
16 extern IDList *idl_fetch();
17 extern IDList *idl_allids();
18 extern struct dbcache *ldbm_cache_open();
20 int index_add_values();
22 static int add_value();
23 static int index2prefix();
34 struct berval *bvals[2];
36 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
40 * dn index entry - make it look like an attribute so it works
41 * with index_add_values() call
44 bv.bv_val = strdup( e->e_dn );
45 bv.bv_len = strlen( bv.bv_val );
46 (void) dn_normalize_case( bv.bv_val );
50 /* add the dn to the indexes */
51 index_add_values( be, "dn", bvals, e->e_id );
55 /* add each attribute to the indexes */
56 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
57 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
60 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
74 for ( ; mods != NULL; mods = mods->mod_next ) {
75 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
77 case LDAP_MOD_REPLACE:
78 rc = index_add_values( be, mods->mod_type,
79 mods->mod_bvalues, id );
106 int indexmask, syntax;
108 char *realval, *tmpval;
111 #ifdef HAVE_BERKELEY_DB2
112 memset( &key, 0, sizeof( key ) );
115 prefix = index2prefix( indextype );
116 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
119 attr_masks( be->be_private, type, &indexmask, &syntax );
120 if ( ! (indextype & indexmask) ) {
121 idl = idl_allids( be );
122 Debug( LDAP_DEBUG_TRACE,
123 "<= index_read %d candidates (allids - not indexed)\n",
124 idl ? idl->b_nids : 0, 0, 0 );
128 attr_normalize( type );
129 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
131 Debug( LDAP_DEBUG_ANY,
132 "<= index_read NULL (could not open %s%s)\n", type,
139 if ( prefix != '\0' ) {
140 unsigned int len = strlen( val );
142 if ( (len + 2) < sizeof(buf) ) {
145 /* value + prefix + null */
146 tmpval = (char *) ch_malloc( len + 2 );
150 strcpy( &realval[1], val );
154 key.dsize = strlen( realval ) + 1;
156 idl = idl_fetch( be, db, key );
157 if ( tmpval != NULL ) {
161 ldbm_cache_close( be, db );
163 Debug( LDAP_DEBUG_TRACE, "<= index_read %d candidates\n",
164 idl ? idl->b_nids : 0, 0, 0 );
182 char *realval, *tmpval, *s;
185 #ifdef HAVE_BERKELEY_DB2
186 memset( &key, 0, sizeof( key ) );
189 prefix = index2prefix( indextype );
190 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
195 if ( prefix != '\0' ) {
196 unsigned int len = strlen( val );
198 if ( (len + 2) < sizeof(buf) ) {
201 /* value + prefix + null */
202 tmpval = (char *) ch_malloc( len + 2 );
206 strcpy( &realval[1], val );
210 key.dsize = strlen( realval ) + 1;
212 rc = idl_insert_key( be, db, key, id );
214 if ( tmpval != NULL ) {
221 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
229 struct berval **vals,
233 char *val, *p, *code, *w;
235 int indexmask, syntax;
236 char buf[SUBLEN + 1];
241 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
244 attr_masks( be->be_private, type, &indexmask, &syntax );
245 if ( indexmask == 0 ) {
249 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
251 Debug( LDAP_DEBUG_ANY,
252 "<= index_add_values -1 (could not open/create %s%s)\n",
253 type, LDBM_SUFFIX, 0 );
258 for ( i = 0; vals[i] != NULL; i++ ) {
260 * presence index entry
262 if ( indexmask & INDEX_PRESENCE ) {
263 add_value( be, db, type, INDEX_PRESENCE, "*", id );
266 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
267 syntax, SYNTAX_BIN, 0 );
268 if ( syntax & SYNTAX_BIN ) {
269 ldbm_cache_close( be, db );
274 len = vals[i]->bv_len;
277 if ( len + 2 > sizeof(vbuf) ) {
278 bigbuf = (char *) ch_malloc( len + 1 );
283 (void) memcpy( val, vals[i]->bv_val, len );
286 value_normalize( val, syntax );
289 * equality index entry
291 if ( indexmask & INDEX_EQUALITY ) {
292 add_value( be, db, type, INDEX_EQUALITY, val, id );
296 * approximate index entry
298 if ( indexmask & INDEX_APPROX ) {
299 for ( w = first_word( val ); w != NULL;
300 w = next_word( w ) ) {
301 if ( (code = phonetic( w )) != NULL ) {
302 add_value( be, db, type, INDEX_APPROX,
310 * substrings index entry
312 if ( indexmask & INDEX_SUB ) {
313 /* leading and trailing */
314 if ( len > SUBLEN - 2 ) {
316 for ( j = 0; j < SUBLEN - 1; j++ ) {
321 add_value( be, db, type, INDEX_SUB, buf, id );
323 p = val + len - SUBLEN + 1;
324 for ( j = 0; j < SUBLEN - 1; j++ ) {
327 buf[SUBLEN - 1] = '$';
330 add_value( be, db, type, INDEX_SUB, buf, id );
334 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
335 for ( j = 0; j < SUBLEN; j++ ) {
340 add_value( be, db, type, INDEX_SUB, buf, id );
344 if ( bigbuf != NULL ) {
348 ldbm_cache_close( be, db );
354 index2prefix( int indextype )
358 switch ( indextype ) {
363 prefix = APPROX_PREFIX;