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 = ch_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 ( ; ml != NULL; ml = ml->ml_next ) {
66 LDAPMod *mod = &ml->ml_mod;
68 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
70 case LDAP_MOD_REPLACE:
71 rc = index_add_values( be, mod->mod_type,
72 mod->mod_bvalues, id );
99 int indexmask, syntax;
101 char *realval, *tmpval;
104 ldbm_datum_init( 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 %ld candidates (allids - not indexed)\n",
115 idl ? ID_BLOCK_NIDS(idl) : 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 != UNKNOWN_PREFIX ) {
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 %ld candidates\n",
155 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
171 ID_BLOCK *idl = NULL;
176 char prefix = index2prefix( indextype );
178 ldbm_datum_init( key );
180 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
182 if ( prefix != UNKNOWN_PREFIX ) {
183 unsigned int len = strlen( val );
185 if ( (len + 2) < sizeof(buf) ) {
188 /* value + prefix + null */
189 tmpval = (char *) ch_malloc( len + 2 );
193 strcpy( &realval[1], val );
197 key.dsize = strlen( realval ) + 1;
199 rc = idl_insert_key( be, db, key, id );
201 if ( tmpval != NULL ) {
209 ldap_pvt_thread_yield();
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;
359 prefix = UNKNOWN_PREFIX;