idl.c 23.6 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* idl.c - ldap id list handling routines */
2
/* $OpenLDAP$ */
3
/*
4
 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5
6
7
8
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

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

#include <stdio.h>
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
static ID_BLOCK* idl_dup( ID_BLOCK *idl );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
19

Kurt Zeilenga's avatar
Kurt Zeilenga committed
20
static void cont_alloc( Datum *cont, Datum *key )
21
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
22
23
24
	ldbm_datum_init( *cont );
	cont->dsize = 1 + sizeof(ID) + key->dsize;
	cont->dptr = ch_malloc( cont->dsize );
25

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
27
	* (unsigned char *) cont->dptr = SLAP_INDEX_CONT_PREFIX;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
28
	AC_MEMCPY( &((unsigned char *)cont->dptr)[1 + sizeof(ID)],
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
		key->dptr, key->dsize );
30
31
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
32
static void cont_id( Datum *cont, ID id )
33
34
35
36
{
	int i;

	for( i=1; i <= sizeof(id); i++) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
37
		((unsigned char *)cont->dptr)[i] = (unsigned char)(id & 0xFF);
38
39
40
41
42
		id >>= 8;
	}

}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
43
static void cont_free( Datum *cont )
44
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
45
	ch_free( cont->dptr );
46
47
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
48
49
50
#ifdef LDBM_DEBUG_IDL
static void idl_check(ID_BLOCK *idl)
{
51
	int i, max;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
52
53
	ID_BLOCK last;

54
	if( ID_BLOCK_ALLIDS(idl) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
55
56
57
	{
		return;
	}
58
59
60
61
62
63
64
65
66
67
	if( ID_BLOCK_INDIRECT(idl) )
	{
		for ( max = 0; ID_BLOCK_ID(idl, max) != NOID; max++ ) ;
	} else
	{
		max = ID_BLOCK_NIDS(idl);
	}

	if (max <= 1)
		return;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
68
69

	for( last = ID_BLOCK_ID(idl, 0), i = 1;
70
		i < max;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
71
72
73
74
75
76
77
		last = ID_BLOCK_ID(idl, i), i++ )
	{
		assert (last < ID_BLOCK_ID(idl, i) );
	}
}
#endif

78
79
80
/* Allocate an ID_BLOCK with room for nids ids */
ID_BLOCK *
idl_alloc( unsigned int nids )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
81
{
82
	ID_BLOCK	*new;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
83
84

	/* nmax + nids + space for the ids */
85
86
87
	new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
	ID_BLOCK_NMAX(new) = nids;
	ID_BLOCK_NIDS(new) = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
88
89
90
91

	return( new );
}

92
93
94

/* Allocate an empty ALLIDS ID_BLOCK */
ID_BLOCK	*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
95
96
idl_allids( Backend *be )
{
97
	ID_BLOCK	*idl;
98
	ID		id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
100

	idl = idl_alloc( 0 );
101
	ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
102
103
104
105
	if ( next_id_get( be, &id ) ) {
		return NULL;
	}
	ID_BLOCK_NIDS(idl) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
106
107
108
109

	return( idl );
}

110
/* Free an ID_BLOCK */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
111
void
112
idl_free( ID_BLOCK *idl )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
113
114
{
	if ( idl == NULL ) {
115
116
117
		Debug( LDAP_DEBUG_TRACE,
			"idl_free: called with NULL pointer\n",
			0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
118
119
120
121
122
123
		return;
	}

	free( (char *) idl );
}

124
125
126

/* Fetch an single ID_BLOCK from the cache */
static ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
127
128
idl_fetch_one(
    Backend		*be,
129
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
130
131
132
    Datum		key
)
{
133
134
	Datum	data;
	ID_BLOCK	*idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
135
136
137
138
139

	/* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */

	data = ldbm_cache_fetch( db, key );

140
141
142
143
	if( data.dptr == NULL ) {
		return NULL;
	}

144
145
	idl = idl_dup((ID_BLOCK *) data.dptr);

146
	ldbm_datum_free( db->dbc_db, data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
147

148
	return idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
149
150
}

151
152
153
154
155
156
157
158
159
160
161

/* Fetch a set of ID_BLOCKs from the cache
 *	if not INDIRECT
 * 		if block return is an ALLIDS block,
 *			return an new ALLIDS block
 * 		otherwise
 *			return block
 *	construct super block from all blocks referenced by INDIRECT block
 *	return super block
 */
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
162
163
idl_fetch(
    Backend		*be,
164
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
165
166
167
    Datum		key
)
{
168
169
170
	Datum	data;
	ID_BLOCK	*idl;
	ID_BLOCK	**tmp;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
171
172
	int	i, nids;

173
	idl = idl_fetch_one( be, db, key );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
174

175
176
	if ( idl == NULL ) {
		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
177
178
	}

179
180
	if ( ID_BLOCK_ALLIDS(idl) ) {
		/* all ids block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
181
		/* make sure we have the current value of highest id */
182
183
184
185
186
187
188
189
		idl_free( idl );
		idl = idl_allids( be );

		return( idl );
	}

	if ( ! ID_BLOCK_INDIRECT( idl ) ) {
		/* regular block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
190
191
192
193
194
195
196
197
198
199
		return( idl );
	}

	/*
	 * this is an indirect block which points to other blocks.
	 * we need to read in all the blocks it points to and construct
	 * a big id list containing all the ids, which we will return.
	 */

	/* count the number of blocks & allocate space for pointers to them */
200
	for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
201
		;	/* NULL */
202
	tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
203
204

	/* read in all the blocks */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
205
	cont_alloc( &data, &key );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
206
	nids = 0;
207
	for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
208
		cont_id( &data, ID_BLOCK_ID(idl, i) );
209
210

		if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
211
			Debug( LDAP_DEBUG_ANY,
212
			    "idl_fetch: one returned NULL\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
213
214
215
			continue;
		}

216
		nids += ID_BLOCK_NIDS(tmp[i]);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
217
218
	}
	tmp[i] = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
219
	cont_free( &data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
220
221
222
223
	idl_free( idl );

	/* allocate space for the big block */
	idl = idl_alloc( nids );
224
	ID_BLOCK_NIDS(idl) = nids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
225
226
227
228
229
230
231
232
	nids = 0;

	/* copy in all the ids from the component blocks */
	for ( i = 0; tmp[i] != NULL; i++ ) {
		if ( tmp[i] == NULL ) {
			continue;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
233
		AC_MEMCPY(
234
235
236
237
			(char *) &ID_BLOCK_ID(idl, nids),
			(char *) &ID_BLOCK_ID(tmp[i], 0),
			ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
		nids += ID_BLOCK_NIDS(tmp[i]);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
239
240
241
242

		idl_free( tmp[i] );
	}
	free( (char *) tmp );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
243
244
245
246
#ifdef LDBM_DEBUG_IDL
	idl_check(idl);
#endif

247
248
	Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
	       ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
249
250
251
	return( idl );
}

252
253

/* store a single block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
254
255
256
static int
idl_store(
    Backend		*be,
257
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
258
    Datum		key, 
259
    ID_BLOCK		*idl
Kurt Zeilenga's avatar
Kurt Zeilenga committed
260
261
)
{
262
	int	rc, flags;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
263
	Datum	data;
264
265
	struct ldbminfo *li = (struct ldbminfo *) be->be_private;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
266
267
268
269
#ifdef LDBM_DEBUG_IDL
	idl_check(idl);
#endif

270
	ldbm_datum_init( data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
271
272
273
274

	/* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */

	data.dptr = (char *) idl;
275
276
277
278
279
280
	data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
	
#ifdef LDBM_DEBUG
	Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
		rc, 0, 0, 0, 0 );
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
281

282
283
	flags = LDBM_REPLACE;
	rc = ldbm_cache_store( db, key, data, flags );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
284
285
286
287
288

	/* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
	return( rc );
}

289
290
291
/* split the block at id 
 *	locate ID greater than or equal to id.
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
292
293
static void
idl_split_block(
294
    ID_BLOCK	*b,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
295
    ID		id,
296
297
    ID_BLOCK	**right,
    ID_BLOCK	**left
Kurt Zeilenga's avatar
Kurt Zeilenga committed
298
299
)
{
300
	unsigned int	nr, nl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
301

302
303
	/* find where to split the block *//* XXX linear search XXX */
	for ( nr = 0; nr < ID_BLOCK_NIDS(b) && id > ID_BLOCK_ID(b, nr); nr++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
304
305
		;	/* NULL */

306
307
308
309
	nl = ID_BLOCK_NIDS(b) - nr;

	*right = idl_alloc( nr == 0 ? 1 : nr );
	*left = idl_alloc( nl + (nr == 0 ? 0 : 1));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
310
311
312
313
314
315

	/*
	 * everything before the id being inserted in the first block
	 * unless there is nothing, in which case the id being inserted
	 * goes there.
	 */
316
317
318
	if ( nr == 0 ) {
		ID_BLOCK_NIDS(*right) = 1;
		ID_BLOCK_ID(*right, 0) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
319
	} else {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320
		AC_MEMCPY(
321
322
323
324
325
			(char *) &ID_BLOCK_ID(*right, 0),
			(char *) &ID_BLOCK_ID(b, 0),
			nr * sizeof(ID) );
		ID_BLOCK_NIDS(*right) = nr;
		ID_BLOCK_ID(*left, 0) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
326
327
328
	}

	/* the id being inserted & everything after in the second block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
329
	AC_MEMCPY(
330
331
332
333
		(char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
	    (char *) &ID_BLOCK_ID(b, nr),
		nl * sizeof(ID) );
	ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
334
335
336
337
338

#ifdef LDBM_DEBUG_IDL
	idl_check(*right);
	idl_check(*left);
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
339
340
}

341

Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
343
344
345
346
347
348
349
/*
 * idl_change_first - called when an indirect block's first key has
 * changed, meaning it needs to be stored under a new key, and the
 * header block pointing to it needs updating.
 */
static int
idl_change_first(
    Backend		*be,
350
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
351
    Datum		hkey,		/* header block key	*/
352
    ID_BLOCK		*h,		/* header block 	*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
353
354
    int			pos,		/* pos in h to update	*/
    Datum		bkey,		/* data block key	*/
355
    ID_BLOCK		*b		/* data block 		*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
356
357
358
359
360
361
362
363
364
)
{
	int	rc;

	/* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */

	/* delete old key block */
	if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
		Debug( LDAP_DEBUG_ANY,
365
366
		    "idl_change_first: ldbm_cache_delete returned %d\n",
			rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
367
368
369
370
		return( rc );
	}

	/* write block with new key */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
371
	cont_id( &bkey, ID_BLOCK_ID(b, 0) );
372

Kurt Zeilenga's avatar
Kurt Zeilenga committed
373
374
	if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
		Debug( LDAP_DEBUG_ANY,
375
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
376
377
378
379
		return( rc );
	}

	/* update + write indirect header block */
380
	ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
381
382
	if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
		Debug( LDAP_DEBUG_ANY,
383
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
384
385
386
387
388
389
		return( rc );
	}

	return( 0 );
}

390

Kurt Zeilenga's avatar
Kurt Zeilenga committed
391
392
393
int
idl_insert_key(
    Backend		*be,
394
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
395
396
397
398
399
    Datum		key,
    ID			id
)
{
	int	i, j, first, rc;
400
	ID_BLOCK	*idl, *tmp, *tmp2, *tmp3;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
401
402
403
404
	Datum	k2;

	if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
		idl = idl_alloc( 1 );
405
		ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
406
407
408
409
410
411
		rc = idl_store( be, db, key, idl );

		idl_free( idl );
		return( rc );
	}

412
413
414
415
416
417
418
419
	if ( ID_BLOCK_ALLIDS( idl ) ) {
		/* ALLIDS */
		idl_free( idl );
		return 0;
	}

	if ( ! ID_BLOCK_INDIRECT( idl ) ) {
		/* regular block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
		switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
		case 0:		/* id inserted - store the updated block */
		case 1:
			rc = idl_store( be, db, key, idl );
			break;

		case 2:		/* id already there - nothing to do */
			rc = 0;
			break;

		case 3:		/* id not inserted - block must be split */
			/* check threshold for marking this an all-id block */
			if ( db->dbc_maxindirect < 2 ) {
				idl_free( idl );
				idl = idl_allids( be );
				rc = idl_store( be, db, key, idl );
436
				break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
437
438
439
440
441
442
443
			}

			idl_split_block( idl, id, &tmp, &tmp2 );
			idl_free( idl );

			/* create the header indirect block */
			idl = idl_alloc( 3 );
444
445
446
447
448
			ID_BLOCK_NMAX(idl) = 3;
			ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
			ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
			ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
			ID_BLOCK_ID(idl, 2) = NOID;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
449
450
451
452

			/* store it */
			rc = idl_store( be, db, key, idl );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
453
454
			cont_alloc( &k2, &key );
			cont_id( &k2, ID_BLOCK_ID(tmp, 0) );
455

Kurt Zeilenga's avatar
Kurt Zeilenga committed
456
457
			rc = idl_store( be, db, k2, tmp );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
458
			cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
459
460
			rc = idl_store( be, db, k2, tmp2 );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
461
			cont_free( &k2 );
462

Kurt Zeilenga's avatar
Kurt Zeilenga committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
			idl_free( tmp );
			idl_free( tmp2 );
			break;
		}

		idl_free( idl );
		return( rc );
	}

	/*
	 * this is an indirect block which points to other blocks.
	 * we need to read in the block into which the id should be
	 * inserted, then insert the id and store the block.  we might
	 * have to split the block if it is full, which means we also
	 * need to write a new "header" block.
	 */

480
481
	/* select the block to try inserting into *//* XXX linear search XXX */
	for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
482
		;	/* NULL */
483

Kurt Zeilenga's avatar
Kurt Zeilenga committed
484
485
486
487
488
489
490
491
	if ( i != 0 ) {
		i--;
		first = 0;
	} else {
		first = 1;
	}

	/* get the block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
492
493
	cont_alloc( &k2, &key );
	cont_id( &k2, ID_BLOCK_ID(idl, i) );
494

Kurt Zeilenga's avatar
Kurt Zeilenga committed
495
	if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
496
497
		Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
		    0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
498
		cont_free( &k2 );
499
		idl_free( idl );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
500
501
502
503
504
505
506
507
		return( -1 );
	}

	/* insert the id */
	switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
	case 0:		/* id inserted ok */
		if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
			Debug( LDAP_DEBUG_ANY,
508
			    "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
		}
		break;

	case 1:		/* id inserted - first id in block has changed */
		/*
		 * key for this block has changed, so we have to
		 * write the block under the new key, delete the
		 * old key block + update and write the indirect
		 * header block.
		 */

		rc = idl_change_first( be, db, key, idl, i, k2, tmp );
		break;

523
524
	case 2:		/* id not inserted - already there, do nothing */
		rc = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
525
526
527
528
529
530
531
532
533
534
		break;

	case 3:		/* id not inserted - block is full */
		/*
		 * first, see if it will fit in the next block,
		 * without splitting, unless we're trying to insert
		 * into the beginning of the first block.
		 */

		/* is there a next block? */
535
		if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
Howard Chu's avatar
Howard Chu committed
536
			Datum k3;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
537
			/* read it in */
Howard Chu's avatar
Howard Chu committed
538
539
540
			cont_alloc( &k3, &key );
			cont_id( &k3, ID_BLOCK_ID(idl, i + 1) );
			if ( (tmp2 = idl_fetch_one( be, db, k3 )) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
541
				Debug( LDAP_DEBUG_ANY,
542
543
				    "idl_insert_key: idl_fetch_one returned NULL\n",
				    0, 0, 0 );
544
				/* split the original block */
Howard Chu's avatar
Howard Chu committed
545
				cont_free( &k3 );
546
				goto split;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
547
548
			}

549
550
551
552
553
554
555
556
557
558
559
560
561
562
			/* If the new id is less than the last id in the
			 * current block, it must not be put into the next
			 * block. Push the last id of the current block
			 * into the next block instead.
			 */
			if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
			    ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);

			    --ID_BLOCK_NIDS(tmp);
			    /* This must succeed since we just popped one
			     * ID off the end of it.
			     */
			    rc = idl_insert( &tmp, id, db->dbc_maxids );

563
			    if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
564
565
566
567
568
569
570
571
572
573
574
				Debug( LDAP_DEBUG_ANY,
			    "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
			    }

			    id = id2;
			    /* This new id will necessarily be inserted
			     * as the first id of the next block by the
			     * following switch() statement.
			     */
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
575
576
577
578
			switch ( (rc = idl_insert( &tmp2, id,
			    db->dbc_maxids )) ) {
			case 1:		/* id inserted first in block */
				rc = idl_change_first( be, db, key, idl,
Howard Chu's avatar
Howard Chu committed
579
				    i + 1, k3, tmp2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
580
581
582
				/* FALL */

			case 2:		/* id already there - how? */
583
584
585
586
587
			case 0:		/* id inserted: this can never be
					 * the result of idl_insert, because
					 * we guaranteed that idl_change_first
					 * will always be called.
					 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
588
589
				if ( rc == 2 ) {
					Debug( LDAP_DEBUG_ANY,
590
					    "idl_insert_key: id %ld already in next block\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
591
592
					    id, 0, 0 );
				}
593

Howard Chu's avatar
Howard Chu committed
594
				cont_free( &k3 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
595
596
				idl_free( tmp );
				idl_free( tmp2 );
597
				cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
598
599
600
601
602
603
				idl_free( idl );
				return( 0 );

			case 3:		/* split the original block */
				break;
			}
604

Howard Chu's avatar
Howard Chu committed
605
			cont_free( &k3 );
606
			idl_free( tmp2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
607
608
		}

609
split:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
610
611
612
613
614
		/*
		 * must split the block, write both new blocks + update
		 * and write the indirect header block.
		 */

615
616
617
		rc = 0;	/* optimistic */


618
619
		/* count how many indirect blocks *//* XXX linear count XXX */
		for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
620
621
622
623
624
625
626
627
628
629
630
631
632
			;	/* NULL */

		/* check it against all-id thresholed */
		if ( j + 1 > db->dbc_maxindirect ) {
			/*
			 * we've passed the all-id threshold, meaning
			 * that this set of blocks should be replaced
			 * by a single "all-id" block.  our job: delete
			 * all the indirect blocks, and replace the header
			 * block by an all-id block.
			 */

			/* delete all indirect blocks */
633
			for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
634
				cont_id( &k2, ID_BLOCK_ID(idl, j) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
635
636
637
638
639
640
641
642
643

				rc = ldbm_cache_delete( db, k2 );
			}

			/* store allid block in place of header block */
			idl_free( idl );
			idl = idl_allids( be );
			rc = idl_store( be, db, key, idl );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
644
			cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
645
646
647
648
649
650
651
652
653
			idl_free( idl );
			idl_free( tmp );
			return( rc );
		}

		idl_split_block( tmp, id, &tmp2, &tmp3 );
		idl_free( tmp );

		/* create a new updated indirect header block */
654
655
		tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
		ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
656
		/* everything up to the split block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
657
		AC_MEMCPY(
658
659
			(char *) &ID_BLOCK_ID(tmp, 0),
			(char *) &ID_BLOCK_ID(idl, 0),
Kurt Zeilenga's avatar
Kurt Zeilenga committed
660
661
		    i * sizeof(ID) );
		/* the two new blocks */
662
663
		ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
		ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
664
		/* everything after the split block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
665
		AC_MEMCPY(
666
667
668
			(char *) &ID_BLOCK_ID(tmp, i + 2),
			(char *) &ID_BLOCK_ID(idl, i + 1),
			(ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
669
670
671
672
673

		/* store the header block */
		rc = idl_store( be, db, key, tmp );

		/* store the first id block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
674
		cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
675
676
677
		rc = idl_store( be, db, k2, tmp2 );

		/* store the second id block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
678
		cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
679
680
681
682
683
684
685
		rc = idl_store( be, db, k2, tmp3 );

		idl_free( tmp2 );
		idl_free( tmp3 );
		break;
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
686
	cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
687
688
689
690
691
	idl_free( tmp );
	idl_free( idl );
	return( rc );
}

692

Kurt Zeilenga's avatar
Kurt Zeilenga committed
693
694
/*
 * idl_insert - insert an id into an id list.
695
696
697
 *
 *	returns
 * 		0	id inserted
Kurt Zeilenga's avatar
Kurt Zeilenga committed
698
699
700
701
702
 *		1	id inserted, first id in block has changed
 *		2	id not inserted, already there
 *		3	id not inserted, block must be split
 */
int
703
idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
704
{
705
	unsigned int	i;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
706

707
	if ( ID_BLOCK_ALLIDS( *idl ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
708
709
710
		return( 2 );	/* already there */
	}

711
712
	/* is it already there? *//* XXX linear search XXX */
	for ( i = 0; i < ID_BLOCK_NIDS(*idl) && id > ID_BLOCK_ID(*idl, i); i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
713
714
		;	/* NULL */
	}
715
	if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
716
717
718
719
		return( 2 );	/* already there */
	}

	/* do we need to make room for it? */
720
	if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
721
		/* make room or indicate block needs splitting */
722
		if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
723
724
725
			return( 3 );	/* block needs splitting */
		}

726
727
728
		ID_BLOCK_NMAX(*idl) *= 2;
		if ( ID_BLOCK_NMAX(*idl) > maxids ) {
			ID_BLOCK_NMAX(*idl) = maxids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
729
		}
730
731
		*idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
		    (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
732
733
734
	}

	/* make a slot for the new id */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
735
	AC_MEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
736
	            (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
737
738
739
740
741
742
743

	ID_BLOCK_ID(*idl, i) = id;
	ID_BLOCK_NIDS(*idl)++;
	(void) memset(
		(char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
		'\0',
	    (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
744

Kurt Zeilenga's avatar
Kurt Zeilenga committed
745
746
747
748
#ifdef LDBM_DEBUG_IDL
	idl_check(*idl);
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
749
750
751
	return( i == 0 ? 1 : 0 );	/* inserted - first id changed or not */
}

752
753
754
755
756
757
758
759
760
761

int
idl_delete_key (
	Backend         *be,
	DBCache  *db,
	Datum           key,
	ID              id
)
{
	Datum  data;
762
	ID_BLOCK *idl;
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
	unsigned i;
	int j, nids;

	if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
	{
		/* It wasn't found.  Hmm... */
		return -1;
	}

	if ( ID_BLOCK_ALLIDS( idl ) ) {
		idl_free( idl );
		return 0;
	}

	if ( ! ID_BLOCK_INDIRECT( idl ) ) {
		for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
			if ( ID_BLOCK_ID(idl, i) == id ) {
				if( --ID_BLOCK_NIDS(idl) == 0 ) {
					ldbm_cache_delete( db, key );

				} else {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
784
					AC_MEMCPY(
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
						&ID_BLOCK_ID(idl, i),
						&ID_BLOCK_ID(idl, i+1),
						(ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );

					ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;

					idl_store( be, db, key, idl );
				}

				idl_free( idl );
				return 0;
			}
			/*  We didn't find the ID.  Hmmm... */
		}
		idl_free( idl );
		return -1;
	}
	
	/* We have to go through an indirect block and find the ID
	   in the list of IDL's
	   */
	for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
		;	/* NULL */
808

Kurt Zeilenga's avatar
Kurt Zeilenga committed
809
	cont_alloc( &data, &key );
810
811
812

	for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) 
	{
813
		ID_BLOCK *tmp;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
814
		cont_id( &data, ID_BLOCK_ID(idl, j) );
815
816
817

		if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
			Debug( LDAP_DEBUG_ANY,
818
			    "idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
819
820
821
822
823
824
825
826
827
			continue;
		}
		/*
		   Now try to find the ID in tmp
		*/
		for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
		{
			if ( ID_BLOCK_ID(tmp, i) == id )
			{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
828
				AC_MEMCPY(
829
830
831
832
833
834
835
836
837
838
839
					&ID_BLOCK_ID(tmp, i),
					&ID_BLOCK_ID(tmp, i+1),
					(ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
				ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
				ID_BLOCK_NIDS(tmp)--;

				if ( ID_BLOCK_NIDS(tmp) ) {
					idl_store ( be, db, data, tmp );

				} else {
					ldbm_cache_delete( db, data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
840
					AC_MEMCPY(
841
842
843
844
845
846
847
848
849
850
851
						&ID_BLOCK_ID(idl, j),
						&ID_BLOCK_ID(idl, j+1),
						(nids-(j+1)) * sizeof(ID));
					ID_BLOCK_ID(idl, nids-1) = NOID;
					nids--;
					if ( ! nids )
						ldbm_cache_delete( db, key );
					else
						idl_store( be, db, key, idl );
				}
				idl_free( tmp );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
852
				cont_free( &data );
853
854
855
856
857
858
				idl_free( idl );
				return 0;
			}
		}
		idl_free( tmp );
	}
859

Kurt Zeilenga's avatar
Kurt Zeilenga committed
860
	cont_free( &data );
861
862
863
864
865
866
867
868
	idl_free( idl );
	return -1;
}


/* return a duplicate of a single ID_BLOCK */
static ID_BLOCK *
idl_dup( ID_BLOCK *idl )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
869
{
870
	ID_BLOCK	*new;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
871
872
873
874
875

	if ( idl == NULL ) {
		return( NULL );
	}

876
877
	new = idl_alloc( ID_BLOCK_NMAX(idl) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
878
	AC_MEMCPY(
879
880
881
		(char *) new,
		(char *) idl,
		(ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
882

Kurt Zeilenga's avatar
Kurt Zeilenga committed
883
884
885
886
#ifdef LDBM_DEBUG_IDL
	idl_check(new);
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
887
888
889
	return( new );
}

890
891
892
893

/* return the smaller ID_BLOCK */
static ID_BLOCK *
idl_min( ID_BLOCK *a, ID_BLOCK *b )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
894
{
895
	return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
896
897
}

898

Kurt Zeilenga's avatar
Kurt Zeilenga committed
899
900
901
/*
 * idl_intersection - return a intersection b
 */
902
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
903
904
idl_intersection(
    Backend	*be,
905
906
    ID_BLOCK	*a,
    ID_BLOCK	*b
Kurt Zeilenga's avatar
Kurt Zeilenga committed
907
908
)
{
909
910
	unsigned int	ai, bi, ni;
	ID_BLOCK		*n;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
911
912
913
914

	if ( a == NULL || b == NULL ) {
		return( NULL );
	}
915
	if ( ID_BLOCK_ALLIDS( a ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
916
917
		return( idl_dup( b ) );
	}
918
	if ( ID_BLOCK_ALLIDS( b ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
919
920
		return( idl_dup( a ) );
	}
921
922
923
	if ( ID_BLOCK_NIDS(a) == 0 || ID_BLOCK_NIDS(b) == 0 ) {
		return( NULL );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
924
925
926

	n = idl_dup( idl_min( a, b ) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
927
928
929
930
931
#ifdef LDBM_DEBUG_IDL
	idl_check(a);
	idl_check(b);
#endif

932
	for ( ni = 0, ai = 0, bi = 0; ; ) {
933
934
		if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
			ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
935
			bi++;
936
937
938
939
940
941
942
943
944
945
946
			ai++;
			if ( ai >= ID_BLOCK_NIDS(a) || bi >= ID_BLOCK_NIDS(b) )
				break;
		} else if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
			ai++;
			if ( ai >= ID_BLOCK_NIDS(a) )
				break;
		} else {
			bi++;
			if ( bi >= ID_BLOCK_NIDS(b) )
				break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
947
948
949
950
951
952
953
		}
	}

	if ( ni == 0 ) {
		idl_free( n );
		return( NULL );
	}
954
	ID_BLOCK_NIDS(n) = ni;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
955

Kurt Zeilenga's avatar
Kurt Zeilenga committed
956
957
958
959
#ifdef LDBM_DEBUG_IDL
	idl_check(n);
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
960
961
962
	return( n );
}

963

Kurt Zeilenga's avatar
Kurt Zeilenga committed
964
965
966
/*
 * idl_union - return a union b
 */
967
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
968
969
idl_union(
    Backend	*be,
970
971
    ID_BLOCK	*a,
    ID_BLOCK	*b
Kurt Zeilenga's avatar
Kurt Zeilenga committed
972
973
)
{
974
975
	unsigned int	ai, bi, ni;
	ID_BLOCK		*n;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
976
977
978
979
980
981
982

	if ( a == NULL ) {
		return( idl_dup( b ) );
	}
	if ( b == NULL ) {
		return( idl_dup( a ) );
	}
983
	if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
984
985
986
		return( idl_allids( be ) );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
987
988
989
990
991
#ifdef LDBM_DEBUG_IDL
	idl_check(a);
	idl_check(b);
#endif

992
	if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
993
994
995
996
997
		n = a;
		a = b;
		b = n;
	}

998
999
1000
	n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );

	for ( ni = 0, ai = 0, bi = 0;
For faster browsing, not all history is shown. View entire blame