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' ) {
133 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 %d candidates\n",
157 idl ? idl->b_nids : 0, 0, 0 );
175 char *realval, *tmpval, *s;
178 prefix = index2prefix( indextype );
179 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
184 if ( prefix != '\0' ) {
185 int len = strlen( val );
187 if ( (len + 2) < sizeof(buf) ) {
190 /* value + prefix + null */
191 tmpval = (char *) ch_malloc( len + 2 );
195 strcpy( &realval[1], val );
199 key.dsize = strlen( realval ) + 1;
201 rc = idl_insert_key( be, db, key, id );
203 if ( tmpval != NULL ) {
210 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
218 struct berval **vals,
222 char *val, *p, *code, *w;
224 int indexmask, syntax;
225 char buf[SUBLEN + 1];
230 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
233 attr_masks( be->be_private, type, &indexmask, &syntax );
234 if ( indexmask == 0 ) {
238 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
240 Debug( LDAP_DEBUG_ANY,
241 "<= index_add_values -1 (could not open/create %s%s)\n",
242 type, LDBM_SUFFIX, 0 );
247 for ( i = 0; vals[i] != NULL; i++ ) {
249 * presence index entry
251 if ( indexmask & INDEX_PRESENCE ) {
252 add_value( be, db, type, INDEX_PRESENCE, "*", id );
255 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
256 syntax, SYNTAX_BIN, 0 );
257 if ( syntax & SYNTAX_BIN ) {
258 ldbm_cache_close( be, db );
263 len = vals[i]->bv_len;
266 if ( len + 2 > sizeof(vbuf) ) {
267 bigbuf = (char *) ch_malloc( len + 1 );
272 (void) memcpy( val, vals[i]->bv_val, len );
275 value_normalize( val, syntax );
278 * equality index entry
280 if ( indexmask & INDEX_EQUALITY ) {
281 add_value( be, db, type, INDEX_EQUALITY, val, id );
285 * approximate index entry
287 if ( indexmask & INDEX_APPROX ) {
288 for ( w = first_word( val ); w != NULL;
289 w = next_word( w ) ) {
290 if ( (code = phonetic( w )) != NULL ) {
291 add_value( be, db, type, INDEX_APPROX,
299 * substrings index entry
301 if ( indexmask & INDEX_SUB ) {
302 /* leading and trailing */
303 if ( len > SUBLEN - 2 ) {
305 for ( j = 0; j < SUBLEN - 1; j++ ) {
310 add_value( be, db, type, INDEX_SUB, buf, id );
312 p = val + len - SUBLEN + 1;
313 for ( j = 0; j < SUBLEN - 1; j++ ) {
316 buf[SUBLEN - 1] = '$';
319 add_value( be, db, type, INDEX_SUB, buf, id );
323 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
324 for ( j = 0; j < SUBLEN; j++ ) {
329 add_value( be, db, type, INDEX_SUB, buf, id );
333 if ( bigbuf != NULL ) {
337 ldbm_cache_close( be, db );
343 index2prefix( int indextype )
347 switch ( indextype ) {
352 prefix = APPROX_PREFIX;