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

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

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

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

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

18
#ifndef SLAPD_SCHEMA_NOT_COMPAT
19

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

int
index_add_entry(
    Backend	*be,
    Entry	*e
)
{
37
#ifndef SLAPD_SCHEMA_NOT_COMPAT
Kurt Zeilenga's avatar
Kurt Zeilenga committed
38
39
40
41
42
43
44
45
46
	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
47
	 * with index_change_values() call
Kurt Zeilenga's avatar
Kurt Zeilenga committed
48
49
	 */

50
	bv.bv_val = ch_strdup( e->e_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
51
52
53
54
55
	bv.bv_len = strlen( bv.bv_val );
	bvals[0] = &bv;
	bvals[1] = NULL;

	/* add the dn to the indexes */
56
57
	{
		char *dn = ch_strdup("dn");
58
59
60
#ifdef SLAPD_SCHEMA_NOT_COMPAT
		/* not yet implemented */
#else
61
		index_change_values( be, dn, bvals, e->e_id, SLAP_INDEX_ADD_OP );
62
#endif
63
64
		free( dn );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
65
66
67
68
69

	free( bv.bv_val );

	/* add each attribute to the indexes */
	for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
70
71
72
#ifdef SLAPD_SCHEMA_NOT_COMPAT
		/* index_change_values( be, SLAP_INDEX_ADD_OP, e->e_id, ap ); */
#else
73
		index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
74
				     SLAP_INDEX_ADD_OP );
75
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
76
77
78
	}

	Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
79
	    e->e_ndn, 0 );
80
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
81
82
83
	return( 0 );
}

84
#ifndef SLAPD_SCHEMA_NOT_COMPAT
85
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
86
87
88
89
90
91
92
index_read(
    Backend	*be,
    char	*type,
    int		indextype,
    char	*val
)
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
93
	DBCache	*db;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
94
	Datum   	key;
95
	ID_BLOCK		*idl;
96
	int		indexmask;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
97
98
99
100
	char		prefix;
	char		*realval, *tmpval;
	char		buf[BUFSIZ];

101
	char		*at_cn;
102

103
	ldbm_datum_init( key );
104

Kurt Zeilenga's avatar
Kurt Zeilenga committed
105
	prefix = index2prefix( indextype );
106
107
	Debug( LDAP_DEBUG_TRACE, "=> index_read(\"%c%s\"->\"%s\")\n",
	    prefix, type, val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
108

109
	attr_mask( be->be_private, type, &indexmask );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
110
111
112
	if ( ! (indextype & indexmask) ) {
		idl =  idl_allids( be );
		Debug( LDAP_DEBUG_TRACE,
113
		    "<= index_read %ld candidates (allids - not indexed)\n",
114
		    idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
115
116
117
		return( idl );
	}

118
119
120
#ifdef SLAPD_SCHEMA_NOT_COMPAT
	at_cn = at_canonical_name( at_find( type ) );
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
121
	attr_normalize( type );
122
	at_cn = at_canonical_name( type );
123
#endif
124

125
126
127
128
129
130
131
	if ( at_cn == NULL ) {
		Debug( LDAP_DEBUG_ANY,
		    "<= index_read no canonical name for type \"%s\"\n",
			type != NULL ? type : "(NULL)", 0, 0 );
		return( NULL );
	}

132
	if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
Kurt Zeilenga's avatar
Kurt Zeilenga committed
133
134
	    == NULL ) {
		Debug( LDAP_DEBUG_ANY,
135
136
		    "<= index_read NULL (could not open %s%s)\n",
			at_cn, LDBM_SUFFIX, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
138
139
140
141
		return( NULL );
	}

	realval = val;
	tmpval = NULL;
142
	if ( prefix != UNKNOWN_PREFIX ) {
143
		unsigned int	len = strlen( val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
144

145
		if ( (len + 2) < sizeof(buf) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
146
147
148
149
150
151
			realval = buf;
		} else {
			/* value + prefix + null */
			tmpval = (char *) ch_malloc( len + 2 );
			realval = tmpval;
		}
152
153
154

		realval[0] = prefix;
		strcpy( &realval[1], val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
155
156
157
158
159
160
	}

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

	idl = idl_fetch( be, db, key );
161
	if ( tmpval != NULL ) {
162
              free( tmpval );
163
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
164
165
166

	ldbm_cache_close( be, db );

167
	Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
168
	       idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
169
170
171
	return( idl );
}

172
/* Add or remove stuff from index files */
173
174

static int
175
change_value(
176
    Backend		*be,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
177
    DBCache	*db,
178
179
180
    char		*type,
    int			indextype,
    char		*val,
181
    ID			id,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
182
    int			(*idl_func)(Backend *, DBCache *, Datum, ID)
183
184
)
{
185
186
187
188
189
190
191
192
193
194
195
	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,
196
197
	       "=> change_value( \"%c%s\", op=%s )\n",
	       prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

	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;

216
	rc = idl_func( be, db, key, id );
217
218
219
220
221
222
223

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

	ldap_pvt_thread_yield();

224
	Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
225
226

	return( rc );
227

228
229
}
#endif
230
231

int
232
index_change_values(
233
234
235
    Backend		*be,
    char		*type,
    struct berval	**vals,
236
237
    ID			id,
    unsigned int	op
238
239
)
{
240
#ifndef SLAPD_SCHEMA_NOT_COMPAT
241
242
	char		*val, *p, *code, *w;
	unsigned	i, j, len;
243
	int		indexmask, syntax;
244
245
246
	char		buf[SUBLEN + 1];
	char		vbuf[BUFSIZ];
	char		*bigbuf;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
247
	DBCache	*db;
248

249
	int		(*idl_funct)(Backend *,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
250
				    DBCache *,
251
252
253
				    Datum, ID);
	char		*at_cn;	/* Attribute canonical name */
	int		mode;
254

255
256
257
	if( vals == NULL ) {
		Debug( LDAP_DEBUG_TRACE,
			"=> index_change_values( %s, NULL, %ld, op=%s )\n", 
258
			type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
259
260
261
		return 0;
	}

262
	Debug( LDAP_DEBUG_TRACE,
263
	       "=> index_change_values( \"%s\", %ld, op=%s )\n", 
264
	       type, id, ((op == SLAP_INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
265
266

	
267
	if (op == SLAP_INDEX_ADD_OP) {
268
269
270
271
272
273
274
275
276
277
	    /* Add values */
	    idl_funct =  idl_insert_key;
	    mode = LDBM_WRCREAT;

	} else {
	    /* Delete values */
	    idl_funct = idl_delete_key;
	    mode = LDBM_WRITER;
	}

278
#ifndef SLAPD_SCHEMA_NOT_COMPAT
279
	attr_normalize(type);
280
#endif
281
	attr_mask( be->be_private, type, &indexmask );
282

283
284
285
286
	if ( indexmask == 0 ) {
		return( 0 );
	}

287
288
289
#ifdef SLAPD_SCHEMA_NOT_COMPAT
	at_cn = at_canonical_name( at_find( type ) );
#else
290
	syntax = attr_syntax( type );
291
	at_cn = at_canonical_name( type );
292
#endif
293

294
295
296
297
298
299
300
	if ( at_cn == NULL ) {
		Debug( LDAP_DEBUG_ANY,
		    "<= index_change_values no canonical name for type \"%s\"\n",
			type != NULL ? type : "(NULL)", 0, 0 );
		return( -1 );
	}

301
	if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
302
303
	     == NULL ) {
		Debug( LDAP_DEBUG_ANY,
304
		       "<= index_change_values (couldn't open(%s%s),md=%s)\n",
305
		       at_cn, LDBM_SUFFIX,
306
		       ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
307
308
309
		return( -1 );
	}

310
311
312
#ifdef SLAPD_SCHEMA_NOT_COMPAT
	/* not yet implemented */
#else
313
314
315
316
317
318
319
	/*
	 * presence index entry
	 */
	if ( indexmask & SLAP_INDEX_PRESENCE ) {
		change_value( be, db, at_cn, SLAP_INDEX_PRESENCE,
			"*", id, idl_funct );
	}
320

321
322
323
	if ( syntax & SYNTAX_BIN ) {
		goto done;
	}
324

325
	for ( i = 0; vals[i] != NULL; i++ ) {
326
		Debug( LDAP_DEBUG_TRACE,
327
328
		       "index_change_values syntax 0x%x\n",
		       syntax, 0, 0 );
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

		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
		 */
351
352
		if ( indexmask & SLAP_INDEX_EQUALITY ) {
			change_value( be, db, at_cn, SLAP_INDEX_EQUALITY,
353
				      val, id, idl_funct);
354
355
356
357
358
		}

		/*
		 * approximate index entry
		 */
359
		if ( indexmask & SLAP_INDEX_APPROX ) {
360
361
			for ( w = first_word( val ); w != NULL;
			    w = next_word( w ) ) {
362
				if ( (code = phonetic( w )) != NULL ) {
363
					change_value( be,
364
365
						      db,
						      at_cn,
366
						      SLAP_INDEX_APPROX,
367
						      code,
368
369
						      id,
						      idl_funct );
370
371
372
373
374
375
376
377
					free( code );
				}
			}
		}

		/*
		 * substrings index entry
		 */
378
		if ( indexmask & SLAP_INDEX_SUBSTR ) {
379
380
381
382
383
384
385
386
			/* leading and trailing */
			if ( len > SUBLEN - 2 ) {
				buf[0] = '^';
				for ( j = 0; j < SUBLEN - 1; j++ ) {
					buf[j + 1] = val[j];
				}
				buf[SUBLEN] = '\0';

387
				change_value( be, db, at_cn, SLAP_INDEX_SUBSTR,
388
					      buf, id, idl_funct );
389
390
391
392
393
394
395
396

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

397
				change_value( be, db, at_cn, SLAP_INDEX_SUBSTR,
398
					      buf, id, idl_funct );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
399
400
401
402
403
404
405
406
407
			}

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

408
				change_value( be, db, at_cn, SLAP_INDEX_SUBSTR,
409
					      buf, id, idl_funct );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
410
411
412
413
414
415
416
			}
		}

		if ( bigbuf != NULL ) {
			free( bigbuf );
		}
	}
417
#endif
418

419
done:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
420
	ldbm_cache_close( be, db );
421
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
422
	return( 0 );
423
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
424

425
#ifndef SLAPD_SCHEMA_NOT_COMPAT
Kurt Zeilenga's avatar
Kurt Zeilenga committed
426
427
428
429
430
431
static int
index2prefix( int indextype )
{
	int	prefix;

	switch ( indextype ) {
432
	case SLAP_INDEX_EQUALITY:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
433
434
		prefix = EQ_PREFIX;
		break;
435
	case SLAP_INDEX_APPROX:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
436
437
		prefix = APPROX_PREFIX;
		break;
438
	case SLAP_INDEX_SUBSTR:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
439
440
441
		prefix = SUB_PREFIX;
		break;
	default:
442
		prefix = UNKNOWN_PREFIX;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
443
444
445
446
447
		break;
	}

	return( prefix );
}
448
#endif