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_ndn );
35 bv.bv_len = strlen( bv.bv_val );
39 /* add the dn to the indexes */
41 char *dn = ch_strdup("dn");
42 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 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 = index_add_values( be, mod->mod_type,
74 mod->mod_bvalues, id );
76 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there */
101 int indexmask, syntax;
103 char *realval, *tmpval;
106 ldbm_datum_init( key );
108 prefix = index2prefix( indextype );
109 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
112 attr_masks( be->be_private, type, &indexmask, &syntax );
113 if ( ! (indextype & indexmask) ) {
114 idl = idl_allids( be );
115 Debug( LDAP_DEBUG_TRACE,
116 "<= index_read %ld candidates (allids - not indexed)\n",
117 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
121 attr_normalize( type );
122 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
124 Debug( LDAP_DEBUG_ANY,
125 "<= 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 = idl_fetch( be, db, key );
150 if ( tmpval != NULL ) {
154 ldbm_cache_close( be, db );
156 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
157 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
173 /* XXX do we need idl ??? */
174 ID_BLOCK *idl = NULL;
179 char prefix = index2prefix( indextype );
181 ldbm_datum_init( key );
183 Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
185 if ( prefix != UNKNOWN_PREFIX ) {
186 unsigned int len = strlen( val );
188 if ( (len + 2) < sizeof(buf) ) {
191 /* value + prefix + null */
192 tmpval = (char *) ch_malloc( len + 2 );
196 strcpy( &realval[1], val );
200 key.dsize = strlen( realval ) + 1;
202 rc = idl_insert_key( be, db, key, id );
204 if ( tmpval != NULL ) {
212 ldap_pvt_thread_yield();
214 /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
222 struct berval **vals,
226 char *val, *p, *code, *w;
228 int indexmask, syntax;
229 char buf[SUBLEN + 1];
234 Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
236 attr_normalize(type);
237 attr_masks( be->be_private, type, &indexmask, &syntax );
238 if ( indexmask == 0 ) {
242 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
244 Debug( LDAP_DEBUG_ANY,
245 "<= index_add_values -1 (could not open/create %s%s)\n",
246 type, LDBM_SUFFIX, 0 );
251 for ( i = 0; vals[i] != NULL; i++ ) {
253 * presence index entry
255 if ( indexmask & INDEX_PRESENCE ) {
256 add_value( be, db, type, INDEX_PRESENCE, "*", id );
259 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
260 syntax, SYNTAX_BIN, 0 );
261 if ( syntax & SYNTAX_BIN ) {
262 ldbm_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, type, 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, type, 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, type, 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, type, 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, type, INDEX_SUB, buf, id );
340 if ( bigbuf != NULL ) {
344 ldbm_cache_close( be, db );
350 index2prefix( int indextype )
354 switch ( indextype ) {
359 prefix = APPROX_PREFIX;
365 prefix = UNKNOWN_PREFIX;