1 /* idl.c - ldap id list handling routines */
10 #include <sys/socket.h>
13 #include "ldapconfig.h"
14 #include "back-ldbm.h"
16 extern Datum ldbm_cache_fetch();
23 /* nmax + nids + space for the ids */
24 new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
32 idl_allids( Backend *be )
37 idl->b_nmax = ALLIDSBLOCK;
38 idl->b_nids = next_id_get( be );
44 idl_free( IDList *idl )
67 memset( &k2, 0, sizeof( k2 ) );
68 memset( &data, 0, sizeof( data ) );
71 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
73 data = ldbm_cache_fetch( db, key );
75 idl = (IDList *) data.dptr;
94 memset( &k2, 0, sizeof( k2 ) );
95 memset( &data, 0, sizeof( data ) );
98 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
100 data = ldbm_cache_fetch( db, key );
102 if ( (idl = (IDList *) data.dptr) == NULL ) {
107 if ( ! INDIRECT_BLOCK( idl ) ) {
109 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
110 idl->b_nids, idl->b_nmax, 0 );
113 /* make sure we have the current value of highest id */
114 if ( idl->b_nmax == ALLIDSBLOCK ) {
116 idl = idl_allids( be );
122 * this is an indirect block which points to other blocks.
123 * we need to read in all the blocks it points to and construct
124 * a big id list containing all the ids, which we will return.
127 /* count the number of blocks & allocate space for pointers to them */
128 for ( i = 0; idl->b_ids[i] != NOID; i++ )
130 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
132 /* read in all the blocks */
133 kstr = (char *) ch_malloc( key.dsize + 20 );
135 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
136 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
138 k2.dsize = strlen( kstr ) + 1;
140 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
141 Debug( LDAP_DEBUG_ANY,
142 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
146 nids += tmp[i]->b_nids;
151 /* allocate space for the big block */
152 idl = idl_alloc( nids );
156 /* copy in all the ids from the component blocks */
157 for ( i = 0; tmp[i] != NULL; i++ ) {
158 if ( tmp[i] == NULL ) {
162 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
163 tmp[i]->b_nids * sizeof(ID) );
164 nids += tmp[i]->b_nids;
168 free( (char *) tmp );
170 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
185 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
188 memset( &data, 0, sizeof( data ) );
191 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
193 data.dptr = (char *) idl;
194 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
197 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
201 flags = LDBM_REPLACE;
202 if( li->li_flush_wrt ) flags |= LDBM_SYNC;
203 rc = ldbm_cache_store( db, key, data, flags );
205 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
219 /* find where to split the block */
220 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
223 *n1 = idl_alloc( i == 0 ? 1 : i );
224 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
227 * everything before the id being inserted in the first block
228 * unless there is nothing, in which case the id being inserted
231 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
233 (*n1)->b_nids = (i == 0 ? 1 : i);
236 (*n1)->b_ids[0] = id;
238 (*n2)->b_ids[0] = id;
241 /* the id being inserted & everything after in the second block */
242 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
243 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
244 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
248 * idl_change_first - called when an indirect block's first key has
249 * changed, meaning it needs to be stored under a new key, and the
250 * header block pointing to it needs updating.
257 Datum hkey, /* header block key */
258 IDList *h, /* header block */
259 int pos, /* pos in h to update */
260 Datum bkey, /* data block key */
261 IDList *b /* data block */
266 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
268 /* delete old key block */
269 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
270 Debug( LDAP_DEBUG_ANY,
271 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
276 /* write block with new key */
277 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
278 bkey.dsize = strlen( bkey.dptr ) + 1;
279 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
280 Debug( LDAP_DEBUG_ANY,
281 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
285 /* update + write indirect header block */
286 h->b_ids[pos] = b->b_ids[0];
287 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
288 Debug( LDAP_DEBUG_ANY,
289 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
305 IDList *idl, *tmp, *tmp2, *tmp3;
310 memset( &k2, 0, sizeof( k2 ) );
313 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
315 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
319 idl = idl_alloc( 1 );
320 idl->b_ids[idl->b_nids++] = id;
321 rc = idl_store( be, db, key, idl );
328 if ( ! INDIRECT_BLOCK( idl ) ) {
329 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
330 case 0: /* id inserted - store the updated block */
332 rc = idl_store( be, db, key, idl );
335 case 2: /* id already there - nothing to do */
339 case 3: /* id not inserted - block must be split */
340 /* check threshold for marking this an all-id block */
341 if ( db->dbc_maxindirect < 2 ) {
343 idl = idl_allids( be );
344 rc = idl_store( be, db, key, idl );
350 idl_split_block( idl, id, &tmp, &tmp2 );
353 /* create the header indirect block */
354 idl = idl_alloc( 3 );
356 idl->b_nids = INDBLOCK;
357 idl->b_ids[0] = tmp->b_ids[0];
358 idl->b_ids[1] = tmp2->b_ids[0];
359 idl->b_ids[2] = NOID;
362 rc = idl_store( be, db, key, idl );
364 /* store the first id block */
365 kstr = (char *) ch_malloc( key.dsize + 20 );
366 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
369 k2.dsize = strlen( kstr ) + 1;
370 rc = idl_store( be, db, k2, tmp );
372 /* store the second id block */
373 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
376 k2.dsize = strlen( kstr ) + 1;
377 rc = idl_store( be, db, k2, tmp2 );
390 * this is an indirect block which points to other blocks.
391 * we need to read in the block into which the id should be
392 * inserted, then insert the id and store the block. we might
393 * have to split the block if it is full, which means we also
394 * need to write a new "header" block.
397 /* select the block to try inserting into */
398 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
408 kstr = (char *) ch_malloc( key.dsize + 20 );
409 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
411 k2.dsize = strlen( kstr ) + 1;
412 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
413 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
419 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
420 case 0: /* id inserted ok */
421 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
422 Debug( LDAP_DEBUG_ANY,
423 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
427 case 1: /* id inserted - first id in block has changed */
429 * key for this block has changed, so we have to
430 * write the block under the new key, delete the
431 * old key block + update and write the indirect
435 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
438 case 2: /* id not inserted - already there */
441 case 3: /* id not inserted - block is full */
443 * first, see if it will fit in the next block,
444 * without splitting, unless we're trying to insert
445 * into the beginning of the first block.
448 /* is there a next block? */
449 if ( !first && idl->b_ids[i + 1] != NOID ) {
451 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
454 k2.dsize = strlen( kstr ) + 1;
455 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
456 Debug( LDAP_DEBUG_ANY,
457 "idl_fetch_one (%s) returns NULL\n",
462 switch ( (rc = idl_insert( &tmp2, id,
463 db->dbc_maxids )) ) {
464 case 1: /* id inserted first in block */
465 rc = idl_change_first( be, db, key, idl,
469 case 2: /* id already there - how? */
470 case 0: /* id inserted */
472 Debug( LDAP_DEBUG_ANY,
473 "id %d already in next block\n",
482 case 3: /* split the original block */
490 * must split the block, write both new blocks + update
491 * and write the indirect header block.
494 /* count how many indirect blocks */
495 for ( j = 0; idl->b_ids[j] != NOID; j++ )
498 /* check it against all-id thresholed */
499 if ( j + 1 > db->dbc_maxindirect ) {
501 * we've passed the all-id threshold, meaning
502 * that this set of blocks should be replaced
503 * by a single "all-id" block. our job: delete
504 * all the indirect blocks, and replace the header
505 * block by an all-id block.
508 /* delete all indirect blocks */
509 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
510 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
513 k2.dsize = strlen( kstr ) + 1;
515 rc = ldbm_cache_delete( db, k2 );
518 /* store allid block in place of header block */
520 idl = idl_allids( be );
521 rc = idl_store( be, db, key, idl );
529 idl_split_block( tmp, id, &tmp2, &tmp3 );
532 /* create a new updated indirect header block */
533 tmp = idl_alloc( idl->b_nmax + 1 );
534 tmp->b_nids = INDBLOCK;
535 /* everything up to the split block */
536 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
538 /* the two new blocks */
539 tmp->b_ids[i] = tmp2->b_ids[0];
540 tmp->b_ids[i + 1] = tmp3->b_ids[0];
541 /* everything after the split block */
542 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
543 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
545 /* store the header block */
546 rc = idl_store( be, db, key, tmp );
548 /* store the first id block */
549 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
552 k2.dsize = strlen( kstr ) + 1;
553 rc = idl_store( be, db, k2, tmp2 );
555 /* store the second id block */
556 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
559 k2.dsize = strlen( kstr ) + 1;
560 rc = idl_store( be, db, k2, tmp3 );
574 * idl_insert - insert an id into an id list.
575 * returns 0 id inserted
576 * 1 id inserted, first id in block has changed
577 * 2 id not inserted, already there
578 * 3 id not inserted, block must be split
582 idl_insert( IDList **idl, ID id, int maxids )
586 if ( ALLIDS( *idl ) ) {
587 return( 2 ); /* already there */
590 /* is it already there? XXX bin search XXX */
591 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
594 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
595 return( 2 ); /* already there */
598 /* do we need to make room for it? */
599 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
600 /* make room or indicate block needs splitting */
601 if ( (*idl)->b_nmax == maxids ) {
602 return( 3 ); /* block needs splitting */
606 if ( (*idl)->b_nmax > maxids ) {
607 (*idl)->b_nmax = maxids;
609 *idl = (IDList *) ch_realloc( (char *) *idl,
610 ((*idl)->b_nmax + 2) * sizeof(ID) );
613 /* make a slot for the new id */
614 for ( j = (*idl)->b_nids; j != i; j-- ) {
615 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
617 (*idl)->b_ids[i] = id;
619 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
620 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
622 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
626 idl_dup( IDList *idl )
634 new = idl_alloc( idl->b_nmax );
635 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
642 idl_min( IDList *a, IDList *b )
644 return( a->b_nids > b->b_nids ? b : a );
648 * idl_intersection - return a intersection b
661 if ( a == NULL || b == NULL ) {
665 return( idl_dup( b ) );
668 return( idl_dup( a ) );
671 n = idl_dup( idl_min( a, b ) );
673 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
674 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
677 if ( bi == b->b_nids ) {
681 if ( b->b_ids[bi] == a->b_ids[ai] ) {
682 n->b_ids[ni++] = a->b_ids[ai];
696 * idl_union - return a union b
710 return( idl_dup( b ) );
713 return( idl_dup( a ) );
715 if ( ALLIDS( a ) || ALLIDS( b ) ) {
716 return( idl_allids( be ) );
719 if ( b->b_nids < a->b_nids ) {
725 n = idl_alloc( a->b_nids + b->b_nids );
727 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
728 if ( a->b_ids[ai] < b->b_ids[bi] ) {
729 n->b_ids[ni++] = a->b_ids[ai++];
730 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
731 n->b_ids[ni++] = b->b_ids[bi++];
733 n->b_ids[ni++] = a->b_ids[ai];
738 for ( ; ai < a->b_nids; ai++ ) {
739 n->b_ids[ni++] = a->b_ids[ai];
741 for ( ; bi < b->b_nids; bi++ ) {
742 n->b_ids[ni++] = b->b_ids[bi];
750 * idl_notin - return a intersection ~b (or a minus b)
766 if ( b == NULL || ALLIDS( b )) {
767 return( idl_dup( a ) );
771 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
774 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
775 bi < b->b_nmax; ai++ ) {
776 if ( b->b_ids[bi] == ai ) {
783 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
787 if ( ni == n->b_nmax ) {
789 return( idl_allids( be ) );
799 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
800 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
805 if ( bi == b->b_nids ) {
809 if ( b->b_ids[bi] != a->b_ids[ai] ) {
810 n->b_ids[ni++] = a->b_ids[ai];
814 for ( ; ai < a->b_nids; ai++ ) {
815 n->b_ids[ni++] = a->b_ids[ai];
823 idl_firstid( IDList *idl )
825 if ( idl == NULL || idl->b_nids == 0 ) {
829 if ( ALLIDS( idl ) ) {
830 return( idl->b_nids == 1 ? NOID : 1 );
833 return( idl->b_ids[0] );
837 idl_nextid( IDList *idl, ID id )
841 if ( ALLIDS( idl ) ) {
842 return( ++id < idl->b_nids ? id : NOID );
845 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
850 if ( i >= idl->b_nids ) {
853 return( idl->b_ids[i] );