idl.c 22.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-2000 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
}

48
49
50
/* Allocate an ID_BLOCK with room for nids ids */
ID_BLOCK *
idl_alloc( unsigned int nids )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
51
{
52
	ID_BLOCK	*new;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
53
54

	/* nmax + nids + space for the ids */
55
56
57
	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
58
59
60
61

	return( new );
}

62
63
64

/* Allocate an empty ALLIDS ID_BLOCK */
ID_BLOCK	*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
65
66
idl_allids( Backend *be )
{
67
	ID_BLOCK	*idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
68
69

	idl = idl_alloc( 0 );
70
71
	ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
	ID_BLOCK_NIDS(idl) = next_id_get( be );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
72
73
74
75

	return( idl );
}

76
/* Free an ID_BLOCK */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
void
78
idl_free( ID_BLOCK *idl )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
79
80
{
	if ( idl == NULL ) {
81
82
83
		Debug( LDAP_DEBUG_TRACE,
			"idl_free: called with NULL pointer\n",
			0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
84
85
86
87
88
89
		return;
	}

	free( (char *) idl );
}

90
91
92

/* Fetch an single ID_BLOCK from the cache */
static ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
93
94
idl_fetch_one(
    Backend		*be,
95
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
96
97
98
    Datum		key
)
{
99
100
	Datum	data;
	ID_BLOCK	*idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
101
102
103
104
105

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

	data = ldbm_cache_fetch( db, key );

106
107
108
109
	if( data.dptr == NULL ) {
		return NULL;
	}

110
111
	idl = idl_dup((ID_BLOCK *) data.dptr);

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

114
	return idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
115
116
}

117
118
119
120
121
122
123
124
125
126
127

/* 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
128
129
idl_fetch(
    Backend		*be,
130
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
131
132
133
    Datum		key
)
{
134
135
136
	Datum	data;
	ID_BLOCK	*idl;
	ID_BLOCK	**tmp;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
138
	int	i, nids;

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

141
142
	if ( idl == NULL ) {
		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
143
144
	}

145
146
	if ( ID_BLOCK_ALLIDS(idl) ) {
		/* all ids block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
147
		/* make sure we have the current value of highest id */
148
149
150
151
152
153
154
155
		idl_free( idl );
		idl = idl_allids( be );

		return( idl );
	}

	if ( ! ID_BLOCK_INDIRECT( idl ) ) {
		/* regular block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156
157
158
159
160
161
162
163
164
165
		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 */
166
	for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
167
		;	/* NULL */
168
	tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
169
170

	/* read in all the blocks */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
171
	cont_alloc( &data, &key );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
172
	nids = 0;
173
	for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
174
		cont_id( &data, ID_BLOCK_ID(idl, i) );
175
176

		if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
177
			Debug( LDAP_DEBUG_ANY,
178
			    "idl_fetch: one returned NULL\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
179
180
181
			continue;
		}

182
		nids += ID_BLOCK_NIDS(tmp[i]);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
183
184
	}
	tmp[i] = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
185
	cont_free( &data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
186
187
188
189
	idl_free( idl );

	/* allocate space for the big block */
	idl = idl_alloc( nids );
190
	ID_BLOCK_NIDS(idl) = nids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
191
192
193
194
195
196
197
198
	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
199
		AC_MEMCPY(
200
201
202
203
			(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
204
205
206
207
208

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

209
210
	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
211
212
213
	return( idl );
}

214
215

/* store a single block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
216
217
218
static int
idl_store(
    Backend		*be,
219
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
220
    Datum		key, 
221
    ID_BLOCK		*idl
Kurt Zeilenga's avatar
Kurt Zeilenga committed
222
223
)
{
224
	int	rc, flags;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
225
	Datum	data;
226
227
228
	struct ldbminfo *li = (struct ldbminfo *) be->be_private;

	ldbm_datum_init( data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
229
230
231
232

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

	data.dptr = (char *) idl;
233
234
235
236
237
238
	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
239

240
241
	flags = LDBM_REPLACE;
	rc = ldbm_cache_store( db, key, data, flags );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242
243
244
245
246

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

247
248
249
/* split the block at id 
 *	locate ID greater than or equal to id.
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
250
251
static void
idl_split_block(
252
    ID_BLOCK	*b,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
253
    ID		id,
254
255
    ID_BLOCK	**right,
    ID_BLOCK	**left
Kurt Zeilenga's avatar
Kurt Zeilenga committed
256
257
)
{
258
	unsigned int	nr, nl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
259

260
261
	/* 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
262
263
		;	/* NULL */

264
265
266
267
	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
268
269
270
271
272
273

	/*
	 * everything before the id being inserted in the first block
	 * unless there is nothing, in which case the id being inserted
	 * goes there.
	 */
274
275
276
	if ( nr == 0 ) {
		ID_BLOCK_NIDS(*right) = 1;
		ID_BLOCK_ID(*right, 0) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
277
	} else {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
278
		AC_MEMCPY(
279
280
281
282
283
			(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
284
285
286
	}

	/* the id being inserted & everything after in the second block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
287
	AC_MEMCPY(
288
289
290
291
		(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
292
293
}

294

Kurt Zeilenga's avatar
Kurt Zeilenga committed
295
296
297
298
299
300
301
302
/*
 * 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,
303
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
304
    Datum		hkey,		/* header block key	*/
305
    ID_BLOCK		*h,		/* header block 	*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
306
307
    int			pos,		/* pos in h to update	*/
    Datum		bkey,		/* data block key	*/
308
    ID_BLOCK		*b		/* data block 		*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
309
310
311
312
313
314
315
316
317
)
{
	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,
318
319
		    "idl_change_first: ldbm_cache_delete returned %d\n",
			rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320
321
322
323
		return( rc );
	}

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
326
327
	if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
		Debug( LDAP_DEBUG_ANY,
328
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
329
330
331
332
		return( rc );
	}

	/* update + write indirect header block */
333
	ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
334
335
	if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
		Debug( LDAP_DEBUG_ANY,
336
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
337
338
339
340
341
342
		return( rc );
	}

	return( 0 );
}

343

Kurt Zeilenga's avatar
Kurt Zeilenga committed
344
345
346
int
idl_insert_key(
    Backend		*be,
347
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
349
350
351
352
    Datum		key,
    ID			id
)
{
	int	i, j, first, rc;
353
	ID_BLOCK	*idl, *tmp, *tmp2, *tmp3;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
354
355
356
357
	Datum	k2;

	if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
		idl = idl_alloc( 1 );
358
		ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
359
360
361
362
363
364
		rc = idl_store( be, db, key, idl );

		idl_free( idl );
		return( rc );
	}

365
366
367
368
369
370
371
372
	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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
		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 );
389
				break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
390
391
392
393
394
395
396
			}

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

			/* create the header indirect block */
			idl = idl_alloc( 3 );
397
398
399
400
401
			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
402
403
404
405

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
406
407
			cont_alloc( &k2, &key );
			cont_id( &k2, ID_BLOCK_ID(tmp, 0) );
408

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
411
			cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
412
413
			rc = idl_store( be, db, k2, tmp2 );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
414
			cont_free( &k2 );
415

Kurt Zeilenga's avatar
Kurt Zeilenga committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
			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.
	 */

433
434
	/* 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
435
		;	/* NULL */
436

Kurt Zeilenga's avatar
Kurt Zeilenga committed
437
438
439
440
441
442
443
444
	if ( i != 0 ) {
		i--;
		first = 0;
	} else {
		first = 1;
	}

	/* get the block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
445
446
	cont_alloc( &k2, &key );
	cont_id( &k2, ID_BLOCK_ID(idl, i) );
447

Kurt Zeilenga's avatar
Kurt Zeilenga committed
448
	if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
449
450
		Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
		    0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
451
		cont_free( &k2 );
452
		idl_free( idl );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
453
454
455
456
457
458
459
460
		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,
461
			    "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
		}
		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;

476
477
	case 2:		/* id not inserted - already there, do nothing */
		rc = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
478
479
480
481
482
483
484
485
486
487
		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? */
488
		if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
489
			/* read it in */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
490
491
			cont_alloc( &k2, &key );
			cont_id( &k2, ID_BLOCK_ID(idl, i) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
492
493
			if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
				Debug( LDAP_DEBUG_ANY,
494
495
				    "idl_insert_key: idl_fetch_one returned NULL\n",
				    0, 0, 0 );
496
				/* split the original block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
497
				cont_free( &k2 );
498
				goto split;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
499
500
			}

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
			/* 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
520
				AC_MEMCPY(k3.dptr, k2.dptr, k3.dsize);
521
522
523
524
525
526
527
528
529
530
531
532
533
534
			    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
535
536
537
538
539
540
541
542
			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? */
543
544
545
546
547
			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
548
549
				if ( rc == 2 ) {
					Debug( LDAP_DEBUG_ANY,
550
					    "idl_insert_key: id %ld already in next block\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
551
552
					    id, 0, 0 );
				}
553
554

				assert( 0 ); /* not yet implemented */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
555
556
557
558
559
560
561
562
				idl_free( tmp );
				idl_free( tmp2 );
				idl_free( idl );
				return( 0 );

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

564
			idl_free( tmp2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
565
566
		}

567
split:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
568
569
570
571
572
		/*
		 * must split the block, write both new blocks + update
		 * and write the indirect header block.
		 */

573
574
575
		rc = 0;	/* optimistic */


576
577
		/* 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
578
579
580
581
582
583
584
585
586
587
588
589
590
			;	/* 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 */
591
			for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
592
				cont_id( &k2, ID_BLOCK_ID(idl, j) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
593
594
595
596
597
598
599
600
601

				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
602
			cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
603
604
605
606
607
608
609
610
611
			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 */
612
613
		tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
		ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
614
		/* everything up to the split block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
615
		AC_MEMCPY(
616
617
			(char *) &ID_BLOCK_ID(tmp, 0),
			(char *) &ID_BLOCK_ID(idl, 0),
Kurt Zeilenga's avatar
Kurt Zeilenga committed
618
619
		    i * sizeof(ID) );
		/* the two new blocks */
620
621
		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
622
		/* everything after the split block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
623
		AC_MEMCPY(
624
625
626
			(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
627
628
629
630
631

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

		/* store the first id block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
632
		cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
633
634
635
		rc = idl_store( be, db, k2, tmp2 );

		/* store the second id block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
636
		cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
637
638
639
640
641
642
643
		rc = idl_store( be, db, k2, tmp3 );

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

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

650

Kurt Zeilenga's avatar
Kurt Zeilenga committed
651
652
/*
 * idl_insert - insert an id into an id list.
653
654
655
 *
 *	returns
 * 		0	id inserted
Kurt Zeilenga's avatar
Kurt Zeilenga committed
656
657
658
659
660
 *		1	id inserted, first id in block has changed
 *		2	id not inserted, already there
 *		3	id not inserted, block must be split
 */
int
661
idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
662
{
663
	unsigned int	i;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
664

665
	if ( ID_BLOCK_ALLIDS( *idl ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
666
667
668
		return( 2 );	/* already there */
	}

669
670
	/* 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
671
672
		;	/* NULL */
	}
673
	if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
674
675
676
677
		return( 2 );	/* already there */
	}

	/* do we need to make room for it? */
678
	if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
679
		/* make room or indicate block needs splitting */
680
		if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
681
682
683
			return( 3 );	/* block needs splitting */
		}

684
685
686
		ID_BLOCK_NMAX(*idl) *= 2;
		if ( ID_BLOCK_NMAX(*idl) > maxids ) {
			ID_BLOCK_NMAX(*idl) = maxids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
687
		}
688
689
		*idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
		    (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
690
691
692
	}

	/* make a slot for the new id */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
693
	AC_MEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
694
	            (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
695
696
697
698
699
700
701

	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
702
703
704
705

	return( i == 0 ? 1 : 0 );	/* inserted - first id changed or not */
}

706
707
708
709
710
711
712
713
714
715

int
idl_delete_key (
	Backend         *be,
	DBCache  *db,
	Datum           key,
	ID              id
)
{
	Datum  data;
716
	ID_BLOCK *idl;
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
	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
738
					AC_MEMCPY(
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
						&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 */
762

Kurt Zeilenga's avatar
Kurt Zeilenga committed
763
	cont_alloc( &data, &key );
764
765
766

	for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) 
	{
767
		ID_BLOCK *tmp;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
768
		cont_id( &data, ID_BLOCK_ID(idl, j) );
769
770
771

		if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
			Debug( LDAP_DEBUG_ANY,
772
			    "idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
773
774
775
776
777
778
779
780
781
			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
782
				AC_MEMCPY(
783
784
785
786
787
788
789
790
791
792
793
					&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
794
					AC_MEMCPY(
795
796
797
798
799
800
801
802
803
804
805
						&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
806
				cont_free( &data );
807
808
809
810
811
812
				idl_free( idl );
				return 0;
			}
		}
		idl_free( tmp );
	}
813

Kurt Zeilenga's avatar
Kurt Zeilenga committed
814
	cont_free( &data );
815
816
817
818
819
820
821
822
	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
823
{
824
	ID_BLOCK	*new;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
825
826
827
828
829

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

830
831
	new = idl_alloc( ID_BLOCK_NMAX(idl) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
832
	AC_MEMCPY(
833
834
835
		(char *) new,
		(char *) idl,
		(ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
836
837
838
839

	return( new );
}

840
841
842
843

/* return the smaller ID_BLOCK */
static ID_BLOCK *
idl_min( ID_BLOCK *a, ID_BLOCK *b )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
844
{
845
	return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
846
847
}

848

Kurt Zeilenga's avatar
Kurt Zeilenga committed
849
850
851
/*
 * idl_intersection - return a intersection b
 */
852
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
853
854
idl_intersection(
    Backend	*be,
855
856
    ID_BLOCK	*a,
    ID_BLOCK	*b
Kurt Zeilenga's avatar
Kurt Zeilenga committed
857
858
)
{
859
860
	unsigned int	ai, bi, ni;
	ID_BLOCK		*n;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
861
862
863
864

	if ( a == NULL || b == NULL ) {
		return( NULL );
	}
865
	if ( ID_BLOCK_ALLIDS( a ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
866
867
		return( idl_dup( b ) );
	}
868
	if ( ID_BLOCK_ALLIDS( b ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
869
870
871
872
873
		return( idl_dup( a ) );
	}

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

874
875
876
877
878
	for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
		for ( ;
			bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
			bi++ )
		{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
879
			;	/* NULL */
880
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
881

882
		if ( bi == ID_BLOCK_NIDS(b) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
883
884
885
			break;
		}

886
887
		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
888
889
890
891
892
893
894
		}
	}

	if ( ni == 0 ) {
		idl_free( n );
		return( NULL );
	}
895
	ID_BLOCK_NIDS(n) = ni;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
896
897
898
899

	return( n );
}

900

Kurt Zeilenga's avatar
Kurt Zeilenga committed
901
902
903
/*
 * idl_union - return a union b
 */
904
ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
905
906
idl_union(
    Backend	*be,
907
908
    ID_BLOCK	*a,
    ID_BLOCK	*b
Kurt Zeilenga's avatar
Kurt Zeilenga committed
909
910
)
{
911
912
	unsigned int	ai, bi, ni;
	ID_BLOCK		*n;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
913
914
915
916
917
918
919

	if ( a == NULL ) {
		return( idl_dup( b ) );
	}
	if ( b == NULL ) {
		return( idl_dup( a ) );
	}
920
	if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
921
922
923
		return( idl_allids( be ) );
	}

924
	if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
925
926
927
928
929
		n = a;
		a = b;
		b = n;
	}

930
931
932
933
934
935
936
937
938
939
940
	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++);

		} else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
			ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
941
942

		} else {
943
			ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
944
945
946
947
			ai++, bi++;
		}
	}

948
949
	for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
		ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
950
	}
951
952
	for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
		ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
953
	}
954
	ID_BLOCK_NIDS(n) = ni;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
955
956
957
958

	return( n );
}

959

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

	if ( a == NULL ) {
		return( NULL );
	}
976
	if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
977
978
979
		return( idl_dup( a ) );
	}

980
	if ( ID_BLOCK_ALLIDS( a ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
981
982
983
		n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
		ni = 0;

984
985
986
987
988
		for ( ai = 1, bi = 0;
			ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
			ai++ )
		{
			if ( ID_BLOCK_ID(b, bi) == ai ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
989
990
				bi++;
			} else {
991
				ID_BLOCK_ID(n, ni++) = ai;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
992
993
994
			}
		}

995
996
		for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
			ID_BLOCK_ID(n, ni++) = ai;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
997
998
		}

999
		if ( ni == ID_BLOCK_NMAX(n) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1000
			idl_free( n );
For faster browsing, not all history is shown. View entire blame