idl.c 26.4 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* idl.c - ldap id list handling routines */
2
3
/* $OpenLDAP$ */
/*
4
 * Copyright 1998-2003 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
static ID_BLOCK* idl_dup( ID_BLOCK *idl );

static void cont_alloc( Datum *cont, Datum *key )
{
	ldbm_datum_init( *cont );
	cont->dsize = 1 + sizeof(ID) + key->dsize;
	cont->dptr = ch_malloc( cont->dsize );

	* (unsigned char *) cont->dptr = SLAP_INDEX_CONT_PREFIX;

	AC_MEMCPY( &((unsigned char *)cont->dptr)[1 + sizeof(ID)],
		key->dptr, key->dsize );
}

static void cont_id( Datum *cont, ID id )
{
	unsigned int i;

	for( i=1; i <= sizeof(id); i++) {
		((unsigned char *)cont->dptr)[i] = (unsigned char)(id & 0xFF);
		id >>= 8;
	}

}

static void cont_free( Datum *cont )
{
	ch_free( cont->dptr );
}

#ifdef LDBM_DEBUG_IDL
static void idl_check(ID_BLOCK *idl)
{
51
	int i, max;
52
53
	ID_BLOCK last;

54
55
56
57
58
59
60
61
62
63
64
65
66
67
	if( ID_BLOCK_ALLIDS(idl) )
	{
		return;
	}
#ifndef USE_INDIRECT_NIDS
	if( ID_BLOCK_INDIRECT(idl) )
	{
		for ( max = 0; !ID_BLOCK_NOID(idl, max); max++ ) ;
	} else
#endif
	{
		max = ID_BLOCK_NIDS(idl);
	}
	if ( max <= 1 )
68
69
70
71
72
	{
		return;
	}

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

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

	/* nmax + nids + space for the ids */
88
89
90
	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
91
92
93
94

	return( new );
}

95
96
97

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

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

	return( idl );
}

113
/* Free an ID_BLOCK */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
114
void
115
idl_free( ID_BLOCK *idl )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
116
117
{
	if ( idl == NULL ) {
118
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
119
		LDAP_LOG( INDEX, INFO, "idl_free: called with NULL pointer\n" , 0,0,0);
120
121
122
123
124
125
#else
		Debug( LDAP_DEBUG_TRACE,
			"idl_free: called with NULL pointer\n",
			0, 0, 0 );
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
126
127
128
129
130
131
		return;
	}

	free( (char *) idl );
}

132
133
134

/* Fetch an single ID_BLOCK from the cache */
static ID_BLOCK *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
135
136
idl_fetch_one(
    Backend		*be,
137
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
138
139
140
    Datum		key
)
{
141
142
	Datum	data;
	ID_BLOCK	*idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
143
144
145
146
147

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

	data = ldbm_cache_fetch( db, key );

148
149
150
	if( data.dptr == NULL ) {
		return NULL;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
151

152
153
154
155
156
157
158
159
160
161
162
	idl = (ID_BLOCK *) data.dptr;
	if ( ID_BLOCK_ALLIDS(idl) ) {
		/* make sure we have the current value of highest id */
		idl = idl_allids( be );
	} else {
		idl = idl_dup((ID_BLOCK *) data.dptr);
	}

	ldbm_datum_free( db->dbc_db, data );

	return idl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
163
164
}

165
166
167
168
169
170
171
172
173
174
175

/* 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
176
177
idl_fetch(
    Backend		*be,
178
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
179
180
181
    Datum		key
)
{
182
183
184
	Datum	data;
	ID_BLOCK	*idl;
	ID_BLOCK	**tmp;
185
	unsigned	i, nids, nblocks;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
186

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

189
190
	if ( idl == NULL ) {
		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
191
192
	}

193
194
195
196
	if ( ID_BLOCK_ALLIDS(idl) ) {
		/* all ids block */
		return( idl );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
197

198
199
	if ( ! ID_BLOCK_INDIRECT( idl ) ) {
		/* regular block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
200
201
202
203
204
205
206
207
208
		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.
	 */

209
#ifndef USE_INDIRECT_NIDS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
210
	/* count the number of blocks & allocate space for pointers to them */
211
	for ( nblocks = 0; !ID_BLOCK_NOID(idl, nblocks); nblocks++ )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
212
		;	/* NULL */
213
#else
214
	nblocks = ID_BLOCK_NIDS(idl);
215
#endif
216
	tmp = (ID_BLOCK **) ch_malloc( nblocks * sizeof(ID_BLOCK *) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
217
218

	/* read in all the blocks */
219
	cont_alloc( &data, &key );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
220
	nids = 0;
221
	for ( i = 0; i < nblocks; i++ ) {
222
223
224
225
		cont_id( &data, ID_BLOCK_ID(idl, i) );

		if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
226
227
			LDAP_LOG( INDEX, INFO,
				   "idl_fetch: idl_fetch_one returned NULL\n", 0,0,0 );
228
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
229
			Debug( LDAP_DEBUG_ANY,
230
231
232
			    "idl_fetch: one returned NULL\n", 0, 0, 0 );
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
233
234
235
			continue;
		}

236
		nids += ID_BLOCK_NIDS(tmp[i]);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
237
	}
238
	cont_free( &data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
240
241
242
	idl_free( idl );

	/* allocate space for the big block */
	idl = idl_alloc( nids );
243
	ID_BLOCK_NIDS(idl) = nids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
244
245
246
	nids = 0;

	/* copy in all the ids from the component blocks */
247
	for ( i = 0; i < nblocks; i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
248
249
250
251
		if ( tmp[i] == NULL ) {
			continue;
		}

252
253
254
255
256
		AC_MEMCPY(
			(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
257
258
259
260
261

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

262
263
	assert( ID_BLOCK_NIDS(idl) == nids );

264
265
266
267
268
#ifdef LDBM_DEBUG_IDL
	idl_check(idl);
#endif

#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
269
	LDAP_LOG( INDEX, ENTRY, 
270
		   "idl_fetch: %ld ids (%ld max)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
271
		   ID_BLOCK_NIDS(idl), ID_BLOCK_NMAXN(idl), 0 );
272
273
274
275
276
#else
	Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
	       ID_BLOCK_NIDS(idl), ID_BLOCK_NMAXN(idl), 0 );
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
277
278
279
	return( idl );
}

280
281

/* store a single block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
282
283
284
static int
idl_store(
    Backend		*be,
285
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
286
    Datum		key, 
287
    ID_BLOCK		*idl
Kurt Zeilenga's avatar
Kurt Zeilenga committed
288
289
)
{
290
	int	rc, flags;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
291
292
	Datum	data;

293
294
295
296
297
298
#ifdef LDBM_DEBUG_IDL
	idl_check(idl);
#endif

	ldbm_datum_init( data );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
299
300
301
	/* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */

	data.dptr = (char *) idl;
302
303
304
305
	data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAXN(idl)) * sizeof(ID);
	
	flags = LDBM_REPLACE;
	rc = ldbm_cache_store( db, key, data, flags );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
306
307
308
309
310

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

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* Binary search for id in block, return index
 *    an index is always returned, even with no match. If no
 * match, the returned index is the insertion point.
 */
static unsigned int
idl_find(
    ID_BLOCK	*b,
    ID		id
)
{
	int lo=0, hi=ID_BLOCK_NIDS(b)-1, nr=0;

	for (;lo<=hi;)
	{
	    nr = ( lo + hi ) / 2;
	    if (ID_BLOCK_ID(b, nr) == id)
	    	break;
	    if (ID_BLOCK_ID(b, nr) > id)
	    	hi = nr - 1;
	    else
	    	lo = nr + 1;
	}
	return nr;
}

/* split the block at id 
 *	locate ID greater than or equal to id.
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
339
340
static void
idl_split_block(
341
    ID_BLOCK	*b,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
    ID		id,
343
344
    ID_BLOCK	**right,
    ID_BLOCK	**left
Kurt Zeilenga's avatar
Kurt Zeilenga committed
345
346
)
{
347
	unsigned int	nr, nl;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
349

	/* find where to split the block */
350
351
352
353
354
	nr = idl_find(b, id);
	if ( ID_BLOCK_ID(b,nr) < id )
		nr++;

	nl = ID_BLOCK_NIDS(b) - nr;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
355

356
357
	*right = idl_alloc( nr == 0 ? 1 : nr );
	*left = idl_alloc( nl + (nr == 0 ? 0 : 1));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
358
359
360
361
362
363

	/*
	 * everything before the id being inserted in the first block
	 * unless there is nothing, in which case the id being inserted
	 * goes there.
	 */
364
365
366
	if ( nr == 0 ) {
		ID_BLOCK_NIDS(*right) = 1;
		ID_BLOCK_ID(*right, 0) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
367
	} else {
368
369
370
371
372
373
		AC_MEMCPY(
			(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
374
375
376
	}

	/* the id being inserted & everything after in the second block */
377
378
379
380
381
382
383
384
385
386
	AC_MEMCPY(
		(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);

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

389

Kurt Zeilenga's avatar
Kurt Zeilenga committed
390
391
392
393
394
395
396
397
/*
 * 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,
398
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
399
    Datum		hkey,		/* header block key	*/
400
    ID_BLOCK		*h,		/* header block		*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
401
402
    int			pos,		/* pos in h to update	*/
    Datum		bkey,		/* data block key	*/
403
    ID_BLOCK		*b		/* data block		*/
Kurt Zeilenga's avatar
Kurt Zeilenga committed
404
405
406
407
408
409
410
411
)
{
	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 ) {
412
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
413
414
		LDAP_LOG( INDEX, INFO, 
			   "idl_change_first: ldbm_cache_delete returned %d\n", rc, 0, 0 );
415
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
416
		Debug( LDAP_DEBUG_ANY,
417
418
419
420
		    "idl_change_first: ldbm_cache_delete returned %d\n",
			rc, 0, 0 );
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
421
422
423
424
		return( rc );
	}

	/* write block with new key */
425
426
	cont_id( &bkey, ID_BLOCK_ID(b, 0) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
427
	if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
428
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
429
430
		LDAP_LOG( INDEX, INFO, 
			   "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
431
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
432
		Debug( LDAP_DEBUG_ANY,
433
434
435
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
436
437
438
439
		return( rc );
	}

	/* update + write indirect header block */
440
	ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
441
	if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
442
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
443
444
		LDAP_LOG( INDEX, INFO, 
			   "idl_change_first: idl_store returned %s\n", rc, 0, 0 );
445
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
446
		Debug( LDAP_DEBUG_ANY,
447
448
449
		    "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
450
451
452
453
454
455
		return( rc );
	}

	return( 0 );
}

456

Kurt Zeilenga's avatar
Kurt Zeilenga committed
457
458
459
int
idl_insert_key(
    Backend		*be,
460
    DBCache	*db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
461
462
463
464
    Datum		key,
    ID			id
)
{
465
466
	int	i, j, first, rc = 0;
	ID_BLOCK	*idl, *tmp, *tmp2, *tmp3;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
467
468
469
470
	Datum	k2;

	if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
		idl = idl_alloc( 1 );
471
		ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
472
473
474
475
476
477
		rc = idl_store( be, db, key, idl );

		idl_free( idl );
		return( rc );
	}

478
479
480
481
482
483
484
485
	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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
		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 );
502
				break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
503
504
505
506
507
508
			}

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

			/* create the header indirect block */
509
#ifndef USE_INDIRECT_NIDS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
510
			idl = idl_alloc( 3 );
511
512
513
514
515
516
517
518
519
520
521
522
			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;
#else
			idl = idl_alloc( 2 );
			ID_BLOCK_NMAX(idl) = 2 | ID_BLOCK_INDIRECT_VALUE;
			ID_BLOCK_NIDS(idl) = 2;
			ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
			ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
523
524
525
526

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

527
528
529
			cont_alloc( &k2, &key );
			cont_id( &k2, ID_BLOCK_ID(tmp, 0) );

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

532
			cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
533
534
			rc = idl_store( be, db, k2, tmp2 );

535
536
			cont_free( &k2 );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
			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.
	 */

554
555
556
#ifndef USE_INDIRECT_NIDS
	/* 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
557
		;	/* NULL */
558
559
560
561
562
563
#else
	i = idl_find(idl, id);
	if (ID_BLOCK_ID(idl, i) < id)
		i++;
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
564
565
566
567
568
569
570
571
	if ( i != 0 ) {
		i--;
		first = 0;
	} else {
		first = 1;
	}

	/* get the block */
572
573
574
	cont_alloc( &k2, &key );
	cont_id( &k2, ID_BLOCK_ID(idl, i) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
575
	if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
576
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
577
578
		LDAP_LOG( INDEX, ERR,
			   "idl_insert_key: nonexistent continuation block\n", 0, 0, 0 );
579
580
581
582
583
584
585
#else
		Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
		    0, 0, 0 );
#endif

		cont_free( &k2 );
		idl_free( idl );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
586
587
588
589
590
591
592
		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 ) {
593
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
594
595
			LDAP_LOG( INDEX, ERR, 
				   "ids_insert_key: idl_store returned %d\n", rc, 0, 0 );
596
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
597
			Debug( LDAP_DEBUG_ANY,
598
599
600
			    "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
601
602
603
604
605
606
607
608
609
610
611
612
613
614
		}
		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;

615
616
	case 2:		/* id not inserted - already there, do nothing */
		rc = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
617
618
619
620
621
622
623
624
625
		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.
		 */

626
#ifndef USE_INDIRECT_NIDS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
627
		/* is there a next block? */
628
629
630
631
		if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
#else
		if ( !first && (unsigned long)(i + 1) < ID_BLOCK_NIDS(idl) ) {
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
632
			Datum k3;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
633
			/* read it in */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
634
635
636
			cont_alloc( &k3, &key );
			cont_id( &k3, ID_BLOCK_ID(idl, i + 1) );
			if ( (tmp2 = idl_fetch_one( be, db, k3 )) == NULL ) {
637
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
638
639
				LDAP_LOG( INDEX, ERR,
					   "idl_insert_key: idl_fetch_one returned NULL\n", 0, 0, 0);
640
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
641
				Debug( LDAP_DEBUG_ANY,
642
643
644
645
646
				    "idl_insert_key: idl_fetch_one returned NULL\n",
				    0, 0, 0 );
#endif

				/* split the original block */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
647
				cont_free( &k3 );
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
				goto split;
			}

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

			    if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
667
668
				LDAP_LOG( INDEX, ERR, 
					"idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
669
670
671
672
673
674
675
676
677
678
679
680
#else
				Debug( LDAP_DEBUG_ANY,
			    "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
#endif

			    }

			    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
681
682
683
684
685
686
			}

			switch ( (rc = idl_insert( &tmp2, id,
			    db->dbc_maxids )) ) {
			case 1:		/* id inserted first in block */
				rc = idl_change_first( be, db, key, idl,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
687
				    i + 1, k3, tmp2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
688
689
690
				/* FALL */

			case 2:		/* id already there - how? */
691
692
693
694
695
			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
696
				if ( rc == 2 ) {
697
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
698
					LDAP_LOG( INDEX, INFO, 
699
						   "idl_insert_key: id %ld is already in next block\n", 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
700
						   id, 0, 0 );
701
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
702
					Debug( LDAP_DEBUG_ANY,
703
					    "idl_insert_key: id %ld already in next block\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
704
					    id, 0, 0 );
705
706
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
707
				}
708

Kurt Zeilenga's avatar
Kurt Zeilenga committed
709
710
				idl_free( tmp );
				idl_free( tmp2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
711
712
				cont_free( &k3 );
				cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
713
714
715
716
717
718
719
				idl_free( idl );
				return( 0 );

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

720
			idl_free( tmp2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
721
			cont_free( &k3 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
722
723
		}

724
split:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
725
726
727
728
729
		/*
		 * must split the block, write both new blocks + update
		 * and write the indirect header block.
		 */

730
731
732
733
734
735
		rc = 0;	/* optimistic */


#ifndef USE_INDIRECT_NIDS
		/* 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
736
			;	/* NULL */
737
738
739
#else
		j = ID_BLOCK_NIDS(idl);
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
740
741
742
743
744
745

		/* 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
746
			 * by a single "all-id" block.	our job: delete
Kurt Zeilenga's avatar
Kurt Zeilenga committed
747
748
749
750
751
			 * all the indirect blocks, and replace the header
			 * block by an all-id block.
			 */

			/* delete all indirect blocks */
752
753
754
755
756
757
#ifndef USE_INDIRECT_NIDS
			for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
#else
			for ( j = 0; (unsigned long) j < ID_BLOCK_NIDS(idl); j++ ) {
#endif
				cont_id( &k2, ID_BLOCK_ID(idl, j) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
758
759
760
761
762
763
764
765
766

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

767
			cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
768
769
770
771
772
773
774
775
776
			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 */
777
778
779
780
781
782
		tmp = idl_alloc( ID_BLOCK_NMAXN(idl) + 1 );
#ifndef USE_INDIRECT_NIDS
		ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
#else
		ID_BLOCK_NMAX(tmp) |= ID_BLOCK_INDIRECT_VALUE;
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
783
		/* everything up to the split block */
784
785
786
		AC_MEMCPY(
			(char *) &ID_BLOCK_ID(tmp, 0),
			(char *) &ID_BLOCK_ID(idl, 0),
Kurt Zeilenga's avatar
Kurt Zeilenga committed
787
788
		    i * sizeof(ID) );
		/* the two new blocks */
789
790
		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
791
		/* everything after the split block */
792
793
794
795
796
797
798
799
800
801
802
803
#ifndef USE_INDIRECT_NIDS
		AC_MEMCPY(
			(char *) &ID_BLOCK_ID(tmp, i + 2),
			(char *) &ID_BLOCK_ID(idl, i + 1),
			(ID_BLOCK_NMAXN(idl) - i - 1) * sizeof(ID) );
#else
		AC_MEMCPY(
			(char *) &ID_BLOCK_ID(tmp, i + 2),
			(char *) &ID_BLOCK_ID(idl, i + 1),
			(ID_BLOCK_NIDS(idl) - i - 1) * sizeof(ID) );
		ID_BLOCK_NIDS(tmp) = ID_BLOCK_NIDS(idl) + 1;
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
804
805
806
807
808

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

		/* store the first id block */
809
		cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
810
811
812
		rc = idl_store( be, db, k2, tmp2 );

		/* store the second id block */
813
		cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
814
815
816
817
818
819
820
		rc = idl_store( be, db, k2, tmp3 );

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

821
	cont_free( &k2 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
822
823
824
825
826
	idl_free( tmp );
	idl_free( idl );
	return( rc );
}

827

Kurt Zeilenga's avatar
Kurt Zeilenga committed
828
829
/*
 * idl_insert - insert an id into an id list.
830
831
832
 *
 *	returns
 *		0	id inserted
Kurt Zeilenga's avatar
Kurt Zeilenga committed
833
834
835
836
837
 *		1	id inserted, first id in block has changed
 *		2	id not inserted, already there
 *		3	id not inserted, block must be split
 */
int
838
idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
839
{
840
	unsigned int	i;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
841

842
	if ( ID_BLOCK_ALLIDS( *idl ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
843
844
845
		return( 2 );	/* already there */
	}

846
847
848
	/* is it already there? */
	i = idl_find(*idl, id);
	if ( ID_BLOCK_ID(*idl, i) == id ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
849
850
		return( 2 );	/* already there */
	}
851
852
	if ( ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) < id )
		i++;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
853
854

	/* do we need to make room for it? */
855
	if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAXN(*idl) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
856
		/* make room or indicate block needs splitting */
857
		if ( ID_BLOCK_NMAXN(*idl) >= maxids ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
858
859
860
			return( 3 );	/* block needs splitting */
		}

861
862
863
		ID_BLOCK_NMAX(*idl) *= 2;
		if ( ID_BLOCK_NMAXN(*idl) > maxids ) {
			ID_BLOCK_NMAX(*idl) = maxids;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
864
		}
865
866
		*idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
		    (ID_BLOCK_NMAXN(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
867
868
869
	}

	/* make a slot for the new id */
870
871
872
873
874
875
876
877
878
879
880
881
882
	AC_MEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
		    (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );

	ID_BLOCK_ID(*idl, i) = id;
	ID_BLOCK_NIDS(*idl)++;
	(void) memset(
		(char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
		'\0',
	    (ID_BLOCK_NMAXN(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );

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

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

887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955

int
idl_delete_key (
	Backend		*be,
	DBCache	 *db,
	Datum		key,
	ID		id
)
{
	Datum  data;
	ID_BLOCK *idl;
	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 ) ) {
		i = idl_find(idl, id);
		if ( ID_BLOCK_ID(idl, i) == id ) {
			if( --ID_BLOCK_NIDS(idl) == 0 ) {
				ldbm_cache_delete( db, key );

			} else {
				AC_MEMCPY(
					&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
	   */
	cont_alloc( &data, &key );
#ifndef USE_INDIRECT_NIDS
	for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
		;	/* NULL */

	for ( j = 0; j<nids; j++ ) 
#else
	nids = ID_BLOCK_NIDS(idl);
	for ( j = idl_find(idl, id); j >= 0; j = -1)	/* execute once */
#endif
	{
		ID_BLOCK *tmp;
		cont_id( &data, ID_BLOCK_ID(idl, j) );

		if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
#ifdef NEW_LOGGING
Kurt Zeilenga's avatar
Kurt Zeilenga committed
956
957
			LDAP_LOG( INDEX, INFO,
				   "idl_delete_key: idl_fetch_one returned NULL\n", 0, 0, 0 );
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#else
			Debug( LDAP_DEBUG_ANY,
			    "idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
#endif

			continue;
		}
		/*
		   Now try to find the ID in tmp
		*/

		i = idl_find(tmp, id);
		if ( ID_BLOCK_ID(tmp, i) == id )
		{
			AC_MEMCPY(
				&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 );
				AC_MEMCPY(
					&ID_BLOCK_ID(idl, j),
					&ID_BLOCK_ID(idl, j+1),
					(nids-(j+1)) * sizeof(ID));
				ID_BLOCK_ID(idl, nids-1) = NOID;
				nids--;
#ifdef USE_INDIRECT_NIDS
				ID_BLOCK_NIDS(idl)--;
#endif
				if ( ! nids )
					ldbm_cache_delete( db, key );
				else
					idl_store( be, db, key, idl );
			}
			idl_free( tmp );
			cont_free( &data );
			idl_free( idl );
For faster browsing, not all history is shown. View entire blame