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 */
622 idl_dup( IDList *idl )
630 new = idl_alloc( idl->b_nmax );
631 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
638 idl_min( IDList *a, IDList *b )
640 return( a->b_nids > b->b_nids ? b : a );
644 * idl_intersection - return a intersection b
654 unsigned int ai, bi, ni;
657 if ( a == NULL || b == NULL ) {
661 return( idl_dup( b ) );
664 return( idl_dup( a ) );
667 n = idl_dup( idl_min( a, b ) );
669 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
670 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
673 if ( bi == b->b_nids ) {
677 if ( b->b_ids[bi] == a->b_ids[ai] ) {
678 n->b_ids[ni++] = a->b_ids[ai];
692 * idl_union - return a union b
702 unsigned int ai, bi, ni;
706 return( idl_dup( b ) );
709 return( idl_dup( a ) );
711 if ( ALLIDS( a ) || ALLIDS( b ) ) {
712 return( idl_allids( be ) );
715 if ( b->b_nids < a->b_nids ) {
721 n = idl_alloc( a->b_nids + b->b_nids );
723 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
724 if ( a->b_ids[ai] < b->b_ids[bi] ) {
725 n->b_ids[ni++] = a->b_ids[ai++];
726 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
727 n->b_ids[ni++] = b->b_ids[bi++];
729 n->b_ids[ni++] = a->b_ids[ai];
734 for ( ; ai < a->b_nids; ai++ ) {
735 n->b_ids[ni++] = a->b_ids[ai];
737 for ( ; bi < b->b_nids; bi++ ) {
738 n->b_ids[ni++] = b->b_ids[bi];
746 * idl_notin - return a intersection ~b (or a minus b)
756 unsigned int ni, ai, bi;
762 if ( b == NULL || ALLIDS( b )) {
763 return( idl_dup( a ) );
767 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
770 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
771 bi < b->b_nmax; ai++ ) {
772 if ( b->b_ids[bi] == ai ) {
779 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
783 if ( ni == n->b_nmax ) {
785 return( idl_allids( be ) );
795 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
796 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
801 if ( bi == b->b_nids ) {
805 if ( b->b_ids[bi] != a->b_ids[ai] ) {
806 n->b_ids[ni++] = a->b_ids[ai];
810 for ( ; ai < a->b_nids; ai++ ) {
811 n->b_ids[ni++] = a->b_ids[ai];
819 idl_firstid( IDList *idl )
821 if ( idl == NULL || idl->b_nids == 0 ) {
825 if ( ALLIDS( idl ) ) {
826 return( idl->b_nids == 1 ? NOID : 1 );
829 return( idl->b_ids[0] );
833 idl_nextid( IDList *idl, ID id )
837 if ( ALLIDS( idl ) ) {
838 return( ++id < idl->b_nids ? id : NOID );
841 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
846 if ( i >= idl->b_nids ) {
849 return( idl->b_ids[i] );