1 /* index.c - routines for dealing with attribute indexes */
11 #include "back-bdb2.h"
13 static int add_value(BackendDB *be, struct dbcache *db, char *type, int indextype, char *val, ID id);
14 static int index2prefix(int indextype);
17 bdb2i_index_add_entry(
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 bdb2i_index_add_values() call
34 bv.bv_val = ch_strdup( e->e_ndn );
35 bv.bv_len = strlen( bv.bv_val );
39 /* add the dn to the indexes */
41 char *dn = ch_strdup( "dn" );
42 bdb2i_index_add_values( be, dn, bvals, e->e_id );
48 /* add each attribute to the indexes */
49 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
50 bdb2i_index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
53 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
67 for ( ; ml != NULL; ml = ml->ml_next ) {
68 LDAPMod *mod = &ml->ml_mod;
70 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
72 case LDAP_MOD_REPLACE:
73 rc = bdb2i_index_add_values( be, mod->mod_type,
74 mod->mod_bvalues, id );
76 case LDAP_MOD_SOFTADD:
101 int indexmask, syntax;
103 char *realval, *tmpval;
106 ldbm_datum_init( key );
108 prefix = index2prefix( indextype );
109 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_read( \"%s\" \"%c\" \"%s\" )\n",
112 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
113 if ( ! (indextype & indexmask) ) {
114 idl = bdb2i_idl_allids( be );
115 Debug( LDAP_DEBUG_TRACE,
116 "<= bdb2i_index_read %ld candidates (allids - not indexed)\n",
117 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
121 attr_normalize( type );
122 if ( (db = bdb2i_cache_open( be, type, BDB2_SUFFIX, LDBM_WRCREAT ))
124 Debug( LDAP_DEBUG_ANY,
125 "<= bdb2i_index_read NULL (could not open %s%s)\n", type,
132 if ( prefix != UNKNOWN_PREFIX ) {
133 unsigned int len = strlen( val );
135 if ( (len + 2) < sizeof(buf) ) {
138 /* value + prefix + null */
139 tmpval = (char *) ch_malloc( len + 2 );
143 strcpy( &realval[1], val );
147 key.dsize = strlen( realval ) + 1;
149 idl = bdb2i_idl_fetch( be, db, key );
150 if ( tmpval != NULL ) {
154 bdb2i_cache_close( be, db );
156 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_index_read %ld candidates\n",
157 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
177 char prefix = index2prefix( indextype );
179 ldbm_datum_init( key );
181 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
183 if ( prefix != UNKNOWN_PREFIX ) {
184 unsigned int len = strlen( val );
186 if ( (len + 2) < sizeof(buf) ) {
189 /* value + prefix + null */
190 tmpval = (char *) ch_malloc( len + 2 );
194 strcpy( &realval[1], val );
198 key.dsize = strlen( realval ) + 1;
200 rc = bdb2i_idl_insert_key( be, db, key, id );
202 if ( tmpval != NULL ) {
206 ldap_pvt_thread_yield();
208 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
213 bdb2i_index_add_values(
216 struct berval **vals,
220 char *val, *p, *code, *w;
222 int indexmask, syntax;
223 char buf[SUBLEN + 1];
228 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_add_values( \"%s\", %ld )\n", type,
230 attr_normalize( type );
231 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
232 if ( indexmask == 0 ) {
236 if ( (db = bdb2i_cache_open( be, type, BDB2_SUFFIX, LDBM_WRCREAT ))
238 Debug( LDAP_DEBUG_ANY,
239 "<= bdb2i_index_add_values -1 (could not open/create %s%s)\n",
240 type, BDB2_SUFFIX, 0 );
244 for ( i = 0; vals[i] != NULL; i++ ) {
246 * presence index entry
248 if ( indexmask & INDEX_PRESENCE ) {
249 add_value( be, db, type, INDEX_PRESENCE, "*", id );
252 Debug( LDAP_DEBUG_TRACE, "*** bdb2i_index_add_values syntax 0x%x syntax bin 0x%x\n",
253 syntax, SYNTAX_BIN, 0 );
254 if ( syntax & SYNTAX_BIN ) {
255 bdb2i_cache_close( be, db );
260 len = vals[i]->bv_len;
263 if ( len + 2 > sizeof(vbuf) ) {
264 bigbuf = (char *) ch_malloc( len + 1 );
269 (void) memcpy( val, vals[i]->bv_val, len );
272 value_normalize( val, syntax );
274 /* value_normalize could change the length of val */
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 bdb2i_cache_close( be, db );
343 index2prefix( int indextype )
347 switch ( indextype ) {
352 prefix = APPROX_PREFIX;
358 prefix = UNKNOWN_PREFIX;