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;
108 ldbm_datum_init( key );
110 prefix = index2prefix( indextype );
111 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_read( \"%s\" \"%c\" \"%s\" )\n",
114 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
115 if ( ! (indextype & indexmask) ) {
116 idl = bdb2i_idl_allids( be );
117 Debug( LDAP_DEBUG_TRACE,
118 "<= bdb2i_index_read %ld candidates (allids - not indexed)\n",
119 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
123 attr_normalize( type );
124 at_cn = at_canonical_name(type);
126 if ( (db = bdb2i_cache_open( be, at_cn, BDB2_SUFFIX, LDBM_WRCREAT ))
128 Debug( LDAP_DEBUG_ANY,
129 "<= bdb2i_index_read NULL (could not open %s%s)\n", at_cn,
136 if ( prefix != UNKNOWN_PREFIX ) {
137 unsigned int len = strlen( val );
139 if ( (len + 2) < sizeof(buf) ) {
142 /* value + prefix + null */
143 tmpval = (char *) ch_malloc( len + 2 );
147 strcpy( &realval[1], val );
151 key.dsize = strlen( realval ) + 1;
153 idl = bdb2i_idl_fetch( be, db, key );
154 if ( tmpval != NULL ) {
158 bdb2i_cache_close( be, db );
160 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_index_read %ld candidates\n",
161 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
181 char prefix = index2prefix( indextype );
183 ldbm_datum_init( key );
185 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
187 if ( prefix != UNKNOWN_PREFIX ) {
188 unsigned int len = strlen( val );
190 if ( (len + 2) < sizeof(buf) ) {
193 /* value + prefix + null */
194 tmpval = (char *) ch_malloc( len + 2 );
198 strcpy( &realval[1], val );
202 key.dsize = strlen( realval ) + 1;
204 rc = bdb2i_idl_insert_key( be, db, key, id );
206 if ( tmpval != NULL ) {
210 ldap_pvt_thread_yield();
212 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
217 bdb2i_index_add_values(
220 struct berval **vals,
224 char *val, *p, *code, *w;
226 int indexmask, syntax;
227 char buf[SUBLEN + 1];
234 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_index_add_values( \"%s\", %ld )\n", type,
236 attr_normalize( type );
237 bdb2i_attr_masks( be->be_private, type, &indexmask, &syntax );
238 if ( indexmask == 0 ) {
241 at_cn = at_canonical_name(type);
243 if ( (db = bdb2i_cache_open( be, at_cn, BDB2_SUFFIX, LDBM_WRCREAT ))
245 Debug( LDAP_DEBUG_ANY,
246 "<= bdb2i_index_add_values -1 (could not open/create %s%s)\n",
247 at_cn, BDB2_SUFFIX, 0 );
251 for ( i = 0; vals[i] != NULL; i++ ) {
253 * presence index entry
255 if ( indexmask & INDEX_PRESENCE ) {
256 add_value( be, db, at_cn, INDEX_PRESENCE, "*", id );
259 Debug( LDAP_DEBUG_TRACE, "*** bdb2i_index_add_values syntax 0x%x syntax bin 0x%x\n",
260 syntax, SYNTAX_BIN, 0 );
261 if ( syntax & SYNTAX_BIN ) {
262 bdb2i_cache_close( be, db );
267 len = vals[i]->bv_len;
270 if ( len + 2 > sizeof(vbuf) ) {
271 bigbuf = (char *) ch_malloc( len + 1 );
276 (void) memcpy( val, vals[i]->bv_val, len );
279 value_normalize( val, syntax );
281 /* value_normalize could change the length of val */
285 * equality index entry
287 if ( indexmask & INDEX_EQUALITY ) {
288 add_value( be, db, at_cn, INDEX_EQUALITY, val, id );
292 * approximate index entry
294 if ( indexmask & INDEX_APPROX ) {
295 for ( w = first_word( val ); w != NULL;
296 w = next_word( w ) ) {
297 if ( (code = phonetic( w )) != NULL ) {
298 add_value( be, db, at_cn, INDEX_APPROX,
306 * substrings index entry
308 if ( indexmask & INDEX_SUB ) {
309 /* leading and trailing */
310 if ( len > SUBLEN - 2 ) {
312 for ( j = 0; j < SUBLEN - 1; j++ ) {
317 add_value( be, db, at_cn, INDEX_SUB, buf, id );
319 p = val + len - SUBLEN + 1;
320 for ( j = 0; j < SUBLEN - 1; j++ ) {
323 buf[SUBLEN - 1] = '$';
326 add_value( be, db, at_cn, INDEX_SUB, buf, id );
330 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
331 for ( j = 0; j < SUBLEN; j++ ) {
336 add_value( be, db, at_cn, INDEX_SUB, buf, id );
340 if ( bigbuf != NULL ) {
344 bdb2i_cache_close( be, db );
350 index2prefix( int indextype )
354 switch ( indextype ) {
359 prefix = APPROX_PREFIX;
365 prefix = UNKNOWN_PREFIX;