1 /* idl.c - ldap id list handling routines */
10 #include "ldapconfig.h"
12 #include "back-ldbm.h"
19 /* nmax + nids + space for the ids */
20 new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
28 idl_allids( Backend *be )
33 idl->b_nmax = ALLIDSBLOCK;
34 idl->b_nids = next_id_get( be );
40 idl_free( IDList *idl )
59 #ifdef HAVE_BERKELEY_DB2
61 memset( &k2, 0, sizeof( k2 ) );
62 memset( &data, 0, sizeof( data ) );
65 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
67 data = ldbm_cache_fetch( db, key );
69 idl = (IDList *) data.dptr;
87 #ifdef HAVE_BERKELEY_DB2
88 memset( &k2, 0, sizeof( k2 ) );
89 memset( &data, 0, sizeof( data ) );
92 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
94 data = ldbm_cache_fetch( db, key );
96 if ( (idl = (IDList *) data.dptr) == NULL ) {
101 if ( ! INDIRECT_BLOCK( idl ) ) {
103 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
104 idl->b_nids, idl->b_nmax, 0 );
107 /* make sure we have the current value of highest id */
108 if ( idl->b_nmax == ALLIDSBLOCK ) {
110 idl = idl_allids( be );
116 * this is an indirect block which points to other blocks.
117 * we need to read in all the blocks it points to and construct
118 * a big id list containing all the ids, which we will return.
121 /* count the number of blocks & allocate space for pointers to them */
122 for ( i = 0; idl->b_ids[i] != NOID; i++ )
124 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
126 /* read in all the blocks */
127 kstr = (char *) ch_malloc( key.dsize + 20 );
129 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
130 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
132 k2.dsize = strlen( kstr ) + 1;
134 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
135 Debug( LDAP_DEBUG_ANY,
136 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
140 nids += tmp[i]->b_nids;
145 /* allocate space for the big block */
146 idl = idl_alloc( nids );
150 /* copy in all the ids from the component blocks */
151 for ( i = 0; tmp[i] != NULL; i++ ) {
152 if ( tmp[i] == NULL ) {
156 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
157 tmp[i]->b_nids * sizeof(ID) );
158 nids += tmp[i]->b_nids;
162 free( (char *) tmp );
164 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %lu ids (%lu max)\n",
165 idl->b_nids, idl->b_nmax, 0 );
179 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
181 #ifdef HAVE_BERKELEY_DB2
182 memset( &data, 0, sizeof( data ) );
185 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
187 data.dptr = (char *) idl;
188 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
191 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
195 flags = LDBM_REPLACE;
196 if( li->li_dbcachewsync ) flags |= LDBM_SYNC;
197 rc = ldbm_cache_store( db, key, data, flags );
199 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
213 /* find where to split the block */
214 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
217 *n1 = idl_alloc( i == 0 ? 1 : i );
218 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
221 * everything before the id being inserted in the first block
222 * unless there is nothing, in which case the id being inserted
225 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
227 (*n1)->b_nids = (i == 0 ? 1 : i);
230 (*n1)->b_ids[0] = id;
232 (*n2)->b_ids[0] = id;
235 /* the id being inserted & everything after in the second block */
236 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
237 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
238 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
242 * idl_change_first - called when an indirect block's first key has
243 * changed, meaning it needs to be stored under a new key, and the
244 * header block pointing to it needs updating.
251 Datum hkey, /* header block key */
252 IDList *h, /* header block */
253 int pos, /* pos in h to update */
254 Datum bkey, /* data block key */
255 IDList *b /* data block */
260 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
262 /* delete old key block */
263 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
264 Debug( LDAP_DEBUG_ANY,
265 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
270 /* write block with new key */
271 sprintf( bkey.dptr, "%c%s%ld", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
272 bkey.dsize = strlen( bkey.dptr ) + 1;
273 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
274 Debug( LDAP_DEBUG_ANY,
275 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
279 /* update + write indirect header block */
280 h->b_ids[pos] = b->b_ids[0];
281 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
282 Debug( LDAP_DEBUG_ANY,
283 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
299 IDList *idl, *tmp, *tmp2, *tmp3;
303 #ifdef HAVE_BERKELEY_DB2
304 memset( &k2, 0, sizeof( k2 ) );
307 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
309 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
313 idl = idl_alloc( 1 );
314 idl->b_ids[idl->b_nids++] = id;
315 rc = idl_store( be, db, key, idl );
322 if ( ! INDIRECT_BLOCK( idl ) ) {
323 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
324 case 0: /* id inserted - store the updated block */
326 rc = idl_store( be, db, key, idl );
329 case 2: /* id already there - nothing to do */
333 case 3: /* id not inserted - block must be split */
334 /* check threshold for marking this an all-id block */
335 if ( db->dbc_maxindirect < 2 ) {
337 idl = idl_allids( be );
338 rc = idl_store( be, db, key, idl );
344 idl_split_block( idl, id, &tmp, &tmp2 );
347 /* create the header indirect block */
348 idl = idl_alloc( 3 );
350 idl->b_nids = INDBLOCK;
351 idl->b_ids[0] = tmp->b_ids[0];
352 idl->b_ids[1] = tmp2->b_ids[0];
353 idl->b_ids[2] = NOID;
356 rc = idl_store( be, db, key, idl );
358 /* store the first id block */
359 kstr = (char *) ch_malloc( key.dsize + 20 );
360 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
363 k2.dsize = strlen( kstr ) + 1;
364 rc = idl_store( be, db, k2, tmp );
366 /* store the second id block */
367 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
370 k2.dsize = strlen( kstr ) + 1;
371 rc = idl_store( be, db, k2, tmp2 );
384 * this is an indirect block which points to other blocks.
385 * we need to read in the block into which the id should be
386 * inserted, then insert the id and store the block. we might
387 * have to split the block if it is full, which means we also
388 * need to write a new "header" block.
391 /* select the block to try inserting into */
392 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
402 kstr = (char *) ch_malloc( key.dsize + 20 );
403 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
405 k2.dsize = strlen( kstr ) + 1;
406 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
407 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
413 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
414 case 0: /* id inserted ok */
415 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
416 Debug( LDAP_DEBUG_ANY,
417 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
421 case 1: /* id inserted - first id in block has changed */
423 * key for this block has changed, so we have to
424 * write the block under the new key, delete the
425 * old key block + update and write the indirect
429 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
432 case 2: /* id not inserted - already there */
435 case 3: /* id not inserted - block is full */
437 * first, see if it will fit in the next block,
438 * without splitting, unless we're trying to insert
439 * into the beginning of the first block.
442 /* is there a next block? */
443 if ( !first && idl->b_ids[i + 1] != NOID ) {
445 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
448 k2.dsize = strlen( kstr ) + 1;
449 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
450 Debug( LDAP_DEBUG_ANY,
451 "idl_fetch_one (%s) returns NULL\n",
456 switch ( (rc = idl_insert( &tmp2, id,
457 db->dbc_maxids )) ) {
458 case 1: /* id inserted first in block */
459 rc = idl_change_first( be, db, key, idl,
463 case 2: /* id already there - how? */
464 case 0: /* id inserted */
466 Debug( LDAP_DEBUG_ANY,
467 "id %lu already in next block\n",
476 case 3: /* split the original block */
484 * must split the block, write both new blocks + update
485 * and write the indirect header block.
488 /* count how many indirect blocks */
489 for ( j = 0; idl->b_ids[j] != NOID; j++ )
492 /* check it against all-id thresholed */
493 if ( j + 1 > db->dbc_maxindirect ) {
495 * we've passed the all-id threshold, meaning
496 * that this set of blocks should be replaced
497 * by a single "all-id" block. our job: delete
498 * all the indirect blocks, and replace the header
499 * block by an all-id block.
502 /* delete all indirect blocks */
503 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
504 sprintf( kstr,"%c%s%ld", CONT_PREFIX, key.dptr,
507 k2.dsize = strlen( kstr ) + 1;
509 rc = ldbm_cache_delete( db, k2 );
512 /* store allid block in place of header block */
514 idl = idl_allids( be );
515 rc = idl_store( be, db, key, idl );
523 idl_split_block( tmp, id, &tmp2, &tmp3 );
526 /* create a new updated indirect header block */
527 tmp = idl_alloc( idl->b_nmax + 1 );
528 tmp->b_nids = INDBLOCK;
529 /* everything up to the split block */
530 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
532 /* the two new blocks */
533 tmp->b_ids[i] = tmp2->b_ids[0];
534 tmp->b_ids[i + 1] = tmp3->b_ids[0];
535 /* everything after the split block */
536 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
537 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
539 /* store the header block */
540 rc = idl_store( be, db, key, tmp );
542 /* store the first id block */
543 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
546 k2.dsize = strlen( kstr ) + 1;
547 rc = idl_store( be, db, k2, tmp2 );
549 /* store the second id block */
550 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
553 k2.dsize = strlen( kstr ) + 1;
554 rc = idl_store( be, db, k2, tmp3 );
568 * idl_insert - insert an id into an id list.
569 * returns 0 id inserted
570 * 1 id inserted, first id in block has changed
571 * 2 id not inserted, already there
572 * 3 id not inserted, block must be split
576 idl_insert( IDList **idl, ID id, int maxids )
580 if ( ALLIDS( *idl ) ) {
581 return( 2 ); /* already there */
584 /* is it already there? XXX bin search XXX */
585 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
588 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
589 return( 2 ); /* already there */
592 /* do we need to make room for it? */
593 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
594 /* make room or indicate block needs splitting */
595 if ( (*idl)->b_nmax == maxids ) {
596 return( 3 ); /* block needs splitting */
600 if ( (*idl)->b_nmax > maxids ) {
601 (*idl)->b_nmax = maxids;
603 *idl = (IDList *) ch_realloc( (char *) *idl,
604 ((*idl)->b_nmax + 2) * sizeof(ID) );
607 /* make a slot for the new id */
608 for ( j = (*idl)->b_nids; j != i; j-- ) {
609 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
611 (*idl)->b_ids[i] = id;
613 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
614 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
616 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
633 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
635 /* It wasn't found. Hmm... */
639 if ( ! INDIRECT_BLOCK( idl ) )
641 for ( i=0; i < idl->b_nids; i++ )
643 if ( idl->b_ids[i] == id )
645 memcpy ( &idl->b_ids[i], &idl->b_ids[i+1], sizeof(ID)*(idl->b_nids-(i+1)));
646 idl->b_ids[idl->b_nids-1] = NOID;
649 idl_store( be, db, key, idl );
651 ldbm_cache_delete( db, key );
654 /* We didn't find the ID. Hmmm... */
659 /* We have to go through an indirect block and find the ID
662 for ( nids = 0; idl->b_ids[nids] != NOID; nids++ )
664 kstr = (char *) ch_malloc( key.dsize + 20 );
665 for ( j = 0; idl->b_ids[j] != NOID; j++ )
667 memset( &k2, 0, sizeof(k2) );
668 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[j] );
670 k2.dsize = strlen( kstr ) + 1;
672 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
673 Debug( LDAP_DEBUG_ANY,
674 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
678 Now try to find the ID in tmp
680 for ( i=0; i < tmp->b_nids; i++ )
682 if ( tmp->b_ids[i] == id )
684 memcpy ( &tmp->b_ids[i], &tmp->b_ids[i+1], sizeof(ID)*(tmp->b_nids-(i+1)));
685 tmp->b_ids[tmp->b_nids-1] = NOID;
688 idl_store ( be, db, k2, tmp );
691 ldbm_cache_delete( db, k2 );
692 memcpy ( &idl->b_ids[j], &idl->b_ids[j+1], sizeof(ID)*(nids-(j+1)));
693 idl->b_ids[nids-1] = NOID;
696 ldbm_cache_delete( db, key );
698 idl_store( be, db, key, idl );
708 idl_dup( IDList *idl )
716 new = idl_alloc( idl->b_nmax );
717 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
724 idl_min( IDList *a, IDList *b )
726 return( a->b_nids > b->b_nids ? b : a );
730 * idl_intersection - return a intersection b
740 unsigned int ai, bi, ni;
743 if ( a == NULL || b == NULL ) {
747 return( idl_dup( b ) );
750 return( idl_dup( a ) );
753 n = idl_dup( idl_min( a, b ) );
755 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
756 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
759 if ( bi == b->b_nids ) {
763 if ( b->b_ids[bi] == a->b_ids[ai] ) {
764 n->b_ids[ni++] = a->b_ids[ai];
778 * idl_union - return a union b
788 unsigned int ai, bi, ni;
792 return( idl_dup( b ) );
795 return( idl_dup( a ) );
797 if ( ALLIDS( a ) || ALLIDS( b ) ) {
798 return( idl_allids( be ) );
801 if ( b->b_nids < a->b_nids ) {
807 n = idl_alloc( a->b_nids + b->b_nids );
809 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
810 if ( a->b_ids[ai] < b->b_ids[bi] ) {
811 n->b_ids[ni++] = a->b_ids[ai++];
812 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
813 n->b_ids[ni++] = b->b_ids[bi++];
815 n->b_ids[ni++] = a->b_ids[ai];
820 for ( ; ai < a->b_nids; ai++ ) {
821 n->b_ids[ni++] = a->b_ids[ai];
823 for ( ; bi < b->b_nids; bi++ ) {
824 n->b_ids[ni++] = b->b_ids[bi];
832 * idl_notin - return a intersection ~b (or a minus b)
842 unsigned int ni, ai, bi;
848 if ( b == NULL || ALLIDS( b )) {
849 return( idl_dup( a ) );
853 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
856 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
857 bi < b->b_nmax; ai++ ) {
858 if ( b->b_ids[bi] == ai ) {
865 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
869 if ( ni == n->b_nmax ) {
871 return( idl_allids( be ) );
881 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
882 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
887 if ( bi == b->b_nids ) {
891 if ( b->b_ids[bi] != a->b_ids[ai] ) {
892 n->b_ids[ni++] = a->b_ids[ai];
896 for ( ; ai < a->b_nids; ai++ ) {
897 n->b_ids[ni++] = a->b_ids[ai];
905 idl_firstid( IDList *idl )
907 if ( idl == NULL || idl->b_nids == 0 ) {
911 if ( ALLIDS( idl ) ) {
912 return( idl->b_nids == 1 ? NOID : 1 );
915 return( idl->b_ids[0] );
919 idl_nextid( IDList *idl, ID id )
923 if ( ALLIDS( idl ) ) {
924 return( ++id < idl->b_nids ? id : NOID );
927 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
932 if ( i >= idl->b_nids ) {
935 return( idl->b_ids[i] );