1 /* index.c - routines for dealing with attribute indexes */
11 #include "back-ldbm.h"
13 static int add_value(Backend *be, struct dbcache *db, char *type, int indextype, char *val, ID id);
14 static int index2prefix(int indextype);
25 struct berval *bvals[2];
27 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
31 * dn index entry - make it look like an attribute so it works
32 * with index_add_values() call
35 bv.bv_val = strdup( e->e_dn );
36 bv.bv_len = strlen( bv.bv_val );
37 (void) dn_normalize_case( bv.bv_val );
41 /* add the dn to the indexes */
42 index_add_values( be, "dn", bvals, e->e_id );
46 /* add each attribute to the indexes */
47 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
48 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
51 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
65 for ( ; mods != NULL; mods = mods->mod_next ) {
66 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
68 case LDAP_MOD_REPLACE:
69 rc = index_add_values( be, mods->mod_type,
70 mods->mod_bvalues, id );
97 int indexmask, syntax;
99 char *realval, *tmpval;
102 #ifdef HAVE_BERKELEY_DB2
103 memset( &key, 0, sizeof( key ) );
106 prefix = index2prefix( indextype );
107 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
110 attr_masks( be->be_private, type, &indexmask, &syntax );
111 if ( ! (indextype & indexmask) ) {
112 idl = idl_allids( be );
113 Debug( LDAP_DEBUG_TRACE,
114 "<= index_read %lu candidates (allids - not indexed)\n",
115 idl ? idl->b_nids : 0, 0, 0 );
119 attr_normalize( type );
120 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
122 Debug( LDAP_DEBUG_ANY,
123 "<= index_read NULL (could not open %s%s)\n", type,
130 if ( prefix != '\0' ) {
131 unsigned int len = strlen( val );
133 if ( (len + 2) < sizeof(buf) ) {
136 /* value + prefix + null */
137 tmpval = (char *) ch_malloc( len + 2 );
141 strcpy( &realval[1], val );
145 key.dsize = strlen( realval ) + 1;
147 idl = idl_fetch( be, db, key );
148 if ( tmpval != NULL ) {
152 ldbm_cache_close( be, db );
154 Debug( LDAP_DEBUG_TRACE, "<= index_read %lu candidates\n",
155 idl ? idl->b_nids : 0, 0, 0 );
173 char *realval, *tmpval, *s;
176 #ifdef HAVE_BERKELEY_DB2
177 memset( &key, 0, sizeof( key ) );
180 prefix = index2prefix( indextype );
181 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
186 if ( prefix != '\0' ) {
187 unsigned int len = strlen( val );
189 if ( (len + 2) < sizeof(buf) ) {
192 /* value + prefix + null */
193 tmpval = (char *) ch_malloc( len + 2 );
197 strcpy( &realval[1], val );
201 key.dsize = strlen( realval ) + 1;
203 rc = idl_insert_key( be, db, key, id );
205 if ( tmpval != NULL ) {
212 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
220 struct berval **vals,
224 char *val, *p, *code, *w;
226 int indexmask, syntax;
227 char buf[SUBLEN + 1];
232 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
235 attr_masks( be->be_private, type, &indexmask, &syntax );
236 if ( indexmask == 0 ) {
240 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
242 Debug( LDAP_DEBUG_ANY,
243 "<= index_add_values -1 (could not open/create %s%s)\n",
244 type, LDBM_SUFFIX, 0 );
249 for ( i = 0; vals[i] != NULL; i++ ) {
251 * presence index entry
253 if ( indexmask & INDEX_PRESENCE ) {
254 add_value( be, db, type, INDEX_PRESENCE, "*", id );
257 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
258 syntax, SYNTAX_BIN, 0 );
259 if ( syntax & SYNTAX_BIN ) {
260 ldbm_cache_close( be, db );
265 len = vals[i]->bv_len;
268 if ( len + 2 > sizeof(vbuf) ) {
269 bigbuf = (char *) ch_malloc( len + 1 );
274 (void) memcpy( val, vals[i]->bv_val, len );
277 value_normalize( val, syntax );
280 * equality index entry
282 if ( indexmask & INDEX_EQUALITY ) {
283 add_value( be, db, type, INDEX_EQUALITY, val, id );
287 * approximate index entry
289 if ( indexmask & INDEX_APPROX ) {
290 for ( w = first_word( val ); w != NULL;
291 w = next_word( w ) ) {
292 if ( (code = phonetic( w )) != NULL ) {
293 add_value( be, db, type, INDEX_APPROX,
301 * substrings index entry
303 if ( indexmask & INDEX_SUB ) {
304 /* leading and trailing */
305 if ( len > SUBLEN - 2 ) {
307 for ( j = 0; j < SUBLEN - 1; j++ ) {
312 add_value( be, db, type, INDEX_SUB, buf, id );
314 p = val + len - SUBLEN + 1;
315 for ( j = 0; j < SUBLEN - 1; j++ ) {
318 buf[SUBLEN - 1] = '$';
321 add_value( be, db, type, INDEX_SUB, buf, id );
325 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
326 for ( j = 0; j < SUBLEN; j++ ) {
331 add_value( be, db, type, INDEX_SUB, buf, id );
335 if ( bigbuf != NULL ) {
339 ldbm_cache_close( be, db );
345 index2prefix( int indextype )
349 switch ( indextype ) {
354 prefix = APPROX_PREFIX;