]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/idl.c
8a249fe0482b49bd480cb6467eb0a50d9af47a31
[openldap] / servers / slapd / back-ldbm / idl.c
1 /* idl.c - ldap id list handling routines */
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #ifdef CLDAP
6 #include <sys/socket.h>
7 #endif
8 #include "slap.h"
9 #include "ldapconfig.h"
10 #include "back-ldbm.h"
11
12 extern Datum    ldbm_cache_fetch();
13
14 IDList *
15 idl_alloc( int nids )
16 {
17         IDList  *new;
18
19         /* nmax + nids + space for the ids */
20         new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
21         new->b_nmax = nids;
22         new->b_nids = 0;
23
24         return( new );
25 }
26
27 IDList  *
28 idl_allids( Backend *be )
29 {
30         IDList  *idl;
31
32         idl = idl_alloc( 0 );
33         idl->b_nmax = ALLIDSBLOCK;
34         idl->b_nids = next_id_get( be );
35
36         return( idl );
37 }
38
39 void
40 idl_free( IDList *idl )
41 {
42         if ( idl == NULL ) {
43                 return;
44         }
45
46         free( (char *) idl );
47 }
48
49 static IDList *
50 idl_fetch_one(
51     Backend             *be,
52     struct dbcache      *db,
53     Datum               key
54 )
55 {
56         Datum   data, k2;
57         IDList  *idl;
58         IDList  **tmp;
59         char    *kstr;
60         int     i, nids;
61
62         /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
63
64         data = ldbm_cache_fetch( db, key );
65
66         idl = (IDList *) data.dptr;
67
68         return( idl );
69 }
70
71 IDList *
72 idl_fetch(
73     Backend             *be,
74     struct dbcache      *db,
75     Datum               key
76 )
77 {
78         Datum   data, k2;
79         IDList  *idl;
80         IDList  **tmp;
81         char    *kstr;
82         int     i, nids;
83
84         /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
85
86         data = ldbm_cache_fetch( db, key );
87
88         if ( (idl = (IDList *) data.dptr) == NULL ) {
89                 return( NULL );
90         }
91
92         /* regular block */
93         if ( ! INDIRECT_BLOCK( idl ) ) {
94                 /*
95                 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
96                     idl->b_nids, idl->b_nmax, 0 );
97                 */
98
99                 /* make sure we have the current value of highest id */
100                 if ( idl->b_nmax == ALLIDSBLOCK ) {
101                         idl_free( idl );
102                         idl = idl_allids( be );
103                 }
104                 return( idl );
105         }
106
107         /*
108          * this is an indirect block which points to other blocks.
109          * we need to read in all the blocks it points to and construct
110          * a big id list containing all the ids, which we will return.
111          */
112
113         /* count the number of blocks & allocate space for pointers to them */
114         for ( i = 0; idl->b_ids[i] != NOID; i++ )
115                 ;       /* NULL */
116         tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
117
118         /* read in all the blocks */
119         kstr = (char *) ch_malloc( key.dsize + 20 );
120         nids = 0;
121         for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
122                 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
123                 k2.dptr = kstr;
124                 k2.dsize = strlen( kstr ) + 1;
125
126                 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
127                         Debug( LDAP_DEBUG_ANY,
128                             "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
129                         continue;
130                 }
131
132                 nids += tmp[i]->b_nids;
133         }
134         tmp[i] = NULL;
135         idl_free( idl );
136
137         /* allocate space for the big block */
138         idl = idl_alloc( nids );
139         idl->b_nids = nids;
140         nids = 0;
141
142         /* copy in all the ids from the component blocks */
143         for ( i = 0; tmp[i] != NULL; i++ ) {
144                 if ( tmp[i] == NULL ) {
145                         continue;
146                 }
147
148                 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
149                     tmp[i]->b_nids * sizeof(ID) );
150                 nids += tmp[i]->b_nids;
151
152                 idl_free( tmp[i] );
153         }
154         free( (char *) tmp );
155
156         Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
157             idl->b_nmax, 0 );
158         return( idl );
159 }
160
161 static int
162 idl_store(
163     Backend             *be,
164     struct dbcache      *db,
165     Datum               key, 
166     IDList              *idl
167 )
168 {
169         int     rc, flags;
170         Datum   data;
171         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
172
173         /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
174
175         data.dptr = (char *) idl;
176         data.dsize = (2 + idl->b_nmax) * sizeof(ID);
177         
178         flags = LDBM_REPLACE;
179         if( li->li_flush_wrt ) flags |= LDBM_SYNC;
180         rc = ldbm_cache_store( db, key, data, flags );
181
182         /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
183         return( rc );
184 }
185
186 static void
187 idl_split_block(
188     IDList      *b,
189     ID          id,
190     IDList      **n1,
191     IDList      **n2
192 )
193 {
194         int     i;
195
196         /* find where to split the block */
197         for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
198                 ;       /* NULL */
199
200         *n1 = idl_alloc( i == 0 ? 1 : i );
201         *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
202
203         /*
204          * everything before the id being inserted in the first block
205          * unless there is nothing, in which case the id being inserted
206          * goes there.
207          */
208         SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
209             i * sizeof(ID) );
210         (*n1)->b_nids = (i == 0 ? 1 : i);
211
212         if ( i == 0 ) {
213                 (*n1)->b_ids[0] = id;
214         } else {
215                 (*n2)->b_ids[0] = id;
216         }
217
218         /* the id being inserted & everything after in the second block */
219         SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
220             (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
221         (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
222 }
223
224 /*
225  * idl_change_first - called when an indirect block's first key has
226  * changed, meaning it needs to be stored under a new key, and the
227  * header block pointing to it needs updating.
228  */
229
230 static int
231 idl_change_first(
232     Backend             *be,
233     struct dbcache      *db,
234     Datum               hkey,           /* header block key     */
235     IDList              *h,             /* header block         */
236     int                 pos,            /* pos in h to update   */
237     Datum               bkey,           /* data block key       */
238     IDList              *b              /* data block           */
239 )
240 {
241         int     rc;
242
243         /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
244
245         /* delete old key block */
246         if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
247                 Debug( LDAP_DEBUG_ANY,
248                     "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
249                     0 );
250                 return( rc );
251         }
252
253         /* write block with new key */
254         sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
255         bkey.dsize = strlen( bkey.dptr ) + 1;
256         if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
257                 Debug( LDAP_DEBUG_ANY,
258                     "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
259                 return( rc );
260         }
261
262         /* update + write indirect header block */
263         h->b_ids[pos] = b->b_ids[0];
264         if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
265                 Debug( LDAP_DEBUG_ANY,
266                     "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
267                 return( rc );
268         }
269
270         return( 0 );
271 }
272
273 int
274 idl_insert_key(
275     Backend             *be,
276     struct dbcache      *db,
277     Datum               key,
278     ID                  id
279 )
280 {
281         int     i, j, first, rc;
282         IDList  *idl, *tmp, *tmp2, *tmp3;
283         char    *kstr;
284         Datum   k2;
285
286         if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
287                 idl = idl_alloc( 1 );
288                 idl->b_ids[idl->b_nids++] = id;
289                 rc = idl_store( be, db, key, idl );
290
291                 idl_free( idl );
292                 return( rc );
293         }
294
295         /* regular block */
296         if ( ! INDIRECT_BLOCK( idl ) ) {
297                 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
298                 case 0:         /* id inserted - store the updated block */
299                 case 1:
300                         rc = idl_store( be, db, key, idl );
301                         break;
302
303                 case 2:         /* id already there - nothing to do */
304                         rc = 0;
305                         break;
306
307                 case 3:         /* id not inserted - block must be split */
308                         /* check threshold for marking this an all-id block */
309                         if ( db->dbc_maxindirect < 2 ) {
310                                 idl_free( idl );
311                                 idl = idl_allids( be );
312                                 rc = idl_store( be, db, key, idl );
313                                 idl_free( idl );
314
315                                 return( rc );
316                         }
317
318                         idl_split_block( idl, id, &tmp, &tmp2 );
319                         idl_free( idl );
320
321                         /* create the header indirect block */
322                         idl = idl_alloc( 3 );
323                         idl->b_nmax = 3;
324                         idl->b_nids = INDBLOCK;
325                         idl->b_ids[0] = tmp->b_ids[0];
326                         idl->b_ids[1] = tmp2->b_ids[0];
327                         idl->b_ids[2] = NOID;
328
329                         /* store it */
330                         rc = idl_store( be, db, key, idl );
331
332                         /* store the first id block */
333                         kstr = (char *) ch_malloc( key.dsize + 20 );
334                         sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
335                             tmp->b_ids[0] );
336                         k2.dptr = kstr;
337                         k2.dsize = strlen( kstr ) + 1;
338                         rc = idl_store( be, db, k2, tmp );
339
340                         /* store the second id block */
341                         sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
342                             tmp2->b_ids[0] );
343                         k2.dptr = kstr;
344                         k2.dsize = strlen( kstr ) + 1;
345                         rc = idl_store( be, db, k2, tmp2 );
346
347                         free( kstr );
348                         idl_free( tmp );
349                         idl_free( tmp2 );
350                         break;
351                 }
352
353                 idl_free( idl );
354                 return( rc );
355         }
356
357         /*
358          * this is an indirect block which points to other blocks.
359          * we need to read in the block into which the id should be
360          * inserted, then insert the id and store the block.  we might
361          * have to split the block if it is full, which means we also
362          * need to write a new "header" block.
363          */
364
365         /* select the block to try inserting into */
366         for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
367                 ;       /* NULL */
368         if ( i != 0 ) {
369                 i--;
370                 first = 0;
371         } else {
372                 first = 1;
373         }
374
375         /* get the block */
376         kstr = (char *) ch_malloc( key.dsize + 20 );
377         sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
378         k2.dptr = kstr;
379         k2.dsize = strlen( kstr ) + 1;
380         if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
381                 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
382                     k2.dptr, 0, 0 );
383                 return( -1 );
384         }
385
386         /* insert the id */
387         switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
388         case 0:         /* id inserted ok */
389                 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
390                         Debug( LDAP_DEBUG_ANY,
391                             "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
392                 }
393                 break;
394
395         case 1:         /* id inserted - first id in block has changed */
396                 /*
397                  * key for this block has changed, so we have to
398                  * write the block under the new key, delete the
399                  * old key block + update and write the indirect
400                  * header block.
401                  */
402
403                 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
404                 break;
405
406         case 2:         /* id not inserted - already there */
407                 break;
408
409         case 3:         /* id not inserted - block is full */
410                 /*
411                  * first, see if it will fit in the next block,
412                  * without splitting, unless we're trying to insert
413                  * into the beginning of the first block.
414                  */
415
416                 /* is there a next block? */
417                 if ( !first && idl->b_ids[i + 1] != NOID ) {
418                         /* read it in */
419                         sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
420                             idl->b_ids[i + 1] );
421                         k2.dptr = kstr;
422                         k2.dsize = strlen( kstr ) + 1;
423                         if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
424                                 Debug( LDAP_DEBUG_ANY,
425                                     "idl_fetch_one (%s) returns NULL\n",
426                                     k2.dptr, 0, 0 );
427                                 break;
428                         }
429
430                         switch ( (rc = idl_insert( &tmp2, id,
431                             db->dbc_maxids )) ) {
432                         case 1:         /* id inserted first in block */
433                                 rc = idl_change_first( be, db, key, idl,
434                                     i + 1, k2, tmp2 );
435                                 /* FALL */
436
437                         case 2:         /* id already there - how? */
438                         case 0:         /* id inserted */
439                                 if ( rc == 2 ) {
440                                         Debug( LDAP_DEBUG_ANY,
441                                             "id %d already in next block\n",
442                                             id, 0, 0 );
443                                 }
444                                 free( kstr );
445                                 idl_free( tmp );
446                                 idl_free( tmp2 );
447                                 idl_free( idl );
448                                 return( 0 );
449
450                         case 3:         /* split the original block */
451                                 idl_free( tmp2 );
452                                 break;
453                         }
454
455                 }
456
457                 /*
458                  * must split the block, write both new blocks + update
459                  * and write the indirect header block.
460                  */
461
462                 /* count how many indirect blocks */
463                 for ( j = 0; idl->b_ids[j] != NOID; j++ )
464                         ;       /* NULL */
465
466                 /* check it against all-id thresholed */
467                 if ( j + 1 > db->dbc_maxindirect ) {
468                         /*
469                          * we've passed the all-id threshold, meaning
470                          * that this set of blocks should be replaced
471                          * by a single "all-id" block.  our job: delete
472                          * all the indirect blocks, and replace the header
473                          * block by an all-id block.
474                          */
475
476                         /* delete all indirect blocks */
477                         for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
478                                 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
479                                     idl->b_ids[j] );
480                                 k2.dptr = kstr;
481                                 k2.dsize = strlen( kstr ) + 1;
482
483                                 rc = ldbm_cache_delete( db, k2 );
484                         }
485
486                         /* store allid block in place of header block */
487                         idl_free( idl );
488                         idl = idl_allids( be );
489                         rc = idl_store( be, db, key, idl );
490
491                         free( kstr );
492                         idl_free( idl );
493                         idl_free( tmp );
494                         return( rc );
495                 }
496
497                 idl_split_block( tmp, id, &tmp2, &tmp3 );
498                 idl_free( tmp );
499
500                 /* create a new updated indirect header block */
501                 tmp = idl_alloc( idl->b_nmax + 1 );
502                 tmp->b_nids = INDBLOCK;
503                 /* everything up to the split block */
504                 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
505                     i * sizeof(ID) );
506                 /* the two new blocks */
507                 tmp->b_ids[i] = tmp2->b_ids[0];
508                 tmp->b_ids[i + 1] = tmp3->b_ids[0];
509                 /* everything after the split block */
510                 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
511                     &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
512
513                 /* store the header block */
514                 rc = idl_store( be, db, key, tmp );
515
516                 /* store the first id block */
517                 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
518                     tmp2->b_ids[0] );
519                 k2.dptr = kstr;
520                 k2.dsize = strlen( kstr ) + 1;
521                 rc = idl_store( be, db, k2, tmp2 );
522
523                 /* store the second id block */
524                 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
525                     tmp3->b_ids[0] );
526                 k2.dptr = kstr;
527                 k2.dsize = strlen( kstr ) + 1;
528                 rc = idl_store( be, db, k2, tmp3 );
529
530                 idl_free( tmp2 );
531                 idl_free( tmp3 );
532                 break;
533         }
534
535         free( kstr );
536         idl_free( tmp );
537         idl_free( idl );
538         return( rc );
539 }
540
541 /*
542  * idl_insert - insert an id into an id list.
543  * returns      0       id inserted
544  *              1       id inserted, first id in block has changed
545  *              2       id not inserted, already there
546  *              3       id not inserted, block must be split
547  */
548
549 int
550 idl_insert( IDList **idl, ID id, int maxids )
551 {
552         int     i, j;
553
554         if ( ALLIDS( *idl ) ) {
555                 return( 2 );    /* already there */
556         }
557
558         /* is it already there? XXX bin search XXX */
559         for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
560                 ;       /* NULL */
561         }
562         if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
563                 return( 2 );    /* already there */
564         }
565
566         /* do we need to make room for it? */
567         if ( (*idl)->b_nids == (*idl)->b_nmax ) {
568                 /* make room or indicate block needs splitting */
569                 if ( (*idl)->b_nmax == maxids ) {
570                         return( 3 );    /* block needs splitting */
571                 }
572
573                 (*idl)->b_nmax *= 2;
574                 if ( (*idl)->b_nmax > maxids ) {
575                         (*idl)->b_nmax = maxids;
576                 }
577                 *idl = (IDList *) ch_realloc( (char *) *idl,
578                     ((*idl)->b_nmax + 2) * sizeof(ID) );
579         }
580
581         /* make a slot for the new id */
582         for ( j = (*idl)->b_nids; j != i; j-- ) {
583                 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
584         }
585         (*idl)->b_ids[i] = id;
586         (*idl)->b_nids++;
587         (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
588             ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
589
590         return( i == 0 ? 1 : 0 );       /* inserted - first id changed or not */
591 }
592
593 static IDList *
594 idl_dup( IDList *idl )
595 {
596         IDList  *new;
597
598         if ( idl == NULL ) {
599                 return( NULL );
600         }
601
602         new = idl_alloc( idl->b_nmax );
603         SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
604             * sizeof(ID) );
605
606         return( new );
607 }
608
609 static IDList *
610 idl_min( IDList *a, IDList *b )
611 {
612         return( a->b_nids > b->b_nids ? b : a );
613 }
614
615 /*
616  * idl_intersection - return a intersection b
617  */
618
619 IDList *
620 idl_intersection(
621     Backend     *be,
622     IDList      *a,
623     IDList      *b
624 )
625 {
626         int     ai, bi, ni;
627         IDList  *n;
628
629         if ( a == NULL || b == NULL ) {
630                 return( NULL );
631         }
632         if ( ALLIDS( a ) ) {
633                 return( idl_dup( b ) );
634         }
635         if ( ALLIDS( b ) ) {
636                 return( idl_dup( a ) );
637         }
638
639         n = idl_dup( idl_min( a, b ) );
640
641         for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
642                 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
643                         ;       /* NULL */
644
645                 if ( bi == b->b_nids ) {
646                         break;
647                 }
648
649                 if ( b->b_ids[bi] == a->b_ids[ai] ) {
650                         n->b_ids[ni++] = a->b_ids[ai];
651                 }
652         }
653
654         if ( ni == 0 ) {
655                 idl_free( n );
656                 return( NULL );
657         }
658         n->b_nids = ni;
659
660         return( n );
661 }
662
663 /*
664  * idl_union - return a union b
665  */
666
667 IDList *
668 idl_union(
669     Backend     *be,
670     IDList      *a,
671     IDList      *b
672 )
673 {
674         int     ai, bi, ni;
675         IDList  *n;
676
677         if ( a == NULL ) {
678                 return( idl_dup( b ) );
679         }
680         if ( b == NULL ) {
681                 return( idl_dup( a ) );
682         }
683         if ( ALLIDS( a ) || ALLIDS( b ) ) {
684                 return( idl_allids( be ) );
685         }
686
687         if ( b->b_nids < a->b_nids ) {
688                 n = a;
689                 a = b;
690                 b = n;
691         }
692
693         n = idl_alloc( a->b_nids + b->b_nids );
694
695         for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
696                 if ( a->b_ids[ai] < b->b_ids[bi] ) {
697                         n->b_ids[ni++] = a->b_ids[ai++];
698                 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
699                         n->b_ids[ni++] = b->b_ids[bi++];
700                 } else {
701                         n->b_ids[ni++] = a->b_ids[ai];
702                         ai++, bi++;
703                 }
704         }
705
706         for ( ; ai < a->b_nids; ai++ ) {
707                 n->b_ids[ni++] = a->b_ids[ai];
708         }
709         for ( ; bi < b->b_nids; bi++ ) {
710                 n->b_ids[ni++] = b->b_ids[bi];
711         }
712         n->b_nids = ni;
713
714         return( n );
715 }
716
717 /*
718  * idl_notin - return a intersection ~b (or a minus b)
719  */
720
721 IDList *
722 idl_notin(
723     Backend     *be,
724     IDList      *a,
725     IDList      *b
726 )
727 {
728         int     ni, ai, bi;
729         IDList  *n;
730
731         if ( a == NULL ) {
732                 return( NULL );
733         }
734         if ( b == NULL || ALLIDS( b )) {
735                 return( idl_dup( a ) );
736         }
737
738         if ( ALLIDS( a ) ) {
739                 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
740                 ni = 0;
741
742                 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
743                     bi < b->b_nmax; ai++ ) {
744                         if ( b->b_ids[bi] == ai ) {
745                                 bi++;
746                         } else {
747                                 n->b_ids[ni++] = ai;
748                         }
749                 }
750
751                 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
752                         n->b_ids[ni++] = ai;
753                 }
754
755                 if ( ni == n->b_nmax ) {
756                         idl_free( n );
757                         return( idl_allids( be ) );
758                 } else {
759                         n->b_nids = ni;
760                         return( n );
761                 }
762         }
763
764         n = idl_dup( a );
765
766         ni = 0;
767         for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
768                 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
769                     bi++ ) {
770                         ;       /* NULL */
771                 }
772
773                 if ( bi == b->b_nids ) {
774                         break;
775                 }
776
777                 if ( b->b_ids[bi] != a->b_ids[ai] ) {
778                         n->b_ids[ni++] = a->b_ids[ai];
779                 }
780         }
781
782         for ( ; ai < a->b_nids; ai++ ) {
783                 n->b_ids[ni++] = a->b_ids[ai];
784         }
785         n->b_nids = ni;
786
787         return( n );
788 }
789
790 ID
791 idl_firstid( IDList *idl )
792 {
793         if ( idl == NULL || idl->b_nids == 0 ) {
794                 return( NOID );
795         }
796
797         if ( ALLIDS( idl ) ) {
798                 return( idl->b_nids == 1 ? NOID : 1 );
799         }
800
801         return( idl->b_ids[0] );
802 }
803
804 ID
805 idl_nextid( IDList *idl, ID id )
806 {
807         int     i;
808
809         if ( ALLIDS( idl ) ) {
810                 return( ++id < idl->b_nids ? id : NOID );
811         }
812
813         for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
814                 ;       /* NULL */
815         }
816         i++;
817
818         if ( i >= idl->b_nids ) {
819                 return( NOID );
820         } else {
821                 return( idl->b_ids[i] );
822         }
823 }