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
139 idl = idl_fetch_one( be, db, key );
145 if ( ID_BLOCK_ALLIDS(idl) ) {
147 /* make sure we have the current value of highest id */
149 idl = idl_allids( be );
154 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
160 * this is an indirect block which points to other blocks.
161 * we need to read in all the blocks it points to and construct
162 * a big id list containing all the ids, which we will return.
165 /* count the number of blocks & allocate space for pointers to them */
166 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
168 tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
170 /* read in all the blocks */
171 #ifdef SLAPD_SCHEMA_NOT_COMPAT
172 cont_alloc( data, key );
174 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
177 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
178 #ifdef SLAPD_SCHEMA_NOT_COMPAT
179 cont_id( data, ID_BLOCK_ID(idl, i) );
181 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
182 ID_BLOCK_ID(idl, i), key.dptr );
185 data.dsize = strlen( kstr ) + 1;
188 if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
189 Debug( LDAP_DEBUG_ANY,
190 "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
194 nids += ID_BLOCK_NIDS(tmp[i]);
197 #ifdef SLAPD_SCHEMA_NOT_COMPAT
204 /* allocate space for the big block */
205 idl = idl_alloc( nids );
206 ID_BLOCK_NIDS(idl) = nids;
209 /* copy in all the ids from the component blocks */
210 for ( i = 0; tmp[i] != NULL; i++ ) {
211 if ( tmp[i] == NULL ) {
216 (char *) &ID_BLOCK_ID(idl, nids),
217 (char *) &ID_BLOCK_ID(tmp[i], 0),
218 ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
219 nids += ID_BLOCK_NIDS(tmp[i]);
223 free( (char *) tmp );
225 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
226 ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
231 /* store a single block */
242 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
244 ldbm_datum_init( data );
246 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
248 data.dptr = (char *) idl;
249 data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
252 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
256 flags = LDBM_REPLACE;
257 rc = ldbm_cache_store( db, key, data, flags );
259 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
263 /* split the block at id
264 * locate ID greater than or equal to id.
276 /* find where to split the block *//* XXX linear search XXX */
277 for ( nr = 0; nr < ID_BLOCK_NIDS(b) && id > ID_BLOCK_ID(b, nr); nr++ )
280 nl = ID_BLOCK_NIDS(b) - nr;
282 *right = idl_alloc( nr == 0 ? 1 : nr );
283 *left = idl_alloc( nl + (nr == 0 ? 0 : 1));
286 * everything before the id being inserted in the first block
287 * unless there is nothing, in which case the id being inserted
291 ID_BLOCK_NIDS(*right) = 1;
292 ID_BLOCK_ID(*right, 0) = id;
295 (char *) &ID_BLOCK_ID(*right, 0),
296 (char *) &ID_BLOCK_ID(b, 0),
298 ID_BLOCK_NIDS(*right) = nr;
299 ID_BLOCK_ID(*left, 0) = id;
302 /* the id being inserted & everything after in the second block */
304 (char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
305 (char *) &ID_BLOCK_ID(b, nr),
307 ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
312 * idl_change_first - called when an indirect block's first key has
313 * changed, meaning it needs to be stored under a new key, and the
314 * header block pointing to it needs updating.
320 Datum hkey, /* header block key */
321 ID_BLOCK *h, /* header block */
322 int pos, /* pos in h to update */
323 Datum bkey, /* data block key */
324 ID_BLOCK *b /* data block */
329 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
331 /* delete old key block */
332 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
333 Debug( LDAP_DEBUG_ANY,
334 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
339 /* write block with new key */
340 #ifdef SLAPD_SCHEMA_NOT_COMPAT
341 cont_id( bkey, ID_BLOCK_ID(b, 0) );
343 sprintf( bkey.dptr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
344 ID_BLOCK_ID(b, 0), hkey.dptr );
345 bkey.dsize = strlen( bkey.dptr ) + 1;
348 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
349 Debug( LDAP_DEBUG_ANY,
350 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
354 /* update + write indirect header block */
355 ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
356 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
357 Debug( LDAP_DEBUG_ANY,
358 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
375 ID_BLOCK *idl, *tmp, *tmp2, *tmp3;
377 #ifndef SLAPD_SCHEMA_NOT_COMPAT
380 ldbm_datum_init( k2 );
383 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
385 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
389 idl = idl_alloc( 1 );
390 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
391 rc = idl_store( be, db, key, idl );
397 if ( ID_BLOCK_ALLIDS( idl ) ) {
403 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
405 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
406 case 0: /* id inserted - store the updated block */
408 rc = idl_store( be, db, key, idl );
411 case 2: /* id already there - nothing to do */
415 case 3: /* id not inserted - block must be split */
416 /* check threshold for marking this an all-id block */
417 if ( db->dbc_maxindirect < 2 ) {
419 idl = idl_allids( be );
420 rc = idl_store( be, db, key, idl );
424 idl_split_block( idl, id, &tmp, &tmp2 );
427 /* create the header indirect block */
428 idl = idl_alloc( 3 );
429 ID_BLOCK_NMAX(idl) = 3;
430 ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
431 ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
432 ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
433 ID_BLOCK_ID(idl, 2) = NOID;
436 rc = idl_store( be, db, key, idl );
438 #ifdef SLAPD_SCHEMA_NOT_COMPAT
439 cont_alloc( k2, key );
440 cont_id( k2, ID_BLOCK_ID(tmp, 0) );
442 /* store the first id block */
443 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
444 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
445 ID_BLOCK_ID(tmp, 0), key.dptr );
448 k2.dsize = strlen( kstr ) + 1;
451 rc = idl_store( be, db, k2, tmp );
453 #ifdef SLAPD_SCHEMA_NOT_COMPAT
454 cont_id( k2, ID_BLOCK_ID(tmp2, 0) );
456 /* store the second id block */
457 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
458 ID_BLOCK_ID(tmp2, 0), key.dptr );
460 k2.dsize = strlen( kstr ) + 1;
462 rc = idl_store( be, db, k2, tmp2 );
464 #ifdef SLAPD_SCHEMA_NOT_COMPAT
480 * this is an indirect block which points to other blocks.
481 * we need to read in the block into which the id should be
482 * inserted, then insert the id and store the block. we might
483 * have to split the block if it is full, which means we also
484 * need to write a new "header" block.
487 /* select the block to try inserting into *//* XXX linear search XXX */
488 for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
499 #ifdef SLAPD_SCHEMA_NOT_COMPAT
500 cont_alloc( k2, key );
501 cont_id( k2, ID_BLOCK_ID(idl, i) );
503 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
504 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
505 ID_BLOCK_ID(idl, i), key.dptr );
507 k2.dsize = strlen( kstr ) + 1;
510 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
511 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
513 #ifdef SLAPD_SCHEMA_NOT_COMPAT
523 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
524 case 0: /* id inserted ok */
525 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
526 Debug( LDAP_DEBUG_ANY,
527 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
531 case 1: /* id inserted - first id in block has changed */
533 * key for this block has changed, so we have to
534 * write the block under the new key, delete the
535 * old key block + update and write the indirect
539 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
542 case 2: /* id not inserted - already there, do nothing */
546 case 3: /* id not inserted - block is full */
548 * first, see if it will fit in the next block,
549 * without splitting, unless we're trying to insert
550 * into the beginning of the first block.
553 /* is there a next block? */
554 if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
556 #ifdef SLAPD_SCHEMA_NOT_COMPAT
557 cont_alloc( k2, key );
558 cont_id( k2, ID_BLOCK_ID(idl, i) );
560 k2.dptr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
561 sprintf( k2.dptr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
562 ID_BLOCK_ID(idl, i + 1), key.dptr );
563 k2.dsize = strlen( k2.dptr ) + 1;
565 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
566 Debug( LDAP_DEBUG_ANY,
567 "idl_fetch_one (%s) returns NULL\n",
569 /* split the original block */
570 #ifdef SLAPD_SCHEMA_NOT_COMPAT
578 /* If the new id is less than the last id in the
579 * current block, it must not be put into the next
580 * block. Push the last id of the current block
581 * into the next block instead.
583 if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
584 ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);
587 ldbm_datum_init( k3 );
589 --ID_BLOCK_NIDS(tmp);
590 /* This must succeed since we just popped one
591 * ID off the end of it.
593 rc = idl_insert( &tmp, id, db->dbc_maxids );
596 #ifdef SLAPD_SCHEMA_NOT_COMPAT
597 k3.dptr = ch_malloc(k2.dsize);
599 memcpy(k3.dptr, k2.dptr, k3.dsize);
601 k3.dptr = strdup( kstr );
602 k3.dsize = strlen( kstr ) + 1;
604 if ( (rc = idl_store( be, db, k3, tmp )) != 0 ) {
605 Debug( LDAP_DEBUG_ANY,
606 "idl_store of (%s) returns %d\n", k3.dptr, rc, 0 );
609 #ifdef SLAPD_SCHEMA_NOT_COMPAT
617 /* This new id will necessarily be inserted
618 * as the first id of the next block by the
619 * following switch() statement.
623 switch ( (rc = idl_insert( &tmp2, id,
624 db->dbc_maxids )) ) {
625 case 1: /* id inserted first in block */
626 rc = idl_change_first( be, db, key, idl,
630 case 2: /* id already there - how? */
631 case 0: /* id inserted: this can never be
632 * the result of idl_insert, because
633 * we guaranteed that idl_change_first
634 * will always be called.
637 Debug( LDAP_DEBUG_ANY,
638 "id %ld already in next block\n",
642 #ifdef SLAPD_SCHEMA_NOT_COMPAT
643 assert( 0 ); /* not yet implemented */
652 case 3: /* split the original block */
661 * must split the block, write both new blocks + update
662 * and write the indirect header block.
665 rc = 0; /* optimistic */
668 /* count how many indirect blocks *//* XXX linear count XXX */
669 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
672 /* check it against all-id thresholed */
673 if ( j + 1 > db->dbc_maxindirect ) {
675 * we've passed the all-id threshold, meaning
676 * that this set of blocks should be replaced
677 * by a single "all-id" block. our job: delete
678 * all the indirect blocks, and replace the header
679 * block by an all-id block.
682 /* delete all indirect blocks */
683 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
684 #ifdef SLAPD_SCHEMA_NOT_COMPAT
685 cont_id( k2, ID_BLOCK_ID(idl, j) );
687 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
688 ID_BLOCK_ID(idl, j), key.dptr );
690 k2.dsize = strlen( kstr ) + 1;
693 rc = ldbm_cache_delete( db, k2 );
696 /* store allid block in place of header block */
698 idl = idl_allids( be );
699 rc = idl_store( be, db, key, idl );
701 #ifdef SLAPD_SCHEMA_NOT_COMPAT
711 idl_split_block( tmp, id, &tmp2, &tmp3 );
714 /* create a new updated indirect header block */
715 tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
716 ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
717 /* everything up to the split block */
719 (char *) &ID_BLOCK_ID(tmp, 0),
720 (char *) &ID_BLOCK_ID(idl, 0),
722 /* the two new blocks */
723 ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
724 ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
725 /* everything after the split block */
727 (char *) &ID_BLOCK_ID(tmp, i + 2),
728 (char *) &ID_BLOCK_ID(idl, i + 1),
729 (ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
731 /* store the header block */
732 rc = idl_store( be, db, key, tmp );
734 /* store the first id block */
735 #ifdef SLAPD_SCHEMA_NOT_COMPAT
736 cont_id( k2, ID_BLOCK_ID(tmp2, 0) );
738 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
739 ID_BLOCK_ID(tmp2, 0), key.dptr );
741 k2.dsize = strlen( kstr ) + 1;
743 rc = idl_store( be, db, k2, tmp2 );
745 /* store the second id block */
746 #ifdef SLAPD_SCHEMA_NOT_COMPAT
747 cont_id( k2, ID_BLOCK_ID(tmp3, 0) );
749 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
750 ID_BLOCK_ID(tmp3, 0), key.dptr );
752 k2.dsize = strlen( kstr ) + 1;
754 rc = idl_store( be, db, k2, tmp3 );
761 #ifdef SLAPD_SCHEMA_NOT_COMPAT
773 * idl_insert - insert an id into an id list.
777 * 1 id inserted, first id in block has changed
778 * 2 id not inserted, already there
779 * 3 id not inserted, block must be split
782 idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
786 if ( ID_BLOCK_ALLIDS( *idl ) ) {
787 return( 2 ); /* already there */
790 /* is it already there? *//* XXX linear search XXX */
791 for ( i = 0; i < ID_BLOCK_NIDS(*idl) && id > ID_BLOCK_ID(*idl, i); i++ ) {
794 if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
795 return( 2 ); /* already there */
798 /* do we need to make room for it? */
799 if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
800 /* make room or indicate block needs splitting */
801 if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
802 return( 3 ); /* block needs splitting */
805 ID_BLOCK_NMAX(*idl) *= 2;
806 if ( ID_BLOCK_NMAX(*idl) > maxids ) {
807 ID_BLOCK_NMAX(*idl) = maxids;
809 *idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
810 (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
813 /* make a slot for the new id */
814 SAFEMEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
815 (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
817 ID_BLOCK_ID(*idl, i) = id;
818 ID_BLOCK_NIDS(*idl)++;
820 (char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
822 (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
824 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
840 #ifndef SLAPD_SCHEMA_NOT_COMPAT
844 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
846 /* It wasn't found. Hmm... */
850 if ( ID_BLOCK_ALLIDS( idl ) ) {
855 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
856 for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
857 if ( ID_BLOCK_ID(idl, i) == id ) {
858 if( --ID_BLOCK_NIDS(idl) == 0 ) {
859 ldbm_cache_delete( db, key );
863 &ID_BLOCK_ID(idl, i),
864 &ID_BLOCK_ID(idl, i+1),
865 (ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
867 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
869 idl_store( be, db, key, idl );
875 /* We didn't find the ID. Hmmm... */
881 /* We have to go through an indirect block and find the ID
884 for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
887 #ifdef SLAPD_SCHEMA_NOT_COMPAT
888 cont_alloc( data, key );
890 kstr = (char *) ch_malloc( key.dsize + SLAP_INDEX_CONT_SIZE );
893 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
896 #ifdef SLAPD_SCHEMA_NOT_COMPAT
897 cont_id( data, ID_BLOCK_ID(idl, j) );
899 ldbm_datum_init( data );
900 sprintf( kstr, "%c%ld%s", SLAP_INDEX_CONT_PREFIX,
901 ID_BLOCK_ID(idl, j), key.dptr );
903 data.dsize = strlen( kstr ) + 1;
906 if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
907 Debug( LDAP_DEBUG_ANY,
908 "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
912 Now try to find the ID in tmp
914 for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
916 if ( ID_BLOCK_ID(tmp, i) == id )
919 &ID_BLOCK_ID(tmp, i),
920 &ID_BLOCK_ID(tmp, i+1),
921 (ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
922 ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
923 ID_BLOCK_NIDS(tmp)--;
925 if ( ID_BLOCK_NIDS(tmp) ) {
926 idl_store ( be, db, data, tmp );
929 ldbm_cache_delete( db, data );
931 &ID_BLOCK_ID(idl, j),
932 &ID_BLOCK_ID(idl, j+1),
933 (nids-(j+1)) * sizeof(ID));
934 ID_BLOCK_ID(idl, nids-1) = NOID;
937 ldbm_cache_delete( db, key );
939 idl_store( be, db, key, idl );
942 #ifdef SLAPD_SCHEMA_NOT_COMPAT
954 #ifdef SLAPD_SCHEMA_NOT_COMPAT
964 /* return a duplicate of a single ID_BLOCK */
966 idl_dup( ID_BLOCK *idl )
974 new = idl_alloc( ID_BLOCK_NMAX(idl) );
979 (ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
985 /* return the smaller ID_BLOCK */
987 idl_min( ID_BLOCK *a, ID_BLOCK *b )
989 return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
994 * idl_intersection - return a intersection b
1003 unsigned int ai, bi, ni;
1006 if ( a == NULL || b == NULL ) {
1009 if ( ID_BLOCK_ALLIDS( a ) ) {
1010 return( idl_dup( b ) );
1012 if ( ID_BLOCK_ALLIDS( b ) ) {
1013 return( idl_dup( a ) );
1016 n = idl_dup( idl_min( a, b ) );
1018 for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1020 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1026 if ( bi == ID_BLOCK_NIDS(b) ) {
1030 if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
1031 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1039 ID_BLOCK_NIDS(n) = ni;
1046 * idl_union - return a union b
1055 unsigned int ai, bi, ni;
1059 return( idl_dup( b ) );
1062 return( idl_dup( a ) );
1064 if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
1065 return( idl_allids( be ) );
1068 if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
1074 n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
1076 for ( ni = 0, ai = 0, bi = 0;
1077 ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
1080 if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
1081 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
1083 } else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
1084 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
1087 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1092 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1093 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1095 for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
1096 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
1098 ID_BLOCK_NIDS(n) = ni;
1105 * idl_notin - return a intersection ~b (or a minus b)
1114 unsigned int ni, ai, bi;
1120 if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
1121 return( idl_dup( a ) );
1124 if ( ID_BLOCK_ALLIDS( a ) ) {
1125 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
1128 for ( ai = 1, bi = 0;
1129 ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
1132 if ( ID_BLOCK_ID(b, bi) == ai ) {
1135 ID_BLOCK_ID(n, ni++) = ai;
1139 for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
1140 ID_BLOCK_ID(n, ni++) = ai;
1143 if ( ni == ID_BLOCK_NMAX(n) ) {
1145 return( idl_allids( be ) );
1147 ID_BLOCK_NIDS(n) = ni;
1155 for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1157 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1163 if ( bi == ID_BLOCK_NIDS(b) ) {
1167 if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
1168 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1172 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1173 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1175 ID_BLOCK_NIDS(n) = ni;
1180 /* return the first ID in the block
1183 * otherwise return NOID
1184 * otherwise return first ID
1186 * cursor is set to 1
1189 idl_firstid( ID_BLOCK *idl, ID *cursor )
1193 if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
1197 if ( ID_BLOCK_ALLIDS( idl ) ) {
1198 return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
1201 return( ID_BLOCK_ID(idl, 0) );
1205 * if ALLIDS block, cursor is id.
1207 * if id < NIDS return id
1209 * otherwise cursor is index into block
1211 * return id at index then increment
1214 idl_nextid( ID_BLOCK *idl, ID *cursor )
1216 if ( ID_BLOCK_ALLIDS( idl ) ) {
1217 if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
1224 if ( *cursor < ID_BLOCK_NIDS(idl) ) {
1225 return( ID_BLOCK_ID(idl, (*cursor)++) );