1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 #define IDL_MAX(x,y) ( x > y ? x : y )
26 #define IDL_MIN(x,y) ( x < y ? x : y )
28 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
31 #define IDL_LRU_DELETE( bdb, e ) do { \
32 if ( e->idl_lru_prev != NULL ) { \
33 e->idl_lru_prev->idl_lru_next = e->idl_lru_next; \
35 bdb->bi_idl_lru_head = e->idl_lru_next; \
37 if ( e->idl_lru_next != NULL ) { \
38 e->idl_lru_next->idl_lru_prev = e->idl_lru_prev; \
40 bdb->bi_idl_lru_tail = e->idl_lru_prev; \
44 #define IDL_LRU_ADD( bdb, e ) do { \
45 e->idl_lru_next = bdb->bi_idl_lru_head; \
46 if ( e->idl_lru_next != NULL ) { \
47 e->idl_lru_next->idl_lru_prev = (e); \
49 (bdb)->bi_idl_lru_head = (e); \
50 e->idl_lru_prev = NULL; \
51 if ( (bdb)->bi_idl_lru_tail == NULL ) { \
52 (bdb)->bi_idl_lru_tail = (e); \
57 bdb_idl_entry_cmp( const void *v_idl1, const void *v_idl2 )
59 const bdb_idl_cache_entry_t *idl1 = v_idl1, *idl2 = v_idl2;
62 if ((rc = SLAP_PTRCMP( idl1->db, idl2->db ))) return rc;
63 if ((rc = idl1->kstr.bv_len - idl2->kstr.bv_len )) return rc;
64 return ( memcmp ( idl1->kstr.bv_val, idl2->kstr.bv_val , idl1->kstr.bv_len ) );
69 static void idl_check( ID *ids )
71 if( BDB_IDL_IS_RANGE( ids ) ) {
72 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
75 for( i=1; i < ids[0]; i++ ) {
76 assert( ids[i+1] > ids[i] );
82 static void idl_dump( ID *ids )
84 if( BDB_IDL_IS_RANGE( ids ) ) {
86 LDAP_LOG( INDEX, INFO, "IDL: range (%ld - %ld)\n",
87 (long) BDB_IDL_RANGE_FIRST( ids ),
88 (long) BDB_IDL_RANGE_LAST( ids ), 0 );
90 Debug( LDAP_DEBUG_ANY,
91 "IDL: range ( %ld - %ld )\n",
92 (long) BDB_IDL_RANGE_FIRST( ids ),
93 (long) BDB_IDL_RANGE_LAST( ids ) );
99 LDAP_LOG( INDEX, INFO, "IDL: size %ld", (long) ids[0], 0, 0 );
101 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
104 for( i=1; i<=ids[0]; i++ ) {
106 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
109 LDAP_LOG( INDEX, INFO, "%02lx",(long)ids[i], 0, 0 );
111 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
115 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
120 #endif /* IDL_DEBUG > 1 */
121 #endif /* IDL_DEBUG > 0 */
123 unsigned bdb_idl_search( ID *ids, ID id )
125 #define IDL_BINARY_SEARCH 1
126 #ifdef IDL_BINARY_SEARCH
128 * binary search of id in ids
129 * if found, returns position of id
130 * if not found, returns first postion greater than id
143 cursor = base + pivot;
144 val = IDL_CMP( id, ids[cursor + 1] );
149 } else if ( val > 0 ) {
165 /* (reverse) linear search */
172 for( i=ids[0]; i; i-- ) {
182 int bdb_idl_insert( ID *ids, ID id )
188 LDAP_LOG( INDEX, DETAIL1, "insert: %04lx at %d\n", (long) id, x, 0 );
190 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
197 if (BDB_IDL_IS_RANGE( ids )) {
198 /* if already in range, treat as a dup */
199 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
201 if (id < BDB_IDL_FIRST(ids))
203 else if (id > BDB_IDL_LAST(ids))
208 x = bdb_idl_search( ids, id );
216 if ( x <= ids[0] && ids[x] == id ) {
221 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
224 ids[2] = ids[ids[0]-1];
225 } else if ( ids[ids[0]-1] < id ) {
228 ids[2] = ids[ids[0]-1];
234 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
248 static int idl_delete( ID *ids, ID id )
250 unsigned x = bdb_idl_search( ids, id );
254 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
256 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
270 if( x > ids[0] || ids[x] != id ) {
274 } else if ( --ids[0] == 0 ) {
280 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
298 if ( key->size == sizeof( ID ) ) {
299 unsigned char *c = key->data;
300 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
307 #ifdef SLAP_IDL_CACHE
309 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
310 * copy the cached IDL into it, otherwise just return the status.
314 struct bdb_info *bdb,
319 bdb_idl_cache_entry_t idl_tmp;
320 bdb_idl_cache_entry_t *matched_idl_entry;
322 DBT2bv( key, &idl_tmp.kstr );
324 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
325 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
327 if ( matched_idl_entry != NULL ) {
328 if ( matched_idl_entry->idl && ids )
329 BDB_IDL_CPY( ids, matched_idl_entry->idl );
330 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
331 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
332 IDL_LRU_DELETE( bdb, matched_idl_entry );
333 IDL_LRU_ADD( bdb, matched_idl_entry );
334 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
335 if ( matched_idl_entry->idl )
340 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
342 return LDAP_NO_SUCH_OBJECT;
347 struct bdb_info *bdb,
353 bdb_idl_cache_entry_t idl_tmp;
354 bdb_idl_cache_entry_t *ee;
356 DBT2bv( key, &idl_tmp.kstr );
358 ee = (bdb_idl_cache_entry_t *) ch_malloc(
359 sizeof( bdb_idl_cache_entry_t ) );
361 if ( rc == DB_NOTFOUND) {
364 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
365 BDB_IDL_CPY( ee->idl, ids );
367 ee->idl_lru_prev = NULL;
368 ee->idl_lru_next = NULL;
369 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
370 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
371 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
372 bdb_idl_entry_cmp, avl_dup_error ))
374 ch_free( ee->kstr.bv_val );
377 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
380 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
381 IDL_LRU_ADD( bdb, ee );
382 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
384 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
385 ee = bdb->bi_idl_lru_tail;
386 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
387 bdb_idl_entry_cmp ) == NULL ) {
389 LDAP_LOG( INDEX, ERR,
390 "bdb_idl_cache_put: AVL delete failed\n",
393 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
394 "AVL delete failed\n",
398 IDL_LRU_DELETE( bdb, ee );
400 --bdb->bi_idl_cache_size;
401 ch_free( ee->kstr.bv_val );
407 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
408 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
413 struct bdb_info *bdb,
417 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
418 DBT2bv( key, &idl_tmp.kstr );
420 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
421 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
423 if ( matched_idl_entry != NULL ) {
424 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
425 bdb_idl_entry_cmp ) == NULL ) {
427 LDAP_LOG( INDEX, ERR,
428 "bdb_idl_cache_del: AVL delete failed\n",
431 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
432 "AVL delete failed\n",
436 --bdb->bi_idl_cache_size;
437 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
438 IDL_LRU_DELETE( bdb, matched_idl_entry );
439 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
440 free( matched_idl_entry->kstr.bv_val );
441 if ( matched_idl_entry->idl )
442 free( matched_idl_entry->idl );
443 free( matched_idl_entry );
445 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
457 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
465 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
467 /* If using BerkeleyDB 4.0, the buf must be large enough to
468 * grab the entire IDL in one get(), otherwise BDB will leak
469 * resources on subsequent get's. We can safely call get()
470 * twice - once for the data, and once to get the DB_NOTFOUND
471 * result meaning there's no more data. See ITS#2040 for details.
472 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
473 * stack space is too limited.
475 * configure now requires Berkeley DB 4.1.
477 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
478 # define BDB_ENOUGH 5
480 # define BDB_ENOUGH 1
482 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
487 LDAP_LOG( INDEX, ARGS,
488 "bdb_idl_fetch_key: %s\n",
489 bdb_show_key( key, keybuf ), 0, 0 );
491 Debug( LDAP_DEBUG_ARGS,
492 "bdb_idl_fetch_key: %s\n",
493 bdb_show_key( key, keybuf ), 0, 0 );
496 assert( ids != NULL );
498 #ifdef SLAP_IDL_CACHE
499 if ( bdb->bi_idl_cache_size ) {
500 rc = bdb_idl_cache_get( bdb, db, key, ids );
501 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
508 data.ulen = sizeof(buf);
509 data.flags = DB_DBT_USERMEM;
511 if ( tid ) flags |= DB_RMW;
513 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
516 LDAP_LOG( INDEX, ERR,
517 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
518 db_strerror(rc), rc, 0 );
520 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
521 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
526 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
532 DB_MULTIPLE_INIT( ptr, &data );
534 DB_MULTIPLE_NEXT(ptr, &data, j, len);
537 AC_MEMCPY( i, j, sizeof(ID) );
540 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
542 if ( rc == DB_NOTFOUND ) rc = 0;
544 /* On disk, a range is denoted by 0 in the first element */
546 if (ids[0] != BDB_IDL_RANGE_SIZE) {
548 LDAP_LOG( INDEX, ERR,
549 "=> bdb_idl_fetch_key: range size mismatch: "
550 "expected %ld, got %ld\n",
551 BDB_IDL_RANGE_SIZE, ids[0], 0 );
553 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
554 "range size mismatch: expected %d, got %ld\n",
555 BDB_IDL_RANGE_SIZE, ids[0], 0 );
557 cursor->c_close( cursor );
560 BDB_IDL_RANGE( ids, ids[2], ids[3] );
562 data.size = BDB_IDL_SIZEOF(ids);
565 rc2 = cursor->c_close( cursor );
568 LDAP_LOG( INDEX, ERR,
569 "bdb_idl_fetch_key: close failed: %s (%d)\n",
570 db_strerror(rc2), rc2, 0 );
572 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
573 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
578 if( rc == DB_NOTFOUND ) {
579 #ifndef SLAP_IDL_CACHE
583 } else if( rc != 0 ) {
585 LDAP_LOG( INDEX, ERR,
586 "bdb_idl_fetch_key: get failed: %s (%d)\n",
587 db_strerror(rc), rc, 0 );
589 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
590 "get failed: %s (%d)\n",
591 db_strerror(rc), rc, 0 );
595 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
596 /* size not multiple of ID size */
598 LDAP_LOG( INDEX, ERR,
599 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
600 (long) sizeof( ID ), (long) data.size, 0 );
602 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
603 "odd size: expected %ld multiple, got %ld\n",
604 (long) sizeof( ID ), (long) data.size, 0 );
608 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
611 LDAP_LOG( INDEX, ERR,
612 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
613 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
615 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
616 "get size mismatch: expected %ld, got %ld\n",
617 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
622 #ifdef SLAP_IDL_CACHE
623 if ( bdb->bi_idl_cache_max_size ) {
624 bdb_idl_cache_put( bdb, db, key, ids, rc );
640 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
650 LDAP_LOG( INDEX, ARGS,
651 "bdb_idl_insert_key: %lx %s\n",
652 (long) id, bdb_show_key( key, buf ), 0 );
654 Debug( LDAP_DEBUG_ARGS,
655 "bdb_idl_insert_key: %lx %s\n",
656 (long) id, bdb_show_key( key, buf ), 0 );
660 assert( id != NOID );
662 #ifdef SLAP_IDL_CACHE
663 if ( bdb->bi_idl_cache_size ) {
664 bdb_idl_cache_del( bdb, db, key );
669 data.size = sizeof( ID );
670 data.ulen = data.size;
671 data.flags = DB_DBT_USERMEM;
673 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
676 LDAP_LOG( INDEX, ERR,
677 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
678 db_strerror(rc), rc, 0 );
680 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
681 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
686 /* Fetch the first data item for this key, to see if it
687 * exists and if it's a range.
689 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
693 /* not a range, count the number of items */
695 rc = cursor->c_count( cursor, &count, 0 );
700 if ( count >= BDB_IDL_DB_MAX ) {
701 /* No room, convert to a range */
704 key2.dlen = key2.ulen;
705 key2.flags |= DB_DBT_PARTIAL;
709 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
710 if ( rc != 0 && rc != DB_NOTFOUND ) {
711 err = "c_get next_nodup";
714 if ( rc == DB_NOTFOUND ) {
715 rc = cursor->c_get( cursor, key, &data, DB_LAST );
721 rc = cursor->c_get( cursor, key, &data, DB_PREV );
731 rc = db->del( db, tid, key, 0 );
738 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
744 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
750 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
756 /* There's room, just store it */
760 /* It's a range, see if we need to rewrite
765 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
772 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
778 if ( id < lo || id > hi ) {
779 /* Delete the current lo/hi */
780 rc = cursor->c_del( cursor, 0 );
786 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
793 } else if ( rc == DB_NOTFOUND ) {
794 put1: data.data = &id;
795 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
796 /* Don't worry if it's already there */
797 if ( rc != 0 && rc != DB_KEYEXIST ) {
802 /* initial c_get failed, nothing was done */
805 LDAP_LOG( INDEX, ERR,
806 "bdb_idl_insert_key: %s failed: %s (%d)\n",
807 err, db_strerror(rc), rc );
809 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
810 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
812 cursor->c_close( cursor );
815 rc = cursor->c_close( cursor );
818 LDAP_LOG( INDEX, ERR,
819 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
820 db_strerror(rc), rc, 0 );
822 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
823 "c_close failed: %s (%d)\n",
824 db_strerror(rc), rc, 0 );
838 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
848 LDAP_LOG( INDEX, ARGS,
849 "bdb_idl_delete_key: %lx %s\n",
850 (long) id, bdb_show_key( key, buf ), 0 );
852 Debug( LDAP_DEBUG_ARGS,
853 "bdb_idl_delete_key: %lx %s\n",
854 (long) id, bdb_show_key( key, buf ), 0 );
857 assert( id != NOID );
859 #ifdef SLAP_IDL_CACHE
860 if ( bdb->bi_idl_cache_max_size ) {
861 bdb_idl_cache_del( bdb, db, key );
867 data.size = sizeof( id );
868 data.ulen = data.size;
869 data.flags = DB_DBT_USERMEM;
871 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
874 LDAP_LOG( INDEX, ERR,
875 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
876 db_strerror(rc), rc, 0 );
878 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
879 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
883 /* Fetch the first data item for this key, to see if it
884 * exists and if it's a range.
886 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
890 /* Not a range, just delete it */
892 /* position to correct item */
894 rc = cursor->c_get( cursor, key, &data,
895 DB_GET_BOTH | DB_RMW );
901 rc = cursor->c_del( cursor, 0 );
907 /* It's a range, see if we need to rewrite
911 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
917 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
922 if ( id == lo || id == hi ) {
926 } else if ( id == hi ) {
931 /* The range has collapsed... */
932 rc = db->del( db, tid, key, 0 );
939 /* reposition on lo slot */
941 cursor->c_get( cursor, key, &data, DB_PREV );
944 rc = cursor->c_del( cursor, 0 );
952 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
961 /* initial c_get failed, nothing was done */
963 if ( rc != DB_NOTFOUND ) {
965 LDAP_LOG( INDEX, ERR,
966 "bdb_idl_delete_key: %s failed: %s (%d)\n",
967 err, db_strerror(rc), rc );
969 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
970 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
973 cursor->c_close( cursor );
976 rc = cursor->c_close( cursor );
979 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
980 db_strerror(rc), rc, 0 );
982 Debug( LDAP_DEBUG_ANY,
983 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
984 db_strerror(rc), rc, 0 );
993 * idl_intersection - return a = a intersection b
996 bdb_idl_intersection(
1002 ID cursora = 0, cursorb = 0, cursorc;
1005 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
1010 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1011 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1012 if ( idmin > idmax ) {
1015 } else if ( idmin == idmax ) {
1021 if ( BDB_IDL_IS_RANGE( a ) ) {
1022 if ( BDB_IDL_IS_RANGE(b) ) {
1023 /* If both are ranges, just shrink the boundaries */
1028 /* Else swap so that b is the range, a is a list */
1036 /* If a range completely covers the list, the result is
1037 * just the list. If idmin to idmax is contiguous, just
1038 * turn it into a range.
1040 if ( BDB_IDL_IS_RANGE( b )
1041 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1042 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1043 if (idmax - idmin + 1 == a[0])
1052 /* Fine, do the intersection one element at a time.
1053 * First advance to idmin in both IDLs.
1055 cursora = cursorb = idmin;
1056 ida = bdb_idl_first( a, &cursora );
1057 idb = bdb_idl_first( b, &cursorb );
1060 while( ida <= idmax || idb <= idmax ) {
1063 ida = bdb_idl_next( a, &cursora );
1064 idb = bdb_idl_next( b, &cursorb );
1065 } else if ( ida < idb ) {
1066 ida = bdb_idl_next( a, &cursora );
1068 idb = bdb_idl_next( b, &cursorb );
1074 BDB_IDL_CPY( b, a );
1081 * idl_union - return a = a union b
1089 ID cursora = 0, cursorb = 0, cursorc;
1091 if ( BDB_IDL_IS_ZERO( b ) ) {
1095 if ( BDB_IDL_IS_ZERO( a ) ) {
1096 BDB_IDL_CPY( a, b );
1100 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1101 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1102 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1109 ida = bdb_idl_first( a, &cursora );
1110 idb = bdb_idl_first( b, &cursorb );
1114 /* The distinct elements of a are cat'd to b */
1115 while( ida != NOID || idb != NOID ) {
1117 if( ++cursorc > BDB_IDL_UM_MAX ) {
1121 ida = bdb_idl_next( a, &cursora );
1125 ida = bdb_idl_next( a, &cursora );
1126 idb = bdb_idl_next( b, &cursorb );
1130 /* b is copied back to a in sorted order */
1135 while (cursorb <= b[0] || cursorc <= a[0]) {
1140 if (cursorb <= b[0] && b[cursorb] < idb)
1141 a[cursora++] = b[cursorb++];
1154 * bdb_idl_notin - return a intersection ~b (or a minus b)
1163 ID cursora = 0, cursorb = 0;
1165 if( BDB_IDL_IS_ZERO( a ) ||
1166 BDB_IDL_IS_ZERO( b ) ||
1167 BDB_IDL_IS_RANGE( b ) )
1169 BDB_IDL_CPY( ids, a );
1173 if( BDB_IDL_IS_RANGE( a ) ) {
1174 BDB_IDL_CPY( ids, a );
1178 ida = bdb_idl_first( a, &cursora ),
1179 idb = bdb_idl_first( b, &cursorb );
1183 while( ida != NOID ) {
1184 if ( idb == NOID ) {
1185 /* we could shortcut this */
1186 ids[++ids[0]] = ida;
1187 ida = bdb_idl_next( a, &cursora );
1189 } else if ( ida < idb ) {
1190 ids[++ids[0]] = ida;
1191 ida = bdb_idl_next( a, &cursora );
1193 } else if ( ida > idb ) {
1194 idb = bdb_idl_next( b, &cursorb );
1197 ida = bdb_idl_next( a, &cursora );
1198 idb = bdb_idl_next( b, &cursorb );
1206 ID bdb_idl_first( ID *ids, ID *cursor )
1210 if ( ids[0] == 0 ) {
1215 if ( BDB_IDL_IS_RANGE( ids ) ) {
1216 if( *cursor < ids[1] ) {
1225 pos = bdb_idl_search( ids, *cursor );
1227 if( pos > ids[0] ) {
1235 ID bdb_idl_next( ID *ids, ID *cursor )
1237 if ( BDB_IDL_IS_RANGE( ids ) ) {
1238 if( ids[2] < ++(*cursor) ) {
1244 if ( ++(*cursor) <= ids[0] ) {
1245 return ids[*cursor];