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