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"
19 static int change_value(Backend *be,
26 (*idl_func)(Backend *, DBCache *, Datum, ID));
27 static int index2prefix(int indextype);
37 struct berval *bvals[2];
39 Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
43 * dn index entry - make it look like an attribute so it works
44 * with index_change_values() call
47 bv.bv_val = ch_strdup( e->e_ndn );
48 bv.bv_len = strlen( bv.bv_val );
52 /* add the dn to the indexes */
54 char *dn = ch_strdup("dn");
55 #ifdef SLAPD_SCHEMA_NOT_COMPAT
56 /* not yet implemented */
58 index_change_values( be, dn, bvals, e->e_id, SLAP_INDEX_ADD_OP );
65 /* add each attribute to the indexes */
66 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
67 #ifdef SLAPD_SCHEMA_NOT_COMPAT
68 /* index_change_values( be, SLAP_INDEX_ADD_OP, e->e_id, ap ); */
70 index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
75 Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
94 char *realval, *tmpval;
99 ldbm_datum_init( key );
101 prefix = index2prefix( indextype );
102 Debug( LDAP_DEBUG_TRACE, "=> index_read(\"%c%s\"->\"%s\")\n",
105 attr_mask( be->be_private, type, &indexmask );
106 if ( ! (indextype & indexmask) ) {
107 idl = idl_allids( be );
108 Debug( LDAP_DEBUG_TRACE,
109 "<= index_read %ld candidates (allids - not indexed)\n",
110 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
114 #ifdef SLAPD_SCHEMA_NOT_COMPAT
115 at_cn = at_canonical_name( at_find( type ) );
117 attr_normalize( type );
118 at_cn = at_canonical_name( type );
121 if ( at_cn == NULL ) {
122 Debug( LDAP_DEBUG_ANY,
123 "<= index_read no canonical name for type \"%s\"\n",
124 type != NULL ? type : "(NULL)", 0, 0 );
128 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
130 Debug( LDAP_DEBUG_ANY,
131 "<= index_read NULL (could not open %s%s)\n",
132 at_cn, LDBM_SUFFIX, 0 );
138 if ( prefix != UNKNOWN_PREFIX ) {
139 unsigned int len = strlen( val );
141 if ( (len + 2) < sizeof(buf) ) {
144 /* value + prefix + null */
145 tmpval = (char *) ch_malloc( len + 2 );
150 strcpy( &realval[1], val );
154 key.dsize = strlen( realval ) + 1;
156 idl = idl_fetch( be, db, key );
157 if ( tmpval != NULL ) {
161 ldbm_cache_close( be, db );
163 Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
164 idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
168 /* Add or remove stuff from index files */
178 int (*idl_func)(Backend *, DBCache *, Datum, ID)
187 char prefix = index2prefix( indextype );
189 ldbm_datum_init( key );
191 Debug( LDAP_DEBUG_TRACE,
192 "=> change_value( \"%c%s\", op=%s )\n",
193 prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
195 if ( prefix != UNKNOWN_PREFIX ) {
196 unsigned int len = strlen( val );
198 if ( (len + 2) < sizeof(buf) ) {
201 /* value + prefix + null */
202 tmpval = (char *) ch_malloc( len + 2 );
206 strcpy( &realval[1], val );
210 key.dsize = strlen( realval ) + 1;
212 rc = idl_func( be, db, key, id );
214 if ( tmpval != NULL ) {
218 ldap_pvt_thread_yield();
220 Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
224 }/* static int change_value() */
231 struct berval **vals,
236 #ifndef SLAPD_SCHEMA_NOT_COMPAT
237 char *val, *p, *code, *w;
239 int indexmask, syntax;
240 char buf[SUBLEN + 1];
245 int (*idl_funct)(Backend *,
248 char *at_cn; /* Attribute canonical name */
252 Debug( LDAP_DEBUG_TRACE,
253 "=> index_change_values( %s, NULL, %ld, op=%s )\n",
254 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
258 Debug( LDAP_DEBUG_TRACE,
259 "=> index_change_values( \"%s\", %ld, op=%s )\n",
260 type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
263 if (op == SLAP_INDEX_ADD_OP) {
265 idl_funct = idl_insert_key;
270 idl_funct = idl_delete_key;
274 #ifndef SLAPD_SCHEMA_NOT_COMPAT
275 attr_normalize(type);
277 attr_mask( be->be_private, type, &indexmask );
279 if ( indexmask == 0 ) {
283 #ifdef SLAPD_SCHEMA_NOT_COMPAT
284 at_cn = at_canonical_name( at_find( type ) );
286 syntax = attr_syntax( type );
287 at_cn = at_canonical_name( type );
290 if ( at_cn == NULL ) {
291 Debug( LDAP_DEBUG_ANY,
292 "<= index_change_values no canonical name for type \"%s\"\n",
293 type != NULL ? type : "(NULL)", 0, 0 );
297 if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
299 Debug( LDAP_DEBUG_ANY,
300 "<= index_change_values (couldn't open(%s%s),md=%s)\n",
302 ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
306 #ifdef SLAPD_SCHEMA_NOT_COMPAT
307 /* not yet implemented */
310 * presence index entry
312 if ( indexmask & SLAP_INDEX_PRESENCE ) {
313 change_value( be, db, at_cn, SLAP_INDEX_PRESENCE,
314 "*", id, idl_funct );
317 if ( syntax & SYNTAX_BIN ) {
321 for ( i = 0; vals[i] != NULL; i++ ) {
322 Debug( LDAP_DEBUG_TRACE,
323 "index_change_values syntax 0x%x\n",
327 len = vals[i]->bv_len;
330 if ( len + 2 > sizeof(vbuf) ) {
331 bigbuf = (char *) ch_malloc( len + 1 );
336 (void) memcpy( val, vals[i]->bv_val, len );
339 value_normalize( val, syntax );
341 /* value_normalize could change the length of val */
345 * equality index entry
347 if ( indexmask & SLAP_INDEX_EQUALITY ) {
348 change_value( be, db, at_cn, SLAP_INDEX_EQUALITY,
353 * approximate index entry
355 if ( indexmask & SLAP_INDEX_APPROX ) {
356 for ( w = first_word( val ); w != NULL;
357 w = next_word( w ) ) {
358 if ( (code = phonetic( w )) != NULL ) {
372 * substrings index entry
374 if ( indexmask & SLAP_INDEX_SUB ) {
375 /* leading and trailing */
376 if ( len > SUBLEN - 2 ) {
378 for ( j = 0; j < SUBLEN - 1; j++ ) {
383 change_value( be, db, at_cn, SLAP_INDEX_SUB,
384 buf, id, idl_funct );
386 p = val + len - SUBLEN + 1;
387 for ( j = 0; j < SUBLEN - 1; j++ ) {
390 buf[SUBLEN - 1] = '$';
393 change_value( be, db, at_cn, SLAP_INDEX_SUB,
394 buf, id, idl_funct );
398 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
399 for ( j = 0; j < SUBLEN; j++ ) {
404 change_value( be, db, at_cn, SLAP_INDEX_SUB,
405 buf, id, idl_funct );
409 if ( bigbuf != NULL ) {
416 ldbm_cache_close( be, db );
422 index2prefix( int indextype )
426 switch ( indextype ) {
427 case SLAP_INDEX_EQUALITY:
430 case SLAP_INDEX_APPROX:
431 prefix = APPROX_PREFIX;
437 prefix = UNKNOWN_PREFIX;