1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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 ) )
30 #define IDL_LRU_DELETE( bdb, e ) do { \
31 if ( e->idl_lru_prev != NULL ) { \
32 e->idl_lru_prev->idl_lru_next = e->idl_lru_next; \
34 bdb->bi_idl_lru_head = e->idl_lru_next; \
36 if ( e->idl_lru_next != NULL ) { \
37 e->idl_lru_next->idl_lru_prev = e->idl_lru_prev; \
39 bdb->bi_idl_lru_tail = e->idl_lru_prev; \
43 #define IDL_LRU_ADD( bdb, e ) do { \
44 e->idl_lru_next = bdb->bi_idl_lru_head; \
45 if ( e->idl_lru_next != NULL ) { \
46 e->idl_lru_next->idl_lru_prev = (e); \
48 (bdb)->bi_idl_lru_head = (e); \
49 e->idl_lru_prev = NULL; \
50 if ( (bdb)->bi_idl_lru_tail == NULL ) { \
51 (bdb)->bi_idl_lru_tail = (e); \
56 bdb_idl_entry_cmp( const void *v_idl1, const void *v_idl2 )
58 const bdb_idl_cache_entry_t *idl1 = v_idl1, *idl2 = v_idl2;
61 if ((rc = SLAP_PTRCMP( idl1->db, idl2->db ))) return rc;
62 if ((rc = idl1->kstr.bv_len - idl2->kstr.bv_len )) return rc;
63 return ( memcmp ( idl1->kstr.bv_val, idl2->kstr.bv_val , idl1->kstr.bv_len ) );
67 static void idl_check( ID *ids )
69 if( BDB_IDL_IS_RANGE( ids ) ) {
70 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
73 for( i=1; i < ids[0]; i++ ) {
74 assert( ids[i+1] > ids[i] );
80 static void idl_dump( ID *ids )
82 if( BDB_IDL_IS_RANGE( ids ) ) {
83 Debug( LDAP_DEBUG_ANY,
84 "IDL: range ( %ld - %ld )\n",
85 (long) BDB_IDL_RANGE_FIRST( ids ),
86 (long) BDB_IDL_RANGE_LAST( ids ) );
90 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
92 for( i=1; i<=ids[0]; i++ ) {
94 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
96 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
99 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
104 #endif /* IDL_DEBUG > 1 */
105 #endif /* IDL_DEBUG > 0 */
107 unsigned bdb_idl_search( ID *ids, ID id )
109 #define IDL_BINARY_SEARCH 1
110 #ifdef IDL_BINARY_SEARCH
112 * binary search of id in ids
113 * if found, returns position of id
114 * if not found, returns first postion greater than id
127 cursor = base + pivot;
128 val = IDL_CMP( id, ids[cursor + 1] );
133 } else if ( val > 0 ) {
149 /* (reverse) linear search */
156 for( i=ids[0]; i; i-- ) {
166 int bdb_idl_insert( ID *ids, ID id )
171 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
177 if (BDB_IDL_IS_RANGE( ids )) {
178 /* if already in range, treat as a dup */
179 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
181 if (id < BDB_IDL_FIRST(ids))
183 else if (id > BDB_IDL_LAST(ids))
188 x = bdb_idl_search( ids, id );
196 if ( x <= ids[0] && ids[x] == id ) {
201 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
204 ids[2] = ids[ids[0]-1];
205 } else if ( ids[ids[0]-1] < id ) {
208 ids[2] = ids[ids[0]-1];
214 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
228 static int idl_delete( ID *ids, ID id )
230 unsigned x = bdb_idl_search( ids, id );
233 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
246 if( x > ids[0] || ids[x] != id ) {
250 } else if ( --ids[0] == 0 ) {
256 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
274 if ( key->size == sizeof( ID ) ) {
275 unsigned char *c = key->data;
276 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
283 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
284 * copy the cached IDL into it, otherwise just return the status.
288 struct bdb_info *bdb,
293 bdb_idl_cache_entry_t idl_tmp;
294 bdb_idl_cache_entry_t *matched_idl_entry;
296 DBT2bv( key, &idl_tmp.kstr );
298 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
299 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
301 if ( matched_idl_entry != NULL ) {
302 if ( matched_idl_entry->idl && ids )
303 BDB_IDL_CPY( ids, matched_idl_entry->idl );
304 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
305 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
306 IDL_LRU_DELETE( bdb, matched_idl_entry );
307 IDL_LRU_ADD( bdb, matched_idl_entry );
308 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
309 if ( matched_idl_entry->idl )
314 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
316 return LDAP_NO_SUCH_OBJECT;
321 struct bdb_info *bdb,
327 bdb_idl_cache_entry_t idl_tmp;
328 bdb_idl_cache_entry_t *ee;
330 DBT2bv( key, &idl_tmp.kstr );
332 ee = (bdb_idl_cache_entry_t *) ch_malloc(
333 sizeof( bdb_idl_cache_entry_t ) );
335 if ( rc == DB_NOTFOUND) {
338 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
339 BDB_IDL_CPY( ee->idl, ids );
341 ee->idl_lru_prev = NULL;
342 ee->idl_lru_next = NULL;
343 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
344 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
345 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
346 bdb_idl_entry_cmp, avl_dup_error ))
348 ch_free( ee->kstr.bv_val );
351 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
354 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
355 IDL_LRU_ADD( bdb, ee );
356 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
358 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
359 ee = bdb->bi_idl_lru_tail;
360 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
361 bdb_idl_entry_cmp ) == NULL ) {
362 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
363 "AVL delete failed\n",
366 IDL_LRU_DELETE( bdb, ee );
368 --bdb->bi_idl_cache_size;
369 ch_free( ee->kstr.bv_val );
375 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
376 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
381 struct bdb_info *bdb,
385 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
386 DBT2bv( key, &idl_tmp.kstr );
388 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
389 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
391 if ( matched_idl_entry != NULL ) {
392 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
393 bdb_idl_entry_cmp ) == NULL ) {
394 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
395 "AVL delete failed\n",
398 --bdb->bi_idl_cache_size;
399 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
400 IDL_LRU_DELETE( bdb, matched_idl_entry );
401 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
402 free( matched_idl_entry->kstr.bv_val );
403 if ( matched_idl_entry->idl )
404 free( matched_idl_entry->idl );
405 free( matched_idl_entry );
407 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
420 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
422 DBT data, key2, *kptr;
428 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
431 /* If using BerkeleyDB 4.0, the buf must be large enough to
432 * grab the entire IDL in one get(), otherwise BDB will leak
433 * resources on subsequent get's. We can safely call get()
434 * twice - once for the data, and once to get the DB_NOTFOUND
435 * result meaning there's no more data. See ITS#2040 for details.
436 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
437 * stack space is too limited.
439 * configure now requires Berkeley DB 4.1.
441 #if DB_VERSION_FULL < 0x04010000
442 # define BDB_ENOUGH 5
444 # define BDB_ENOUGH 1
446 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
450 Debug( LDAP_DEBUG_ARGS,
451 "bdb_idl_fetch_key: %s\n",
452 bdb_show_key( key, keybuf ), 0, 0 );
454 assert( ids != NULL );
456 if ( saved_cursor && *saved_cursor ) {
458 } else if ( get_flag == LDAP_FILTER_GE ) {
459 opflag = DB_SET_RANGE;
460 } else if ( get_flag == LDAP_FILTER_LE ) {
466 /* only non-range lookups can use the IDL cache */
467 if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
468 rc = bdb_idl_cache_get( bdb, db, key, ids );
469 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
475 data.ulen = sizeof(buf);
476 data.flags = DB_DBT_USERMEM;
478 if ( tid ) flags |= DB_RMW;
480 /* If we're not reusing an existing cursor, get a new one */
481 if( opflag != DB_NEXT ) {
482 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
484 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
485 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
489 cursor = *saved_cursor;
492 /* If this is a LE lookup, save original key so we can determine
493 * when to stop. If this is a GE lookup, save the key since it
494 * will be overwritten.
496 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
498 key2.flags = DB_DBT_USERMEM;
499 key2.ulen = sizeof(keybuf);
501 key2.size = key->size;
502 AC_MEMCPY( keybuf, key->data, key->size );
508 rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
510 /* skip presence key on range inequality lookups */
511 while (rc == 0 && kptr->size != len) {
512 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
514 /* If we're doing a LE compare and the new key is greater than
515 * our search key, we're done
517 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
518 key->data, key->size ) > 0 ) {
526 DB_MULTIPLE_INIT( ptr, &data );
528 DB_MULTIPLE_NEXT(ptr, &data, j, len);
534 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
536 if ( rc == DB_NOTFOUND ) rc = 0;
538 /* On disk, a range is denoted by 0 in the first element */
540 if (ids[0] != BDB_IDL_RANGE_SIZE) {
541 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
542 "range size mismatch: expected %d, got %ld\n",
543 BDB_IDL_RANGE_SIZE, ids[0], 0 );
544 cursor->c_close( cursor );
547 BDB_IDL_RANGE( ids, ids[2], ids[3] );
549 data.size = BDB_IDL_SIZEOF(ids);
552 if ( saved_cursor && rc == 0 ) {
553 if ( !*saved_cursor )
554 *saved_cursor = cursor;
558 rc2 = cursor->c_close( cursor );
560 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
561 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
565 if( rc == DB_NOTFOUND ) {
568 } else if( rc != 0 ) {
569 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
570 "get failed: %s (%d)\n",
571 db_strerror(rc), rc, 0 );
574 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
575 /* size not multiple of ID size */
576 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
577 "odd size: expected %ld multiple, got %ld\n",
578 (long) sizeof( ID ), (long) data.size, 0 );
581 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
583 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
584 "get size mismatch: expected %ld, got %ld\n",
585 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
589 if ( bdb->bi_idl_cache_max_size ) {
590 bdb_idl_cache_put( bdb, db, key, ids, rc );
605 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
609 ID lo, hi, tmp, nlo, nhi, nid;
614 Debug( LDAP_DEBUG_ARGS,
615 "bdb_idl_insert_key: %lx %s\n",
616 (long) id, bdb_show_key( key, buf ), 0 );
619 assert( id != NOID );
621 if ( bdb->bi_idl_cache_size ) {
622 bdb_idl_cache_del( bdb, db, key );
626 data.size = sizeof( ID );
627 data.ulen = data.size;
628 data.flags = DB_DBT_USERMEM;
630 BDB_ID2DISK( id, &nid );
632 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
634 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
635 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
639 /* Fetch the first data item for this key, to see if it
640 * exists and if it's a range.
642 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
646 /* not a range, count the number of items */
648 rc = cursor->c_count( cursor, &count, 0 );
653 if ( count >= BDB_IDL_DB_MAX ) {
654 /* No room, convert to a range */
658 key2.dlen = key2.ulen;
659 key2.flags |= DB_DBT_PARTIAL;
661 BDB_DISK2ID( &nlo, &lo );
664 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
665 if ( rc != 0 && rc != DB_NOTFOUND ) {
666 err = "c_get next_nodup";
669 if ( rc == DB_NOTFOUND ) {
670 rc = cursor->c_get( cursor, key, &data, DB_LAST );
676 rc = cursor->c_get( cursor, key, &data, DB_PREV );
682 BDB_DISK2ID( &nhi, &hi );
683 /* Update hi/lo if needed, then delete all the items
688 rc = cursor->c_del( cursor, 0 );
693 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
699 /* Don't fetch anything, just position cursor */
700 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
701 data.dlen = data.ulen = 0;
702 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
709 rc = cursor->c_del( cursor, 0 );
714 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
720 /* Delete all the records between lo and hi */
721 for ( i=2; i<count; i++ ) {
722 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_DUP | DB_RMW );
723 if ( rc != 0 && rc != DB_NOTFOUND ) {
724 err = "c_get next_dup";
727 rc = cursor->c_del( cursor, 0 );
728 if ( rc != 0 && rc != DB_NOTFOUND ) {
733 /* Store the range marker */
734 data.size = data.ulen = sizeof(ID);
735 data.flags = DB_DBT_USERMEM;
737 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
743 /* There's room, just store it */
747 /* It's a range, see if we need to rewrite
752 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
757 BDB_DISK2ID( &nlo, &lo );
760 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
765 BDB_DISK2ID( &nhi, &hi );
767 if ( id < lo || id > hi ) {
768 /* Delete the current lo/hi */
769 rc = cursor->c_del( cursor, 0 );
775 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
782 } else if ( rc == DB_NOTFOUND ) {
783 put1: data.data = &nid;
784 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
785 /* Don't worry if it's already there */
786 if ( rc != 0 && rc != DB_KEYEXIST ) {
791 /* initial c_get failed, nothing was done */
793 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
794 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
795 cursor->c_close( cursor );
798 rc = cursor->c_close( cursor );
800 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
801 "c_close failed: %s (%d)\n",
802 db_strerror(rc), rc, 0 );
815 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
819 ID lo, hi, tmp, nid, nlo, nhi;
824 Debug( LDAP_DEBUG_ARGS,
825 "bdb_idl_delete_key: %lx %s\n",
826 (long) id, bdb_show_key( key, buf ), 0 );
828 assert( id != NOID );
830 if ( bdb->bi_idl_cache_max_size ) {
831 bdb_idl_cache_del( bdb, db, key );
834 BDB_ID2DISK( id, &nid );
838 data.size = sizeof( id );
839 data.ulen = data.size;
840 data.flags = DB_DBT_USERMEM;
842 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
844 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
845 "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 );
881 BDB_DISK2ID( &nlo, &lo );
883 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
888 BDB_DISK2ID( &nhi, &hi );
889 if ( id == lo || id == hi ) {
893 } else if ( id == hi ) {
898 /* The range has collapsed... */
899 rc = db->del( db, tid, key, 0 );
906 /* reposition on lo slot */
908 cursor->c_get( cursor, key, &data, DB_PREV );
910 rc = cursor->c_del( cursor, 0 );
917 BDB_ID2DISK( id, &nid );
919 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
928 /* initial c_get failed, nothing was done */
930 if ( rc != DB_NOTFOUND ) {
931 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
932 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
934 cursor->c_close( cursor );
937 rc = cursor->c_close( cursor );
939 Debug( LDAP_DEBUG_ANY,
940 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
941 db_strerror(rc), rc, 0 );
949 * idl_intersection - return a = a intersection b
952 bdb_idl_intersection(
958 ID cursora = 0, cursorb = 0, cursorc;
961 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
966 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
967 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
968 if ( idmin > idmax ) {
971 } else if ( idmin == idmax ) {
977 if ( BDB_IDL_IS_RANGE( a ) ) {
978 if ( BDB_IDL_IS_RANGE(b) ) {
979 /* If both are ranges, just shrink the boundaries */
984 /* Else swap so that b is the range, a is a list */
992 /* If a range completely covers the list, the result is
993 * just the list. If idmin to idmax is contiguous, just
994 * turn it into a range.
996 if ( BDB_IDL_IS_RANGE( b )
997 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
998 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
999 if (idmax - idmin + 1 == a[0])
1008 /* Fine, do the intersection one element at a time.
1009 * First advance to idmin in both IDLs.
1011 cursora = cursorb = idmin;
1012 ida = bdb_idl_first( a, &cursora );
1013 idb = bdb_idl_first( b, &cursorb );
1016 while( ida <= idmax || idb <= idmax ) {
1019 ida = bdb_idl_next( a, &cursora );
1020 idb = bdb_idl_next( b, &cursorb );
1021 } else if ( ida < idb ) {
1022 ida = bdb_idl_next( a, &cursora );
1024 idb = bdb_idl_next( b, &cursorb );
1030 BDB_IDL_CPY( b, a );
1037 * idl_union - return a = a union b
1045 ID cursora = 0, cursorb = 0, cursorc;
1047 if ( BDB_IDL_IS_ZERO( b ) ) {
1051 if ( BDB_IDL_IS_ZERO( a ) ) {
1052 BDB_IDL_CPY( a, b );
1056 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1057 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1058 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1065 ida = bdb_idl_first( a, &cursora );
1066 idb = bdb_idl_first( b, &cursorb );
1070 /* The distinct elements of a are cat'd to b */
1071 while( ida != NOID || idb != NOID ) {
1073 if( ++cursorc > BDB_IDL_UM_MAX ) {
1077 ida = bdb_idl_next( a, &cursora );
1081 ida = bdb_idl_next( a, &cursora );
1082 idb = bdb_idl_next( b, &cursorb );
1086 /* b is copied back to a in sorted order */
1091 while (cursorb <= b[0] || cursorc <= a[0]) {
1096 if (cursorb <= b[0] && b[cursorb] < idb)
1097 a[cursora++] = b[cursorb++];
1110 * bdb_idl_notin - return a intersection ~b (or a minus b)
1119 ID cursora = 0, cursorb = 0;
1121 if( BDB_IDL_IS_ZERO( a ) ||
1122 BDB_IDL_IS_ZERO( b ) ||
1123 BDB_IDL_IS_RANGE( b ) )
1125 BDB_IDL_CPY( ids, a );
1129 if( BDB_IDL_IS_RANGE( a ) ) {
1130 BDB_IDL_CPY( ids, a );
1134 ida = bdb_idl_first( a, &cursora ),
1135 idb = bdb_idl_first( b, &cursorb );
1139 while( ida != NOID ) {
1140 if ( idb == NOID ) {
1141 /* we could shortcut this */
1142 ids[++ids[0]] = ida;
1143 ida = bdb_idl_next( a, &cursora );
1145 } else if ( ida < idb ) {
1146 ids[++ids[0]] = ida;
1147 ida = bdb_idl_next( a, &cursora );
1149 } else if ( ida > idb ) {
1150 idb = bdb_idl_next( b, &cursorb );
1153 ida = bdb_idl_next( a, &cursora );
1154 idb = bdb_idl_next( b, &cursorb );
1162 ID bdb_idl_first( ID *ids, ID *cursor )
1166 if ( ids[0] == 0 ) {
1171 if ( BDB_IDL_IS_RANGE( ids ) ) {
1172 if( *cursor < ids[1] ) {
1181 pos = bdb_idl_search( ids, *cursor );
1183 if( pos > ids[0] ) {
1191 ID bdb_idl_next( ID *ids, ID *cursor )
1193 if ( BDB_IDL_IS_RANGE( ids ) ) {
1194 if( ids[2] < ++(*cursor) ) {
1200 if ( ++(*cursor) <= ids[0] ) {
1201 return ids[*cursor];