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
62 ldbm_datum_init( data );
63 ldbm_datum_init( k2 );
66 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
68 data = ldbm_cache_fetch( db, key );
70 idl = (IDList *) data.dptr;
88 ldbm_datum_init( k2 );
89 ldbm_datum_init( data );
91 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
93 data = ldbm_cache_fetch( db, key );
95 if ( (idl = (IDList *) data.dptr) == NULL ) {
100 if ( ! INDIRECT_BLOCK( idl ) ) {
102 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
103 idl->b_nids, idl->b_nmax, 0 );
106 /* make sure we have the current value of highest id */
107 if ( idl->b_nmax == ALLIDSBLOCK ) {
109 idl = idl_allids( be );
115 * this is an indirect block which points to other blocks.
116 * we need to read in all the blocks it points to and construct
117 * a big id list containing all the ids, which we will return.
120 /* count the number of blocks & allocate space for pointers to them */
121 for ( i = 0; idl->b_ids[i] != NOID; i++ )
123 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
125 /* read in all the blocks */
126 kstr = (char *) ch_malloc( key.dsize + 20 );
128 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
129 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
131 k2.dsize = strlen( kstr ) + 1;
133 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
134 Debug( LDAP_DEBUG_ANY,
135 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
139 nids += tmp[i]->b_nids;
144 /* allocate space for the big block */
145 idl = idl_alloc( nids );
149 /* copy in all the ids from the component blocks */
150 for ( i = 0; tmp[i] != NULL; i++ ) {
151 if ( tmp[i] == NULL ) {
155 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
156 tmp[i]->b_nids * sizeof(ID) );
157 nids += tmp[i]->b_nids;
161 free( (char *) tmp );
163 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %lu ids (%lu max)\n",
164 idl->b_nids, idl->b_nmax, 0 );
178 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
180 ldbm_datum_init( data );
182 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
184 data.dptr = (char *) idl;
185 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
188 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
192 flags = LDBM_REPLACE;
193 if( li->li_dbcachewsync ) flags |= LDBM_SYNC;
194 rc = ldbm_cache_store( db, key, data, flags );
196 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
210 /* find where to split the block */
211 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
214 *n1 = idl_alloc( i == 0 ? 1 : i );
215 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
218 * everything before the id being inserted in the first block
219 * unless there is nothing, in which case the id being inserted
222 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
224 (*n1)->b_nids = (i == 0 ? 1 : i);
227 (*n1)->b_ids[0] = id;
229 (*n2)->b_ids[0] = id;
232 /* the id being inserted & everything after in the second block */
233 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
234 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
235 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
239 * idl_change_first - called when an indirect block's first key has
240 * changed, meaning it needs to be stored under a new key, and the
241 * header block pointing to it needs updating.
248 Datum hkey, /* header block key */
249 IDList *h, /* header block */
250 int pos, /* pos in h to update */
251 Datum bkey, /* data block key */
252 IDList *b /* data block */
257 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
259 /* delete old key block */
260 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
261 Debug( LDAP_DEBUG_ANY,
262 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
267 /* write block with new key */
268 sprintf( bkey.dptr, "%c%s%ld", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
269 bkey.dsize = strlen( bkey.dptr ) + 1;
270 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
271 Debug( LDAP_DEBUG_ANY,
272 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
276 /* update + write indirect header block */
277 h->b_ids[pos] = b->b_ids[0];
278 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
279 Debug( LDAP_DEBUG_ANY,
280 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
296 IDList *idl, *tmp, *tmp2, *tmp3;
300 ldbm_datum_init( k2 );
302 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
304 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
308 idl = idl_alloc( 1 );
309 idl->b_ids[idl->b_nids++] = id;
310 rc = idl_store( be, db, key, idl );
317 if ( ! INDIRECT_BLOCK( idl ) ) {
318 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
319 case 0: /* id inserted - store the updated block */
321 rc = idl_store( be, db, key, idl );
324 case 2: /* id already there - nothing to do */
328 case 3: /* id not inserted - block must be split */
329 /* check threshold for marking this an all-id block */
330 if ( db->dbc_maxindirect < 2 ) {
332 idl = idl_allids( be );
333 rc = idl_store( be, db, key, idl );
339 idl_split_block( idl, id, &tmp, &tmp2 );
342 /* create the header indirect block */
343 idl = idl_alloc( 3 );
345 idl->b_nids = INDBLOCK;
346 idl->b_ids[0] = tmp->b_ids[0];
347 idl->b_ids[1] = tmp2->b_ids[0];
348 idl->b_ids[2] = NOID;
351 rc = idl_store( be, db, key, idl );
353 /* store the first id block */
354 kstr = (char *) ch_malloc( key.dsize + 20 );
355 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
358 k2.dsize = strlen( kstr ) + 1;
359 rc = idl_store( be, db, k2, tmp );
361 /* store the second id block */
362 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
365 k2.dsize = strlen( kstr ) + 1;
366 rc = idl_store( be, db, k2, tmp2 );
379 * this is an indirect block which points to other blocks.
380 * we need to read in the block into which the id should be
381 * inserted, then insert the id and store the block. we might
382 * have to split the block if it is full, which means we also
383 * need to write a new "header" block.
386 /* select the block to try inserting into */
387 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
397 kstr = (char *) ch_malloc( key.dsize + 20 );
398 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
400 k2.dsize = strlen( kstr ) + 1;
401 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
402 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
408 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
409 case 0: /* id inserted ok */
410 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
411 Debug( LDAP_DEBUG_ANY,
412 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
416 case 1: /* id inserted - first id in block has changed */
418 * key for this block has changed, so we have to
419 * write the block under the new key, delete the
420 * old key block + update and write the indirect
424 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
427 case 2: /* id not inserted - already there */
430 case 3: /* id not inserted - block is full */
432 * first, see if it will fit in the next block,
433 * without splitting, unless we're trying to insert
434 * into the beginning of the first block.
437 /* is there a next block? */
438 if ( !first && idl->b_ids[i + 1] != NOID ) {
440 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
443 k2.dsize = strlen( kstr ) + 1;
444 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
445 Debug( LDAP_DEBUG_ANY,
446 "idl_fetch_one (%s) returns NULL\n",
451 switch ( (rc = idl_insert( &tmp2, id,
452 db->dbc_maxids )) ) {
453 case 1: /* id inserted first in block */
454 rc = idl_change_first( be, db, key, idl,
458 case 2: /* id already there - how? */
459 case 0: /* id inserted */
461 Debug( LDAP_DEBUG_ANY,
462 "id %lu already in next block\n",
471 case 3: /* split the original block */
479 * must split the block, write both new blocks + update
480 * and write the indirect header block.
483 /* count how many indirect blocks */
484 for ( j = 0; idl->b_ids[j] != NOID; j++ )
487 /* check it against all-id thresholed */
488 if ( j + 1 > db->dbc_maxindirect ) {
490 * we've passed the all-id threshold, meaning
491 * that this set of blocks should be replaced
492 * by a single "all-id" block. our job: delete
493 * all the indirect blocks, and replace the header
494 * block by an all-id block.
497 /* delete all indirect blocks */
498 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
499 sprintf( kstr,"%c%s%ld", CONT_PREFIX, key.dptr,
502 k2.dsize = strlen( kstr ) + 1;
504 rc = ldbm_cache_delete( db, k2 );
507 /* store allid block in place of header block */
509 idl = idl_allids( be );
510 rc = idl_store( be, db, key, idl );
518 idl_split_block( tmp, id, &tmp2, &tmp3 );
521 /* create a new updated indirect header block */
522 tmp = idl_alloc( idl->b_nmax + 1 );
523 tmp->b_nids = INDBLOCK;
524 /* everything up to the split block */
525 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
527 /* the two new blocks */
528 tmp->b_ids[i] = tmp2->b_ids[0];
529 tmp->b_ids[i + 1] = tmp3->b_ids[0];
530 /* everything after the split block */
531 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
532 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
534 /* store the header block */
535 rc = idl_store( be, db, key, tmp );
537 /* store the first id block */
538 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
541 k2.dsize = strlen( kstr ) + 1;
542 rc = idl_store( be, db, k2, tmp2 );
544 /* store the second 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, tmp3 );
563 * idl_insert - insert an id into an id list.
564 * returns 0 id inserted
565 * 1 id inserted, first id in block has changed
566 * 2 id not inserted, already there
567 * 3 id not inserted, block must be split
571 idl_insert( IDList **idl, ID id, int maxids )
575 if ( ALLIDS( *idl ) ) {
576 return( 2 ); /* already there */
579 /* is it already there? XXX bin search XXX */
580 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
583 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
584 return( 2 ); /* already there */
587 /* do we need to make room for it? */
588 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
589 /* make room or indicate block needs splitting */
590 if ( (*idl)->b_nmax == maxids ) {
591 return( 3 ); /* block needs splitting */
595 if ( (*idl)->b_nmax > maxids ) {
596 (*idl)->b_nmax = maxids;
598 *idl = (IDList *) ch_realloc( (char *) *idl,
599 ((*idl)->b_nmax + 2) * sizeof(ID) );
602 /* make a slot for the new id */
603 for ( j = (*idl)->b_nids; j != i; j-- ) {
604 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
606 (*idl)->b_ids[i] = id;
608 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
609 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
611 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
628 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
630 /* It wasn't found. Hmm... */
634 if ( ! INDIRECT_BLOCK( idl ) )
636 for ( i=0; i < idl->b_nids; i++ )
638 if ( idl->b_ids[i] == id )
640 memcpy ( &idl->b_ids[i], &idl->b_ids[i+1], sizeof(ID)*(idl->b_nids-(i+1)));
641 idl->b_ids[idl->b_nids-1] = NOID;
644 idl_store( be, db, key, idl );
646 ldbm_cache_delete( db, key );
649 /* We didn't find the ID. Hmmm... */
654 /* We have to go through an indirect block and find the ID
657 for ( nids = 0; idl->b_ids[nids] != NOID; nids++ )
659 kstr = (char *) ch_malloc( key.dsize + 20 );
660 for ( j = 0; idl->b_ids[j] != NOID; j++ )
662 ldbm_datum_init( k2 );
663 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[j] );
665 k2.dsize = strlen( kstr ) + 1;
667 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
668 Debug( LDAP_DEBUG_ANY,
669 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
673 Now try to find the ID in tmp
675 for ( i=0; i < tmp->b_nids; i++ )
677 if ( tmp->b_ids[i] == id )
679 memcpy ( &tmp->b_ids[i], &tmp->b_ids[i+1], sizeof(ID)*(tmp->b_nids-(i+1)));
680 tmp->b_ids[tmp->b_nids-1] = NOID;
683 idl_store ( be, db, k2, tmp );
686 ldbm_cache_delete( db, k2 );
687 memcpy ( &idl->b_ids[j], &idl->b_ids[j+1], sizeof(ID)*(nids-(j+1)));
688 idl->b_ids[nids-1] = NOID;
691 ldbm_cache_delete( db, key );
693 idl_store( be, db, key, idl );
703 idl_dup( IDList *idl )
711 new = idl_alloc( idl->b_nmax );
712 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
719 idl_min( IDList *a, IDList *b )
721 return( a->b_nids > b->b_nids ? b : a );
725 * idl_intersection - return a intersection b
735 unsigned int ai, bi, ni;
738 if ( a == NULL || b == NULL ) {
742 return( idl_dup( b ) );
745 return( idl_dup( a ) );
748 n = idl_dup( idl_min( a, b ) );
750 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
751 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
754 if ( bi == b->b_nids ) {
758 if ( b->b_ids[bi] == a->b_ids[ai] ) {
759 n->b_ids[ni++] = a->b_ids[ai];
773 * idl_union - return a union b
783 unsigned int ai, bi, ni;
787 return( idl_dup( b ) );
790 return( idl_dup( a ) );
792 if ( ALLIDS( a ) || ALLIDS( b ) ) {
793 return( idl_allids( be ) );
796 if ( b->b_nids < a->b_nids ) {
802 n = idl_alloc( a->b_nids + b->b_nids );
804 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
805 if ( a->b_ids[ai] < b->b_ids[bi] ) {
806 n->b_ids[ni++] = a->b_ids[ai++];
807 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
808 n->b_ids[ni++] = b->b_ids[bi++];
810 n->b_ids[ni++] = a->b_ids[ai];
815 for ( ; ai < a->b_nids; ai++ ) {
816 n->b_ids[ni++] = a->b_ids[ai];
818 for ( ; bi < b->b_nids; bi++ ) {
819 n->b_ids[ni++] = b->b_ids[bi];
827 * idl_notin - return a intersection ~b (or a minus b)
837 unsigned int ni, ai, bi;
843 if ( b == NULL || ALLIDS( b )) {
844 return( idl_dup( a ) );
848 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
851 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
852 bi < b->b_nmax; ai++ ) {
853 if ( b->b_ids[bi] == ai ) {
860 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
864 if ( ni == n->b_nmax ) {
866 return( idl_allids( be ) );
876 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
877 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
882 if ( bi == b->b_nids ) {
886 if ( b->b_ids[bi] != a->b_ids[ai] ) {
887 n->b_ids[ni++] = a->b_ids[ai];
891 for ( ; ai < a->b_nids; ai++ ) {
892 n->b_ids[ni++] = a->b_ids[ai];
900 idl_firstid( IDList *idl )
902 if ( idl == NULL || idl->b_nids == 0 ) {
906 if ( ALLIDS( idl ) ) {
907 return( idl->b_nids == 1 ? NOID : 1 );
910 return( idl->b_ids[0] );
914 idl_nextid( IDList *idl, ID id )
918 if ( ALLIDS( idl ) ) {
919 return( ++id < idl->b_nids ? id : NOID );
922 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
927 if ( i >= idl->b_nids ) {
930 return( idl->b_ids[i] );