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 ldbm_datum_init( data );
61 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
63 data = ldbm_cache_fetch( db, key );
65 idl = (IDList *) data.dptr;
83 ldbm_datum_init( k2 );
84 ldbm_datum_init( data );
86 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
88 data = ldbm_cache_fetch( db, key );
90 if ( (idl = (IDList *) data.dptr) == NULL ) {
95 if ( ! INDIRECT_BLOCK( idl ) ) {
97 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
98 idl->b_nids, idl->b_nmax, 0 );
101 /* make sure we have the current value of highest id */
102 if ( idl->b_nmax == ALLIDSBLOCK ) {
104 idl = idl_allids( be );
110 * this is an indirect block which points to other blocks.
111 * we need to read in all the blocks it points to and construct
112 * a big id list containing all the ids, which we will return.
115 /* count the number of blocks & allocate space for pointers to them */
116 for ( i = 0; idl->b_ids[i] != NOID; i++ )
118 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
120 /* read in all the blocks */
121 kstr = (char *) ch_malloc( key.dsize + 20 );
123 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
124 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
126 k2.dsize = strlen( kstr ) + 1;
128 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
129 Debug( LDAP_DEBUG_ANY,
130 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
134 nids += tmp[i]->b_nids;
139 /* allocate space for the big block */
140 idl = idl_alloc( nids );
144 /* copy in all the ids from the component blocks */
145 for ( i = 0; tmp[i] != NULL; i++ ) {
146 if ( tmp[i] == NULL ) {
150 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
151 tmp[i]->b_nids * sizeof(ID) );
152 nids += tmp[i]->b_nids;
156 free( (char *) tmp );
158 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %lu ids (%lu max)\n",
159 idl->b_nids, idl->b_nmax, 0 );
173 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
175 ldbm_datum_init( data );
177 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
179 data.dptr = (char *) idl;
180 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
183 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
187 flags = LDBM_REPLACE;
188 if( li->li_dbcachewsync ) flags |= LDBM_SYNC;
189 rc = ldbm_cache_store( db, key, data, flags );
191 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
205 /* find where to split the block */
206 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
209 *n1 = idl_alloc( i == 0 ? 1 : i );
210 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
213 * everything before the id being inserted in the first block
214 * unless there is nothing, in which case the id being inserted
217 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
219 (*n1)->b_nids = (i == 0 ? 1 : i);
222 (*n1)->b_ids[0] = id;
224 (*n2)->b_ids[0] = id;
227 /* the id being inserted & everything after in the second block */
228 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
229 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
230 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
234 * idl_change_first - called when an indirect block's first key has
235 * changed, meaning it needs to be stored under a new key, and the
236 * header block pointing to it needs updating.
243 Datum hkey, /* header block key */
244 IDList *h, /* header block */
245 int pos, /* pos in h to update */
246 Datum bkey, /* data block key */
247 IDList *b /* data block */
252 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
254 /* delete old key block */
255 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
256 Debug( LDAP_DEBUG_ANY,
257 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
262 /* write block with new key */
263 sprintf( bkey.dptr, "%c%s%ld", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
264 bkey.dsize = strlen( bkey.dptr ) + 1;
265 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
266 Debug( LDAP_DEBUG_ANY,
267 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
271 /* update + write indirect header block */
272 h->b_ids[pos] = b->b_ids[0];
273 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
274 Debug( LDAP_DEBUG_ANY,
275 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
291 IDList *idl, *tmp, *tmp2, *tmp3;
295 ldbm_datum_init( k2 );
297 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
299 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
303 idl = idl_alloc( 1 );
304 idl->b_ids[idl->b_nids++] = id;
305 rc = idl_store( be, db, key, idl );
312 if ( ! INDIRECT_BLOCK( idl ) ) {
313 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
314 case 0: /* id inserted - store the updated block */
316 rc = idl_store( be, db, key, idl );
319 case 2: /* id already there - nothing to do */
323 case 3: /* id not inserted - block must be split */
324 /* check threshold for marking this an all-id block */
325 if ( db->dbc_maxindirect < 2 ) {
327 idl = idl_allids( be );
328 rc = idl_store( be, db, key, idl );
334 idl_split_block( idl, id, &tmp, &tmp2 );
337 /* create the header indirect block */
338 idl = idl_alloc( 3 );
340 idl->b_nids = INDBLOCK;
341 idl->b_ids[0] = tmp->b_ids[0];
342 idl->b_ids[1] = tmp2->b_ids[0];
343 idl->b_ids[2] = NOID;
346 rc = idl_store( be, db, key, idl );
348 /* store the first id block */
349 kstr = (char *) ch_malloc( key.dsize + 20 );
350 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
353 k2.dsize = strlen( kstr ) + 1;
354 rc = idl_store( be, db, k2, tmp );
356 /* store the second id block */
357 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
360 k2.dsize = strlen( kstr ) + 1;
361 rc = idl_store( be, db, k2, tmp2 );
374 * this is an indirect block which points to other blocks.
375 * we need to read in the block into which the id should be
376 * inserted, then insert the id and store the block. we might
377 * have to split the block if it is full, which means we also
378 * need to write a new "header" block.
381 /* select the block to try inserting into */
382 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
392 kstr = (char *) ch_malloc( key.dsize + 20 );
393 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[i] );
395 k2.dsize = strlen( kstr ) + 1;
396 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
397 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
403 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
404 case 0: /* id inserted ok */
405 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
406 Debug( LDAP_DEBUG_ANY,
407 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
411 case 1: /* id inserted - first id in block has changed */
413 * key for this block has changed, so we have to
414 * write the block under the new key, delete the
415 * old key block + update and write the indirect
419 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
422 case 2: /* id not inserted - already there */
425 case 3: /* id not inserted - block is full */
427 * first, see if it will fit in the next block,
428 * without splitting, unless we're trying to insert
429 * into the beginning of the first block.
432 /* is there a next block? */
433 if ( !first && idl->b_ids[i + 1] != NOID ) {
435 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
438 k2.dsize = strlen( kstr ) + 1;
439 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
440 Debug( LDAP_DEBUG_ANY,
441 "idl_fetch_one (%s) returns NULL\n",
446 switch ( (rc = idl_insert( &tmp2, id,
447 db->dbc_maxids )) ) {
448 case 1: /* id inserted first in block */
449 rc = idl_change_first( be, db, key, idl,
453 case 2: /* id already there - how? */
454 case 0: /* id inserted */
456 Debug( LDAP_DEBUG_ANY,
457 "id %lu already in next block\n",
466 case 3: /* split the original block */
474 * must split the block, write both new blocks + update
475 * and write the indirect header block.
478 /* count how many indirect blocks */
479 for ( j = 0; idl->b_ids[j] != NOID; j++ )
482 /* check it against all-id thresholed */
483 if ( j + 1 > db->dbc_maxindirect ) {
485 * we've passed the all-id threshold, meaning
486 * that this set of blocks should be replaced
487 * by a single "all-id" block. our job: delete
488 * all the indirect blocks, and replace the header
489 * block by an all-id block.
492 /* delete all indirect blocks */
493 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
494 sprintf( kstr,"%c%s%ld", CONT_PREFIX, key.dptr,
497 k2.dsize = strlen( kstr ) + 1;
499 rc = ldbm_cache_delete( db, k2 );
502 /* store allid block in place of header block */
504 idl = idl_allids( be );
505 rc = idl_store( be, db, key, idl );
513 idl_split_block( tmp, id, &tmp2, &tmp3 );
516 /* create a new updated indirect header block */
517 tmp = idl_alloc( idl->b_nmax + 1 );
518 tmp->b_nids = INDBLOCK;
519 /* everything up to the split block */
520 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
522 /* the two new blocks */
523 tmp->b_ids[i] = tmp2->b_ids[0];
524 tmp->b_ids[i + 1] = tmp3->b_ids[0];
525 /* everything after the split block */
526 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
527 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
529 /* store the header block */
530 rc = idl_store( be, db, key, tmp );
532 /* store the first id block */
533 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
536 k2.dsize = strlen( kstr ) + 1;
537 rc = idl_store( be, db, k2, tmp2 );
539 /* store the second id block */
540 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
543 k2.dsize = strlen( kstr ) + 1;
544 rc = idl_store( be, db, k2, tmp3 );
558 * idl_insert - insert an id into an id list.
559 * returns 0 id inserted
560 * 1 id inserted, first id in block has changed
561 * 2 id not inserted, already there
562 * 3 id not inserted, block must be split
566 idl_insert( IDList **idl, ID id, int maxids )
570 if ( ALLIDS( *idl ) ) {
571 return( 2 ); /* already there */
574 /* is it already there? XXX bin search XXX */
575 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
578 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
579 return( 2 ); /* already there */
582 /* do we need to make room for it? */
583 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
584 /* make room or indicate block needs splitting */
585 if ( (*idl)->b_nmax == maxids ) {
586 return( 3 ); /* block needs splitting */
590 if ( (*idl)->b_nmax > maxids ) {
591 (*idl)->b_nmax = maxids;
593 *idl = (IDList *) ch_realloc( (char *) *idl,
594 ((*idl)->b_nmax + 2) * sizeof(ID) );
597 /* make a slot for the new id */
598 for ( j = (*idl)->b_nids; j != i; j-- ) {
599 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
601 (*idl)->b_ids[i] = id;
603 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
604 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
606 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
623 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
625 /* It wasn't found. Hmm... */
629 if ( ! INDIRECT_BLOCK( idl ) )
631 for ( i=0; i < idl->b_nids; i++ )
633 if ( idl->b_ids[i] == id )
635 memcpy ( &idl->b_ids[i], &idl->b_ids[i+1], sizeof(ID)*(idl->b_nids-(i+1)));
636 idl->b_ids[idl->b_nids-1] = NOID;
639 idl_store( be, db, key, idl );
641 ldbm_cache_delete( db, key );
644 /* We didn't find the ID. Hmmm... */
649 /* We have to go through an indirect block and find the ID
652 for ( nids = 0; idl->b_ids[nids] != NOID; nids++ )
654 kstr = (char *) ch_malloc( key.dsize + 20 );
655 for ( j = 0; idl->b_ids[j] != NOID; j++ )
657 ldbm_datum_init( k2 );
658 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, idl->b_ids[j] );
660 k2.dsize = strlen( kstr ) + 1;
662 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
663 Debug( LDAP_DEBUG_ANY,
664 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
668 Now try to find the ID in tmp
670 for ( i=0; i < tmp->b_nids; i++ )
672 if ( tmp->b_ids[i] == id )
674 memcpy ( &tmp->b_ids[i], &tmp->b_ids[i+1], sizeof(ID)*(tmp->b_nids-(i+1)));
675 tmp->b_ids[tmp->b_nids-1] = NOID;
678 idl_store ( be, db, k2, tmp );
681 ldbm_cache_delete( db, k2 );
682 memcpy ( &idl->b_ids[j], &idl->b_ids[j+1], sizeof(ID)*(nids-(j+1)));
683 idl->b_ids[nids-1] = NOID;
686 ldbm_cache_delete( db, key );
688 idl_store( be, db, key, idl );
698 idl_dup( IDList *idl )
706 new = idl_alloc( idl->b_nmax );
707 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
714 idl_min( IDList *a, IDList *b )
716 return( a->b_nids > b->b_nids ? b : a );
720 * idl_intersection - return a intersection b
730 unsigned int ai, bi, ni;
733 if ( a == NULL || b == NULL ) {
737 return( idl_dup( b ) );
740 return( idl_dup( a ) );
743 n = idl_dup( idl_min( a, b ) );
745 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
746 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
749 if ( bi == b->b_nids ) {
753 if ( b->b_ids[bi] == a->b_ids[ai] ) {
754 n->b_ids[ni++] = a->b_ids[ai];
768 * idl_union - return a union b
778 unsigned int ai, bi, ni;
782 return( idl_dup( b ) );
785 return( idl_dup( a ) );
787 if ( ALLIDS( a ) || ALLIDS( b ) ) {
788 return( idl_allids( be ) );
791 if ( b->b_nids < a->b_nids ) {
797 n = idl_alloc( a->b_nids + b->b_nids );
799 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
800 if ( a->b_ids[ai] < b->b_ids[bi] ) {
801 n->b_ids[ni++] = a->b_ids[ai++];
802 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
803 n->b_ids[ni++] = b->b_ids[bi++];
805 n->b_ids[ni++] = a->b_ids[ai];
810 for ( ; ai < a->b_nids; ai++ ) {
811 n->b_ids[ni++] = a->b_ids[ai];
813 for ( ; bi < b->b_nids; bi++ ) {
814 n->b_ids[ni++] = b->b_ids[bi];
822 * idl_notin - return a intersection ~b (or a minus b)
832 unsigned int ni, ai, bi;
838 if ( b == NULL || ALLIDS( b )) {
839 return( idl_dup( a ) );
843 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
846 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
847 bi < b->b_nmax; ai++ ) {
848 if ( b->b_ids[bi] == ai ) {
855 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
859 if ( ni == n->b_nmax ) {
861 return( idl_allids( be ) );
871 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
872 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
877 if ( bi == b->b_nids ) {
881 if ( b->b_ids[bi] != a->b_ids[ai] ) {
882 n->b_ids[ni++] = a->b_ids[ai];
886 for ( ; ai < a->b_nids; ai++ ) {
887 n->b_ids[ni++] = a->b_ids[ai];
895 idl_firstid( IDList *idl )
897 if ( idl == NULL || idl->b_nids == 0 ) {
901 if ( ALLIDS( idl ) ) {
902 return( idl->b_nids == 1 ? NOID : 1 );
905 return( idl->b_ids[0] );
909 idl_nextid( IDList *idl, ID id )
913 if ( ALLIDS( idl ) ) {
914 return( ++id < idl->b_nids ? id : NOID );
917 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
922 if ( i >= idl->b_nids ) {
925 return( idl->b_ids[i] );