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