1 /* index.c - routines for dealing with attribute indexes */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
18 #ifndef SLAPD_SCHEMA_NOT_COMPAT
20 static int change_value(Backend *be,
27 (*idl_func)(Backend *, DBCache *, Datum, ID));
28 static int index2prefix(int indextype);
37 #ifndef SLAPD_SCHEMA_NOT_COMPAT
40 struct berval *bvals[2];
42 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
46 * dn index entry - make it look like an attribute so it works
47 * with index_change_values() call
50 bv.bv_val = ch_strdup( e->e_ndn );
51 bv.bv_len = strlen( bv.bv_val );
55 /* add the dn to the indexes */
57 char *dn = ch_strdup("dn");
58 #ifdef SLAPD_SCHEMA_NOT_COMPAT
59 /* not yet implemented */
61 index_change_values( be, dn, bvals, e->e_id, SLAP_INDEX_ADD_OP );
68 /* add each attribute to the indexes */
69 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
70 #ifdef SLAPD_SCHEMA_NOT_COMPAT
71 /* index_change_values( be, SLAP_INDEX_ADD_OP, e->e_id, ap ); */
73 index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
78 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
84 #ifndef SLAPD_SCHEMA_NOT_COMPAT
98 char *realval, *tmpval;
103 ldbm_datum_init( key );
105 prefix = index2prefix( indextype );
106 Debug( LDAP_DEBUG_TRACE, "=> index_read(\"%c%s\"->\"%s\")\n",
109 attr_mask( be->be_private, type, &indexmask );
110 if ( ! (indextype & indexmask) ) {
111 idl = idl_allids( be );
112 Debug( LDAP_DEBUG_TRACE,
113 "<= index_read %ld candidates (allids - not indexed)\n",
114 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
118 #ifdef SLAPD_SCHEMA_NOT_COMPAT
119 at_cn = at_canonical_name( at_find( type ) );
121 attr_normalize( type );
122 at_cn = at_canonical_name( type );
125 if ( at_cn == NULL ) {
126 Debug( LDAP_DEBUG_ANY,
127 "<= index_read no canonical name for type \"%s\"\n",
128 type != NULL ? type : "(NULL)", 0, 0 );
132 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
134 Debug( LDAP_DEBUG_ANY,
135 "<= index_read NULL (could not open %s%s)\n",
136 at_cn, LDBM_SUFFIX, 0 );
142 if ( prefix != UNKNOWN_PREFIX ) {
143 unsigned int len = strlen( val );
145 if ( (len + 2) < sizeof(buf) ) {
148 /* value + prefix + null */
149 tmpval = (char *) ch_malloc( len + 2 );
154 strcpy( &realval[1], val );
158 key.dsize = strlen( realval ) + 1;
160 idl = idl_fetch( be, db, key );
161 if ( tmpval != NULL ) {
165 ldbm_cache_close( be, db );
167 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
168 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
172 /* Add or remove stuff from index files */
182 int (*idl_func)(Backend *, DBCache *, Datum, ID)
191 char prefix = index2prefix( indextype );
193 ldbm_datum_init( key );
195 Debug( LDAP_DEBUG_TRACE,
196 "=> change_value( \"%c%s\", op=%s )\n",
197 prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
199 if ( prefix != UNKNOWN_PREFIX ) {
200 unsigned int len = strlen( val );
202 if ( (len + 2) < sizeof(buf) ) {
205 /* value + prefix + null */
206 tmpval = (char *) ch_malloc( len + 2 );
210 strcpy( &realval[1], val );
214 key.dsize = strlen( realval ) + 1;
216 rc = idl_func( be, db, key, id );
218 if ( tmpval != NULL ) {
222 ldap_pvt_thread_yield();
224 Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
235 struct berval **vals,
240 #ifndef SLAPD_SCHEMA_NOT_COMPAT
241 char *val, *p, *code, *w;
243 int indexmask, syntax;
244 char buf[SUBLEN + 1];
249 int (*idl_funct)(Backend *,
252 char *at_cn; /* Attribute canonical name */
256 Debug( LDAP_DEBUG_TRACE,
257 "=> index_change_values( %s, NULL, %ld, op=%s )\n",
258 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
262 Debug( LDAP_DEBUG_TRACE,
263 "=> index_change_values( \"%s\", %ld, op=%s )\n",
264 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
267 if (op == SLAP_INDEX_ADD_OP) {
269 idl_funct = idl_insert_key;
274 idl_funct = idl_delete_key;
278 #ifndef SLAPD_SCHEMA_NOT_COMPAT
279 attr_normalize(type);
281 attr_mask( be->be_private, type, &indexmask );
283 if ( indexmask == 0 ) {
287 #ifdef SLAPD_SCHEMA_NOT_COMPAT
288 at_cn = at_canonical_name( at_find( type ) );
290 syntax = attr_syntax( type );
291 at_cn = at_canonical_name( type );
294 if ( at_cn == NULL ) {
295 Debug( LDAP_DEBUG_ANY,
296 "<= index_change_values no canonical name for type \"%s\"\n",
297 type != NULL ? type : "(NULL)", 0, 0 );
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",
306 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
310 #ifdef SLAPD_SCHEMA_NOT_COMPAT
311 /* not yet implemented */
314 * presence index entry
316 if ( indexmask & SLAP_INDEX_PRESENCE ) {
317 change_value( be, db, at_cn, SLAP_INDEX_PRESENCE,
318 "*", id, idl_funct );
321 if ( syntax & SYNTAX_BIN ) {
325 for ( i = 0; vals[i] != NULL; i++ ) {
326 Debug( LDAP_DEBUG_TRACE,
327 "index_change_values syntax 0x%x\n",
331 len = vals[i]->bv_len;
334 if ( len + 2 > sizeof(vbuf) ) {
335 bigbuf = (char *) ch_malloc( len + 1 );
340 (void) memcpy( val, vals[i]->bv_val, len );
343 value_normalize( val, syntax );
345 /* value_normalize could change the length of val */
349 * equality index entry
351 if ( indexmask & SLAP_INDEX_EQUALITY ) {
352 change_value( be, db, at_cn, SLAP_INDEX_EQUALITY,
357 * approximate index entry
359 if ( indexmask & SLAP_INDEX_APPROX ) {
360 for ( w = first_word( val ); w != NULL;
361 w = next_word( w ) ) {
362 if ( (code = phonetic( w )) != NULL ) {
376 * substrings index entry
378 if ( indexmask & SLAP_INDEX_SUBSTR ) {
379 /* leading and trailing */
380 if ( len > SUBLEN - 2 ) {
382 for ( j = 0; j < SUBLEN - 1; j++ ) {
387 change_value( be, db, at_cn, SLAP_INDEX_SUBSTR,
388 buf, id, idl_funct );
390 p = val + len - SUBLEN + 1;
391 for ( j = 0; j < SUBLEN - 1; j++ ) {
394 buf[SUBLEN - 1] = '$';
397 change_value( be, db, at_cn, SLAP_INDEX_SUBSTR,
398 buf, id, idl_funct );
402 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
403 for ( j = 0; j < SUBLEN; j++ ) {
408 change_value( be, db, at_cn, SLAP_INDEX_SUBSTR,
409 buf, id, idl_funct );
413 if ( bigbuf != NULL ) {
420 ldbm_cache_close( be, db );
425 #ifndef SLAPD_SCHEMA_NOT_COMPAT
427 index2prefix( int indextype )
431 switch ( indextype ) {
432 case SLAP_INDEX_EQUALITY:
435 case SLAP_INDEX_APPROX:
436 prefix = APPROX_PREFIX;
438 case SLAP_INDEX_SUBSTR:
442 prefix = UNKNOWN_PREFIX;