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);*/
15 static int change_value(Backend *be,
22 (*idl_func)(Backend *, struct dbcache *, Datum, ID));
23 static int index2prefix(int indextype);
34 struct berval *bvals[2];
36 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
40 * dn index entry - make it look like an attribute so it works
41 * with index_add_values() call
44 bv.bv_val = ch_strdup( e->e_ndn );
45 bv.bv_len = strlen( bv.bv_val );
49 /* add the dn to the indexes */
51 char *dn = ch_strdup("dn");
52 index_change_values( be, dn, bvals, e->e_id, __INDEX_ADD_OP );
58 /* add each attribute to the indexes */
59 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
60 index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
64 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
78 for ( ; mods != NULL; mods = mods->mod_next ) {
79 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
80 case LDAP_MOD_REPLACE:
81 /* XXX: Delete old index data==>problem when this
82 * gets called we lost values already!
85 rc = index_change_values( be,
91 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there already */
95 rc = index_change_values( be,
122 int indexmask, syntax;
124 char *realval, *tmpval;
127 ldbm_datum_init( key );
129 prefix = index2prefix( indextype );
130 Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
133 attr_masks( be->be_private, type, &indexmask, &syntax );
134 if ( ! (indextype & indexmask) ) {
135 idl = idl_allids( be );
136 Debug( LDAP_DEBUG_TRACE,
137 "<= index_read %lu candidates (allids - not indexed)\n",
138 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
142 attr_normalize( type );
143 if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
145 Debug( LDAP_DEBUG_ANY,
146 "<= index_read NULL (could not open %s%s)\n", type,
153 if ( prefix != UNKNOWN_PREFIX ) {
154 unsigned int len = strlen( val );
156 if ( (len + 2) < sizeof(buf) ) {
159 /* value + prefix + null */
160 tmpval = (char *) ch_malloc( len + 2 );
164 strcpy( &realval[1], val );
168 key.dsize = strlen( realval ) + 1;
170 idl = idl_fetch( be, db, key );
171 if ( tmpval != NULL ) {
175 ldbm_cache_close( be, db );
177 Debug( LDAP_DEBUG_TRACE, "<= index_read %lu candidates\n",
178 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
182 /* Add or remove stuff from index files */
192 int (*idl_func)(Backend *, struct dbcache *, Datum, ID)
201 char prefix = index2prefix( indextype );
203 ldbm_datum_init( key );
205 Debug( LDAP_DEBUG_TRACE,
206 "=> change_value( \"%c%s\", op=%s )\n",
207 prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
209 if ( prefix != UNKNOWN_PREFIX ) {
210 unsigned int len = strlen( val );
212 if ( (len + 2) < sizeof(buf) ) {
215 /* value + prefix + null */
216 tmpval = (char *) ch_malloc( len + 2 );
220 strcpy( &realval[1], val );
224 key.dsize = strlen( realval ) + 1;
226 rc = idl_func( be, db, key, id );
228 if ( tmpval != NULL ) {
232 ldap_pvt_thread_yield();
234 Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
238 }/* static int change_value() */
244 struct berval **vals,
249 char *val, *p, *code, *w;
251 int indexmask, syntax;
252 char buf[SUBLEN + 1];
257 int (*idl_funct)(Backend *,
260 char *at_cn; /* Attribute canonical name */
263 Debug( LDAP_DEBUG_TRACE,
264 "=> index_change_values( \"%s\", %ld, op=%s )\n",
265 type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
268 if (op == __INDEX_ADD_OP) {
272 idl_funct = idl_insert_key;
279 idl_funct = idl_delete_key;
284 attr_normalize(type);
285 attr_masks( be->be_private, type, &indexmask, &syntax );
287 if ( indexmask == 0 ) {
291 /* at_cn = at_canonical_name( type ); */
294 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
296 Debug( LDAP_DEBUG_ANY,
297 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
300 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
305 for ( i = 0; vals[i] != NULL; i++ ) {
307 * presence index entry
309 if ( indexmask & INDEX_PRESENCE ) {
311 change_value( be, db, at_cn, INDEX_PRESENCE,
312 "*", id, idl_funct );
316 Debug( LDAP_DEBUG_TRACE,
317 "index_change_values syntax 0x%x syntax bin 0x%x\n",
318 syntax, SYNTAX_BIN, 0 );
320 if ( syntax & SYNTAX_BIN ) {
322 ldbm_cache_close( be, db );
328 len = vals[i]->bv_len;
331 if ( len + 2 > sizeof(vbuf) ) {
332 bigbuf = (char *) ch_malloc( len + 1 );
337 (void) memcpy( val, vals[i]->bv_val, len );
340 value_normalize( val, syntax );
342 /* value_normalize could change the length of val */
346 * equality index entry
348 if ( indexmask & INDEX_EQUALITY ) {
350 change_value( be, db, at_cn, INDEX_EQUALITY,
356 * approximate index entry
358 if ( indexmask & INDEX_APPROX ) {
359 for ( w = first_word( val ); w != NULL;
360 w = next_word( w ) ) {
361 if ( (code = phonetic( w )) != NULL ) {
375 * substrings index entry
377 if ( indexmask & INDEX_SUB ) {
378 /* leading and trailing */
379 if ( len > SUBLEN - 2 ) {
381 for ( j = 0; j < SUBLEN - 1; j++ ) {
386 change_value( be, db, at_cn, INDEX_SUB,
387 buf, id, idl_funct );
389 p = val + len - SUBLEN + 1;
390 for ( j = 0; j < SUBLEN - 1; j++ ) {
393 buf[SUBLEN - 1] = '$';
396 change_value( be, db, at_cn, INDEX_SUB,
397 buf, id, idl_funct );
401 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
402 for ( j = 0; j < SUBLEN; j++ ) {
407 change_value( be, db, at_cn, INDEX_SUB,
408 buf, id, idl_funct );
412 if ( bigbuf != NULL ) {
416 ldbm_cache_close( be, db );
420 }/* int index_change_values() */
423 index2prefix( int indextype )
427 switch ( indextype ) {
432 prefix = APPROX_PREFIX;
438 prefix = UNKNOWN_PREFIX;