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;
109 memset( &key, 0, sizeof( key ) );
112 prefix = index2prefix( indextype );
113 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
116 attr_masks( be->be_private, type, &indexmask, &syntax );
117 if ( ! (indextype & indexmask) ) {
118 idl = idl_allids( be );
119 Debug( LDAP_DEBUG_TRACE,
120 "<= index_read %d candidates (allids - not indexed)\n",
121 idl ? idl->b_nids : 0, 0, 0 );
125 attr_normalize( type );
126 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
128 Debug( LDAP_DEBUG_ANY,
129 "<= index_read NULL (could not open %s%s)\n", type,
136 if ( prefix != '\0' ) {
137 int len = strlen( val );
139 if ( (len + 2) < sizeof(buf) ) {
142 /* value + prefix + null */
143 tmpval = (char *) ch_malloc( len + 2 );
147 strcpy( &realval[1], val );
151 key.dsize = strlen( realval ) + 1;
153 idl = idl_fetch( be, db, key );
154 if ( tmpval != NULL ) {
158 ldbm_cache_close( be, db );
160 Debug( LDAP_DEBUG_TRACE, "<= index_read %d candidates\n",
161 idl ? idl->b_nids : 0, 0, 0 );
179 char *realval, *tmpval, *s;
183 memset( &key, 0, sizeof( key ) );
186 prefix = index2prefix( indextype );
187 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
192 if ( prefix != '\0' ) {
193 int len = strlen( val );
195 if ( (len + 2) < sizeof(buf) ) {
198 /* value + prefix + null */
199 tmpval = (char *) ch_malloc( len + 2 );
203 strcpy( &realval[1], val );
207 key.dsize = strlen( realval ) + 1;
209 rc = idl_insert_key( be, db, key, id );
211 if ( tmpval != NULL ) {
218 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
226 struct berval **vals,
230 char *val, *p, *code, *w;
232 int indexmask, syntax;
233 char buf[SUBLEN + 1];
238 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
241 attr_masks( be->be_private, type, &indexmask, &syntax );
242 if ( indexmask == 0 ) {
246 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
248 Debug( LDAP_DEBUG_ANY,
249 "<= index_add_values -1 (could not open/create %s%s)\n",
250 type, LDBM_SUFFIX, 0 );
255 for ( i = 0; vals[i] != NULL; i++ ) {
257 * presence index entry
259 if ( indexmask & INDEX_PRESENCE ) {
260 add_value( be, db, type, INDEX_PRESENCE, "*", id );
263 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
264 syntax, SYNTAX_BIN, 0 );
265 if ( syntax & SYNTAX_BIN ) {
266 ldbm_cache_close( be, db );
271 len = vals[i]->bv_len;
274 if ( len + 2 > sizeof(vbuf) ) {
275 bigbuf = (char *) ch_malloc( len + 1 );
280 (void) memcpy( val, vals[i]->bv_val, len );
283 value_normalize( val, syntax );
286 * equality index entry
288 if ( indexmask & INDEX_EQUALITY ) {
289 add_value( be, db, type, INDEX_EQUALITY, val, id );
293 * approximate index entry
295 if ( indexmask & INDEX_APPROX ) {
296 for ( w = first_word( val ); w != NULL;
297 w = next_word( w ) ) {
298 if ( (code = phonetic( w )) != NULL ) {
299 add_value( be, db, type, INDEX_APPROX,
307 * substrings index entry
309 if ( indexmask & INDEX_SUB ) {
310 /* leading and trailing */
311 if ( len > SUBLEN - 2 ) {
313 for ( j = 0; j < SUBLEN - 1; j++ ) {
318 add_value( be, db, type, INDEX_SUB, buf, id );
320 p = val + len - SUBLEN + 1;
321 for ( j = 0; j < SUBLEN - 1; j++ ) {
324 buf[SUBLEN - 1] = '$';
327 add_value( be, db, type, INDEX_SUB, buf, id );
331 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
332 for ( j = 0; j < SUBLEN; j++ ) {
337 add_value( be, db, type, INDEX_SUB, buf, id );
341 if ( bigbuf != NULL ) {
345 ldbm_cache_close( be, db );
351 index2prefix( int indextype )
355 switch ( indextype ) {
360 prefix = APPROX_PREFIX;