1 /* idl.c - ldap id list handling routines */
11 #include "ldapconfig.h"
12 #include "back-ldbm.h"
14 extern Datum ldbm_cache_fetch();
21 /* nmax + nids + space for the ids */
22 new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
30 idl_allids( Backend *be )
35 idl->b_nmax = ALLIDSBLOCK;
36 idl->b_nids = next_id_get( be );
42 idl_free( IDList *idl )
64 #ifdef HAVE_BERKELEY_DB2
65 memset( &k2, 0, sizeof( k2 ) );
66 memset( &data, 0, sizeof( data ) );
69 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
71 data = ldbm_cache_fetch( db, key );
73 idl = (IDList *) data.dptr;
91 #ifdef HAVE_BERKELEY_DB2
92 memset( &k2, 0, sizeof( k2 ) );
93 memset( &data, 0, sizeof( data ) );
96 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
98 data = ldbm_cache_fetch( db, key );
100 if ( (idl = (IDList *) data.dptr) == NULL ) {
105 if ( ! INDIRECT_BLOCK( idl ) ) {
107 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
108 idl->b_nids, idl->b_nmax, 0 );
111 /* make sure we have the current value of highest id */
112 if ( idl->b_nmax == ALLIDSBLOCK ) {
114 idl = idl_allids( be );
120 * this is an indirect block which points to other blocks.
121 * we need to read in all the blocks it points to and construct
122 * a big id list containing all the ids, which we will return.
125 /* count the number of blocks & allocate space for pointers to them */
126 for ( i = 0; idl->b_ids[i] != NOID; i++ )
128 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
130 /* read in all the blocks */
131 kstr = (char *) ch_malloc( key.dsize + 20 );
133 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
134 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
136 k2.dsize = strlen( kstr ) + 1;
138 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
139 Debug( LDAP_DEBUG_ANY,
140 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
144 nids += tmp[i]->b_nids;
149 /* allocate space for the big block */
150 idl = idl_alloc( nids );
154 /* copy in all the ids from the component blocks */
155 for ( i = 0; tmp[i] != NULL; i++ ) {
156 if ( tmp[i] == NULL ) {
160 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
161 tmp[i]->b_nids * sizeof(ID) );
162 nids += tmp[i]->b_nids;
166 free( (char *) tmp );
168 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
183 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
185 #ifdef HAVE_BERKELEY_DB2
186 memset( &data, 0, sizeof( data ) );
189 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
191 data.dptr = (char *) idl;
192 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
195 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
199 flags = LDBM_REPLACE;
200 if( li->li_flush_wrt ) flags |= LDBM_SYNC;
201 rc = ldbm_cache_store( db, key, data, flags );
203 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
217 /* find where to split the block */
218 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
221 *n1 = idl_alloc( i == 0 ? 1 : i );
222 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
225 * everything before the id being inserted in the first block
226 * unless there is nothing, in which case the id being inserted
229 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
231 (*n1)->b_nids = (i == 0 ? 1 : i);
234 (*n1)->b_ids[0] = id;
236 (*n2)->b_ids[0] = id;
239 /* the id being inserted & everything after in the second block */
240 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
241 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
242 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
246 * idl_change_first - called when an indirect block's first key has
247 * changed, meaning it needs to be stored under a new key, and the
248 * header block pointing to it needs updating.
255 Datum hkey, /* header block key */
256 IDList *h, /* header block */
257 int pos, /* pos in h to update */
258 Datum bkey, /* data block key */
259 IDList *b /* data block */
264 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
266 /* delete old key block */
267 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
268 Debug( LDAP_DEBUG_ANY,
269 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
274 /* write block with new key */
275 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
276 bkey.dsize = strlen( bkey.dptr ) + 1;
277 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
278 Debug( LDAP_DEBUG_ANY,
279 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
283 /* update + write indirect header block */
284 h->b_ids[pos] = b->b_ids[0];
285 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
286 Debug( LDAP_DEBUG_ANY,
287 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
303 IDList *idl, *tmp, *tmp2, *tmp3;
307 #ifdef HAVE_BERKELEY_DB2
308 memset( &k2, 0, sizeof( k2 ) );
311 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
313 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
317 idl = idl_alloc( 1 );
318 idl->b_ids[idl->b_nids++] = id;
319 rc = idl_store( be, db, key, idl );
326 if ( ! INDIRECT_BLOCK( idl ) ) {
327 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
328 case 0: /* id inserted - store the updated block */
330 rc = idl_store( be, db, key, idl );
333 case 2: /* id already there - nothing to do */
337 case 3: /* id not inserted - block must be split */
338 /* check threshold for marking this an all-id block */
339 if ( db->dbc_maxindirect < 2 ) {
341 idl = idl_allids( be );
342 rc = idl_store( be, db, key, idl );
348 idl_split_block( idl, id, &tmp, &tmp2 );
351 /* create the header indirect block */
352 idl = idl_alloc( 3 );
354 idl->b_nids = INDBLOCK;
355 idl->b_ids[0] = tmp->b_ids[0];
356 idl->b_ids[1] = tmp2->b_ids[0];
357 idl->b_ids[2] = NOID;
360 rc = idl_store( be, db, key, idl );
362 /* store the first id block */
363 kstr = (char *) ch_malloc( key.dsize + 20 );
364 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
367 k2.dsize = strlen( kstr ) + 1;
368 rc = idl_store( be, db, k2, tmp );
370 /* store the second id block */
371 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
374 k2.dsize = strlen( kstr ) + 1;
375 rc = idl_store( be, db, k2, tmp2 );
388 * this is an indirect block which points to other blocks.
389 * we need to read in the block into which the id should be
390 * inserted, then insert the id and store the block. we might
391 * have to split the block if it is full, which means we also
392 * need to write a new "header" block.
395 /* select the block to try inserting into */
396 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
406 kstr = (char *) ch_malloc( key.dsize + 20 );
407 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
409 k2.dsize = strlen( kstr ) + 1;
410 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
411 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
417 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
418 case 0: /* id inserted ok */
419 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
420 Debug( LDAP_DEBUG_ANY,
421 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
425 case 1: /* id inserted - first id in block has changed */
427 * key for this block has changed, so we have to
428 * write the block under the new key, delete the
429 * old key block + update and write the indirect
433 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
436 case 2: /* id not inserted - already there */
439 case 3: /* id not inserted - block is full */
441 * first, see if it will fit in the next block,
442 * without splitting, unless we're trying to insert
443 * into the beginning of the first block.
446 /* is there a next block? */
447 if ( !first && idl->b_ids[i + 1] != NOID ) {
449 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
452 k2.dsize = strlen( kstr ) + 1;
453 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
454 Debug( LDAP_DEBUG_ANY,
455 "idl_fetch_one (%s) returns NULL\n",
460 switch ( (rc = idl_insert( &tmp2, id,
461 db->dbc_maxids )) ) {
462 case 1: /* id inserted first in block */
463 rc = idl_change_first( be, db, key, idl,
467 case 2: /* id already there - how? */
468 case 0: /* id inserted */
470 Debug( LDAP_DEBUG_ANY,
471 "id %d already in next block\n",
480 case 3: /* split the original block */
488 * must split the block, write both new blocks + update
489 * and write the indirect header block.
492 /* count how many indirect blocks */
493 for ( j = 0; idl->b_ids[j] != NOID; j++ )
496 /* check it against all-id thresholed */
497 if ( j + 1 > db->dbc_maxindirect ) {
499 * we've passed the all-id threshold, meaning
500 * that this set of blocks should be replaced
501 * by a single "all-id" block. our job: delete
502 * all the indirect blocks, and replace the header
503 * block by an all-id block.
506 /* delete all indirect blocks */
507 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
508 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
511 k2.dsize = strlen( kstr ) + 1;
513 rc = ldbm_cache_delete( db, k2 );
516 /* store allid block in place of header block */
518 idl = idl_allids( be );
519 rc = idl_store( be, db, key, idl );
527 idl_split_block( tmp, id, &tmp2, &tmp3 );
530 /* create a new updated indirect header block */
531 tmp = idl_alloc( idl->b_nmax + 1 );
532 tmp->b_nids = INDBLOCK;
533 /* everything up to the split block */
534 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
536 /* the two new blocks */
537 tmp->b_ids[i] = tmp2->b_ids[0];
538 tmp->b_ids[i + 1] = tmp3->b_ids[0];
539 /* everything after the split block */
540 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
541 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
543 /* store the header block */
544 rc = idl_store( be, db, key, tmp );
546 /* store the first id block */
547 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
550 k2.dsize = strlen( kstr ) + 1;
551 rc = idl_store( be, db, k2, tmp2 );
553 /* store the second id block */
554 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
557 k2.dsize = strlen( kstr ) + 1;
558 rc = idl_store( be, db, k2, tmp3 );
572 * idl_insert - insert an id into an id list.
573 * returns 0 id inserted
574 * 1 id inserted, first id in block has changed
575 * 2 id not inserted, already there
576 * 3 id not inserted, block must be split
580 idl_insert( IDList **idl, ID id, int maxids )
584 if ( ALLIDS( *idl ) ) {
585 return( 2 ); /* already there */
588 /* is it already there? XXX bin search XXX */
589 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
592 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
593 return( 2 ); /* already there */
596 /* do we need to make room for it? */
597 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
598 /* make room or indicate block needs splitting */
599 if ( (*idl)->b_nmax == maxids ) {
600 return( 3 ); /* block needs splitting */
604 if ( (*idl)->b_nmax > maxids ) {
605 (*idl)->b_nmax = maxids;
607 *idl = (IDList *) ch_realloc( (char *) *idl,
608 ((*idl)->b_nmax + 2) * sizeof(ID) );
611 /* make a slot for the new id */
612 for ( j = (*idl)->b_nids; j != i; j-- ) {
613 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
615 (*idl)->b_ids[i] = id;
617 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
618 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
620 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
624 idl_dup( IDList *idl )
632 new = idl_alloc( idl->b_nmax );
633 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
640 idl_min( IDList *a, IDList *b )
642 return( a->b_nids > b->b_nids ? b : a );
646 * idl_intersection - return a intersection b
659 if ( a == NULL || b == NULL ) {
663 return( idl_dup( b ) );
666 return( idl_dup( a ) );
669 n = idl_dup( idl_min( a, b ) );
671 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
672 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
675 if ( bi == b->b_nids ) {
679 if ( b->b_ids[bi] == a->b_ids[ai] ) {
680 n->b_ids[ni++] = a->b_ids[ai];
694 * idl_union - return a union b
708 return( idl_dup( b ) );
711 return( idl_dup( a ) );
713 if ( ALLIDS( a ) || ALLIDS( b ) ) {
714 return( idl_allids( be ) );
717 if ( b->b_nids < a->b_nids ) {
723 n = idl_alloc( a->b_nids + b->b_nids );
725 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
726 if ( a->b_ids[ai] < b->b_ids[bi] ) {
727 n->b_ids[ni++] = a->b_ids[ai++];
728 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
729 n->b_ids[ni++] = b->b_ids[bi++];
731 n->b_ids[ni++] = a->b_ids[ai];
736 for ( ; ai < a->b_nids; ai++ ) {
737 n->b_ids[ni++] = a->b_ids[ai];
739 for ( ; bi < b->b_nids; bi++ ) {
740 n->b_ids[ni++] = b->b_ids[bi];
748 * idl_notin - return a intersection ~b (or a minus b)
764 if ( b == NULL || ALLIDS( b )) {
765 return( idl_dup( a ) );
769 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
772 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
773 bi < b->b_nmax; ai++ ) {
774 if ( b->b_ids[bi] == ai ) {
781 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
785 if ( ni == n->b_nmax ) {
787 return( idl_allids( be ) );
797 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
798 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
803 if ( bi == b->b_nids ) {
807 if ( b->b_ids[bi] != a->b_ids[ai] ) {
808 n->b_ids[ni++] = a->b_ids[ai];
812 for ( ; ai < a->b_nids; ai++ ) {
813 n->b_ids[ni++] = a->b_ids[ai];
821 idl_firstid( IDList *idl )
823 if ( idl == NULL || idl->b_nids == 0 ) {
827 if ( ALLIDS( idl ) ) {
828 return( idl->b_nids == 1 ? NOID : 1 );
831 return( idl->b_ids[0] );
835 idl_nextid( IDList *idl, ID id )
839 if ( ALLIDS( idl ) ) {
840 return( ++id < idl->b_nids ? id : NOID );
843 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
848 if ( i >= idl->b_nids ) {
851 return( idl->b_ids[i] );