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 )
61 #ifdef HAVE_BERKELEY_DB2
63 memset( &k2, 0, sizeof( k2 ) );
64 memset( &data, 0, sizeof( data ) );
67 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
69 data = ldbm_cache_fetch( db, key );
71 idl = (IDList *) data.dptr;
89 #ifdef HAVE_BERKELEY_DB2
90 memset( &k2, 0, sizeof( k2 ) );
91 memset( &data, 0, sizeof( data ) );
94 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
96 data = ldbm_cache_fetch( db, key );
98 if ( (idl = (IDList *) data.dptr) == NULL ) {
103 if ( ! INDIRECT_BLOCK( idl ) ) {
105 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
106 idl->b_nids, idl->b_nmax, 0 );
109 /* make sure we have the current value of highest id */
110 if ( idl->b_nmax == ALLIDSBLOCK ) {
112 idl = idl_allids( be );
118 * this is an indirect block which points to other blocks.
119 * we need to read in all the blocks it points to and construct
120 * a big id list containing all the ids, which we will return.
123 /* count the number of blocks & allocate space for pointers to them */
124 for ( i = 0; idl->b_ids[i] != NOID; i++ )
126 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
128 /* read in all the blocks */
129 kstr = (char *) ch_malloc( key.dsize + 20 );
131 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
132 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
134 k2.dsize = strlen( kstr ) + 1;
136 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
137 Debug( LDAP_DEBUG_ANY,
138 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
142 nids += tmp[i]->b_nids;
147 /* allocate space for the big block */
148 idl = idl_alloc( nids );
152 /* copy in all the ids from the component blocks */
153 for ( i = 0; tmp[i] != NULL; i++ ) {
154 if ( tmp[i] == NULL ) {
158 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
159 tmp[i]->b_nids * sizeof(ID) );
160 nids += tmp[i]->b_nids;
164 free( (char *) tmp );
166 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
181 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
183 #ifdef HAVE_BERKELEY_DB2
184 memset( &data, 0, sizeof( data ) );
187 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
189 data.dptr = (char *) idl;
190 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
193 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
197 flags = LDBM_REPLACE;
198 if( li->li_dbcachewsync ) flags |= LDBM_SYNC;
199 rc = ldbm_cache_store( db, key, data, flags );
201 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
215 /* find where to split the block */
216 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
219 *n1 = idl_alloc( i == 0 ? 1 : i );
220 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
223 * everything before the id being inserted in the first block
224 * unless there is nothing, in which case the id being inserted
227 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
229 (*n1)->b_nids = (i == 0 ? 1 : i);
232 (*n1)->b_ids[0] = id;
234 (*n2)->b_ids[0] = id;
237 /* the id being inserted & everything after in the second block */
238 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
239 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
240 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
244 * idl_change_first - called when an indirect block's first key has
245 * changed, meaning it needs to be stored under a new key, and the
246 * header block pointing to it needs updating.
253 Datum hkey, /* header block key */
254 IDList *h, /* header block */
255 int pos, /* pos in h to update */
256 Datum bkey, /* data block key */
257 IDList *b /* data block */
262 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
264 /* delete old key block */
265 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
266 Debug( LDAP_DEBUG_ANY,
267 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
272 /* write block with new key */
273 sprintf( bkey.dptr, "%c%s%ld", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
274 bkey.dsize = strlen( bkey.dptr ) + 1;
275 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
276 Debug( LDAP_DEBUG_ANY,
277 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
281 /* update + write indirect header block */
282 h->b_ids[pos] = b->b_ids[0];
283 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
284 Debug( LDAP_DEBUG_ANY,
285 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
301 IDList *idl, *tmp, *tmp2, *tmp3;
305 #ifdef HAVE_BERKELEY_DB2
306 memset( &k2, 0, sizeof( k2 ) );
309 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
311 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
315 idl = idl_alloc( 1 );
316 idl->b_ids[idl->b_nids++] = id;
317 rc = idl_store( be, db, key, idl );
324 if ( ! INDIRECT_BLOCK( idl ) ) {
325 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
326 case 0: /* id inserted - store the updated block */
328 rc = idl_store( be, db, key, idl );
331 case 2: /* id already there - nothing to do */
335 case 3: /* id not inserted - block must be split */
336 /* check threshold for marking this an all-id block */
337 if ( db->dbc_maxindirect < 2 ) {
339 idl = idl_allids( be );
340 rc = idl_store( be, db, key, idl );
346 idl_split_block( idl, id, &tmp, &tmp2 );
349 /* create the header indirect block */
350 idl = idl_alloc( 3 );
352 idl->b_nids = INDBLOCK;
353 idl->b_ids[0] = tmp->b_ids[0];
354 idl->b_ids[1] = tmp2->b_ids[0];
355 idl->b_ids[2] = NOID;
358 rc = idl_store( be, db, key, idl );
360 /* store the first id block */
361 kstr = (char *) ch_malloc( key.dsize + 20 );
362 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
365 k2.dsize = strlen( kstr ) + 1;
366 rc = idl_store( be, db, k2, tmp );
368 /* store the second id block */
369 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
372 k2.dsize = strlen( kstr ) + 1;
373 rc = idl_store( be, db, k2, tmp2 );
386 * this is an indirect block which points to other blocks.
387 * we need to read in the block into which the id should be
388 * inserted, then insert the id and store the block. we might
389 * have to split the block if it is full, which means we also
390 * need to write a new "header" block.
393 /* select the block to try inserting into */
394 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
404 kstr = (char *) ch_malloc( key.dsize + 20 );
405 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
407 k2.dsize = strlen( kstr ) + 1;
408 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
409 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
415 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
416 case 0: /* id inserted ok */
417 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
418 Debug( LDAP_DEBUG_ANY,
419 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
423 case 1: /* id inserted - first id in block has changed */
425 * key for this block has changed, so we have to
426 * write the block under the new key, delete the
427 * old key block + update and write the indirect
431 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
434 case 2: /* id not inserted - already there */
437 case 3: /* id not inserted - block is full */
439 * first, see if it will fit in the next block,
440 * without splitting, unless we're trying to insert
441 * into the beginning of the first block.
444 /* is there a next block? */
445 if ( !first && idl->b_ids[i + 1] != NOID ) {
447 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
450 k2.dsize = strlen( kstr ) + 1;
451 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
452 Debug( LDAP_DEBUG_ANY,
453 "idl_fetch_one (%s) returns NULL\n",
458 switch ( (rc = idl_insert( &tmp2, id,
459 db->dbc_maxids )) ) {
460 case 1: /* id inserted first in block */
461 rc = idl_change_first( be, db, key, idl,
465 case 2: /* id already there - how? */
466 case 0: /* id inserted */
468 Debug( LDAP_DEBUG_ANY,
469 "id %d already in next block\n",
478 case 3: /* split the original block */
486 * must split the block, write both new blocks + update
487 * and write the indirect header block.
490 /* count how many indirect blocks */
491 for ( j = 0; idl->b_ids[j] != NOID; j++ )
494 /* check it against all-id thresholed */
495 if ( j + 1 > db->dbc_maxindirect ) {
497 * we've passed the all-id threshold, meaning
498 * that this set of blocks should be replaced
499 * by a single "all-id" block. our job: delete
500 * all the indirect blocks, and replace the header
501 * block by an all-id block.
504 /* delete all indirect blocks */
505 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
506 sprintf( kstr,"%c%s%ld", CONT_PREFIX, key.dptr,
509 k2.dsize = strlen( kstr ) + 1;
511 rc = ldbm_cache_delete( db, k2 );
514 /* store allid block in place of header block */
516 idl = idl_allids( be );
517 rc = idl_store( be, db, key, idl );
525 idl_split_block( tmp, id, &tmp2, &tmp3 );
528 /* create a new updated indirect header block */
529 tmp = idl_alloc( idl->b_nmax + 1 );
530 tmp->b_nids = INDBLOCK;
531 /* everything up to the split block */
532 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
534 /* the two new blocks */
535 tmp->b_ids[i] = tmp2->b_ids[0];
536 tmp->b_ids[i + 1] = tmp3->b_ids[0];
537 /* everything after the split block */
538 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
539 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
541 /* store the header block */
542 rc = idl_store( be, db, key, tmp );
544 /* store the first id block */
545 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
548 k2.dsize = strlen( kstr ) + 1;
549 rc = idl_store( be, db, k2, tmp2 );
551 /* store the second id block */
552 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
555 k2.dsize = strlen( kstr ) + 1;
556 rc = idl_store( be, db, k2, tmp3 );
570 * idl_insert - insert an id into an id list.
571 * returns 0 id inserted
572 * 1 id inserted, first id in block has changed
573 * 2 id not inserted, already there
574 * 3 id not inserted, block must be split
578 idl_insert( IDList **idl, ID id, int maxids )
582 if ( ALLIDS( *idl ) ) {
583 return( 2 ); /* already there */
586 /* is it already there? XXX bin search XXX */
587 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
590 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
591 return( 2 ); /* already there */
594 /* do we need to make room for it? */
595 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
596 /* make room or indicate block needs splitting */
597 if ( (*idl)->b_nmax == maxids ) {
598 return( 3 ); /* block needs splitting */
602 if ( (*idl)->b_nmax > maxids ) {
603 (*idl)->b_nmax = maxids;
605 *idl = (IDList *) ch_realloc( (char *) *idl,
606 ((*idl)->b_nmax + 2) * sizeof(ID) );
609 /* make a slot for the new id */
610 for ( j = (*idl)->b_nids; j != i; j-- ) {
611 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
613 (*idl)->b_ids[i] = id;
615 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
616 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
618 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
634 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
636 /* It wasn't found. Hmm... */
640 if ( ! INDIRECT_BLOCK( idl ) )
642 for ( i=0; i < idl->b_nids; i++ )
644 if ( idl->b_ids[i] == id )
646 memcpy ( &idl->b_ids[i], &idl->b_ids[i+1], sizeof(ID)*(idl->b_nids-(i+1)));
647 idl->b_ids[idl->b_nids-1] = NOID;
650 idl_store( be, db, key, idl );
652 ldbm_cache_delete( db, key );
655 /* We didn't find the ID. Hmmm... */
660 /* We have to go through an indirect block and find the ID
663 for ( nids = 0; idl->b_ids[nids] != NOID; nids++ )
665 kstr = (char *) ch_malloc( key.dsize + 20 );
666 for ( j = 0; idl->b_ids[j] != NOID; j++ )
668 memset( &k2, 0, sizeof(k2) );
669 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[j] );
671 k2.dsize = strlen( kstr ) + 1;
673 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
674 Debug( LDAP_DEBUG_ANY,
675 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
679 Now try to find the ID in tmp
681 for ( i=0; i < tmp->b_nids; i++ )
683 if ( tmp->b_ids[i] == id )
685 memcpy ( &tmp->b_ids[i], &tmp->b_ids[i+1], sizeof(ID)*(tmp->b_nids-(i+1)));
686 tmp->b_ids[tmp->b_nids-1] = NOID;
689 idl_store ( be, db, k2, tmp );
692 ldbm_cache_delete( db, k2 );
693 memcpy ( &idl->b_ids[j], &idl->b_ids[j+1], sizeof(ID)*(nids-(j+1)));
694 idl->b_ids[nids-1] = NOID;
697 ldbm_cache_delete( db, key );
699 idl_store( be, db, key, idl );
709 idl_dup( IDList *idl )
717 new = idl_alloc( idl->b_nmax );
718 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
725 idl_min( IDList *a, IDList *b )
727 return( a->b_nids > b->b_nids ? b : a );
731 * idl_intersection - return a intersection b
741 unsigned int ai, bi, ni;
744 if ( a == NULL || b == NULL ) {
748 return( idl_dup( b ) );
751 return( idl_dup( a ) );
754 n = idl_dup( idl_min( a, b ) );
756 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
757 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
760 if ( bi == b->b_nids ) {
764 if ( b->b_ids[bi] == a->b_ids[ai] ) {
765 n->b_ids[ni++] = a->b_ids[ai];
779 * idl_union - return a union b
789 unsigned int ai, bi, ni;
793 return( idl_dup( b ) );
796 return( idl_dup( a ) );
798 if ( ALLIDS( a ) || ALLIDS( b ) ) {
799 return( idl_allids( be ) );
802 if ( b->b_nids < a->b_nids ) {
808 n = idl_alloc( a->b_nids + b->b_nids );
810 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
811 if ( a->b_ids[ai] < b->b_ids[bi] ) {
812 n->b_ids[ni++] = a->b_ids[ai++];
813 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
814 n->b_ids[ni++] = b->b_ids[bi++];
816 n->b_ids[ni++] = a->b_ids[ai];
821 for ( ; ai < a->b_nids; ai++ ) {
822 n->b_ids[ni++] = a->b_ids[ai];
824 for ( ; bi < b->b_nids; bi++ ) {
825 n->b_ids[ni++] = b->b_ids[bi];
833 * idl_notin - return a intersection ~b (or a minus b)
843 unsigned int ni, ai, bi;
849 if ( b == NULL || ALLIDS( b )) {
850 return( idl_dup( a ) );
854 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
857 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
858 bi < b->b_nmax; ai++ ) {
859 if ( b->b_ids[bi] == ai ) {
866 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
870 if ( ni == n->b_nmax ) {
872 return( idl_allids( be ) );
882 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
883 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
888 if ( bi == b->b_nids ) {
892 if ( b->b_ids[bi] != a->b_ids[ai] ) {
893 n->b_ids[ni++] = a->b_ids[ai];
897 for ( ; ai < a->b_nids; ai++ ) {
898 n->b_ids[ni++] = a->b_ids[ai];
906 idl_firstid( IDList *idl )
908 if ( idl == NULL || idl->b_nids == 0 ) {
912 if ( ALLIDS( idl ) ) {
913 return( idl->b_nids == 1 ? NOID : 1 );
916 return( idl->b_ids[0] );
920 idl_nextid( IDList *idl, ID id )
924 if ( ALLIDS( idl ) ) {
925 return( ++id < idl->b_nids ? id : NOID );
928 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
933 if ( i >= idl->b_nids ) {
936 return( idl->b_ids[i] );