index.c 8.73 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* index.c - routines for dealing with attribute indexes */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4
5
/*
 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6

Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
8
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
11
12
13

#include <ac/string.h>
#include <ac/socket.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
14
15
16
#include "slap.h"
#include "back-ldbm.h"

17
static int	change_value(Backend *be,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
18
			  DBCache *db,
19
20
21
			  char *type,
			  int indextype,
			  char *val,
22
23
			  ID id,
			  int
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
			  (*idl_func)(Backend *, DBCache *, Datum, ID));
25
static int	index2prefix(int indextype);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

int
index_add_entry(
    Backend	*be,
    Entry	*e
)
{
	Attribute	*ap;
	struct berval	bv;
	struct berval	*bvals[2];

	Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
	    e->e_dn, 0 );

	/*
	 * dn index entry - make it look like an attribute so it works
42
	 * with index_change_values() call
Kurt Zeilenga's avatar
Kurt Zeilenga committed
43
44
	 */

45
	bv.bv_val = ch_strdup( e->e_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
46
47
48
49
50
	bv.bv_len = strlen( bv.bv_val );
	bvals[0] = &bv;
	bvals[1] = NULL;

	/* add the dn to the indexes */
51
52
	{
		char *dn = ch_strdup("dn");
53
		index_change_values( be, dn, bvals, e->e_id, SLAP_INDEX_ADD_OP );
54
55
		free( dn );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
56
57
58
59
60

	free( bv.bv_val );

	/* add each attribute to the indexes */
	for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
61
		index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
62
				     SLAP_INDEX_ADD_OP );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
63
64
65
	}

	Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
66
	    e->e_ndn, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
67
68
69
70
71
72
	return( 0 );
}

int
index_add_mods(
    Backend	*be,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
73
    LDAPModList	*ml,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
74
75
76
77
78
    ID		id
)
{
	int	rc;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
79
80
81
82
	for ( ; ml != NULL; ml = ml->ml_next ) {
		LDAPMod *mod = &ml->ml_mod;

		switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
83
		case LDAP_MOD_REPLACE:
84
85
86
87
			/* XXX: Delete old index data==>problem when this 
			 * gets called we lost values already!
			 */
		case LDAP_MOD_ADD:
88
89
90
91
			rc = index_change_values( be,
					       mod->mod_type,
					       mod->mod_bvalues,
					       id,
92
					       SLAP_INDEX_ADD_OP );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
93
94
			break;
		case LDAP_MOD_DELETE:
95
96
97
98
			rc =  index_change_values( be,
						   mod->mod_type,
						   mod->mod_bvalues,
						   id,
99
						   SLAP_INDEX_DELETE_OP );
100
101
			break;
 		case LDAP_MOD_SOFTADD:	/* SOFTADD means index was there */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
102
103
104
105
106
107
108
109
110
111
112
113
			rc = 0;
			break;
		}

		if ( rc != 0 ) {
			return( rc );
		}
	}

	return( 0 );
}

114
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
115
116
117
118
119
120
121
index_read(
    Backend	*be,
    char	*type,
    int		indextype,
    char	*val
)
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
122
	DBCache	*db;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
123
	Datum   	key;
124
	ID_BLOCK		*idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
125
126
127
128
129
	int		indexmask, syntax;
	char		prefix;
	char		*realval, *tmpval;
	char		buf[BUFSIZ];

130
	char		*at_cn;
131

132
	ldbm_datum_init( key );
133

Kurt Zeilenga's avatar
Kurt Zeilenga committed
134
	prefix = index2prefix( indextype );
135
136
	Debug( LDAP_DEBUG_TRACE, "=> index_read(\"%c%s\"->\"%s\")\n",
	    prefix, type, val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
138
139
140
141

	attr_masks( be->be_private, type, &indexmask, &syntax );
	if ( ! (indextype & indexmask) ) {
		idl =  idl_allids( be );
		Debug( LDAP_DEBUG_TRACE,
142
		    "<= index_read %ld candidates (allids - not indexed)\n",
143
		    idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
144
145
146
147
		return( idl );
	}

	attr_normalize( type );
148
	at_cn = at_canonical_name( type );
149

150
	if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
Kurt Zeilenga's avatar
Kurt Zeilenga committed
151
152
	    == NULL ) {
		Debug( LDAP_DEBUG_ANY,
153
		    "<= index_read NULL (could not open %s%s)\n", at_cn,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
154
155
156
157
158
159
		    LDBM_SUFFIX, 0 );
		return( NULL );
	}

	realval = val;
	tmpval = NULL;
160
	if ( prefix != UNKNOWN_PREFIX ) {
161
		unsigned int	len = strlen( val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
162

163
		if ( (len + 2) < sizeof(buf) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
164
165
166
167
168
169
			realval = buf;
		} else {
			/* value + prefix + null */
			tmpval = (char *) ch_malloc( len + 2 );
			realval = tmpval;
		}
170
171
172

		realval[0] = prefix;
		strcpy( &realval[1], val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
173
174
175
176
177
178
	}

	key.dptr = realval;
	key.dsize = strlen( realval ) + 1;

	idl = idl_fetch( be, db, key );
179
	if ( tmpval != NULL ) {
180
              free( tmpval );
181
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
182
183
184

	ldbm_cache_close( be, db );

185
	Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
186
	       idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
187
188
189
	return( idl );
}

190
/* Add or remove stuff from index files */
191
192

static int
193
change_value(
194
    Backend		*be,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
195
    DBCache	*db,
196
197
198
    char		*type,
    int			indextype,
    char		*val,
199
    ID			id,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
200
    int			(*idl_func)(Backend *, DBCache *, Datum, ID)
201
202
)
{
203
204
205
206
207
208
209
210
211
212
213
	int	rc;
	Datum   key;
	char	*tmpval = NULL;
	char	*realval = val;
	char	buf[BUFSIZ];

	char	prefix = index2prefix( indextype );

	ldbm_datum_init( key );

	Debug( LDAP_DEBUG_TRACE,
214
215
	       "=> change_value( \"%c%s\", op=%s )\n",
	       prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

	if ( prefix != UNKNOWN_PREFIX ) {
              unsigned int     len = strlen( val );

              if ( (len + 2) < sizeof(buf) ) {
			realval = buf;
	      } else {
			/* value + prefix + null */
			tmpval = (char *) ch_malloc( len + 2 );
			realval = tmpval;
	      }
              realval[0] = prefix;
              strcpy( &realval[1], val );
	}

	key.dptr = realval;
	key.dsize = strlen( realval ) + 1;

234
	rc = idl_func( be, db, key, id );
235
236
237
238
239
240
241

	if ( tmpval != NULL ) {
		free( tmpval );
	}

	ldap_pvt_thread_yield();

242
	Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
243
244

	return( rc );
245

246
}/* static int change_value() */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
247

248
249

int
250
index_change_values(
251
252
253
    Backend		*be,
    char		*type,
    struct berval	**vals,
254
255
    ID			id,
    unsigned int	op
256
257
)
{
258
259
	char		*val, *p, *code, *w;
	unsigned	i, j, len;
260
	int		indexmask, syntax;
261
262
263
	char		buf[SUBLEN + 1];
	char		vbuf[BUFSIZ];
	char		*bigbuf;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
264
	DBCache	*db;
265

266
	int		(*idl_funct)(Backend *,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
267
				    DBCache *,
268
269
270
				    Datum, ID);
	char		*at_cn;	/* Attribute canonical name */
	int		mode;
271

272
273
274
	if( vals == NULL ) {
		Debug( LDAP_DEBUG_TRACE,
			"=> index_change_values( %s, NULL, %ld, op=%s )\n", 
275
			type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
276
277
278
		return 0;
	}

279
	Debug( LDAP_DEBUG_TRACE,
280
	       "=> index_change_values( \"%s\", %ld, op=%s )\n", 
281
	       type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
282
283

	
284
	if (op == SLAP_INDEX_ADD_OP) {
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

	    /* Add values */

	    idl_funct =  idl_insert_key;
	    mode = LDBM_WRCREAT;

	} else {

	    /* Delete values */

	    idl_funct = idl_delete_key;
	    mode = LDBM_WRITER;

	}

300
301
	attr_normalize(type);
	attr_masks( be->be_private, type, &indexmask, &syntax );
302

303
304
305
306
307
308
	if ( indexmask == 0 ) {
		return( 0 );
	}

	at_cn = at_canonical_name( type );

309
	if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
310
311
	     == NULL ) {
		Debug( LDAP_DEBUG_ANY,
312
		       "<= index_change_values (couldn't open(%s%s),md=%s)\n",
313
314
		       at_cn,
		       LDBM_SUFFIX,
315
		       ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
316
317
318
319
		return( -1 );
	}


320
	for ( i = 0; vals[i] != NULL; i++ ) {
321
		/*
322
		 * presence index entry
323
324
		 */
		if ( indexmask & INDEX_PRESENCE ) {
325
326
327
328

			change_value( be, db, at_cn, INDEX_PRESENCE,
				      "*", id, idl_funct );

329
330
331
		}

		Debug( LDAP_DEBUG_TRACE,
332
		       "index_change_values syntax 0x%x syntax bin 0x%x\n",
333
334
335
		       syntax, SYNTAX_BIN, 0 );

		if ( syntax & SYNTAX_BIN ) {
336

337
338
			ldbm_cache_close( be, db );
			return( 0 );
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
		}

		bigbuf = NULL;
		len = vals[i]->bv_len;

		/* value + null */
		if ( len + 2 > sizeof(vbuf) ) {
			bigbuf = (char *) ch_malloc( len + 1 );
			val = bigbuf;
		} else {
			val = vbuf;
		}
		(void) memcpy( val, vals[i]->bv_val, len );
		val[len] = '\0';

		value_normalize( val, syntax );

		/* value_normalize could change the length of val */
		len = strlen( val );

		/*
		 * equality index entry
		 */
		if ( indexmask & INDEX_EQUALITY ) {
		    
365
366
			change_value( be, db, at_cn, INDEX_EQUALITY,
				      val, id, idl_funct);
367
368
369
370
371
372
373

		}

		/*
		 * approximate index entry
		 */
		if ( indexmask & INDEX_APPROX ) {
374
375
			for ( w = first_word( val ); w != NULL;
			    w = next_word( w ) ) {
376
				if ( (code = phonetic( w )) != NULL ) {
377
					change_value( be,
378
379
380
381
						      db,
						      at_cn,
						      INDEX_APPROX,
						      code,
382
383
						      id,
						      idl_funct );
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
					free( code );
				}
			}
		}

		/*
		 * substrings index entry
		 */
		if ( indexmask & INDEX_SUB ) {
			/* leading and trailing */
			if ( len > SUBLEN - 2 ) {
				buf[0] = '^';
				for ( j = 0; j < SUBLEN - 1; j++ ) {
					buf[j + 1] = val[j];
				}
				buf[SUBLEN] = '\0';

401
402
				change_value( be, db, at_cn, INDEX_SUB,
					      buf, id, idl_funct );
403
404
405
406
407
408
409
410

				p = val + len - SUBLEN + 1;
				for ( j = 0; j < SUBLEN - 1; j++ ) {
					buf[j] = p[j];
				}
				buf[SUBLEN - 1] = '$';
				buf[SUBLEN] = '\0';

411
412
				change_value( be, db, at_cn, INDEX_SUB,
					      buf, id, idl_funct );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
413
414
415
416
417
418
419
420
421
			}

			/* any */
			for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
				for ( j = 0; j < SUBLEN; j++ ) {
					buf[j] = p[j];
				}
				buf[SUBLEN] = '\0';

422
423
				change_value( be, db, at_cn, INDEX_SUB,
					      buf, id, idl_funct );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
424
425
426
427
428
429
430
			}
		}

		if ( bigbuf != NULL ) {
			free( bigbuf );
		}
	}
431

Kurt Zeilenga's avatar
Kurt Zeilenga committed
432
433
434
	ldbm_cache_close( be, db );

	return( 0 );
435

436
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

static int
index2prefix( int indextype )
{
	int	prefix;

	switch ( indextype ) {
	case INDEX_EQUALITY:
		prefix = EQ_PREFIX;
		break;
	case INDEX_APPROX:
		prefix = APPROX_PREFIX;
		break;
	case INDEX_SUB:
		prefix = SUB_PREFIX;
		break;
	default:
454
		prefix = UNKNOWN_PREFIX;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
455
456
457
458
459
		break;
	}

	return( prefix );
}