idl.c 23.5 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifdef LDBM_DEBUG_IDL
static void idl_check(ID_BLOCK *idl)
{
	int i;
	ID_BLOCK last;

	if( ID_BLOCK_INDIRECT(idl) || ID_BLOCK_ALLIDS(idl)
		|| ID_BLOCK_NIDS(idl) <= 1 )
	{
		return;
	}

	for( last = ID_BLOCK_ID(idl, 0), i = 1;
		i < ID_BLOCK_NIDS(idl);
		last = ID_BLOCK_ID(idl, i), i++ )
	{
		assert (last < ID_BLOCK_ID(idl, i) );
	}
}
#endif

69
70
71
/* Allocate an ID_BLOCK with room for nids ids */
ID_BLOCK *
idl_alloc( unsigned int nids )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
72
{
73
	ID_BLOCK	*new;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
74
75

	/* nmax + nids + space for the ids */
76
77
78
	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
79
80
81
82

	return( new );
}

83
84
85

/* Allocate an empty ALLIDS ID_BLOCK */
ID_BLOCK	*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
86
87
idl_allids( Backend *be )
{
88
	ID_BLOCK	*idl;
89
	ID		id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
90
91

	idl = idl_alloc( 0 );
92
	ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
93
94
95
96
	if ( next_id_get( be, &id ) ) {
		return NULL;
	}
	ID_BLOCK_NIDS(idl) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
97
98
99
100

	return( idl );
}

101
/* Free an ID_BLOCK */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
102
void
103
idl_free( ID_BLOCK *idl )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
105
{
	if ( idl == NULL ) {
106
107
108
		Debug( LDAP_DEBUG_TRACE,
			"idl_free: called with NULL pointer\n",
			0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
109
110
111
112
113
114
		return;
	}

	free( (char *) idl );
}

115
116
117

/* Fetch an single ID_BLOCK from the cache */
static ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
118
119
idl_fetch_one(
    Backend		*be,
120
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
121
122
123
    Datum		key
)
{
124
125
	Datum	data;
	ID_BLOCK	*idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
126
127
128
129
130

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

	data = ldbm_cache_fetch( db, key );

131
132
133
134
	if( data.dptr == NULL ) {
		return NULL;
	}

135
136
	idl = idl_dup((ID_BLOCK *) data.dptr);

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

139
	return idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
140
141
}

142
143
144
145
146
147
148
149
150
151
152

/* 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
153
154
idl_fetch(
    Backend		*be,
155
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156
157
158
    Datum		key
)
{
159
160
161
	Datum	data;
	ID_BLOCK	*idl;
	ID_BLOCK	**tmp;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
162
163
	int	i, nids;

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

166
167
	if ( idl == NULL ) {
		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
168
169
	}

170
171
	if ( ID_BLOCK_ALLIDS(idl) ) {
		/* all ids block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
172
		/* make sure we have the current value of highest id */
173
174
175
176
177
178
179
180
		idl_free( idl );
		idl = idl_allids( be );

		return( idl );
	}

	if ( ! ID_BLOCK_INDIRECT( idl ) ) {
		/* regular block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
181
182
183
184
185
186
187
188
189
190
		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 */
191
	for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
192
		;	/* NULL */
193
	tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
194
195

	/* read in all the blocks */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
196
	cont_alloc( &data, &key );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
197
	nids = 0;
198
	for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
199
		cont_id( &data, ID_BLOCK_ID(idl, i) );
200
201

		if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
202
			Debug( LDAP_DEBUG_ANY,
203
			    "idl_fetch: one returned NULL\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
204
205
206
			continue;
		}

207
		nids += ID_BLOCK_NIDS(tmp[i]);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
208
209
	}
	tmp[i] = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
210
	cont_free( &data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
211
212
213
214
	idl_free( idl );

	/* allocate space for the big block */
	idl = idl_alloc( nids );
215
	ID_BLOCK_NIDS(idl) = nids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
216
217
218
219
220
221
222
223
	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
224
		AC_MEMCPY(
225
226
227
228
			(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
229
230
231
232
233

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
234
235
236
237
#ifdef LDBM_DEBUG_IDL
	idl_check(idl);
#endif

238
239
	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
240
241
242
	return( idl );
}

243
244

/* store a single block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
245
246
247
static int
idl_store(
    Backend		*be,
248
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
249
    Datum		key, 
250
    ID_BLOCK		*idl
Kurt Zeilenga's avatar
Kurt Zeilenga committed
251
252
)
{
253
	int	rc, flags;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
254
	Datum	data;
255
256
	struct ldbminfo *li = (struct ldbminfo *) be->be_private;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
257
258
259
260
#ifdef LDBM_DEBUG_IDL
	idl_check(idl);
#endif

261
	ldbm_datum_init( data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
262
263
264
265

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

	data.dptr = (char *) idl;
266
267
268
269
270
271
	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
272

273
274
	flags = LDBM_REPLACE;
	rc = ldbm_cache_store( db, key, data, flags );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
275
276
277
278
279

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

280
281
282
/* split the block at id 
 *	locate ID greater than or equal to id.
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
283
284
static void
idl_split_block(
285
    ID_BLOCK	*b,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
286
    ID		id,
287
288
    ID_BLOCK	**right,
    ID_BLOCK	**left
Kurt Zeilenga's avatar
Kurt Zeilenga committed
289
290
)
{
291
	unsigned int	nr, nl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
292

293
294
	/* 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
295
296
		;	/* NULL */

297
298
299
300
	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
301
302
303
304
305
306

	/*
	 * everything before the id being inserted in the first block
	 * unless there is nothing, in which case the id being inserted
	 * goes there.
	 */
307
308
309
	if ( nr == 0 ) {
		ID_BLOCK_NIDS(*right) = 1;
		ID_BLOCK_ID(*right, 0) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
310
	} else {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
311
		AC_MEMCPY(
312
313
314
315
316
			(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
317
318
319
	}

	/* the id being inserted & everything after in the second block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320
	AC_MEMCPY(
321
322
323
324
		(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
325
326
327
328
329

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

332

Kurt Zeilenga's avatar
Kurt Zeilenga committed
333
334
335
336
337
338
339
340
/*
 * 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,
341
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
    Datum		hkey,		/* header block key	*/
343
    ID_BLOCK		*h,		/* header block 	*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
344
345
    int			pos,		/* pos in h to update	*/
    Datum		bkey,		/* data block key	*/
346
    ID_BLOCK		*b		/* data block 		*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
347
348
349
350
351
352
353
354
355
)
{
	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,
356
357
		    "idl_change_first: ldbm_cache_delete returned %d\n",
			rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
358
359
360
361
		return( rc );
	}

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
364
365
	if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
		Debug( LDAP_DEBUG_ANY,
366
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
367
368
369
370
		return( rc );
	}

	/* update + write indirect header block */
371
	ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
372
373
	if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
		Debug( LDAP_DEBUG_ANY,
374
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
375
376
377
378
379
380
		return( rc );
	}

	return( 0 );
}

381

Kurt Zeilenga's avatar
Kurt Zeilenga committed
382
383
384
int
idl_insert_key(
    Backend		*be,
385
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
386
387
388
389
390
    Datum		key,
    ID			id
)
{
	int	i, j, first, rc;
391
	ID_BLOCK	*idl, *tmp, *tmp2, *tmp3;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
392
393
394
395
	Datum	k2;

	if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
		idl = idl_alloc( 1 );
396
		ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
397
398
399
400
401
402
		rc = idl_store( be, db, key, idl );

		idl_free( idl );
		return( rc );
	}

403
404
405
406
407
408
409
410
	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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
		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 );
427
				break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
428
429
430
431
432
433
434
			}

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

			/* create the header indirect block */
			idl = idl_alloc( 3 );
435
436
437
438
439
			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
440
441
442
443

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
444
445
			cont_alloc( &k2, &key );
			cont_id( &k2, ID_BLOCK_ID(tmp, 0) );
446

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
449
			cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
450
451
			rc = idl_store( be, db, k2, tmp2 );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
452
			cont_free( &k2 );
453

Kurt Zeilenga's avatar
Kurt Zeilenga committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
			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.
	 */

471
472
	/* 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
473
		;	/* NULL */
474

Kurt Zeilenga's avatar
Kurt Zeilenga committed
475
476
477
478
479
480
481
482
	if ( i != 0 ) {
		i--;
		first = 0;
	} else {
		first = 1;
	}

	/* get the block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
483
484
	cont_alloc( &k2, &key );
	cont_id( &k2, ID_BLOCK_ID(idl, i) );
485

Kurt Zeilenga's avatar
Kurt Zeilenga committed
486
	if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
487
488
		Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
		    0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
489
		cont_free( &k2 );
490
		idl_free( idl );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
491
492
493
494
495
496
497
498
		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,
499
			    "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
		}
		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;

514
515
	case 2:		/* id not inserted - already there, do nothing */
		rc = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
516
517
518
519
520
521
522
523
524
525
		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? */
526
		if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
527
			/* read it in */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
528
529
			cont_alloc( &k2, &key );
			cont_id( &k2, ID_BLOCK_ID(idl, i) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
530
531
			if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
				Debug( LDAP_DEBUG_ANY,
532
533
				    "idl_insert_key: idl_fetch_one returned NULL\n",
				    0, 0, 0 );
534
				/* split the original block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
535
				cont_free( &k2 );
536
				goto split;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
537
538
			}

539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
			/* 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);
			    Datum k3;

			    ldbm_datum_init( k3 );

			    --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 );

				k3.dptr = ch_malloc(k2.dsize);
				k3.dsize = k2.dsize;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
558
				AC_MEMCPY(k3.dptr, k2.dptr, k3.dsize);
559
560
561
562
563
564
565
566
567
568
569
570
571
572
			    if ( (rc = idl_store( be, db, k3, tmp )) != 0 ) {
				Debug( LDAP_DEBUG_ANY,
			    "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
			    }

				free( k3.dptr );

			    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
573
574
575
576
577
578
579
580
			switch ( (rc = idl_insert( &tmp2, id,
			    db->dbc_maxids )) ) {
			case 1:		/* id inserted first in block */
				rc = idl_change_first( be, db, key, idl,
				    i + 1, k2, tmp2 );
				/* FALL */

			case 2:		/* id already there - how? */
581
582
583
584
585
			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
586
587
				if ( rc == 2 ) {
					Debug( LDAP_DEBUG_ANY,
588
					    "idl_insert_key: id %ld already in next block\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
589
590
					    id, 0, 0 );
				}
591

Kurt Zeilenga's avatar
Kurt Zeilenga committed
592
593
594
595
596
597
598
599
				idl_free( tmp );
				idl_free( tmp2 );
				idl_free( idl );
				return( 0 );

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

601
			idl_free( tmp2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
602
603
		}

604
split:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
605
606
607
608
609
		/*
		 * must split the block, write both new blocks + update
		 * and write the indirect header block.
		 */

610
611
612
		rc = 0;	/* optimistic */


613
614
		/* 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
615
616
617
618
619
620
621
622
623
624
625
626
627
			;	/* 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 */
628
			for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
629
				cont_id( &k2, ID_BLOCK_ID(idl, j) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
630
631
632
633
634
635
636
637
638

				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
639
			cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
640
641
642
643
644
645
646
647
648
			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 */
649
650
		tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
		ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
651
		/* everything up to the split block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
652
		AC_MEMCPY(
653
654
			(char *) &ID_BLOCK_ID(tmp, 0),
			(char *) &ID_BLOCK_ID(idl, 0),
Kurt Zeilenga's avatar
Kurt Zeilenga committed
655
656
		    i * sizeof(ID) );
		/* the two new blocks */
657
658
		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
659
		/* everything after the split block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
660
		AC_MEMCPY(
661
662
663
			(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
664
665
666
667
668

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

		/* store the first id block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
669
		cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
670
671
672
		rc = idl_store( be, db, k2, tmp2 );

		/* store the second id block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
673
		cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
674
675
676
677
678
679
680
		rc = idl_store( be, db, k2, tmp3 );

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
681
	cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
682
683
684
685
686
	idl_free( tmp );
	idl_free( idl );
	return( rc );
}

687

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

702
	if ( ID_BLOCK_ALLIDS( *idl ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
703
704
705
		return( 2 );	/* already there */
	}

706
707
	/* 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
708
709
		;	/* NULL */
	}
710
	if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
711
712
713
714
		return( 2 );	/* already there */
	}

	/* do we need to make room for it? */
715
	if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
716
		/* make room or indicate block needs splitting */
717
		if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
718
719
720
			return( 3 );	/* block needs splitting */
		}

721
722
723
		ID_BLOCK_NMAX(*idl) *= 2;
		if ( ID_BLOCK_NMAX(*idl) > maxids ) {
			ID_BLOCK_NMAX(*idl) = maxids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
724
		}
725
726
		*idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
		    (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
727
728
729
	}

	/* make a slot for the new id */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
730
	AC_MEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
731
	            (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
732
733
734
735
736
737
738

	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
739

Kurt Zeilenga's avatar
Kurt Zeilenga committed
740
741
742
743
#ifdef LDBM_DEBUG_IDL
	idl_check(*idl);
#endif

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

747
748
749
750
751
752
753
754
755
756

int
idl_delete_key (
	Backend         *be,
	DBCache  *db,
	Datum           key,
	ID              id
)
{
	Datum  data;
757
	ID_BLOCK *idl;
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
	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
779
					AC_MEMCPY(
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
						&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 */
803

Kurt Zeilenga's avatar
Kurt Zeilenga committed
804
	cont_alloc( &data, &key );
805
806
807

	for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) 
	{
808
		ID_BLOCK *tmp;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
809
		cont_id( &data, ID_BLOCK_ID(idl, j) );
810
811
812

		if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
			Debug( LDAP_DEBUG_ANY,
813
			    "idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
814
815
816
817
818
819
820
821
822
			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
823
				AC_MEMCPY(
824
825
826
827
828
829
830
831
832
833
834
					&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
835
					AC_MEMCPY(
836
837
838
839
840
841
842
843
844
845
846
						&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
847
				cont_free( &data );
848
849
850
851
852
853
				idl_free( idl );
				return 0;
			}
		}
		idl_free( tmp );
	}
854

Kurt Zeilenga's avatar
Kurt Zeilenga committed
855
	cont_free( &data );
856
857
858
859
860
861
862
863
	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
864
{
865
	ID_BLOCK	*new;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
866
867
868
869
870

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

871
872
	new = idl_alloc( ID_BLOCK_NMAX(idl) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
873
	AC_MEMCPY(
874
875
876
		(char *) new,
		(char *) idl,
		(ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
877

Kurt Zeilenga's avatar
Kurt Zeilenga committed
878
879
880
881
#ifdef LDBM_DEBUG_IDL
	idl_check(new);
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
882
883
884
	return( new );
}

885
886
887
888

/* return the smaller ID_BLOCK */
static ID_BLOCK *
idl_min( ID_BLOCK *a, ID_BLOCK *b )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
889
{
890
	return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
891
892
}

893

Kurt Zeilenga's avatar
Kurt Zeilenga committed
894
895
896
/*
 * idl_intersection - return a intersection b
 */
897
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
898
899
idl_intersection(
    Backend	*be,
900
901
    ID_BLOCK	*a,
    ID_BLOCK	*b
Kurt Zeilenga's avatar
Kurt Zeilenga committed
902
903
)
{
904
905
	unsigned int	ai, bi, ni;
	ID_BLOCK		*n;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
906
907
908
909

	if ( a == NULL || b == NULL ) {
		return( NULL );
	}
910
	if ( ID_BLOCK_ALLIDS( a ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
911
912
		return( idl_dup( b ) );
	}
913
	if ( ID_BLOCK_ALLIDS( b ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
914
915
916
917
918
		return( idl_dup( a ) );
	}

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
919
920
921
922
923
#ifdef LDBM_DEBUG_IDL
	idl_check(a);
	idl_check(b);
#endif

924
	for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
925
926
927
		if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
			continue;
		}
928
929
930
931
		for ( ;
			bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
			bi++ )
		{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
932
			;	/* NULL */
933
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
934

935
		if ( bi == ID_BLOCK_NIDS(b) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
936
937
938
			break;
		}

939
940
		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
941
			bi++;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
942
943
944
945
946
947
948
		}
	}

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
951
952
953
954
#ifdef LDBM_DEBUG_IDL
	idl_check(n);
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
955
956
957
	return( n );
}

958

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

	if ( a == NULL ) {
		return( idl_dup( b ) );
	}
	if ( b == NULL ) {
		return( idl_dup( a ) );
	}
978
	if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
979
980
981
		return( idl_allids( be ) );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
982
983
984
985
986
#ifdef LDBM_DEBUG_IDL
	idl_check(a);
	idl_check(b);
#endif

987
	if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
988
989
990
991
992
		n = a;
		a = b;
		b = n;
	}

993
994
995
996
997
998
999
1000
	n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );

	for ( ni = 0, ai = 0, bi = 0;
		ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
		)
	{
		if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
			ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
For faster browsing, not all history is shown. View entire blame