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 */
41 index_add_values( be, "dn", bvals, e->e_id );
45 /* add each attribute to the indexes */
46 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
47 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
50 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
64 for ( ; ml != NULL; ml = ml->ml_next ) {
65 LDAPMod *mod = &ml->ml_mod;
67 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
69 case LDAP_MOD_REPLACE:
70 rc = index_add_values( be, mod->mod_type,
71 mod->mod_bvalues, id );
98 int indexmask, syntax;
100 char *realval, *tmpval;
103 ldbm_datum_init( key );
105 prefix = index2prefix( indextype );
106 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
109 attr_masks( be->be_private, type, &indexmask, &syntax );
110 if ( ! (indextype & indexmask) ) {
111 idl = idl_allids( be );
112 Debug( LDAP_DEBUG_TRACE,
113 "<= index_read %ld candidates (allids - not indexed)\n",
114 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
118 attr_normalize( type );
119 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
121 Debug( LDAP_DEBUG_ANY,
122 "<= index_read NULL (could not open %s%s)\n", type,
129 if ( prefix != UNKNOWN_PREFIX ) {
130 unsigned int len = strlen( val );
132 if ( (len + 2) < sizeof(buf) ) {
135 /* value + prefix + null */
136 tmpval = (char *) ch_malloc( len + 2 );
140 strcpy( &realval[1], val );
144 key.dsize = strlen( realval ) + 1;
146 idl = idl_fetch( be, db, key );
147 if ( tmpval != NULL ) {
151 ldbm_cache_close( be, db );
153 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
154 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
170 ID_BLOCK *idl = NULL;
175 char prefix = index2prefix( indextype );
177 ldbm_datum_init( key );
179 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
181 if ( prefix != UNKNOWN_PREFIX ) {
182 unsigned int len = strlen( val );
184 if ( (len + 2) < sizeof(buf) ) {
187 /* value + prefix + null */
188 tmpval = (char *) ch_malloc( len + 2 );
192 strcpy( &realval[1], val );
196 key.dsize = strlen( realval ) + 1;
198 rc = idl_insert_key( be, db, key, id );
200 if ( tmpval != NULL ) {
208 ldap_pvt_thread_yield();
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;
358 prefix = UNKNOWN_PREFIX;