index.c 8.54 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
/* index.c - routines for dealing with attribute indexes */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
11
12
#include "slap.h"
#include "back-ldbm.h"

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

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
38
	 * with index_change_values() call
Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
40
	 */

41
	bv.bv_val = ch_strdup( e->e_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
42
43
44
45
46
	bv.bv_len = strlen( bv.bv_val );
	bvals[0] = &bv;
	bvals[1] = NULL;

	/* add the dn to the indexes */
47
48
	{
		char *dn = ch_strdup("dn");
49
		index_change_values( be, dn, bvals, e->e_id, __INDEX_ADD_OP );
50
51
		free( dn );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
52
53
54
55
56

	free( bv.bv_val );

	/* add each attribute to the indexes */
	for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
57

58
59
		index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
				     __INDEX_ADD_OP );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
60
61
62
	}

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

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
76
77
78
79
	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
80
		case LDAP_MOD_REPLACE:
81
82
83
84
			/* XXX: Delete old index data==>problem when this 
			 * gets called we lost values already!
			 */
		case LDAP_MOD_ADD:
85
86
87
88
89
			rc = index_change_values( be,
					       mod->mod_type,
					       mod->mod_bvalues,
					       id,
					       __INDEX_ADD_OP);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
90
91
			break;
		case LDAP_MOD_DELETE:
92
93
94
95
96
			rc =  index_change_values( be,
						   mod->mod_type,
						   mod->mod_bvalues,
						   id,
						   __INDEX_DELETE_OP );
97
98
			break;
 		case LDAP_MOD_SOFTADD:	/* SOFTADD means index was there */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
100
101
102
103
104
105
106
107
108
109
110
			rc = 0;
			break;
		}

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

	return( 0 );
}

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

127
	char		*at_cn;
128

129
	ldbm_datum_init( key );
130

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

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

	attr_normalize( type );
145
	at_cn = at_canonical_name( type );
146

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

	realval = val;
	tmpval = NULL;
157
	if ( prefix != UNKNOWN_PREFIX ) {
Hallvard Furuseth's avatar
Hallvard Furuseth committed
158
              unsigned int	len = strlen( val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
159

160
              if ( (len + 2) < sizeof(buf) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
161
162
163
164
165
166
			realval = buf;
		} else {
			/* value + prefix + null */
			tmpval = (char *) ch_malloc( len + 2 );
			realval = tmpval;
		}
167
168
              realval[0] = prefix;
              strcpy( &realval[1], val );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
169
170
171
172
173
174
	}

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

	idl = idl_fetch( be, db, key );
175
	if ( tmpval != NULL ) {
176
              free( tmpval );
177
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
178
179
180

	ldbm_cache_close( be, db );

181
	Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
182
	       idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
183
184
185
	return( idl );
}

186
/* Add or remove stuff from index files */
187
188

static int
189
change_value(
190
191
192
193
194
    Backend		*be,
    struct dbcache	*db,
    char		*type,
    int			indextype,
    char		*val,
195
196
    ID			id,
    int			(*idl_func)(Backend *, struct dbcache *, Datum, ID)
197
198
)
{
199
200
201
202
203
204
205
206
207
208
209
	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,
210
211
	       "=> change_value( \"%c%s\", op=%s )\n",
	       prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

	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;

230
	rc = idl_func( be, db, key, id );
231
232
233
234
235
236
237

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

	ldap_pvt_thread_yield();

238
	Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
239
240

	return( rc );
241

242
}/* static int change_value() */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
243

244
245

int
246
index_change_values(
247
248
249
    Backend		*be,
    char		*type,
    struct berval	**vals,
250
251
    ID			id,
    unsigned int	op
252
253
)
{
254
255
	char		*val, *p, *code, *w;
	unsigned	i, j, len;
256
	int		indexmask, syntax;
257
258
259
	char		buf[SUBLEN + 1];
	char		vbuf[BUFSIZ];
	char		*bigbuf;
260
	struct dbcache	*db;
261

262
263
264
265
266
	int		(*idl_funct)(Backend *,
				    struct dbcache *,
				    Datum, ID);
	char		*at_cn;	/* Attribute canonical name */
	int		mode;
267
268

	Debug( LDAP_DEBUG_TRACE,
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
	       "=> index_change_values( \"%s\", %ld, op=%s )\n", 
	       type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );

	
	if (op == __INDEX_ADD_OP) {

	    /* Add values */

	    idl_funct =  idl_insert_key;
	    mode = LDBM_WRCREAT;

	} else {

	    /* Delete values */

	    idl_funct = idl_delete_key;
	    mode = LDBM_WRITER;

	}

289
290
	attr_normalize(type);
	attr_masks( be->be_private, type, &indexmask, &syntax );
291

292
293
294
295
296
297
	if ( indexmask == 0 ) {
		return( 0 );
	}

	at_cn = at_canonical_name( type );

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


309
	for ( i = 0; vals[i] != NULL; i++ ) {
310
		/*
311
		 * presence index entry
312
313
		 */
		if ( indexmask & INDEX_PRESENCE ) {
314
315
316
317

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

318
319
320
		}

		Debug( LDAP_DEBUG_TRACE,
321
		       "index_change_values syntax 0x%x syntax bin 0x%x\n",
322
323
324
		       syntax, SYNTAX_BIN, 0 );

		if ( syntax & SYNTAX_BIN ) {
325

326
327
			ldbm_cache_close( be, db );
			return( 0 );
328

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
		}

		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 ) {
		    
354
355
			change_value( be, db, at_cn, INDEX_EQUALITY,
				      val, id, idl_funct);
356
357
358
359
360
361
362

		}

		/*
		 * approximate index entry
		 */
		if ( indexmask & INDEX_APPROX ) {
363
364
			for ( w = first_word( val ); w != NULL;
			    w = next_word( w ) ) {
365
				if ( (code = phonetic( w )) != NULL ) {
366
					change_value( be,
367
368
369
370
						      db,
						      at_cn,
						      INDEX_APPROX,
						      code,
371
372
						      id,
						      idl_funct );
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
					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';

390
391
				change_value( be, db, at_cn, INDEX_SUB,
					      buf, id, idl_funct );
392
393
394
395
396
397
398
399

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

400
401
				change_value( be, db, at_cn, INDEX_SUB,
					      buf, id, idl_funct );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
402
403
404
405
406
407
408
409
410
			}

			/* any */
			for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
				for ( j = 0; j < SUBLEN; j++ ) {
					buf[j] = p[j];
				}
				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
422
			}
		}

		if ( bigbuf != NULL ) {
			free( bigbuf );
		}
	}
	ldbm_cache_close( be, db );

	return( 0 );
423
424

}/* int index_change_values() */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

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:
442
		prefix = UNKNOWN_PREFIX;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
443
444
445
446
447
		break;
	}

	return( prefix );
}