filterindex.c 19.7 KB
Newer Older
1
2
3
/* filterindex.c - generate the list of candidate entries from a filter */
/* $OpenLDAP$ */
/*
4
 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include "portable.h"

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

#include "back-bdb.h"
#include "idl.h"

static int presence_candidates(
	Backend *be,
	AttributeDescription *desc,
	ID *ids );

static int equality_candidates(
	Backend *be,
	AttributeAssertion *ava,
	ID *ids,
	ID *tmp );
static int approx_candidates(
	Backend *be,
	AttributeAssertion *ava,
	ID *ids,
	ID *tmp );
static int substring_candidates(
	Backend *be,
	SubstringsAssertion *sub,
	ID *ids,
	ID *tmp );

static int list_candidates(
	Backend *be,
	Filter *flist,
	int ftype,
	ID *ids,
	ID *tmp,
	ID *stack );

int
bdb_filter_candidates(
	Backend	*be,
	Filter	*f,
	ID *ids,
	ID *tmp,
	ID *stack )
{
Howard Chu's avatar
Howard Chu committed
53
	int rc = 0;
54
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
55
	LDAP_LOG ( INDEX, ENTRY, "=> bdb_filter_candidates\n", 0, 0, 0 );
56
57
58
59
60
#else
	Debug( LDAP_DEBUG_FILTER, "=> bdb_filter_candidates\n", 0, 0, 0 );
#endif

	switch ( f->f_choice ) {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	case SLAPD_FILTER_COMPUTED:
		switch( f->f_result ) {
		case LDAP_COMPARE_FALSE:
			BDB_IDL_ZERO( ids );
			break;
		case LDAP_COMPARE_TRUE: {
			struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
			BDB_IDL_ALL( bdb, ids );
			} break;
		case SLAPD_COMPARE_UNDEFINED:
			break;
		}
		break;

75
76
	case SLAPD_FILTER_DN_ONE:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tDN ONE\n", 0, 0, 0 );
78
79
80
81
#else
		Debug( LDAP_DEBUG_FILTER, "\tDN ONE\n", 0, 0, 0 );
#endif
		rc = bdb_dn2idl( be, f->f_dn, DN_ONE_PREFIX, ids );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
82
83
84
85
		if( rc == DB_NOTFOUND ) {
			BDB_IDL_ZERO( ids );
			rc = 0;
		}
86
87
88
89
		break;

	case SLAPD_FILTER_DN_SUBTREE:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
90
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tDN SUBTREE\n", 0, 0, 0 );
91
92
93
94
95
96
97
98
#else
		Debug( LDAP_DEBUG_FILTER, "\tDN SUBTREE\n", 0, 0, 0 );
#endif
		rc = bdb_dn2idl( be, f->f_dn, DN_SUBTREE_PREFIX, ids );
		break;

	case LDAP_FILTER_PRESENT:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tPRESENT\n", 0, 0, 0 );
100
101
102
103
104
105
106
107
#else
		Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
#endif
		rc = presence_candidates( be, f->f_desc, ids );
		break;

	case LDAP_FILTER_EQUALITY:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
108
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tEQUALITY\n", 0, 0, 0 );
109
110
111
112
113
114
115
116
#else
		Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
#endif
		rc = equality_candidates( be, f->f_ava, ids, tmp );
		break;

	case LDAP_FILTER_APPROX:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
117
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tAPPROX\n", 0, 0, 0 );
118
119
120
121
122
123
124
125
#else
		Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
#endif
		rc = approx_candidates( be, f->f_ava, ids, tmp );
		break;

	case LDAP_FILTER_SUBSTRINGS:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
126
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tSUBSTRINGS\n", 0, 0, 0 );
127
128
129
130
131
132
133
134
135
#else
		Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
#endif
		rc = substring_candidates( be, f->f_sub, ids, tmp );
		break;

	case LDAP_FILTER_GE:
		/* no GE index, use pres */
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
136
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tGE\n", 0, 0, 0 );
137
138
139
140
141
142
143
144
145
#else
		Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
#endif
		rc = presence_candidates( be, f->f_ava->aa_desc, ids );
		break;

	case LDAP_FILTER_LE:
		/* no LE index, use pres */
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
146
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tLE\n", 0, 0, 0 );
147
148
149
150
151
152
153
154
155
#else
		Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
#endif
		rc = presence_candidates( be, f->f_ava->aa_desc, ids );
		break;

	case LDAP_FILTER_NOT:
		/* no indexing to support NOT filters */
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tNOT\n",0, 0, 0 );
157
158
159
#else
		Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
#endif
Howard Chu's avatar
Howard Chu committed
160
161
162
                { struct bdb_info *bdb = (struct bdb_info *) be->be_private;
		BDB_IDL_ALL( bdb, ids );
		}
163
164
165
166
		break;

	case LDAP_FILTER_AND:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
167
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tAND\n", 0, 0, 0 );
168
169
170
171
172
173
174
175
176
#else
		Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
#endif
		rc = list_candidates( be, 
			f->f_and, LDAP_FILTER_AND, ids, tmp, stack );
		break;

	case LDAP_FILTER_OR:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
177
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tOR\n", 0, 0, 0 );
178
179
180
181
182
183
184
185
186
#else
		Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
#endif
		rc = list_candidates( be, 
			f->f_or, LDAP_FILTER_OR, ids, tmp, stack );
		break;

	default:
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
187
		LDAP_LOG ( INDEX, ARGS, "=> bdb_filter_candidates: \tUNKNOWN\n", 0, 0, 0 );
188
189
190
191
#else
		Debug( LDAP_DEBUG_FILTER, "\tUNKNOWN %lu\n",
			(unsigned long) f->f_choice, 0, 0 );
#endif
192
193
194
195
		/* Must not return NULL, otherwise extended filters break */
                { struct bdb_info *bdb = (struct bdb_info *) be->be_private;
		BDB_IDL_ALL( bdb, ids );
		}
196
197
198
	}

#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
199
	LDAP_LOG ( INDEX, RESULTS, 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
200
		"<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n", 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
201
		(long)ids[0], (long)BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ));
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#else
	Debug( LDAP_DEBUG_FILTER,
		"<= bdb_filter_candidates: id=%ld first=%ld last=%ld\n",
		(long) ids[0],
		(long) BDB_IDL_FIRST( ids ),
		(long) BDB_IDL_LAST( ids ) );
#endif

	return rc;
}

static int
list_candidates(
	Backend	*be,
	Filter	*flist,
	int		ftype,
	ID *ids,
	ID *tmp,
	ID *save )
{
	struct bdb_info *bdb = (struct bdb_info *) be->be_private;
	int rc = 0;
	Filter	*f;

#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
227
	LDAP_LOG ( INDEX, ARGS, "=> bdb_list_candidates: 0x%x\n", ftype, 0 , 0 );
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#else
	Debug( LDAP_DEBUG_FILTER, "=> bdb_list_candidates 0x%x\n", ftype, 0, 0 );
#endif

	for ( f = flist; f != NULL; f = f->f_next ) {
		rc = bdb_filter_candidates( be, f, save, tmp,
			save+BDB_IDL_UM_SIZE );

		if ( rc != 0 ) {
			if ( ftype == LDAP_FILTER_AND ) {
				rc = 0;
				continue;
			}
			break;
		}
		
		if ( ftype == LDAP_FILTER_AND ) {
Howard Chu's avatar
Howard Chu committed
245
246
247
248
249
			if ( f == flist ) {
				BDB_IDL_CPY( ids, save );
			} else {
				bdb_idl_intersection( ids, save );
			}
250
251
252
			if( BDB_IDL_IS_ZERO( ids ) )
				break;
		} else {
Howard Chu's avatar
Howard Chu committed
253
254
255
256
257
			if ( f == flist ) {
				BDB_IDL_CPY( ids, save );
			} else {
				bdb_idl_union( ids, save );
			}
258
259
260
		}
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
261
	if( rc == LDAP_SUCCESS ) {
262
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
263
264
265
266
		LDAP_LOG ( INDEX, RESULTS, 
			"<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
			(long) ids[0], (long) BDB_IDL_FIRST( ids ), 
			(long) BDB_IDL_LAST( ids ) );
267
268
269
270
271
272
273
274
275
276
#else
		Debug( LDAP_DEBUG_FILTER,
			"<= bdb_list_candidates: id=%ld first=%ld last=%ld\n",
			(long) ids[0],
			(long) BDB_IDL_FIRST(ids),
			(long) BDB_IDL_LAST(ids) );
#endif

	} else {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
277
		LDAP_LOG ( INDEX, ARGS, "<= bdb_list_candidates: rc=%d\n", rc, 0, 0 );
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#else
		Debug( LDAP_DEBUG_FILTER,
			"<= bdb_list_candidates: undefined rc=%d\n",
			rc, 0, 0 );
#endif
	}

	return rc;
}

static int
presence_candidates(
	Backend	*be,
	AttributeDescription *desc,
	ID *ids )
{
	struct bdb_info *bdb = (struct bdb_info *) be->be_private;
	DB *db;
	int rc;
	slap_mask_t mask;
298
	struct berval prefix = {0, NULL};
299
300

#ifdef NEW_LOGGING
301
302
	LDAP_LOG ( INDEX, ENTRY, "=> bdb_presence_candidates (%s)\n", 
			desc->ad_cname.bv_val, 0, 0 );
303
#else
304
305
	Debug( LDAP_DEBUG_TRACE, "=> bdb_presence_candidates (%s)\n",
			desc->ad_cname.bv_val, 0, 0 );
306
307
308
309
310
311
312
313
314
315
316
317
#endif

	if( desc == slap_schema.si_ad_objectClass ) {
		BDB_IDL_ALL( bdb, ids );
		return 0;
	}

	rc = bdb_index_param( be, desc, LDAP_FILTER_PRESENT,
		&db, &mask, &prefix );

	if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
318
		LDAP_LOG ( INDEX, RESULTS, 
319
320
321
			"<= bdb_presence_candidates: (%s) index_param "
			"returned=%d\n",
			desc->ad_cname.bv_val, rc, 0 );
322
323
#else
		Debug( LDAP_DEBUG_TRACE,
324
325
326
			"<= bdb_presence_candidates: (%s) index_param "
			"returned=%d\n",
			desc->ad_cname.bv_val, rc, 0 );
327
328
329
330
331
332
333
#endif
		return 0;
	}

	if( db == NULL ) {
		/* not indexed */
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
334
		LDAP_LOG(INDEX, RESULTS, 
335
336
			"<= bdb_presence_candidates: (%s) not indexed\n",
			desc->ad_cname.bv_val, 0, 0 );
337
338
#else
		Debug( LDAP_DEBUG_TRACE,
339
340
			"<= bdb_presence_candidates: (%s) not indexed\n",
			desc->ad_cname.bv_val, 0, 0 );
341
342
343
344
345
346
#endif
		return 0;
	}

	if( prefix.bv_val == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
347
		LDAP_LOG(INDEX, RESULTS, 
348
349
			"<= bdb_presence_candidates: (%s) no prefix\n",
			desc->ad_cname.bv_val, 0, 0 );
350
351
#else
		Debug( LDAP_DEBUG_TRACE,
352
353
			"<= bdb_presence_candidates: (%s) no prefix\n",
			desc->ad_cname.bv_val, 0, 0 );
354
355
356
357
358
359
360
361
362
363
364
#endif
		return 0;
	}

	rc = bdb_key_read( be, db, NULL, &prefix, ids );

	if( rc == DB_NOTFOUND ) {
		BDB_IDL_ZERO( ids );
		rc = 0;
	} else if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
365
		LDAP_LOG ( INDEX, RESULTS, 
366
367
368
			"<= bdb_presence_candidates: (%s) "
			"key read failed (%d)\n",
			desc->ad_cname.bv_val, rc, 0 );
369
370
#else
		Debug( LDAP_DEBUG_TRACE,
371
372
373
			"<= bdb_presense_candidates: (%s) "
			"key read failed (%d)\n",
			desc->ad_cname.bv_val, rc, 0 );
374
375
376
377
378
#endif
		goto done;
	}

#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
379
380
381
	LDAP_LOG ( INDEX, RESULTS, 
		"<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
		(long)ids[0], (long)BDB_IDL_FIRST( ids ), (long)BDB_IDL_LAST( ids ) );
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#else
	Debug(LDAP_DEBUG_TRACE,
		"<= bdb_presence_candidates: id=%ld first=%ld last=%ld\n",
		(long) ids[0],
		(long) BDB_IDL_FIRST(ids),
		(long) BDB_IDL_LAST(ids) );
#endif

done:
	return rc;
}

static int
equality_candidates(
	Backend	*be,
	AttributeAssertion *ava,
	ID *ids,
	ID *tmp )
{
	DB	*db;
	int i;
	int rc;
	slap_mask_t mask;
405
	struct berval prefix = {0, NULL};
406
407
408
409
	struct berval *keys = NULL;
	MatchingRule *mr;

#ifdef NEW_LOGGING
410
411
	LDAP_LOG ( INDEX, ENTRY, "=> bdb_equality_candidates (%s)\n",
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
412
#else
413
414
	Debug( LDAP_DEBUG_TRACE, "=> bdb_equality_candidates (%s)\n",
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
415
416
417
418
419
420
421
#endif

	rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_EQUALITY,
		&db, &mask, &prefix );

	if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
422
		LDAP_LOG ( INDEX, RESULTS, 
423
424
425
			"<= bdb_equality_candidates: (%s) "
			"index_param failed (%d)\n", 
			ava->aa_desc->ad_cname.bv_val, rc, 0);
426
427
#else
		Debug( LDAP_DEBUG_ANY,
428
429
430
			"<= bdb_equality_candidates: (%s) "
			"index_param failed (%d)\n",
			ava->aa_desc->ad_cname.bv_val, rc, 0 );
431
432
433
434
435
436
#endif
		return rc;
	}

	if ( db == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
437
		LDAP_LOG(INDEX, RESULTS, 
438
439
			"<= bdb_equality_candidates: (%s) not indexed\n", 
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
440
441
#else
		Debug( LDAP_DEBUG_ANY,
442
443
			"<= bdb_equality_candidates: (%s) not indexed\n", 
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#endif
		return -1;
	}

	mr = ava->aa_desc->ad_type->sat_equality;
	if( !mr ) {
		return -1;
	}

	if( !mr->smr_filter ) {
		return -1;
	}

	rc = (mr->smr_filter)(
		LDAP_FILTER_EQUALITY,
		mask,
		ava->aa_desc->ad_type->sat_syntax,
		mr,
		&prefix,
		&ava->aa_value,
		&keys );

	if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
468
		LDAP_LOG ( INDEX, RESULTS, 
469
470
471
			"<= bdb_equality_candidates: (%s, %s) "
			"MR filter failed (%d)\n",
			prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
472
473
#else
		Debug( LDAP_DEBUG_TRACE,
474
475
476
			"<= bdb_equality_candidates: (%s, %s) "
			"MR filter failed (%d)\n",
			prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
477
478
479
480
481
482
#endif
		return rc;
	}

	if( keys == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
483
		LDAP_LOG ( INDEX, RESULTS, 
484
485
			"<= bdb_equality_candidates: (%s) no keys\n", 
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
486
487
#else
		Debug( LDAP_DEBUG_TRACE,
488
489
			"<= bdb_equality_candidates: (%s) no keys\n",
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
490
491
492
493
494
495
496
497
498
499
#endif
		return 0;
	}

	for ( i= 0; keys[i].bv_val != NULL; i++ ) {
		rc = bdb_key_read( be, db, NULL, &keys[i], tmp );

		if( rc == DB_NOTFOUND ) {
			BDB_IDL_ZERO( ids );
			rc = 0;
500
			break;
501
502
		} else if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
503
			LDAP_LOG ( INDEX, RESULTS, 
504
505
506
				"<= bdb_equality_candidates: (%s) "
				"key read failed (%d)\n",
				ava->aa_desc->ad_cname.bv_val, rc, 0 );
507
508
#else
			Debug( LDAP_DEBUG_TRACE,
509
510
511
				"<= bdb_equality_candidates: (%s) "
				"key read failed (%d)\n",
				ava->aa_desc->ad_cname.bv_val, rc, 0 );
512
513
514
515
516
517
#endif
			break;
		}

		if( BDB_IDL_IS_ZERO( tmp ) ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
518
			LDAP_LOG ( INDEX, RESULTS,
519
520
				"<= bdb_equality_candidates: (%s) NULL\n",
				ava->aa_desc->ad_cname.bv_val, 0, 0);
521
522
#else
			Debug( LDAP_DEBUG_TRACE,
523
524
				"<= bdb_equality_candidates: (%s) NULL\n", 
				ava->aa_desc->ad_cname.bv_val, 0, 0 );
525
526
527
528
529
#endif
			BDB_IDL_ZERO( ids );
			break;
		}

Howard Chu's avatar
Howard Chu committed
530
531
532
533
534
		if ( i == 0 ) {
			BDB_IDL_CPY( ids, tmp );
		} else {
			bdb_idl_intersection( ids, tmp );
		}
535
536
537
538
539
540
541
542

		if( BDB_IDL_IS_ZERO( ids ) )
			break;
	}

	ber_bvarray_free( keys );

#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
543
544
545
546
	LDAP_LOG ( INDEX, RESULTS, 
		"<= bdb_equality_candidates: id=%ld first=%ld last=%ld\n", 
		(long) ids[0], (long) BDB_IDL_FIRST( ids ), 
		(long) BDB_IDL_LAST( ids ) );
547
548
#else
	Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
549
		"<= bdb_equality_candidates: id=%ld, first=%ld, last=%ld\n",
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
		(long) ids[0],
		(long) BDB_IDL_FIRST(ids),
		(long) BDB_IDL_LAST(ids) );
#endif
	return( rc );
}


static int
approx_candidates(
	Backend	*be,
	AttributeAssertion *ava,
	ID *ids,
	ID *tmp )
{
	DB	*db;
	int i;
	int rc;
	slap_mask_t mask;
569
	struct berval prefix = {0, NULL};
570
571
572
573
	struct berval *keys = NULL;
	MatchingRule *mr;

#ifdef NEW_LOGGING
574
575
	LDAP_LOG ( INDEX, ENTRY, "=> bdb_approx_candidates (%s)\n",
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
576
#else
577
578
	Debug( LDAP_DEBUG_TRACE, "=> bdb_approx_candidates (%s)\n",
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
579
580
581
582
583
584
585
#endif

	rc = bdb_index_param( be, ava->aa_desc, LDAP_FILTER_APPROX,
		&db, &mask, &prefix );

	if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
586
		LDAP_LOG ( INDEX, RESULTS, 
587
588
589
			"<= bdb_approx_candidates: (%s) "
			"index_param failed (%d)\n",
			ava->aa_desc->ad_cname.bv_val, rc, 0 );
590
591
#else
		Debug( LDAP_DEBUG_ANY,
592
593
594
			"<= bdb_approx_candidates: (%s) "
			"index_param failed (%d)\n",
			ava->aa_desc->ad_cname.bv_val, rc, 0 );
595
596
597
598
599
600
#endif
		return rc;
	}

	if ( db == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
601
		LDAP_LOG(INDEX, RESULTS, 
602
603
			"<= bdb_approx_candidates: (%s) not indexed\n",
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
604
605
#else
		Debug( LDAP_DEBUG_ANY,
606
607
			"<= bdb_approx_candidates: (%s) not indexed\n",
			ava->aa_desc->ad_cname.bv_val, 0, 0 );
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#endif
		return -1;
	}

	mr = ava->aa_desc->ad_type->sat_approx;
	if( !mr ) {
		/* no approx matching rule, try equality matching rule */
		mr = ava->aa_desc->ad_type->sat_equality;
	}

	if( !mr ) {
		return -1;
	}

	if( !mr->smr_filter ) {
		return -1;
	}

	rc = (mr->smr_filter)(
		LDAP_FILTER_APPROX,
		mask,
		ava->aa_desc->ad_type->sat_syntax,
		mr,
		&prefix,
		&ava->aa_value,
		&keys );

	if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
637
		LDAP_LOG ( INDEX, RESULTS, 
638
639
640
			"<= bdb_approx_candidates: (%s, %s) "
			"MR filter failed (%d)\n",
			prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
641
642
#else
		Debug( LDAP_DEBUG_TRACE,
643
644
645
			"<= bdb_approx_candidates: (%s, %s) "
			"MR filter failed (%d)\n",
			prefix.bv_val, ava->aa_desc->ad_cname.bv_val, rc );
646
647
648
649
650
651
#endif
		return rc;
	}

	if( keys == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
652
		LDAP_LOG ( INDEX, RESULTS, 
653
654
			"<= bdb_approx_candidates: (%s) no keys (%s)\n",
			prefix.bv_val, ava->aa_desc->ad_cname.bv_val, 0 );
655
656
#else
		Debug( LDAP_DEBUG_TRACE,
657
658
			"<= bdb_approx_candidates: (%s) no keys (%s)\n",
			prefix.bv_val, ava->aa_desc->ad_cname.bv_val, 0 );
659
660
661
662
663
664
665
666
667
668
669
670
671
#endif
		return 0;
	}

	for ( i= 0; keys[i].bv_val != NULL; i++ ) {
		rc = bdb_key_read( be, db, NULL, &keys[i], tmp );

		if( rc == DB_NOTFOUND ) {
			BDB_IDL_ZERO( ids );
			rc = 0;
			break;
		} else if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
672
673
674
675
			LDAP_LOG ( INDEX, RESULTS, 
				"<= bdb_approx_candidates: (%s) "
				"key read failed (%d)\n",
				ava->aa_desc->ad_cname.bv_val, rc, 0);
676
#else
677
678
679
680
			Debug( LDAP_DEBUG_TRACE,
				"<= bdb_approx_candidates: (%s) "
				"key read failed (%d)\n",
				ava->aa_desc->ad_cname.bv_val, rc, 0 );
681
682
683
684
685
686
#endif
			break;
		}

		if( BDB_IDL_IS_ZERO( tmp ) ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
687
			LDAP_LOG ( INDEX, RESULTS, 
688
689
				"<= bdb_approx_candidates: (%s) NULL\n",
				ava->aa_desc->ad_cname.bv_val, 0, 0 );
690
#else
691
692
693
			Debug( LDAP_DEBUG_TRACE,
				"<= bdb_approx_candidates: (%s) NULL\n",
				ava->aa_desc->ad_cname.bv_val, 0, 0 );
694
695
696
697
698
#endif
			BDB_IDL_ZERO( ids );
			break;
		}

Howard Chu's avatar
Howard Chu committed
699
700
701
702
703
		if ( i == 0 ) {
			BDB_IDL_CPY( ids, tmp );
		} else {
			bdb_idl_intersection( ids, tmp );
		}
704
705
706
707
708
709
710
711

		if( BDB_IDL_IS_ZERO( ids ) )
			break;
	}

	ber_bvarray_free( keys );

#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
712
713
714
715
	LDAP_LOG ( INDEX, RESULTS, 
		"<= bdb_approx_candidates: id=%ld first=%ld last=%ld\n", 
		(long) ids[0], (long) BDB_IDL_FIRST( ids ), 
		(long) BDB_IDL_LAST( ids ) );
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#else
	Debug( LDAP_DEBUG_TRACE, "<= bdb_approx_candidates %ld, first=%ld, last=%ld\n",
		(long) ids[0],
		(long) BDB_IDL_FIRST(ids),
		(long) BDB_IDL_LAST(ids) );
#endif
	return( rc );
}

static int
substring_candidates(
	Backend	*be,
	SubstringsAssertion	*sub,
	ID *ids,
	ID *tmp )
{
	DB	*db;
	int i;
	int rc;
	slap_mask_t mask;
736
	struct berval prefix = {0, NULL};
737
738
739
740
	struct berval *keys = NULL;
	MatchingRule *mr;

#ifdef NEW_LOGGING
741
742
	LDAP_LOG ( INDEX, ENTRY, "=> bdb_substring_candidates (%s)\n",
			sub->sa_desc->ad_cname.bv_val, 0, 0 );
743
#else
744
745
	Debug( LDAP_DEBUG_TRACE, "=> bdb_substring_candidates (%s)\n",
			sub->sa_desc->ad_cname.bv_val, 0, 0 );
746
747
748
749
750
751
752
#endif

	rc = bdb_index_param( be, sub->sa_desc, LDAP_FILTER_SUBSTRINGS,
		&db, &mask, &prefix );

	if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
753
		LDAP_LOG ( INDEX, RESULTS, 
754
755
756
			"<= bdb_substring_candidates: (%s) "
			"index_param failed (%d)\n",
			sub->sa_desc->ad_cname.bv_val, rc, 0);
757
758
#else
		Debug( LDAP_DEBUG_ANY,
759
760
761
			"<= bdb_substring_candidates: (%s) "
			"index_param failed (%d)\n",
			sub->sa_desc->ad_cname.bv_val, rc, 0 );
762
763
764
765
766
767
#endif
		return rc;
	}

	if ( db == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
768
		LDAP_LOG ( INDEX, RESULTS, 
769
770
			"<= bdb_substring_candidates: (%s) not indexed\n",
			sub->sa_desc->ad_cname.bv_val, 0, 0 );
771
772
#else
		Debug( LDAP_DEBUG_ANY,
773
774
			"<= bdb_substring_candidates: (%s) not indexed\n",
			sub->sa_desc->ad_cname.bv_val, 0, 0 );
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#endif
		return -1;
	}

	mr = sub->sa_desc->ad_type->sat_substr;

	if( !mr ) {
		return -1;
	}

	if( !mr->smr_filter ) {
		return -1;
	}

	rc = (mr->smr_filter)(
		LDAP_FILTER_SUBSTRINGS,
		mask,
		sub->sa_desc->ad_type->sat_syntax,
		mr,
		&prefix,
		sub,
		&keys );

	if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
800
		LDAP_LOG ( INDEX, RESULTS, 
801
802
			"<= bdb_substring_candidates: (%s) "
			"MR filter failed (%d)\n", 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
803
			sub->sa_desc->ad_cname.bv_val, rc, 0 );
804
805
#else
		Debug( LDAP_DEBUG_TRACE,
806
807
			"<= bdb_substring_candidates: (%s) "
			"MR filter failed (%d)\n",
808
809
810
811
812
813
814
			sub->sa_desc->ad_cname.bv_val, rc, 0 );
#endif
		return rc;
	}

	if( keys == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
815
		LDAP_LOG ( INDEX, RESULTS, 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
816
			"<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
817
			mask, sub->sa_desc->ad_cname.bv_val, 0 );
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
#else
		Debug( LDAP_DEBUG_TRACE,
			"<= bdb_substring_candidates: (0x%04lx) no keys (%s)\n",
			mask, sub->sa_desc->ad_cname.bv_val, 0 );
#endif
		return 0;
	}

	for ( i= 0; keys[i].bv_val != NULL; i++ ) {
		rc = bdb_key_read( be, db, NULL, &keys[i], tmp );

		if( rc == DB_NOTFOUND ) {
			BDB_IDL_ZERO( ids );
			rc = 0;
			break;
		} else if( rc != LDAP_SUCCESS ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
835
			LDAP_LOG ( INDEX, RESULTS, 
836
837
838
				"<= bdb_substring_candidates: (%s) "
				"key read failed (%d)\n",
				sub->sa_desc->ad_cname.bv_val, rc, 0 );
839
#else
840
841
842
843
			Debug( LDAP_DEBUG_TRACE,
				"<= bdb_substring_candidates: (%s) "
				"key read failed (%d)\n",
				sub->sa_desc->ad_cname.bv_val, rc, 0 );
844
845
846
847
848
849
#endif
			break;
		}

		if( BDB_IDL_IS_ZERO( tmp ) ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
850
			LDAP_LOG ( INDEX, RESULTS, 
851
852
				"<= bdb_substring_candidates: (%s) NULL\n",
				sub->sa_desc->ad_cname.bv_val, 0, 0 );
853
#else
854
855
856
			Debug( LDAP_DEBUG_TRACE,
				"<= bdb_substring_candidates: (%s) NULL\n",
				sub->sa_desc->ad_cname.bv_val, 0, 0 );
857
858
859
860
861
#endif
			BDB_IDL_ZERO( ids );
			break;
		}

Howard Chu's avatar
Howard Chu committed
862
863
864
865
866
		if ( i == 0 ) {
			BDB_IDL_CPY( ids, tmp );
		} else {
			bdb_idl_intersection( ids, tmp );
		}
867
868
869
870
871
872
873
874

		if( BDB_IDL_IS_ZERO( ids ) )
			break;
	}

	ber_bvarray_free( keys );

#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
875
876
877
878
	LDAP_LOG ( INDEX, RESULTS, 
		"<= bdb_substring_candidates: id=%ld first=%ld last=%ld\n",
		(long) ids[0], (long) BDB_IDL_FIRST( ids ), 
		(long) BDB_IDL_LAST( ids ) );
879
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
880
	Debug( LDAP_DEBUG_TRACE, "<= bdb_substring_candidates: %ld, first=%ld, last=%ld\n",
881
882
883
884
885
886
887
		(long) ids[0],
		(long) BDB_IDL_FIRST(ids),
		(long) BDB_IDL_LAST(ids) );
#endif
	return( 0 );
}