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,
92 rc = index_change_values( be,
98 case LDAP_MOD_SOFTADD: /* SOFTADD means index was there */
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 */
264 Debug( LDAP_DEBUG_TRACE,
265 "=> index_change_values( \"%s\", NULL, %ld, op=%s )\n",
266 type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
270 Debug( LDAP_DEBUG_TRACE,
271 "=> index_change_values( \"%s\", %ld, op=%s )\n",
272 type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
275 if (op == __INDEX_ADD_OP) {
279 idl_funct = idl_insert_key;
286 idl_funct = idl_delete_key;
291 attr_normalize(type);
292 attr_masks( be->be_private, type, &indexmask, &syntax );
294 if ( indexmask == 0 ) {
298 /* at_cn = at_canonical_name( type ); */
301 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
303 Debug( LDAP_DEBUG_ANY,
304 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
307 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
312 for ( i = 0; vals[i] != NULL; i++ ) {
314 * presence index entry
316 if ( indexmask & INDEX_PRESENCE ) {
318 change_value( be, db, at_cn, INDEX_PRESENCE,
319 "*", id, idl_funct );
323 Debug( LDAP_DEBUG_TRACE,
324 "index_change_values syntax 0x%x syntax bin 0x%x\n",
325 syntax, SYNTAX_BIN, 0 );
327 if ( syntax & SYNTAX_BIN ) {
329 ldbm_cache_close( be, db );
335 len = vals[i]->bv_len;
338 if ( len + 2 > sizeof(vbuf) ) {
339 bigbuf = (char *) ch_malloc( len + 1 );
344 (void) memcpy( val, vals[i]->bv_val, len );
347 value_normalize( val, syntax );
349 /* value_normalize could change the length of val */
353 * equality index entry
355 if ( indexmask & INDEX_EQUALITY ) {
357 change_value( be, db, at_cn, INDEX_EQUALITY,
363 * approximate index entry
365 if ( indexmask & INDEX_APPROX ) {
366 for ( w = first_word( val ); w != NULL;
367 w = next_word( w ) ) {
368 if ( (code = phonetic( w )) != NULL ) {
382 * substrings index entry
384 if ( indexmask & INDEX_SUB ) {
385 /* leading and trailing */
386 if ( len > SUBLEN - 2 ) {
388 for ( j = 0; j < SUBLEN - 1; j++ ) {
393 change_value( be, db, at_cn, INDEX_SUB,
394 buf, id, idl_funct );
396 p = val + len - SUBLEN + 1;
397 for ( j = 0; j < SUBLEN - 1; j++ ) {
400 buf[SUBLEN - 1] = '$';
403 change_value( be, db, at_cn, INDEX_SUB,
404 buf, id, idl_funct );
408 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
409 for ( j = 0; j < SUBLEN; j++ ) {
414 change_value( be, db, at_cn, INDEX_SUB,
415 buf, id, idl_funct );
419 if ( bigbuf != NULL ) {
423 ldbm_cache_close( be, db );
427 }/* int index_change_values() */
430 index2prefix( int indextype )
434 switch ( indextype ) {
439 prefix = APPROX_PREFIX;
445 prefix = UNKNOWN_PREFIX;