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);
24 struct berval *bvals[2];
26 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
30 * dn index entry - make it look like an attribute so it works
31 * with index_add_values() call
34 bv.bv_val = ch_strdup( e->e_dn );
35 bv.bv_len = strlen( bv.bv_val );
36 (void) dn_normalize_case( bv.bv_val );
40 /* add the dn to the indexes */
42 char *dn = ch_strdup("dn");
43 index_add_values( be, dn, bvals, e->e_id );
49 /* add each attribute to the indexes */
50 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
51 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
54 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
68 for ( ; ml != NULL; ml = ml->ml_next ) {
69 LDAPMod *mod = &ml->ml_mod;
71 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
73 case LDAP_MOD_REPLACE:
74 rc = index_add_values( be, mod->mod_type,
75 mod->mod_bvalues, id );
77 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there */
102 int indexmask, syntax;
104 char *realval, *tmpval;
107 ldbm_datum_init( key );
109 prefix = index2prefix( indextype );
110 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
113 attr_masks( be->be_private, type, &indexmask, &syntax );
114 if ( ! (indextype & indexmask) ) {
115 idl = idl_allids( be );
116 Debug( LDAP_DEBUG_TRACE,
117 "<= index_read %ld candidates (allids - not indexed)\n",
118 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
122 attr_normalize( type );
123 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
125 Debug( LDAP_DEBUG_ANY,
126 "<= index_read NULL (could not open %s%s)\n", type,
133 if ( prefix != UNKNOWN_PREFIX ) {
134 unsigned int len = strlen( val );
136 if ( (len + 2) < sizeof(buf) ) {
139 /* value + prefix + null */
140 tmpval = (char *) ch_malloc( len + 2 );
144 strcpy( &realval[1], val );
148 key.dsize = strlen( realval ) + 1;
150 idl = idl_fetch( be, db, key );
151 if ( tmpval != NULL ) {
155 ldbm_cache_close( be, db );
157 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
158 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
174 /* XXX do we need idl ??? */
175 ID_BLOCK *idl = NULL;
180 char prefix = index2prefix( indextype );
182 ldbm_datum_init( key );
184 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
186 if ( prefix != UNKNOWN_PREFIX ) {
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 ) {
213 ldap_pvt_thread_yield();
215 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
223 struct berval **vals,
227 char *val, *p, *code, *w;
229 int indexmask, syntax;
230 char buf[SUBLEN + 1];
235 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
237 attr_normalize(type);
238 attr_masks( be->be_private, type, &indexmask, &syntax );
239 if ( indexmask == 0 ) {
243 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
245 Debug( LDAP_DEBUG_ANY,
246 "<= index_add_values -1 (could not open/create %s%s)\n",
247 type, LDBM_SUFFIX, 0 );
252 for ( i = 0; vals[i] != NULL; i++ ) {
254 * presence index entry
256 if ( indexmask & INDEX_PRESENCE ) {
257 add_value( be, db, type, INDEX_PRESENCE, "*", id );
260 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
261 syntax, SYNTAX_BIN, 0 );
262 if ( syntax & SYNTAX_BIN ) {
263 ldbm_cache_close( be, db );
268 len = vals[i]->bv_len;
271 if ( len + 2 > sizeof(vbuf) ) {
272 bigbuf = (char *) ch_malloc( len + 1 );
277 (void) memcpy( val, vals[i]->bv_val, len );
280 value_normalize( val, syntax );
283 * equality index entry
285 if ( indexmask & INDEX_EQUALITY ) {
286 add_value( be, db, type, INDEX_EQUALITY, val, id );
290 * approximate index entry
292 if ( indexmask & INDEX_APPROX ) {
293 for ( w = first_word( val ); w != NULL;
294 w = next_word( w ) ) {
295 if ( (code = phonetic( w )) != NULL ) {
296 add_value( be, db, type, INDEX_APPROX,
304 * substrings index entry
306 if ( indexmask & INDEX_SUB ) {
307 /* leading and trailing */
308 if ( len > SUBLEN - 2 ) {
310 for ( j = 0; j < SUBLEN - 1; j++ ) {
315 add_value( be, db, type, INDEX_SUB, buf, id );
317 p = val + len - SUBLEN + 1;
318 for ( j = 0; j < SUBLEN - 1; j++ ) {
321 buf[SUBLEN - 1] = '$';
324 add_value( be, db, type, INDEX_SUB, buf, id );
328 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
329 for ( j = 0; j < SUBLEN; j++ ) {
334 add_value( be, db, type, INDEX_SUB, buf, id );
338 if ( bigbuf != NULL ) {
342 ldbm_cache_close( be, db );
348 index2prefix( int indextype )
352 switch ( indextype ) {
357 prefix = APPROX_PREFIX;
363 prefix = UNKNOWN_PREFIX;