1 /* idl.c - ldap id list handling routines */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 #define IDL_MAX(x,y) ( x > y ? x : y )
17 #define IDL_MIN(x,y) ( x < y ? x : y )
19 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
22 #define IDL_LRU_DELETE( bdb, e ) do { \
23 if ( e->idl_lru_prev != NULL ) { \
24 e->idl_lru_prev->idl_lru_next = e->idl_lru_next; \
26 bdb->bi_idl_lru_head = e->idl_lru_next; \
28 if ( e->idl_lru_next != NULL ) { \
29 e->idl_lru_next->idl_lru_prev = e->idl_lru_prev; \
31 bdb->bi_idl_lru_tail = e->idl_lru_prev; \
35 #define IDL_LRU_ADD( bdb, e ) do { \
36 e->idl_lru_next = bdb->bi_idl_lru_head; \
37 if ( e->idl_lru_next != NULL ) { \
38 e->idl_lru_next->idl_lru_prev = (e); \
40 (bdb)->bi_idl_lru_head = (e); \
41 e->idl_lru_prev = NULL; \
42 if ( (bdb)->bi_idl_lru_tail == NULL ) { \
43 (bdb)->bi_idl_lru_tail = (e); \
48 bdb_idl_entry_cmp( const void *v_idl1, const void *v_idl2 )
50 const bdb_idl_cache_entry_t *idl1 = v_idl1, *idl2 = v_idl2;
53 if ((rc = idl1->db - idl2->db )) return rc;
54 if ((rc = idl1->kstr.bv_len - idl2->kstr.bv_len )) return rc;
55 return ( memcmp ( idl1->kstr.bv_val, idl2->kstr.bv_val , idl1->kstr.bv_len ) );
60 static void idl_check( ID *ids )
62 if( BDB_IDL_IS_RANGE( ids ) ) {
63 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
66 for( i=1; i < ids[0]; i++ ) {
67 assert( ids[i+1] > ids[i] );
73 static void idl_dump( ID *ids )
75 if( BDB_IDL_IS_RANGE( ids ) ) {
77 LDAP_LOG( INDEX, INFO, "IDL: range (%ld - %ld)\n",
78 (long) BDB_IDL_RANGE_FIRST( ids ),
79 (long) BDB_IDL_RANGE_LAST( ids ), 0 );
81 Debug( LDAP_DEBUG_ANY,
82 "IDL: range ( %ld - %ld )\n",
83 (long) BDB_IDL_RANGE_FIRST( ids ),
84 (long) BDB_IDL_RANGE_LAST( ids ) );
90 LDAP_LOG( INDEX, INFO, "IDL: size %ld", (long) ids[0], 0, 0 );
92 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
95 for( i=1; i<=ids[0]; i++ ) {
97 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
100 LDAP_LOG( INDEX, INFO, "%02lx",(long)ids[i], 0, 0 );
102 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
106 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
111 #endif /* IDL_DEBUG > 1 */
112 #endif /* IDL_DEBUG > 0 */
114 unsigned bdb_idl_search( ID *ids, ID id )
116 #define IDL_BINARY_SEARCH 1
117 #ifdef IDL_BINARY_SEARCH
119 * binary search of id in ids
120 * if found, returns position of id
121 * if not found, returns first postion greater than id
134 cursor = base + pivot;
135 val = IDL_CMP( id, ids[cursor + 1] );
140 } else if ( val > 0 ) {
156 /* (reverse) linear search */
163 for( i=ids[0]; i; i-- ) {
173 int bdb_idl_insert( ID *ids, ID id )
179 LDAP_LOG( INDEX, DETAIL1, "insert: %04lx at %d\n", (long) id, x, 0 );
181 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
188 if (BDB_IDL_IS_RANGE( ids )) {
189 /* if already in range, treat as a dup */
190 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
192 if (id < BDB_IDL_FIRST(ids))
194 else if (id > BDB_IDL_LAST(ids))
199 x = bdb_idl_search( ids, id );
207 if ( x <= ids[0] && ids[x] == id ) {
212 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
215 ids[2] = ids[ids[0]-1];
216 } else if ( ids[ids[0]-1] < id ) {
219 ids[2] = ids[ids[0]-1];
225 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
239 static int idl_delete( ID *ids, ID id )
241 unsigned x = bdb_idl_search( ids, id );
245 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
247 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
261 if( x > ids[0] || ids[x] != id ) {
265 } else if ( --ids[0] == 0 ) {
271 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
289 if ( key->size == sizeof( ID ) ) {
290 unsigned char *c = key->data;
291 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
306 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
314 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
315 #ifdef SLAP_IDL_CACHE
316 bdb_idl_cache_entry_t idl_tmp;
319 /* If using BerkeleyDB 4.0, the buf must be large enough to
320 * grab the entire IDL in one get(), otherwise BDB will leak
321 * resources on subsequent get's. We can safely call get()
322 * twice - once for the data, and once to get the DB_NOTFOUND
323 * result meaning there's no more data. See ITS#2040 for details.
324 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
325 * stack space is too limited.
327 * configure now requires Berkeley DB 4.1.
329 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
330 # define BDB_ENOUGH 5
332 # define BDB_ENOUGH 1
334 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
339 LDAP_LOG( INDEX, ARGS,
340 "bdb_idl_fetch_key: %s\n",
341 bdb_show_key( key, keybuf ), 0, 0 );
343 Debug( LDAP_DEBUG_ARGS,
344 "bdb_idl_fetch_key: %s\n",
345 bdb_show_key( key, keybuf ), 0, 0 );
348 assert( ids != NULL );
350 #ifdef SLAP_IDL_CACHE
351 if ( bdb->bi_idl_cache_max_size ) {
352 bdb_idl_cache_entry_t *matched_idl_entry;
353 DBT2bv( key, &idl_tmp.kstr );
355 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
356 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
358 if ( matched_idl_entry != NULL ) {
359 BDB_IDL_CPY( ids, matched_idl_entry->idl );
360 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
361 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
362 IDL_LRU_DELETE( bdb, matched_idl_entry );
363 IDL_LRU_ADD( bdb, matched_idl_entry );
364 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
367 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
374 data.ulen = sizeof(buf);
375 data.flags = DB_DBT_USERMEM;
377 if ( tid ) flags |= DB_RMW;
379 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
382 LDAP_LOG( INDEX, ERR,
383 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
384 db_strerror(rc), rc, 0 );
386 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
387 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
392 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
398 DB_MULTIPLE_INIT( ptr, &data );
400 DB_MULTIPLE_NEXT(ptr, &data, j, len);
403 AC_MEMCPY( i, j, sizeof(ID) );
406 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
408 if ( rc == DB_NOTFOUND ) rc = 0;
410 /* On disk, a range is denoted by 0 in the first element */
412 if (ids[0] != BDB_IDL_RANGE_SIZE) {
414 LDAP_LOG( INDEX, ERR,
415 "=> bdb_idl_fetch_key: range size mismatch: "
416 "expected %ld, got %ld\n",
417 BDB_IDL_RANGE_SIZE, ids[0], 0 );
419 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
420 "range size mismatch: expected %d, got %ld\n",
421 BDB_IDL_RANGE_SIZE, ids[0], 0 );
423 cursor->c_close( cursor );
426 BDB_IDL_RANGE( ids, ids[2], ids[3] );
428 data.size = BDB_IDL_SIZEOF(ids);
431 rc2 = cursor->c_close( cursor );
434 LDAP_LOG( INDEX, ERR,
435 "bdb_idl_fetch_key: close failed: %s (%d)\n",
436 db_strerror(rc2), rc2, 0 );
438 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
439 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
444 if( rc == DB_NOTFOUND ) {
447 } else if( rc != 0 ) {
449 LDAP_LOG( INDEX, ERR,
450 "bdb_idl_fetch_key: get failed: %s (%d)\n",
451 db_strerror(rc), rc, 0 );
453 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
454 "get failed: %s (%d)\n",
455 db_strerror(rc), rc, 0 );
459 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
460 /* size not multiple of ID size */
462 LDAP_LOG( INDEX, ERR,
463 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
464 (long) sizeof( ID ), (long) data.size, 0 );
466 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
467 "odd size: expected %ld multiple, got %ld\n",
468 (long) sizeof( ID ), (long) data.size, 0 );
472 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
475 LDAP_LOG( INDEX, ERR,
476 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
477 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
479 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
480 "get size mismatch: expected %ld, got %ld\n",
481 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
486 #ifdef SLAP_IDL_CACHE
487 if ( bdb->bi_idl_cache_max_size ) {
488 bdb_idl_cache_entry_t *ee;
489 ee = (bdb_idl_cache_entry_t *) ch_malloc(
490 sizeof( bdb_idl_cache_entry_t ) );
492 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
493 ee->idl_lru_prev = NULL;
494 ee->idl_lru_next = NULL;
495 BDB_IDL_CPY( ee->idl, ids );
496 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
497 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
498 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
499 bdb_idl_entry_cmp, avl_dup_error ))
501 ch_free( ee->kstr.bv_val );
504 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
507 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
508 IDL_LRU_ADD( bdb, ee );
509 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
511 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
512 ee = bdb->bi_idl_lru_tail;
513 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
514 bdb_idl_entry_cmp ) == NULL ) {
516 LDAP_LOG( INDEX, ERR,
517 "bdb_idl_fetch_key: AVL delete failed\n",
520 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
521 "AVL delete failed\n",
525 IDL_LRU_DELETE( bdb, ee );
527 --bdb->bi_idl_cache_size;
528 ch_free( ee->kstr.bv_val );
534 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
535 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
551 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
561 LDAP_LOG( INDEX, ARGS,
562 "bdb_idl_insert_key: %lx %s\n",
563 (long) id, bdb_show_key( key, buf ), 0 );
565 Debug( LDAP_DEBUG_ARGS,
566 "bdb_idl_insert_key: %lx %s\n",
567 (long) id, bdb_show_key( key, buf ), 0 );
571 assert( id != NOID );
573 #ifdef SLAP_IDL_CACHE
574 if ( bdb->bi_idl_cache_size ) {
575 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
576 DBT2bv( key, &idl_tmp.kstr );
578 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
579 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
581 if ( matched_idl_entry != NULL ) {
582 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
583 bdb_idl_entry_cmp ) == NULL ) {
585 LDAP_LOG( INDEX, ERR,
586 "bdb_idl_fetch_key: AVL delete failed\n",
589 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
590 "AVL delete failed\n",
594 --bdb->bi_idl_cache_size;
595 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
596 IDL_LRU_DELETE( bdb, matched_idl_entry );
597 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
598 free( matched_idl_entry->kstr.bv_val );
599 free( matched_idl_entry->idl );
600 free( matched_idl_entry );
602 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
607 data.size = sizeof( ID );
608 data.ulen = data.size;
609 data.flags = DB_DBT_USERMEM;
611 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
614 LDAP_LOG( INDEX, ERR,
615 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
616 db_strerror(rc), rc, 0 );
618 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
619 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
624 /* Fetch the first data item for this key, to see if it
625 * exists and if it's a range.
627 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
631 /* not a range, count the number of items */
633 rc = cursor->c_count( cursor, &count, 0 );
638 if ( count >= BDB_IDL_DB_MAX ) {
639 /* No room, convert to a range */
642 key2.dlen = key2.ulen;
643 key2.flags |= DB_DBT_PARTIAL;
647 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
648 if ( rc != 0 && rc != DB_NOTFOUND ) {
649 err = "c_get next_nodup";
652 if ( rc == DB_NOTFOUND ) {
653 rc = cursor->c_get( cursor, key, &data, DB_LAST );
659 rc = cursor->c_get( cursor, key, &data, DB_PREV );
669 rc = db->del( db, tid, key, 0 );
676 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
682 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
688 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
694 /* There's room, just store it */
698 /* It's a range, see if we need to rewrite
703 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
710 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
716 if ( id < lo || id > hi ) {
717 /* Delete the current lo/hi */
718 rc = cursor->c_del( cursor, 0 );
724 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
731 } else if ( rc == DB_NOTFOUND ) {
732 put1: data.data = &id;
733 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
734 /* Don't worry if it's already there */
735 if ( rc != 0 && rc != DB_KEYEXIST ) {
740 /* initial c_get failed, nothing was done */
743 LDAP_LOG( INDEX, ERR,
744 "bdb_idl_insert_key: %s failed: %s (%d)\n",
745 err, db_strerror(rc), rc );
747 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
748 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
750 cursor->c_close( cursor );
753 rc = cursor->c_close( cursor );
756 LDAP_LOG( INDEX, ERR,
757 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
758 db_strerror(rc), rc, 0 );
760 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
761 "c_close failed: %s (%d)\n",
762 db_strerror(rc), rc, 0 );
776 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
786 LDAP_LOG( INDEX, ARGS,
787 "bdb_idl_delete_key: %lx %s\n",
788 (long) id, bdb_show_key( key, buf ), 0 );
790 Debug( LDAP_DEBUG_ARGS,
791 "bdb_idl_delete_key: %lx %s\n",
792 (long) id, bdb_show_key( key, buf ), 0 );
795 assert( id != NOID );
797 #ifdef SLAP_IDL_CACHE
798 if ( bdb->bi_idl_cache_max_size ) {
799 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
800 DBT2bv( key, &idl_tmp.kstr );
802 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
803 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
805 if ( matched_idl_entry != NULL ) {
806 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
807 bdb_idl_entry_cmp ) == NULL ) {
809 LDAP_LOG( INDEX, ERR,
810 "bdb_idl_fetch_key: AVL delete failed\n",
813 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
814 "AVL delete failed\n",
818 --bdb->bi_idl_cache_size;
819 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
820 IDL_LRU_DELETE( bdb, matched_idl_entry );
821 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
822 free( matched_idl_entry->kstr.bv_val );
823 free( matched_idl_entry->idl );
824 free( matched_idl_entry );
826 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
832 data.size = sizeof( id );
833 data.ulen = data.size;
834 data.flags = DB_DBT_USERMEM;
836 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
839 LDAP_LOG( INDEX, ERR,
840 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
841 db_strerror(rc), rc, 0 );
843 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
844 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
848 /* Fetch the first data item for this key, to see if it
849 * exists and if it's a range.
851 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
855 /* Not a range, just delete it */
857 /* position to correct item */
859 rc = cursor->c_get( cursor, key, &data,
860 DB_GET_BOTH | DB_RMW );
866 rc = cursor->c_del( cursor, 0 );
872 /* It's a range, see if we need to rewrite
876 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
882 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
887 if ( id == lo || id == hi ) {
891 } else if ( id == hi ) {
896 /* The range has collapsed... */
897 rc = db->del( db, tid, key, 0 );
904 /* reposition on lo slot */
906 cursor->c_get( cursor, key, &data, DB_PREV );
909 rc = cursor->c_del( cursor, 0 );
917 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
926 /* initial c_get failed, nothing was done */
928 if ( rc != DB_NOTFOUND ) {
930 LDAP_LOG( INDEX, ERR,
931 "bdb_idl_delete_key: %s failed: %s (%d)\n",
932 err, db_strerror(rc), rc );
934 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
935 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
938 cursor->c_close( cursor );
941 rc = cursor->c_close( cursor );
944 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
945 db_strerror(rc), rc, 0 );
947 Debug( LDAP_DEBUG_ANY,
948 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
949 db_strerror(rc), rc, 0 );
958 * idl_intersection - return a = a intersection b
961 bdb_idl_intersection(
967 ID cursora = 0, cursorb = 0, cursorc;
970 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
975 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
976 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
977 if ( idmin > idmax ) {
980 } else if ( idmin == idmax ) {
986 if ( BDB_IDL_IS_RANGE( a ) ) {
987 if ( BDB_IDL_IS_RANGE(b) ) {
988 /* If both are ranges, just shrink the boundaries */
993 /* Else swap so that b is the range, a is a list */
1001 /* If a range completely covers the list, the result is
1002 * just the list. If idmin to idmax is contiguous, just
1003 * turn it into a range.
1005 if ( BDB_IDL_IS_RANGE( b )
1006 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1007 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1008 if (idmax - idmin + 1 == a[0])
1017 /* Fine, do the intersection one element at a time.
1018 * First advance to idmin in both IDLs.
1020 cursora = cursorb = idmin;
1021 ida = bdb_idl_first( a, &cursora );
1022 idb = bdb_idl_first( b, &cursorb );
1025 while( ida <= idmax || idb <= idmax ) {
1028 ida = bdb_idl_next( a, &cursora );
1029 idb = bdb_idl_next( b, &cursorb );
1030 } else if ( ida < idb ) {
1031 ida = bdb_idl_next( a, &cursora );
1033 idb = bdb_idl_next( b, &cursorb );
1039 BDB_IDL_CPY( b, a );
1046 * idl_union - return a = a union b
1054 ID cursora = 0, cursorb = 0, cursorc;
1056 if ( BDB_IDL_IS_ZERO( b ) ) {
1060 if ( BDB_IDL_IS_ZERO( a ) ) {
1061 BDB_IDL_CPY( a, b );
1065 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1066 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1067 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1074 ida = bdb_idl_first( a, &cursora );
1075 idb = bdb_idl_first( b, &cursorb );
1079 /* The distinct elements of a are cat'd to b */
1080 while( ida != NOID || idb != NOID ) {
1082 if( ++cursorc > BDB_IDL_UM_MAX ) {
1086 ida = bdb_idl_next( a, &cursora );
1090 ida = bdb_idl_next( a, &cursora );
1091 idb = bdb_idl_next( b, &cursorb );
1095 /* b is copied back to a in sorted order */
1100 while (cursorb <= b[0] || cursorc <= a[0]) {
1105 if (cursorb <= b[0] && b[cursorb] < idb)
1106 a[cursora++] = b[cursorb++];
1119 * bdb_idl_notin - return a intersection ~b (or a minus b)
1128 ID cursora = 0, cursorb = 0;
1130 if( BDB_IDL_IS_ZERO( a ) ||
1131 BDB_IDL_IS_ZERO( b ) ||
1132 BDB_IDL_IS_RANGE( b ) )
1134 BDB_IDL_CPY( ids, a );
1138 if( BDB_IDL_IS_RANGE( a ) ) {
1139 BDB_IDL_CPY( ids, a );
1143 ida = bdb_idl_first( a, &cursora ),
1144 idb = bdb_idl_first( b, &cursorb );
1148 while( ida != NOID ) {
1149 if ( idb == NOID ) {
1150 /* we could shortcut this */
1151 ids[++ids[0]] = ida;
1152 ida = bdb_idl_next( a, &cursora );
1154 } else if ( ida < idb ) {
1155 ids[++ids[0]] = ida;
1156 ida = bdb_idl_next( a, &cursora );
1158 } else if ( ida > idb ) {
1159 idb = bdb_idl_next( b, &cursorb );
1162 ida = bdb_idl_next( a, &cursora );
1163 idb = bdb_idl_next( b, &cursorb );
1171 ID bdb_idl_first( ID *ids, ID *cursor )
1175 if ( ids[0] == 0 ) {
1180 if ( BDB_IDL_IS_RANGE( ids ) ) {
1181 if( *cursor < ids[1] ) {
1190 pos = bdb_idl_search( ids, *cursor );
1192 if( pos > ids[0] ) {
1200 ID bdb_idl_next( ID *ids, ID *cursor )
1202 if ( BDB_IDL_IS_RANGE( ids ) ) {
1203 if( ids[2] < ++(*cursor) ) {
1209 if ( ++(*cursor) <= ids[0] ) {
1210 return ids[*cursor];