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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
21 static void cont_alloc( Datum cont, Datum key )
23 ldbm_datum_init( cont );
24 cont.dsize = 1 + sizeof(ID) + key.dsize;
25 cont.dptr = ch_malloc( cont.dsize );
27 memcpy( &((unsigned char *)cont.dptr)[1 + sizeof(ID)],
28 key.dptr, key.dsize );
31 static void cont_id( Datum cont, ID id )
35 for( i=1; i <= sizeof(id); i++) {
36 ((unsigned char *)cont.dptr)[i] = (unsigned char)(id & 0xFF);
42 static void cont_free( Datum cont )
48 /* Allocate an ID_BLOCK with room for nids ids */
50 idl_alloc( unsigned int nids )
54 /* nmax + nids + space for the ids */
55 new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
56 ID_BLOCK_NMAX(new) = nids;
57 ID_BLOCK_NIDS(new) = 0;
63 /* Allocate an empty ALLIDS ID_BLOCK */
65 idl_allids( Backend *be )
70 ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
71 ID_BLOCK_NIDS(idl) = next_id_get( be );
76 /* Free an ID_BLOCK */
78 idl_free( ID_BLOCK *idl )
81 Debug( LDAP_DEBUG_TRACE,
82 "idl_free: called with NULL pointer\n",
91 /* Fetch an single ID_BLOCK from the cache */
102 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
104 data = ldbm_cache_fetch( db, key );
106 if( data.dptr == NULL ) {
110 idl = idl_dup((ID_BLOCK *) data.dptr);
112 ldbm_datum_free( db->dbc_db, data );
118 /* Fetch a set of ID_BLOCKs from the cache
120 * if block return is an ALLIDS block,
121 * return an new ALLIDS block
124 * construct super block from all blocks referenced by INDIRECT block
138 #ifndef SLAPD_SCHEMA_NOT_COMPAT
142 idl = idl_fetch_one( be, db, key );
148 if ( ID_BLOCK_ALLIDS(idl) ) {
150 /* make sure we have the current value of highest id */
152 idl = idl_allids( be );
157 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
163 * this is an indirect block which points to other blocks.
164 * we need to read in all the blocks it points to and construct
165 * a big id list containing all the ids, which we will return.
168 /* count the number of blocks & allocate space for pointers to them */
169 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
171 tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
173 /* read in all the blocks */
174 #ifdef SLAPD_SCHEMA_NOT_COMPAT
175 cont_alloc( data, key );
177 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
180 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
181 #ifdef SLAPD_SCHEMA_NOT_COMPAT
182 cont_id( data, ID_BLOCK_ID(idl, i) );
184 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
185 ID_BLOCK_ID(idl, i), key.dptr );
188 data.dsize = strlen( kstr ) + 1;
191 if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
192 Debug( LDAP_DEBUG_ANY,
193 "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
197 nids += ID_BLOCK_NIDS(tmp[i]);
200 #ifdef SLAPD_SCHEMA_NOT_COMPAT
207 /* allocate space for the big block */
208 idl = idl_alloc( nids );
209 ID_BLOCK_NIDS(idl) = nids;
212 /* copy in all the ids from the component blocks */
213 for ( i = 0; tmp[i] != NULL; i++ ) {
214 if ( tmp[i] == NULL ) {
219 (char *) &ID_BLOCK_ID(idl, nids),
220 (char *) &ID_BLOCK_ID(tmp[i], 0),
221 ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
222 nids += ID_BLOCK_NIDS(tmp[i]);
226 free( (char *) tmp );
228 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
229 ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
234 /* store a single block */
245 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
247 ldbm_datum_init( data );
249 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
251 data.dptr = (char *) idl;
252 data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
255 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
259 flags = LDBM_REPLACE;
260 rc = ldbm_cache_store( db, key, data, flags );
262 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
266 /* split the block at id
267 * locate ID greater than or equal to id.
279 /* find where to split the block *//* XXX linear search XXX */
280 for ( nr = 0; nr < ID_BLOCK_NIDS(b) && id > ID_BLOCK_ID(b, nr); nr++ )
283 nl = ID_BLOCK_NIDS(b) - nr;
285 *right = idl_alloc( nr == 0 ? 1 : nr );
286 *left = idl_alloc( nl + (nr == 0 ? 0 : 1));
289 * everything before the id being inserted in the first block
290 * unless there is nothing, in which case the id being inserted
294 ID_BLOCK_NIDS(*right) = 1;
295 ID_BLOCK_ID(*right, 0) = id;
298 (char *) &ID_BLOCK_ID(*right, 0),
299 (char *) &ID_BLOCK_ID(b, 0),
301 ID_BLOCK_NIDS(*right) = nr;
302 ID_BLOCK_ID(*left, 0) = id;
305 /* the id being inserted & everything after in the second block */
307 (char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
308 (char *) &ID_BLOCK_ID(b, nr),
310 ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
315 * idl_change_first - called when an indirect block's first key has
316 * changed, meaning it needs to be stored under a new key, and the
317 * header block pointing to it needs updating.
323 Datum hkey, /* header block key */
324 ID_BLOCK *h, /* header block */
325 int pos, /* pos in h to update */
326 Datum bkey, /* data block key */
327 ID_BLOCK *b /* data block */
332 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
334 /* delete old key block */
335 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
336 Debug( LDAP_DEBUG_ANY,
337 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
342 /* write block with new key */
343 #ifdef SLAPD_SCHEMA_NOT_COMPAT
344 cont_id( bkey, ID_BLOCK_ID(b, 0) );
346 sprintf( bkey.dptr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
347 ID_BLOCK_ID(b, 0), hkey.dptr );
348 bkey.dsize = strlen( bkey.dptr ) + 1;
351 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
352 Debug( LDAP_DEBUG_ANY,
353 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
357 /* update + write indirect header block */
358 ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
359 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
360 Debug( LDAP_DEBUG_ANY,
361 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
378 ID_BLOCK *idl, *tmp, *tmp2, *tmp3;
380 #ifndef SLAPD_SCHEMA_NOT_COMPAT
383 ldbm_datum_init( k2 );
386 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
388 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
392 idl = idl_alloc( 1 );
393 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
394 rc = idl_store( be, db, key, idl );
400 if ( ID_BLOCK_ALLIDS( idl ) ) {
406 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
408 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
409 case 0: /* id inserted - store the updated block */
411 rc = idl_store( be, db, key, idl );
414 case 2: /* id already there - nothing to do */
418 case 3: /* id not inserted - block must be split */
419 /* check threshold for marking this an all-id block */
420 if ( db->dbc_maxindirect < 2 ) {
422 idl = idl_allids( be );
423 rc = idl_store( be, db, key, idl );
427 idl_split_block( idl, id, &tmp, &tmp2 );
430 /* create the header indirect block */
431 idl = idl_alloc( 3 );
432 ID_BLOCK_NMAX(idl) = 3;
433 ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
434 ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
435 ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
436 ID_BLOCK_ID(idl, 2) = NOID;
439 rc = idl_store( be, db, key, idl );
441 #ifdef SLAPD_SCHEMA_NOT_COMPAT
442 cont_alloc( k2, key );
443 cont_id( k2, ID_BLOCK_ID(tmp, 0) );
445 /* store the first id block */
446 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
447 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
448 ID_BLOCK_ID(tmp, 0), key.dptr );
451 k2.dsize = strlen( kstr ) + 1;
454 rc = idl_store( be, db, k2, tmp );
456 #ifdef SLAPD_SCHEMA_NOT_COMPAT
457 cont_id( k2, ID_BLOCK_ID(tmp2, 0) );
459 /* store the second id block */
460 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
461 ID_BLOCK_ID(tmp2, 0), key.dptr );
463 k2.dsize = strlen( kstr ) + 1;
465 rc = idl_store( be, db, k2, tmp2 );
467 #ifdef SLAPD_SCHEMA_NOT_COMPAT
483 * this is an indirect block which points to other blocks.
484 * we need to read in the block into which the id should be
485 * inserted, then insert the id and store the block. we might
486 * have to split the block if it is full, which means we also
487 * need to write a new "header" block.
490 /* select the block to try inserting into *//* XXX linear search XXX */
491 for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
502 #ifdef SLAPD_SCHEMA_NOT_COMPAT
503 cont_alloc( k2, key );
504 cont_id( k2, ID_BLOCK_ID(idl, i) );
506 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
507 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
508 ID_BLOCK_ID(idl, i), key.dptr );
510 k2.dsize = strlen( kstr ) + 1;
513 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
514 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
516 #ifdef SLAPD_SCHEMA_NOT_COMPAT
526 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
527 case 0: /* id inserted ok */
528 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
529 Debug( LDAP_DEBUG_ANY,
530 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
534 case 1: /* id inserted - first id in block has changed */
536 * key for this block has changed, so we have to
537 * write the block under the new key, delete the
538 * old key block + update and write the indirect
542 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
545 case 2: /* id not inserted - already there, do nothing */
549 case 3: /* id not inserted - block is full */
551 * first, see if it will fit in the next block,
552 * without splitting, unless we're trying to insert
553 * into the beginning of the first block.
556 /* is there a next block? */
557 if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
559 #ifdef SLAPD_SCHEMA_NOT_COMPAT
560 cont_alloc( k2, key );
561 cont_id( k2, ID_BLOCK_ID(idl, i) );
563 k2.dptr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
564 sprintf( k2.dptr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
565 ID_BLOCK_ID(idl, i + 1), key.dptr );
566 k2.dsize = strlen( k2.dptr ) + 1;
568 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
569 Debug( LDAP_DEBUG_ANY,
570 "idl_fetch_one (%s) returns NULL\n",
572 /* split the original block */
573 #ifdef SLAPD_SCHEMA_NOT_COMPAT
581 /* If the new id is less than the last id in the
582 * current block, it must not be put into the next
583 * block. Push the last id of the current block
584 * into the next block instead.
586 if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
587 ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);
590 ldbm_datum_init( k3 );
592 --ID_BLOCK_NIDS(tmp);
593 /* This must succeed since we just popped one
594 * ID off the end of it.
596 rc = idl_insert( &tmp, id, db->dbc_maxids );
598 #ifdef SLAPD_SCHEMA_NOT_COMPAT
599 k3.dptr = ch_malloc(k2.dsize);
601 memcpy(k3.dptr, k2.dptr, k3.dsize);
603 k3.dptr = strdup( kstr );
604 k3.dsize = strlen( kstr ) + 1;
606 if ( (rc = idl_store( be, db, k3, tmp )) != 0 ) {
607 Debug( LDAP_DEBUG_ANY,
608 "idl_store of (%s) returns %d\n", k3.dptr, rc, 0 );
611 #ifdef SLAPD_SCHEMA_NOT_COMPAT
619 /* This new id will necessarily be inserted
620 * as the first id of the next block by the
621 * following switch() statement.
625 switch ( (rc = idl_insert( &tmp2, id,
626 db->dbc_maxids )) ) {
627 case 1: /* id inserted first in block */
628 rc = idl_change_first( be, db, key, idl,
632 case 2: /* id already there - how? */
633 case 0: /* id inserted: this can never be
634 * the result of idl_insert, because
635 * we guaranteed that idl_change_first
636 * will always be called.
639 Debug( LDAP_DEBUG_ANY,
640 "id %ld already in next block\n",
644 #ifdef SLAPD_SCHEMA_NOT_COMPAT
645 assert( 0 ); /* not yet implemented */
654 case 3: /* split the original block */
663 * must split the block, write both new blocks + update
664 * and write the indirect header block.
667 rc = 0; /* optimistic */
670 /* count how many indirect blocks *//* XXX linear count XXX */
671 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
674 /* check it against all-id thresholed */
675 if ( j + 1 > db->dbc_maxindirect ) {
677 * we've passed the all-id threshold, meaning
678 * that this set of blocks should be replaced
679 * by a single "all-id" block. our job: delete
680 * all the indirect blocks, and replace the header
681 * block by an all-id block.
684 /* delete all indirect blocks */
685 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
686 #ifdef SLAPD_SCHEMA_NOT_COMPAT
687 cont_id( k2, ID_BLOCK_ID(idl, j) );
689 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
690 ID_BLOCK_ID(idl, j), key.dptr );
692 k2.dsize = strlen( kstr ) + 1;
695 rc = ldbm_cache_delete( db, k2 );
698 /* store allid block in place of header block */
700 idl = idl_allids( be );
701 rc = idl_store( be, db, key, idl );
703 #ifdef SLAPD_SCHEMA_NOT_COMPAT
713 idl_split_block( tmp, id, &tmp2, &tmp3 );
716 /* create a new updated indirect header block */
717 tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
718 ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
719 /* everything up to the split block */
721 (char *) &ID_BLOCK_ID(tmp, 0),
722 (char *) &ID_BLOCK_ID(idl, 0),
724 /* the two new blocks */
725 ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
726 ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
727 /* everything after the split block */
729 (char *) &ID_BLOCK_ID(tmp, i + 2),
730 (char *) &ID_BLOCK_ID(idl, i + 1),
731 (ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
733 /* store the header block */
734 rc = idl_store( be, db, key, tmp );
736 /* store the first id block */
737 #ifdef SLAPD_SCHEMA_NOT_COMPAT
738 cont_id( k2, ID_BLOCK_ID(tmp2, 0) );
740 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
741 ID_BLOCK_ID(tmp2, 0), key.dptr );
743 k2.dsize = strlen( kstr ) + 1;
745 rc = idl_store( be, db, k2, tmp2 );
747 /* store the second id block */
748 #ifdef SLAPD_SCHEMA_NOT_COMPAT
749 cont_id( k2, ID_BLOCK_ID(tmp3, 0) );
751 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
752 ID_BLOCK_ID(tmp3, 0), key.dptr );
754 k2.dsize = strlen( kstr ) + 1;
756 rc = idl_store( be, db, k2, tmp3 );
763 #ifdef SLAPD_SCHEMA_NOT_COMPAT
775 * idl_insert - insert an id into an id list.
779 * 1 id inserted, first id in block has changed
780 * 2 id not inserted, already there
781 * 3 id not inserted, block must be split
784 idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
788 if ( ID_BLOCK_ALLIDS( *idl ) ) {
789 return( 2 ); /* already there */
792 /* is it already there? *//* XXX linear search XXX */
793 for ( i = 0; i < ID_BLOCK_NIDS(*idl) && id > ID_BLOCK_ID(*idl, i); i++ ) {
796 if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
797 return( 2 ); /* already there */
800 /* do we need to make room for it? */
801 if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
802 /* make room or indicate block needs splitting */
803 if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
804 return( 3 ); /* block needs splitting */
807 ID_BLOCK_NMAX(*idl) *= 2;
808 if ( ID_BLOCK_NMAX(*idl) > maxids ) {
809 ID_BLOCK_NMAX(*idl) = maxids;
811 *idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
812 (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
815 /* make a slot for the new id */
816 SAFEMEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
817 (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
819 ID_BLOCK_ID(*idl, i) = id;
820 ID_BLOCK_NIDS(*idl)++;
822 (char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
824 (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
826 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
842 #ifndef SLAPD_SCHEMA_NOT_COMPAT
846 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
848 /* It wasn't found. Hmm... */
852 if ( ID_BLOCK_ALLIDS( idl ) ) {
857 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
858 for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
859 if ( ID_BLOCK_ID(idl, i) == id ) {
860 if( --ID_BLOCK_NIDS(idl) == 0 ) {
861 ldbm_cache_delete( db, key );
865 &ID_BLOCK_ID(idl, i),
866 &ID_BLOCK_ID(idl, i+1),
867 (ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
869 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
871 idl_store( be, db, key, idl );
877 /* We didn't find the ID. Hmmm... */
883 /* We have to go through an indirect block and find the ID
886 for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
889 #ifdef SLAPD_SCHEMA_NOT_COMPAT
890 cont_alloc( data, key );
892 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
895 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
898 #ifdef SLAPD_SCHEMA_NOT_COMPAT
899 cont_id( data, ID_BLOCK_ID(idl, j) );
901 ldbm_datum_init( data );
902 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
903 ID_BLOCK_ID(idl, j), key.dptr );
905 data.dsize = strlen( kstr ) + 1;
908 if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
909 Debug( LDAP_DEBUG_ANY,
910 "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
914 Now try to find the ID in tmp
916 for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
918 if ( ID_BLOCK_ID(tmp, i) == id )
921 &ID_BLOCK_ID(tmp, i),
922 &ID_BLOCK_ID(tmp, i+1),
923 (ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
924 ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
925 ID_BLOCK_NIDS(tmp)--;
927 if ( ID_BLOCK_NIDS(tmp) ) {
928 idl_store ( be, db, data, tmp );
931 ldbm_cache_delete( db, data );
933 &ID_BLOCK_ID(idl, j),
934 &ID_BLOCK_ID(idl, j+1),
935 (nids-(j+1)) * sizeof(ID));
936 ID_BLOCK_ID(idl, nids-1) = NOID;
939 ldbm_cache_delete( db, key );
941 idl_store( be, db, key, idl );
944 #ifdef SLAPD_SCHEMA_NOT_COMPAT
956 #ifdef SLAPD_SCHEMA_NOT_COMPAT
966 /* return a duplicate of a single ID_BLOCK */
968 idl_dup( ID_BLOCK *idl )
976 new = idl_alloc( ID_BLOCK_NMAX(idl) );
981 (ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
987 /* return the smaller ID_BLOCK */
989 idl_min( ID_BLOCK *a, ID_BLOCK *b )
991 return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
996 * idl_intersection - return a intersection b
1005 unsigned int ai, bi, ni;
1008 if ( a == NULL || b == NULL ) {
1011 if ( ID_BLOCK_ALLIDS( a ) ) {
1012 return( idl_dup( b ) );
1014 if ( ID_BLOCK_ALLIDS( b ) ) {
1015 return( idl_dup( a ) );
1018 n = idl_dup( idl_min( a, b ) );
1020 for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1022 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1028 if ( bi == ID_BLOCK_NIDS(b) ) {
1032 if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
1033 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1041 ID_BLOCK_NIDS(n) = ni;
1048 * idl_union - return a union b
1057 unsigned int ai, bi, ni;
1061 return( idl_dup( b ) );
1064 return( idl_dup( a ) );
1066 if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
1067 return( idl_allids( be ) );
1070 if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
1076 n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
1078 for ( ni = 0, ai = 0, bi = 0;
1079 ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
1082 if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
1083 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
1085 } else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
1086 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
1089 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1094 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1095 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1097 for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
1098 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
1100 ID_BLOCK_NIDS(n) = ni;
1107 * idl_notin - return a intersection ~b (or a minus b)
1116 unsigned int ni, ai, bi;
1122 if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
1123 return( idl_dup( a ) );
1126 if ( ID_BLOCK_ALLIDS( a ) ) {
1127 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
1130 for ( ai = 1, bi = 0;
1131 ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
1134 if ( ID_BLOCK_ID(b, bi) == ai ) {
1137 ID_BLOCK_ID(n, ni++) = ai;
1141 for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
1142 ID_BLOCK_ID(n, ni++) = ai;
1145 if ( ni == ID_BLOCK_NMAX(n) ) {
1147 return( idl_allids( be ) );
1149 ID_BLOCK_NIDS(n) = ni;
1157 for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1159 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1165 if ( bi == ID_BLOCK_NIDS(b) ) {
1169 if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
1170 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1174 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1175 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1177 ID_BLOCK_NIDS(n) = ni;
1182 /* return the first ID in the block
1185 * otherwise return NOID
1186 * otherwise return first ID
1188 * cursor is set to 1
1191 idl_firstid( ID_BLOCK *idl, ID *cursor )
1195 if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
1199 if ( ID_BLOCK_ALLIDS( idl ) ) {
1200 return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
1203 return( ID_BLOCK_ID(idl, 0) );
1207 * if ALLIDS block, cursor is id.
1209 * if id < NIDS return id
1211 * otherwise cursor is index into block
1213 * return id at index then increment
1216 idl_nextid( ID_BLOCK *idl, ID *cursor )
1218 if ( ID_BLOCK_ALLIDS( idl ) ) {
1219 if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
1226 if ( *cursor < ID_BLOCK_NIDS(idl) ) {
1227 return( ID_BLOCK_ID(idl, (*cursor)++) );