]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/idl.c
disable debug checks
[openldap] / servers / slapd / back-bdb / idl.c
1 /* idl.c - ldap id list handling routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "idl.h"
15
16 #define IDL_MAX(x,y)    ( x > y ? x : y )
17 #define IDL_MIN(x,y)    ( x < y ? x : y )
18
19 #define IDL_CMP(x,y)    ( x < y ? -1 : ( x > y ? 1 : 0 ) )
20
21 #if IDL_DEBUG > 0
22 static void idl_check( ID *ids )
23 {
24         if( BDB_IDL_IS_RANGE( ids ) ) {
25                 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
26         } else {
27                 ID i;
28                 for( i=1; i < ids[0]; i++ ) {
29                         assert( ids[i+1] > ids[i] );
30                 }
31         }
32 }
33
34 #if IDL_DEBUG > 1
35 static void idl_dump( ID *ids )
36 {
37         if( BDB_IDL_IS_RANGE( ids ) ) {
38                 Debug( LDAP_DEBUG_ANY,
39                         "IDL: range ( %ld - %ld )\n",
40                         (long) BDB_IDL_RANGE_FIRST( ids ),
41                         (long) BDB_IDL_RANGE_LAST( ids ) );
42
43         } else {
44                 ID i;
45                 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
46
47                 for( i=1; i<=ids[0]; i++ ) {
48                         if( i % 16 == 1 ) {
49                                 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
50                         }
51                         Debug( LDAP_DEBUG_ANY, "  %02lx", (long) ids[i], 0, 0 );
52                 }
53
54                 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
55         }
56
57         idl_check( ids );
58 }
59 #endif /* IDL_DEBUG > 1 */
60 #endif /* IDL_DEBUG > 0 */
61
62 unsigned bdb_idl_search( ID *ids, ID id )
63 {
64 #define IDL_BINARY_SEARCH 1
65 #ifdef IDL_BINARY_SEARCH
66         /*
67          * binary search of id in ids
68          * if found, returns position of id
69          * if not found, returns first postion greater than id
70          */
71         unsigned base = 0;
72         unsigned cursor = 0;
73         int val = 0;
74         unsigned n = ids[0];
75
76 #if IDL_DEBUG > 0
77         idl_check( ids );
78 #endif
79
80         while( 0 < n ) {
81                 int pivot = n >> 1;
82                 cursor = base + pivot;
83                 val = IDL_CMP( id, ids[cursor + 1] );
84
85                 if( val < 0 ) {
86                         n = pivot;
87
88                 } else if ( val > 0 ) {
89                         base = cursor + 1;
90                         n -= pivot + 1;
91
92                 } else {
93                         return cursor + 1;
94                 }
95         }
96         
97         if( val > 0 ) {
98                 return cursor + 2;
99         } else {
100                 return cursor + 1;
101         }
102
103 #else
104         /* (reverse) linear search */
105         int i;
106
107 #if IDL_DEBUG > 0
108         idl_check( ids );
109 #endif
110
111         for( i=ids[0]; i; i-- ) {
112                 if( id > ids[i] ) {
113                         break;
114                 }
115         }
116
117         return i+1;
118 #endif
119 }
120
121 int bdb_idl_insert( ID *ids, ID id )
122 {
123         unsigned x = bdb_idl_search( ids, id );
124
125 #if IDL_DEBUG > 1
126         Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
127         idl_dump( ids );
128 #elif IDL_DEBUG > 0
129         idl_check( ids );
130 #endif
131
132         assert( x > 0 );
133
134         if( x < 1 ) {
135                 /* internal error */
136                 return -2;
137         }
138
139         if ( x <= ids[0] && ids[x] == id ) {
140                 /* duplicate */
141                 return -1;
142         }
143
144         if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
145                 if( id < ids[1] ) {
146                         ids[1] = id;
147                         ids[2] = ids[ids[0]-1];
148                 } else if ( ids[ids[0]-1] < id ) {
149                         ids[2] = id;
150                 } else {
151                         ids[2] = ids[ids[0]-1];
152                 }
153                 ids[0] = NOID;
154         
155         } else {
156                 /* insert id */
157                 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
158                 ids[x] = id;
159         }
160
161 #if IDL_DEBUG > 1
162         idl_dump( ids );
163 #elif IDL_DEBUG > 0
164         idl_check( ids );
165 #endif
166
167         return 0;
168 }
169
170 static int idl_delete( ID *ids, ID id )
171 {
172         unsigned x = bdb_idl_search( ids, id );
173
174 #if IDL_DEBUG > 1
175         Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
176         idl_dump( ids );
177 #elif IDL_DEBUG > 0
178         idl_check( ids );
179 #endif
180
181         assert( x > 0 );
182
183         if( x <= 0 ) {
184                 /* internal error */
185                 return -2;
186         }
187
188         if( x > ids[0] || ids[x] != id ) {
189                 /* not found */
190                 return -1;
191
192         } else if ( --ids[0] == 0 ) {
193                 if( x != 1 ) {
194                         return -3;
195                 }
196
197         } else {
198                 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
199         }
200
201 #if IDL_DEBUG > 1
202         idl_dump( ids );
203 #elif IDL_DEBUG > 0
204         idl_check( ids );
205 #endif
206
207         return 0;
208 }
209
210 int
211 bdb_idl_fetch_key(
212         BackendDB       *be,
213         DB                      *db,
214         DB_TXN          *tid,
215         DBT                     *key,
216         ID                      *ids )
217 {
218         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
219         int rc;
220         DBT data;
221
222         assert( ids != NULL );
223
224         DBTzero( &data );
225
226 #ifdef BDB_IDL_MULTI
227         {
228                 DBC *cursor;
229                 ID buf[BDB_IDL_UM_SIZE];
230                 ID *i;
231                 void *ptr;
232                 size_t len;
233                 int rc2;
234                 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
235                 data.data = buf;
236                 data.ulen = BDB_IDL_UM_SIZEOF;
237                 data.flags = DB_DBT_USERMEM;
238
239                 if ( tid )
240                         flags |= DB_RMW;
241
242                 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
243                 if( rc != 0 ) {
244                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
245                                 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
246                         return rc;
247                 }
248                 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
249                 if (rc == 0) {
250                         i = ids;
251                         while (rc == 0) {
252                                 u_int8_t *j;
253
254                                 DB_MULTIPLE_INIT( ptr, &data );
255                                 while (ptr) {
256                                         DB_MULTIPLE_NEXT(ptr, &data, j, len);
257                                         if (j) {
258                                                 ++i;
259                                                 AC_MEMCPY( i, j, sizeof(ID) );
260                                         }
261                                 }
262                                 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
263                         }
264                         if ( rc == DB_NOTFOUND ) rc = 0;
265                         ids[0] = i - ids;
266                         /* On disk, a range is denoted by 0 in the first element */
267                         if (ids[1] == 0) {
268                                 if (ids[0] != BDB_IDL_RANGE_SIZE) {
269                                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
270                                                 "range size mismatch: expected %ld, got %ld\n",
271                                                 BDB_IDL_RANGE_SIZE, ids[0], 0 );
272                                         cursor->c_close( cursor );
273                                         return -1;
274                                 }
275                                 BDB_IDL_RANGE( ids, ids[2], ids[3] );
276                         }
277                         data.size = BDB_IDL_SIZEOF(ids);
278                 }
279                 rc2 = cursor->c_close( cursor );
280                 if (rc2) {
281                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
282                                 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
283                         return rc2;
284                 }
285         }
286 #else
287         data.data = ids;
288         data.ulen = BDB_IDL_UM_SIZEOF;
289         data.flags = DB_DBT_USERMEM;
290         /* fetch it */
291         rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
292 #endif
293
294         if( rc == DB_NOTFOUND ) {
295                 return rc;
296
297         } else if( rc != 0 ) {
298                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
299                         "get failed: %s (%d)\n",
300                         db_strerror(rc), rc, 0 );
301                 return rc;
302
303         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
304                 /* size not multiple of ID size */
305                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
306                         "odd size: expected %ld multiple, got %ld\n",
307                         (long) sizeof( ID ), (long) data.size, 0 );
308                 return -1;
309
310         } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
311                 /* size mismatch */
312                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
313                         "get size mismatch: expected %ld, got %ld\n",
314                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
315                 return -1;
316         }
317
318         return rc;
319 }
320
321 int
322 bdb_idl_insert_key(
323         BackendDB       *be,
324         DB                      *db,
325         DB_TXN          *tid,
326         DBT                     *key,
327         ID                      id )
328 {
329         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
330         int     rc;
331         DBT data;
332 #ifndef BDB_IDL_MULTI
333         ID ids[BDB_IDL_DB_SIZE];
334 #endif
335
336         /* for printable keys only */
337         Debug( LDAP_DEBUG_ARGS,
338                 "=> bdb_idl_insert_key: %s %ld\n",
339                 (char *)key->data, (long) id, 0 );
340
341         assert( id != NOID );
342
343         DBTzero( &data );
344 #ifdef BDB_IDL_MULTI
345         {
346                 ID buf[BDB_IDL_DB_MAX];
347                 DBC *cursor;
348                 ID lo, hi;
349                 char *err;
350
351                 data.size = sizeof( ID );
352                 data.ulen = data.size;
353                 data.flags = DB_DBT_USERMEM;
354
355                 rc = bdb_idl_fetch_key( be, db, tid, key, buf );
356                 if ( rc && rc != DB_NOTFOUND )
357                         return rc;
358
359                 /* If it never existed, or there's room in the current key,
360                  * just store it.
361                  */
362                 if ( rc == DB_NOTFOUND || ( !BDB_IDL_IS_RANGE(buf) &&
363                         BDB_IDL_N(buf) < BDB_IDL_DB_MAX ) ) {
364                         data.data = &id;
365                         rc = db->put( db, tid, key, &data, DB_NODUPDATA );
366                 } else if ( BDB_IDL_IS_RANGE(buf) ) {
367                         /* If it's a range and we're outside the boundaries,
368                          * rewrite the range boundaries.
369                          */
370                         if ( id < BDB_IDL_RANGE_FIRST(buf) ||
371                                 id > BDB_IDL_RANGE_LAST(buf) ) {
372                                 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
373                                 if ( rc != 0 ) {
374                                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
375                                                 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
376                                         return rc;
377                                 }
378                                 if ( id < BDB_IDL_RANGE_FIRST(buf) ) {
379                                         data.data = buf+1;
380                                 } else {
381                                         data.data = buf+2;
382                                 }
383                                 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH | DB_RMW );
384                                 if ( rc != 0 ) {
385                                         err = "c_get";
386 fail:                           Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
387                                                 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
388                                         if ( cursor ) cursor->c_close( cursor );
389                                         return rc;
390                                 }
391                                 data.data = &id;
392                                 /* We should have been able to just overwrite the old
393                                  * value with the new, but apparently we have to delete
394                                  * it first.
395                                  */
396                                 rc = cursor->c_del( cursor, 0 );
397                                 if ( rc ) {
398                                         err = "c_del";
399                                         goto fail;
400                                 }
401                                 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
402                                 if ( rc ) {
403                                         err = "c_put";
404                                         goto fail;
405                                 }
406                                 rc = cursor->c_close( cursor );
407                                 if ( rc ) {
408                                         cursor = NULL;
409                                         err = "c_close";
410                                         goto fail;
411                                 }
412                         }
413                 } else {                /* convert to a range */
414                         lo = BDB_IDL_FIRST(buf);
415                         hi = BDB_IDL_LAST(buf);
416
417                         if (id < lo)
418                                 lo = id;
419                         else if (id > hi)
420                                 hi = id;
421
422                         cursor = NULL;
423
424                         /* Delete all of the old IDL so we can replace with a range */
425                         rc = db->del( db, tid, key, 0 );
426                         if ( rc ) {
427                                 err = "del";
428                                 goto fail;
429                         }
430
431                         /* Write the range */
432                         data.data = &id;
433                         id = 0;
434                         rc = db->put( db, tid, key, &data, 0 );
435                         if ( rc ) {
436                                 err = "put #1";
437                                 goto fail;
438                         }
439                         id = lo;
440                         rc = db->put( db, tid, key, &data, 0 );
441                         if ( rc ) {
442                                 err = "put #2";
443                                 goto fail;
444                         }
445                         id = hi;
446                         rc = db->put( db, tid, key, &data, 0 );
447                         if ( rc ) {
448                                 err = "put #3";
449                                 goto fail;
450                         }
451                 }
452         }
453 #else
454         data.data = ids;
455         data.ulen = sizeof ids;
456         data.flags = DB_DBT_USERMEM;
457
458         /* fetch the key for read/modify/write */
459         rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
460
461         if( rc == DB_NOTFOUND ) {
462                 ids[0] = 1;
463                 ids[1] = id;
464                 data.size = 2 * sizeof( ID );
465
466         } else if ( rc != 0 ) {
467                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
468                         "get failed: %s (%d)\n",
469                         db_strerror(rc), rc, 0 );
470                 return rc;
471
472         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
473                 /* size not multiple of ID size */
474                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
475                         "odd size: expected %ld multiple, got %ld\n",
476                         (long) sizeof( ID ), (long) data.size, 0 );
477                 return -1;
478         
479         } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
480                 /* size mismatch */
481                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
482                         "get size mismatch: expected %ld, got %ld\n",
483                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
484                 return -1;
485
486         } else if ( BDB_IDL_IS_RANGE(ids) ) {
487                 if( id < ids[1] ) {
488                         ids[1] = id;
489                 } else if ( ids[2] > id ) {
490                         ids[2] = id;
491                 } else {
492                         return 0;
493                 }
494
495         } else {
496                 rc = bdb_idl_insert( ids, id );
497
498                 if( rc == -1 ) {
499                         Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
500                                 0, 0, 0 );
501                         return 0;
502                 }
503                 if( rc != 0 ) {
504                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
505                                 "bdb_idl_insert failed (%d)\n",
506                                 rc, 0, 0 );
507                         
508                         return rc;
509                 }
510
511                 data.size = BDB_IDL_SIZEOF( ids );
512         }
513
514         /* store the key */
515         rc = db->put( db, tid, key, &data, 0 );
516 #endif
517         if( rc == DB_KEYEXIST ) rc = 0;
518
519         if( rc != 0 ) {
520                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
521                         "put failed: %s (%d)\n",
522                         db_strerror(rc), rc, 0 );
523         }
524         return rc;
525 }
526
527 int
528 bdb_idl_delete_key(
529         BackendDB       *be,
530         DB                      *db,
531         DB_TXN          *tid,
532         DBT                     *key,
533         ID                      id )
534 {
535         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
536         int     rc;
537         DBT data;
538 #ifndef BDB_IDL_MULTI
539         ID ids[BDB_IDL_DB_SIZE];
540 #endif
541
542         /* for printable keys only */
543         Debug( LDAP_DEBUG_ARGS,
544                 "=> bdb_idl_delete_key: %s %ld\n",
545                 (char *)key->data, (long) id, 0 );
546
547         assert( id != NOID );
548
549         DBTzero( &data );
550 #ifdef BDB_IDL_MULTI
551         {
552                 DBC *cursor;
553
554                 data.data = &id;
555                 data.size = sizeof( id );
556                 data.ulen = data.size;
557                 data.flags = DB_DBT_USERMEM;
558
559                 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
560                 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
561                         DB_GET_BOTH | DB_RMW  );
562                 if (rc == 0)
563                         rc = cursor->c_del( cursor, 0 );
564                 rc = cursor->c_close( cursor );
565         }
566 #else
567         data.data = ids;
568         data.ulen = sizeof( ids );
569         data.flags = DB_DBT_USERMEM;
570
571         /* fetch the key for read/modify/write */
572         rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
573
574         if ( rc != 0 ) {
575                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
576                         "get failed: %s (%d)\n",
577                         db_strerror(rc), rc, 0 );
578                 return rc;
579
580         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
581                 /* size not multiple of ID size */
582                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
583                         "odd size: expected %ld multiple, got %ld\n",
584                         (long) sizeof( ID ), (long) data.size, 0 );
585                 return -1;
586         
587         } else if ( BDB_IDL_IS_RANGE(ids) ) {
588                 return 0;
589
590         } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
591                 /* size mismatch */
592                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
593                         "get size mismatch: expected %ld, got %ld\n",
594                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
595                 return -1;
596
597         } else {
598                 rc = idl_delete( ids, id );
599
600                 if( rc != 0 ) {
601                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
602                                 "idl_delete failed (%d)\n",
603                                 rc, 0, 0 );
604                         return rc;
605                 }
606
607                 if( ids[0] == 0 ) {
608                         /* delete the key */
609                         rc = db->del( db, tid, key, 0 );
610                         if( rc != 0 ) {
611                                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
612                                         "delete failed: %s (%d)\n",
613                                         db_strerror(rc), rc, 0 );
614                         }
615                         return rc;
616                 }
617
618                 data.size = (ids[0]+1) * sizeof( ID );
619         }
620
621         /* store the key */
622         rc = db->put( db, tid, key, &data, 0 );
623
624 #endif /* BDB_IDL_MULTI */
625
626         if( rc != 0 ) {
627                 Debug( LDAP_DEBUG_ANY,
628                         "=> bdb_idl_delete_key: put failed: %s (%d)\n",
629                         db_strerror(rc), rc, 0 );
630         }
631
632         return rc;
633 }
634
635
636 /*
637  * idl_intersection - return a = a intersection b
638  */
639 int
640 bdb_idl_intersection(
641         ID *a,
642         ID *b )
643 {
644         ID ida, idb;
645         ID idmax, idmin;
646         ID cursora = 0, cursorb = 0, cursorc;
647         int swap = 0;
648
649         if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
650                 a[0] = 0;
651                 return 0;
652         }
653
654         idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
655         idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
656         if ( idmin > idmax ) {
657                 a[0] = 0;
658                 return 0;
659         } else if ( idmin == idmax ) {
660                 a[0] = 1;
661                 a[1] = idmin;
662                 return 0;
663         }
664
665         if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
666                 a[1] = idmin;
667                 a[2] = idmax;
668                 return 0;
669         }
670
671         if ( BDB_IDL_IS_RANGE( a ) ) {
672                 ID *tmp = a;
673                 a = b;
674                 b = tmp;
675                 swap = 1;
676         }
677
678         if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
679                 BDB_IDL_LAST( b ) >= idmax) {
680                 if (idmax - idmin + 1 == a[0])
681                 {
682                         a[0] = NOID;
683                         a[1] = idmin;
684                         a[2] = idmax;
685                 }
686                 goto done;
687         }
688
689         ida = bdb_idl_first( a, &cursora );
690         idb = bdb_idl_first( b, &cursorb );
691         cursorc = 0;
692
693         while( ida < idmin )
694                 ida = bdb_idl_next( a, &cursora );
695         while( idb < idmin )
696                 idb = bdb_idl_next( b, &cursorb );
697
698         while( ida <= idmax || idb <= idmax ) {
699                 if( ida == idb ) {
700                         a[++cursorc] = ida;
701                         ida = bdb_idl_next( a, &cursora );
702                         idb = bdb_idl_next( b, &cursorb );
703                 } else if ( ida < idb ) {
704                         ida = bdb_idl_next( a, &cursora );
705                 } else {
706                         idb = bdb_idl_next( b, &cursorb );
707                 }
708         }
709         a[0] = cursorc;
710 done:
711         if (swap)
712                 BDB_IDL_CPY( b, a );
713
714         return 0;
715 }
716
717
718 /*
719  * idl_union - return a = a union b
720  */
721 int
722 bdb_idl_union(
723         ID      *a,
724         ID      *b )
725 {
726         ID ida, idb;
727         ID cursora = 0, cursorb = 0, cursorc;
728
729         if ( BDB_IDL_IS_ZERO( b ) ) {
730                 return 0;
731         }
732
733         if ( BDB_IDL_IS_ZERO( a ) ) {
734                 BDB_IDL_CPY( a, b );
735                 return 0;
736         }
737
738         if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
739 over:           a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
740                 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
741                 return 0;
742         }
743
744         ida = bdb_idl_first( a, &cursora );
745         idb = bdb_idl_first( b, &cursorb );
746
747         cursorc = b[0];
748
749         /* The distinct elements of a are cat'd to b */
750         while( ida != NOID || idb != NOID ) {
751                 if ( ida < idb ) {
752                         if( ++cursorc > BDB_IDL_UM_MAX ) {
753                                 a[0] = NOID;
754                                 goto over;
755                         }
756                         b[cursorc] = ida;
757                         ida = bdb_idl_next( a, &cursora );
758
759                 } else {
760                         if ( ida == idb )
761                                 ida = bdb_idl_next( a, &cursora );
762                         idb = bdb_idl_next( b, &cursorb );
763                 }
764         }
765
766         /* b is copied back to a in sorted order */
767         a[0] = cursorc;
768         cursora = 1;
769         cursorb = 1;
770         cursorc = b[0]+1;
771         while (cursorb <= b[0] || cursorc <= a[0]) {
772                 if (cursorc > a[0])
773                         idb = NOID;
774                 else
775                         idb = b[cursorc];
776                 if (b[cursorb] < idb)
777                         a[cursora++] = b[cursorb++];
778                 else {
779                         a[cursora++] = idb;
780                         cursorc++;
781                 }
782         }
783
784         return 0;
785 }
786
787
788 #if 0
789 /*
790  * bdb_idl_notin - return a intersection ~b (or a minus b)
791  */
792 int
793 bdb_idl_notin(
794         ID      *a,
795         ID      *b,
796         ID *ids )
797 {
798         ID ida, idb;
799         ID cursora = 0, cursorb = 0;
800
801         if( BDB_IDL_IS_ZERO( a ) ||
802                 BDB_IDL_IS_ZERO( b ) ||
803                 BDB_IDL_IS_RANGE( b ) )
804         {
805                 BDB_IDL_CPY( ids, a );
806                 return 0;
807         }
808
809         if( BDB_IDL_IS_RANGE( a ) ) {
810                 BDB_IDL_CPY( ids, a );
811                 return 0;
812         }
813
814         ida = bdb_idl_first( a, &cursora ),
815         idb = bdb_idl_first( b, &cursorb );
816
817         ids[0] = 0;
818
819         while( ida != NOID ) {
820                 if ( idb == NOID ) {
821                         /* we could shortcut this */
822                         ids[++ids[0]] = ida;
823                         ida = bdb_idl_next( a, &cursora );
824
825                 } else if ( ida < idb ) {
826                         ids[++ids[0]] = ida;
827                         ida = bdb_idl_next( a, &cursora );
828
829                 } else if ( ida > idb ) {
830                         idb = bdb_idl_next( b, &cursorb );
831
832                 } else {
833                         ida = bdb_idl_next( a, &cursora );
834                         idb = bdb_idl_next( b, &cursorb );
835                 }
836         }
837
838         return 0;
839 }
840 #endif
841
842 ID bdb_idl_first( ID *ids, ID *cursor )
843 {
844         ID pos;
845
846         if ( ids[0] == 0 ) {
847                 *cursor = NOID;
848                 return NOID;
849         }
850
851         if ( BDB_IDL_IS_RANGE( ids ) ) {
852                 if( *cursor < ids[1] ) {
853                         *cursor = ids[1];
854                 }
855                 return *cursor;
856         }
857
858         if ( *cursor == 0 )
859                 pos = 1;
860         else
861                 pos = bdb_idl_search( ids, *cursor );
862
863         if( pos > ids[0] ) {
864                 return NOID;
865         }
866
867         *cursor = pos;
868         return ids[pos];
869 }
870
871 ID bdb_idl_next( ID *ids, ID *cursor )
872 {
873         if ( BDB_IDL_IS_RANGE( ids ) ) {
874                 if( ids[2] < ++(*cursor) ) {
875                         return NOID;
876                 }
877                 return *cursor;
878         }
879
880         if ( ++(*cursor) <= ids[0] ) {
881                 return ids[*cursor];
882         }
883
884         return NOID;
885 }