1 /* idl.c - ldap id list handling routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
18 static ID_BLOCK* idl_dup( ID_BLOCK *idl );
20 static void cont_alloc( Datum *cont, Datum *key )
22 ldbm_datum_init( *cont );
23 cont->dsize = 1 + sizeof(ID) + key->dsize;
24 cont->dptr = ch_malloc( cont->dsize );
26 * (unsigned char *) cont->dptr = SLAP_INDEX_CONT_PREFIX;
28 AC_MEMCPY( &((unsigned char *)cont->dptr)[1 + sizeof(ID)],
29 key->dptr, key->dsize );
32 static void cont_id( Datum *cont, ID id )
36 for( i=1; i <= sizeof(id); i++) {
37 ((unsigned char *)cont->dptr)[i] = (unsigned char)(id & 0xFF);
43 static void cont_free( Datum *cont )
45 ch_free( cont->dptr );
49 static void idl_check(ID_BLOCK *idl)
54 if( ID_BLOCK_INDIRECT(idl) || ID_BLOCK_ALLIDS(idl)
55 || ID_BLOCK_NIDS(idl) <= 1 )
60 for( last = ID_BLOCK_ID(idl, 0), i = 1;
61 i < ID_BLOCK_NIDS(idl);
62 last = ID_BLOCK_ID(idl, i), i++ )
64 assert (last < ID_BLOCK_ID(idl, i) );
69 /* Allocate an ID_BLOCK with room for nids ids */
71 idl_alloc( unsigned int nids )
75 /* nmax + nids + space for the ids */
76 new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
77 ID_BLOCK_NMAX(new) = nids;
78 ID_BLOCK_NIDS(new) = 0;
84 /* Allocate an empty ALLIDS ID_BLOCK */
86 idl_allids( Backend *be )
92 ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
93 if ( next_id_get( be, &id ) ) {
96 ID_BLOCK_NIDS(idl) = id;
101 /* Free an ID_BLOCK */
103 idl_free( ID_BLOCK *idl )
107 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
108 "idl_free: called with NULL pointer\n" ));
110 Debug( LDAP_DEBUG_TRACE,
111 "idl_free: called with NULL pointer\n",
118 free( (char *) idl );
122 /* Fetch an single ID_BLOCK from the cache */
133 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
135 data = ldbm_cache_fetch( db, key );
137 if( data.dptr == NULL ) {
141 idl = idl_dup((ID_BLOCK *) data.dptr);
143 ldbm_datum_free( db->dbc_db, data );
149 /* Fetch a set of ID_BLOCKs from the cache
151 * if block return is an ALLIDS block,
152 * return an new ALLIDS block
155 * construct super block from all blocks referenced by INDIRECT block
170 idl = idl_fetch_one( be, db, key );
176 if ( ID_BLOCK_ALLIDS(idl) ) {
178 /* make sure we have the current value of highest id */
180 idl = idl_allids( be );
185 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
191 * this is an indirect block which points to other blocks.
192 * we need to read in all the blocks it points to and construct
193 * a big id list containing all the ids, which we will return.
196 /* count the number of blocks & allocate space for pointers to them */
197 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
199 tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
201 /* read in all the blocks */
202 cont_alloc( &data, &key );
204 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
205 cont_id( &data, ID_BLOCK_ID(idl, i) );
207 if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
209 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
210 "idl_fetch: idl_fetch_one returned NULL\n" ));
212 Debug( LDAP_DEBUG_ANY,
213 "idl_fetch: one returned NULL\n", 0, 0, 0 );
219 nids += ID_BLOCK_NIDS(tmp[i]);
225 /* allocate space for the big block */
226 idl = idl_alloc( nids );
227 ID_BLOCK_NIDS(idl) = nids;
230 /* copy in all the ids from the component blocks */
231 for ( i = 0; tmp[i] != NULL; i++ ) {
232 if ( tmp[i] == NULL ) {
237 (char *) &ID_BLOCK_ID(idl, nids),
238 (char *) &ID_BLOCK_ID(tmp[i], 0),
239 ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
240 nids += ID_BLOCK_NIDS(tmp[i]);
244 free( (char *) tmp );
246 #ifdef LDBM_DEBUG_IDL
251 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
252 "idl_fetch: %ld ids (%ld max)\n",
253 ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl) ));
255 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
256 ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
263 /* store a single block */
274 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
276 #ifdef LDBM_DEBUG_IDL
280 ldbm_datum_init( data );
282 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
284 data.dptr = (char *) idl;
285 data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
288 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
292 flags = LDBM_REPLACE;
293 rc = ldbm_cache_store( db, key, data, flags );
295 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
299 /* Binary search for id in block, return index
300 * an index is always returned, even with no match. If no
301 * match, the returned index is the insertion point.
309 int lo=0, hi=ID_BLOCK_NIDS(b)-1, nr=0;
313 nr = ( lo + hi ) / 2;
314 if (ID_BLOCK_ID(b, nr) == id)
316 if (ID_BLOCK_ID(b, nr) > id)
324 /* split the block at id
325 * locate ID greater than or equal to id.
337 /* find where to split the block */
338 nr = idl_find(b, id);
339 if ( ID_BLOCK_ID(b,nr) < id )
342 nl = ID_BLOCK_NIDS(b) - nr;
344 *right = idl_alloc( nr == 0 ? 1 : nr );
345 *left = idl_alloc( nl + (nr == 0 ? 0 : 1));
348 * everything before the id being inserted in the first block
349 * unless there is nothing, in which case the id being inserted
353 ID_BLOCK_NIDS(*right) = 1;
354 ID_BLOCK_ID(*right, 0) = id;
357 (char *) &ID_BLOCK_ID(*right, 0),
358 (char *) &ID_BLOCK_ID(b, 0),
360 ID_BLOCK_NIDS(*right) = nr;
361 ID_BLOCK_ID(*left, 0) = id;
364 /* the id being inserted & everything after in the second block */
366 (char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
367 (char *) &ID_BLOCK_ID(b, nr),
369 ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
371 #ifdef LDBM_DEBUG_IDL
379 * idl_change_first - called when an indirect block's first key has
380 * changed, meaning it needs to be stored under a new key, and the
381 * header block pointing to it needs updating.
387 Datum hkey, /* header block key */
388 ID_BLOCK *h, /* header block */
389 int pos, /* pos in h to update */
390 Datum bkey, /* data block key */
391 ID_BLOCK *b /* data block */
396 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
398 /* delete old key block */
399 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
401 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
402 "idl_change_first: ldbm_cache_delete returned %d\n", rc ));
404 Debug( LDAP_DEBUG_ANY,
405 "idl_change_first: ldbm_cache_delete returned %d\n",
412 /* write block with new key */
413 cont_id( &bkey, ID_BLOCK_ID(b, 0) );
415 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
417 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
418 "idl_change_first: idl_store returned %d\n", rc ));
420 Debug( LDAP_DEBUG_ANY,
421 "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
427 /* update + write indirect header block */
428 ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
429 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
431 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
432 "idl_change_first: idl_store returned %s\n", rc ));
434 Debug( LDAP_DEBUG_ANY,
435 "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
454 ID_BLOCK *idl, *tmp, *tmp2, *tmp3;
457 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
458 idl = idl_alloc( 1 );
459 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
460 rc = idl_store( be, db, key, idl );
466 if ( ID_BLOCK_ALLIDS( idl ) ) {
472 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
474 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
475 case 0: /* id inserted - store the updated block */
477 rc = idl_store( be, db, key, idl );
480 case 2: /* id already there - nothing to do */
484 case 3: /* id not inserted - block must be split */
485 /* check threshold for marking this an all-id block */
486 if ( db->dbc_maxindirect < 2 ) {
488 idl = idl_allids( be );
489 rc = idl_store( be, db, key, idl );
493 idl_split_block( idl, id, &tmp, &tmp2 );
496 /* create the header indirect block */
497 idl = idl_alloc( 3 );
498 ID_BLOCK_NMAX(idl) = 3;
499 ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
500 ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
501 ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
502 ID_BLOCK_ID(idl, 2) = NOID;
505 rc = idl_store( be, db, key, idl );
507 cont_alloc( &k2, &key );
508 cont_id( &k2, ID_BLOCK_ID(tmp, 0) );
510 rc = idl_store( be, db, k2, tmp );
512 cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
513 rc = idl_store( be, db, k2, tmp2 );
527 * this is an indirect block which points to other blocks.
528 * we need to read in the block into which the id should be
529 * inserted, then insert the id and store the block. we might
530 * have to split the block if it is full, which means we also
531 * need to write a new "header" block.
534 /* select the block to try inserting into *//* XXX linear search XXX */
535 for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
546 cont_alloc( &k2, &key );
547 cont_id( &k2, ID_BLOCK_ID(idl, i) );
549 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
551 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
552 "idl_insert_key: nonexistent continuation block\n" ));
554 Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
564 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
565 case 0: /* id inserted ok */
566 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
568 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
569 "ids_insert_key: idl_store returned %d\n", rc ));
571 Debug( LDAP_DEBUG_ANY,
572 "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
578 case 1: /* id inserted - first id in block has changed */
580 * key for this block has changed, so we have to
581 * write the block under the new key, delete the
582 * old key block + update and write the indirect
586 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
589 case 2: /* id not inserted - already there, do nothing */
593 case 3: /* id not inserted - block is full */
595 * first, see if it will fit in the next block,
596 * without splitting, unless we're trying to insert
597 * into the beginning of the first block.
600 /* is there a next block? */
601 if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
603 cont_alloc( &k2, &key );
604 cont_id( &k2, ID_BLOCK_ID(idl, i) );
605 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
607 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
608 "idl_insert_key: idl_fetch_one returned NULL\n"));
610 Debug( LDAP_DEBUG_ANY,
611 "idl_insert_key: idl_fetch_one returned NULL\n",
615 /* split the original block */
620 /* If the new id is less than the last id in the
621 * current block, it must not be put into the next
622 * block. Push the last id of the current block
623 * into the next block instead.
625 if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
626 ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);
629 ldbm_datum_init( k3 );
631 --ID_BLOCK_NIDS(tmp);
632 /* This must succeed since we just popped one
633 * ID off the end of it.
635 rc = idl_insert( &tmp, id, db->dbc_maxids );
637 k3.dptr = ch_malloc(k2.dsize);
639 AC_MEMCPY(k3.dptr, k2.dptr, k3.dsize);
640 if ( (rc = idl_store( be, db, k3, tmp )) != 0 ) {
642 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
643 "idl_insert_key: idl_store returned %d\n", rc ));
645 Debug( LDAP_DEBUG_ANY,
646 "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
654 /* This new id will necessarily be inserted
655 * as the first id of the next block by the
656 * following switch() statement.
660 switch ( (rc = idl_insert( &tmp2, id,
661 db->dbc_maxids )) ) {
662 case 1: /* id inserted first in block */
663 rc = idl_change_first( be, db, key, idl,
667 case 2: /* id already there - how? */
668 case 0: /* id inserted: this can never be
669 * the result of idl_insert, because
670 * we guaranteed that idl_change_first
671 * will always be called.
675 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
676 "idl_insert_key: id %ld is already in next block\n",
679 Debug( LDAP_DEBUG_ANY,
680 "idl_insert_key: id %ld already in next block\n",
691 case 3: /* split the original block */
700 * must split the block, write both new blocks + update
701 * and write the indirect header block.
704 rc = 0; /* optimistic */
707 /* count how many indirect blocks *//* XXX linear count XXX */
708 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
711 /* check it against all-id thresholed */
712 if ( j + 1 > db->dbc_maxindirect ) {
714 * we've passed the all-id threshold, meaning
715 * that this set of blocks should be replaced
716 * by a single "all-id" block. our job: delete
717 * all the indirect blocks, and replace the header
718 * block by an all-id block.
721 /* delete all indirect blocks */
722 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
723 cont_id( &k2, ID_BLOCK_ID(idl, j) );
725 rc = ldbm_cache_delete( db, k2 );
728 /* store allid block in place of header block */
730 idl = idl_allids( be );
731 rc = idl_store( be, db, key, idl );
739 idl_split_block( tmp, id, &tmp2, &tmp3 );
742 /* create a new updated indirect header block */
743 tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
744 ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
745 /* everything up to the split block */
747 (char *) &ID_BLOCK_ID(tmp, 0),
748 (char *) &ID_BLOCK_ID(idl, 0),
750 /* the two new blocks */
751 ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
752 ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
753 /* everything after the split block */
755 (char *) &ID_BLOCK_ID(tmp, i + 2),
756 (char *) &ID_BLOCK_ID(idl, i + 1),
757 (ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
759 /* store the header block */
760 rc = idl_store( be, db, key, tmp );
762 /* store the first id block */
763 cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
764 rc = idl_store( be, db, k2, tmp2 );
766 /* store the second id block */
767 cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
768 rc = idl_store( be, db, k2, tmp3 );
783 * idl_insert - insert an id into an id list.
787 * 1 id inserted, first id in block has changed
788 * 2 id not inserted, already there
789 * 3 id not inserted, block must be split
792 idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
796 if ( ID_BLOCK_ALLIDS( *idl ) ) {
797 return( 2 ); /* already there */
800 /* is it already there? */
801 i = idl_find(*idl, id);
802 if ( ID_BLOCK_ID(*idl, i) == id ) {
803 return( 2 ); /* already there */
805 if ( ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) < id )
808 /* do we need to make room for it? */
809 if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
810 /* make room or indicate block needs splitting */
811 if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
812 return( 3 ); /* block needs splitting */
815 ID_BLOCK_NMAX(*idl) *= 2;
816 if ( ID_BLOCK_NMAX(*idl) > maxids ) {
817 ID_BLOCK_NMAX(*idl) = maxids;
819 *idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
820 (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
823 /* make a slot for the new id */
824 AC_MEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
825 (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
827 ID_BLOCK_ID(*idl, i) = id;
828 ID_BLOCK_NIDS(*idl)++;
830 (char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
832 (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
834 #ifdef LDBM_DEBUG_IDL
838 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
855 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
857 /* It wasn't found. Hmm... */
861 if ( ID_BLOCK_ALLIDS( idl ) ) {
866 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
867 for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
868 if ( ID_BLOCK_ID(idl, i) == id ) {
869 if( --ID_BLOCK_NIDS(idl) == 0 ) {
870 ldbm_cache_delete( db, key );
874 &ID_BLOCK_ID(idl, i),
875 &ID_BLOCK_ID(idl, i+1),
876 (ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
878 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
880 idl_store( be, db, key, idl );
886 /* We didn't find the ID. Hmmm... */
892 /* We have to go through an indirect block and find the ID
895 for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
898 cont_alloc( &data, &key );
900 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
903 cont_id( &data, ID_BLOCK_ID(idl, j) );
905 if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
907 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
908 "idl_delete_key: idl_fetch_one returned NULL\n" ));
910 Debug( LDAP_DEBUG_ANY,
911 "idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
917 Now try to find the ID in tmp
919 for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
921 if ( ID_BLOCK_ID(tmp, i) == id )
924 &ID_BLOCK_ID(tmp, i),
925 &ID_BLOCK_ID(tmp, i+1),
926 (ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
927 ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
928 ID_BLOCK_NIDS(tmp)--;
930 if ( ID_BLOCK_NIDS(tmp) ) {
931 idl_store ( be, db, data, tmp );
934 ldbm_cache_delete( db, data );
936 &ID_BLOCK_ID(idl, j),
937 &ID_BLOCK_ID(idl, j+1),
938 (nids-(j+1)) * sizeof(ID));
939 ID_BLOCK_ID(idl, nids-1) = NOID;
942 ldbm_cache_delete( db, key );
944 idl_store( be, db, key, idl );
961 /* return a duplicate of a single ID_BLOCK */
963 idl_dup( ID_BLOCK *idl )
971 new = idl_alloc( ID_BLOCK_NMAX(idl) );
976 (ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
978 #ifdef LDBM_DEBUG_IDL
986 /* return the smaller ID_BLOCK */
988 idl_min( ID_BLOCK *a, ID_BLOCK *b )
990 return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
995 * idl_intersection - return a intersection b
1004 unsigned int ai, bi, ni;
1007 if ( a == NULL || b == NULL ) {
1010 if ( ID_BLOCK_ALLIDS( a ) ) {
1011 return( idl_dup( b ) );
1013 if ( ID_BLOCK_ALLIDS( b ) ) {
1014 return( idl_dup( a ) );
1017 n = idl_dup( idl_min( a, b ) );
1019 #ifdef LDBM_DEBUG_IDL
1024 for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1026 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1032 if ( bi == ID_BLOCK_NIDS(b) ) {
1036 if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
1037 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1045 ID_BLOCK_NIDS(n) = ni;
1047 #ifdef LDBM_DEBUG_IDL
1056 * idl_union - return a union b
1065 unsigned int ai, bi, ni;
1069 return( idl_dup( b ) );
1072 return( idl_dup( a ) );
1074 if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
1075 return( idl_allids( be ) );
1078 #ifdef LDBM_DEBUG_IDL
1083 if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
1089 n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
1091 for ( ni = 0, ai = 0, bi = 0;
1092 ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
1095 if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
1096 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
1098 } else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
1099 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
1102 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1107 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1108 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1110 for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
1111 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
1113 ID_BLOCK_NIDS(n) = ni;
1115 #ifdef LDBM_DEBUG_IDL
1124 * idl_notin - return a intersection ~b (or a minus b)
1133 unsigned int ni, ai, bi;
1139 if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
1140 return( idl_dup( a ) );
1143 if ( ID_BLOCK_ALLIDS( a ) ) {
1144 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
1147 for ( ai = 1, bi = 0;
1148 ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
1151 if ( ID_BLOCK_ID(b, bi) == ai ) {
1154 ID_BLOCK_ID(n, ni++) = ai;
1158 for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
1159 ID_BLOCK_ID(n, ni++) = ai;
1162 if ( ni == ID_BLOCK_NMAX(n) ) {
1164 return( idl_allids( be ) );
1166 ID_BLOCK_NIDS(n) = ni;
1174 for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1176 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1182 if ( bi == ID_BLOCK_NIDS(b) ) {
1186 if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
1187 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1191 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1192 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1194 ID_BLOCK_NIDS(n) = ni;
1196 #ifdef LDBM_DEBUG_IDL
1203 /* return the first ID in the block
1206 * otherwise return NOID
1207 * otherwise return first ID
1209 * cursor is set to 1
1212 idl_firstid( ID_BLOCK *idl, ID *cursor )
1216 if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
1220 if ( ID_BLOCK_ALLIDS( idl ) ) {
1221 return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
1224 return( ID_BLOCK_ID(idl, 0) );
1228 * if ALLIDS block, cursor is id.
1230 * if id < NIDS return id
1232 * otherwise cursor is index into block
1234 * return id at index then increment
1237 idl_nextid( ID_BLOCK *idl, ID *cursor )
1239 if ( ID_BLOCK_ALLIDS( idl ) ) {
1240 if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
1247 if ( *cursor < ID_BLOCK_NIDS(idl) ) {
1248 return( ID_BLOCK_ID(idl, (*cursor)++) );