1 /* index.c - routines for dealing with attribute indexes */
6 #include <sys/socket.h>
10 extern char *first_word();
11 extern char *next_word();
12 extern char *phonetic();
13 extern IDList *idl_fetch();
14 extern IDList *idl_allids();
15 extern struct dbcache *ldbm_cache_open();
17 int index_add_values();
19 static int add_value();
20 static int index2prefix();
31 struct berval *bvals[2];
33 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
37 * dn index entry - make it look like an attribute so it works
38 * with index_add_values() call
41 bv.bv_val = strdup( e->e_dn );
42 bv.bv_len = strlen( bv.bv_val );
43 (void) dn_normalize_case( bv.bv_val );
47 /* add the dn to the indexes */
48 index_add_values( be, "dn", bvals, e->e_id );
52 /* add each attribute to the indexes */
53 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
54 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
57 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
71 for ( ; mods != NULL; mods = mods->mod_next ) {
72 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
74 case LDAP_MOD_REPLACE:
75 rc = index_add_values( be, mods->mod_type,
76 mods->mod_bvalues, id );
103 int indexmask, syntax;
105 char *realval, *tmpval;
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 %d 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' ) {
135 if ( (len = strlen( val )) < sizeof(buf) ) {
137 strcpy( &buf[1], val );
140 /* value + prefix + null */
141 tmpval = (char *) ch_malloc( len + 2 );
143 strcat( &tmpval[1], val );
149 key.dsize = strlen( realval ) + 1;
151 idl = idl_fetch( be, db, key );
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' ) {
186 if ( (len = strlen( val )) < sizeof(buf) ) {
188 strcpy( &buf[1], val );
191 /* value + prefix + null */
192 tmpval = (char *) ch_malloc( len + 2 );
194 strcat( &tmpval[1], val );
200 key.dsize = strlen( realval ) + 1;
202 rc = idl_insert_key( be, db, key, id );
204 if ( tmpval != NULL ) {
211 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
219 struct berval **vals,
223 char *val, *p, *code, *w;
225 int indexmask, syntax;
226 char buf[SUBLEN + 1];
231 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
234 attr_masks( be->be_private, type, &indexmask, &syntax );
235 if ( indexmask == 0 ) {
239 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
241 Debug( LDAP_DEBUG_ANY,
242 "<= index_add_values -1 (could not open/create %s%s)\n",
243 type, LDBM_SUFFIX, 0 );
248 for ( i = 0; vals[i] != NULL; i++ ) {
250 * presence index entry
252 if ( indexmask & INDEX_PRESENCE ) {
253 add_value( be, db, type, INDEX_PRESENCE, "*", id );
256 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
257 syntax, SYNTAX_BIN, 0 );
258 if ( syntax & SYNTAX_BIN ) {
259 ldbm_cache_close( be, db );
264 len = vals[i]->bv_len;
267 if ( len + 2 > sizeof(vbuf) ) {
268 bigbuf = (char *) ch_malloc( len + 1 );
273 (void) memcpy( val, vals[i]->bv_val, len );
276 value_normalize( val, syntax );
279 * equality index entry
281 if ( indexmask & INDEX_EQUALITY ) {
282 add_value( be, db, type, INDEX_EQUALITY, val, id );
286 * approximate index entry
288 if ( indexmask & INDEX_APPROX ) {
289 for ( w = first_word( val ); w != NULL;
290 w = next_word( w ) ) {
291 if ( (code = phonetic( w )) != NULL ) {
292 add_value( be, db, type, INDEX_APPROX,
300 * substrings index entry
302 if ( indexmask & INDEX_SUB ) {
303 /* leading and trailing */
304 if ( len > SUBLEN - 2 ) {
306 for ( j = 0; j < SUBLEN - 1; j++ ) {
311 add_value( be, db, type, INDEX_SUB, buf, id );
313 p = val + len - SUBLEN + 1;
314 for ( j = 0; j < SUBLEN - 1; j++ ) {
317 buf[SUBLEN - 1] = '$';
320 add_value( be, db, type, INDEX_SUB, buf, id );
324 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
325 for ( j = 0; j < SUBLEN; j++ ) {
330 add_value( be, db, type, INDEX_SUB, buf, id );
334 if ( bigbuf != NULL ) {
338 ldbm_cache_close( be, db );
344 index2prefix( int indextype )
348 switch ( indextype ) {
353 prefix = APPROX_PREFIX;