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(
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 bdb2i_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 bdb2i_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 bdb2i_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 = bdb2i_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, "=> bdb2i_index_read( \"%s\" \"%c\" \"%s\" )\n",
110 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
111 if ( ! (indextype & indexmask) ) {
112 idl = bdb2i_idl_allids( be );
113 Debug( LDAP_DEBUG_TRACE,
114 "<= bdb2i_index_read %ld candidates (allids - not indexed)\n",
115 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
119 attr_normalize( type );
120 if ( (db = bdb2i_cache_open( be, type, BDB2_SUFFIX, LDBM_WRCREAT ))
122 Debug( LDAP_DEBUG_ANY,
123 "<= bdb2i_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 = bdb2i_idl_fetch( be, db, key );
148 if ( tmpval != NULL ) {
152 bdb2i_cache_close( be, db );
154 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_index_read %ld candidates\n",
155 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
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 = bdb2i_idl_insert_key( be, db, key, id );
200 if ( tmpval != NULL ) {
204 ldap_pvt_thread_yield();
206 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
211 bdb2i_index_add_values(
214 struct berval **vals,
218 char *val, *p, *code, *w;
220 int indexmask, syntax;
221 char buf[SUBLEN + 1];
226 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_add_values( \"%s\", %ld )\n", type,
229 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
230 if ( indexmask == 0 ) {
234 if ( (db = bdb2i_cache_open( be, type, BDB2_SUFFIX, LDBM_WRCREAT ))
236 Debug( LDAP_DEBUG_ANY,
237 "<= bdb2i_index_add_values -1 (could not open/create %s%s)\n",
238 type, BDB2_SUFFIX, 0 );
242 for ( i = 0; vals[i] != NULL; i++ ) {
244 * presence index entry
246 if ( indexmask & INDEX_PRESENCE ) {
247 add_value( be, db, type, INDEX_PRESENCE, "*", id );
250 Debug( LDAP_DEBUG_TRACE, "*** bdb2i_index_add_values syntax 0x%x syntax bin 0x%x\n",
251 syntax, SYNTAX_BIN, 0 );
252 if ( syntax & SYNTAX_BIN ) {
253 bdb2i_cache_close( be, db );
258 len = vals[i]->bv_len;
261 if ( len + 2 > sizeof(vbuf) ) {
262 bigbuf = (char *) ch_malloc( len + 1 );
267 (void) memcpy( val, vals[i]->bv_val, len );
270 value_normalize( val, syntax );
273 * equality index entry
275 if ( indexmask & INDEX_EQUALITY ) {
276 add_value( be, db, type, INDEX_EQUALITY, val, id );
280 * approximate index entry
282 if ( indexmask & INDEX_APPROX ) {
283 for ( w = first_word( val ); w != NULL;
284 w = next_word( w ) ) {
285 if ( (code = phonetic( w )) != NULL ) {
286 add_value( be, db, type, INDEX_APPROX,
294 * substrings index entry
296 if ( indexmask & INDEX_SUB ) {
297 /* leading and trailing */
298 if ( len > SUBLEN - 2 ) {
300 for ( j = 0; j < SUBLEN - 1; j++ ) {
305 add_value( be, db, type, INDEX_SUB, buf, id );
307 p = val + len - SUBLEN + 1;
308 for ( j = 0; j < SUBLEN - 1; j++ ) {
311 buf[SUBLEN - 1] = '$';
314 add_value( be, db, type, INDEX_SUB, buf, id );
318 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
319 for ( j = 0; j < SUBLEN; j++ ) {
324 add_value( be, db, type, INDEX_SUB, buf, id );
328 if ( bigbuf != NULL ) {
332 bdb2i_cache_close( be, db );
338 index2prefix( int indextype )
342 switch ( indextype ) {
347 prefix = APPROX_PREFIX;
353 prefix = UNKNOWN_PREFIX;