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