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 ) )
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 ) ) {
84 LDAP_LOG( INDEX, INFO, "IDL: range (%ld - %ld)\n",
85 (long) BDB_IDL_RANGE_FIRST( ids ),
86 (long) BDB_IDL_RANGE_LAST( ids ), 0 );
88 Debug( LDAP_DEBUG_ANY,
89 "IDL: range ( %ld - %ld )\n",
90 (long) BDB_IDL_RANGE_FIRST( ids ),
91 (long) BDB_IDL_RANGE_LAST( ids ) );
97 LDAP_LOG( INDEX, INFO, "IDL: size %ld", (long) ids[0], 0, 0 );
99 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
102 for( i=1; i<=ids[0]; i++ ) {
104 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
107 LDAP_LOG( INDEX, INFO, "%02lx",(long)ids[i], 0, 0 );
109 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
113 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
118 #endif /* IDL_DEBUG > 1 */
119 #endif /* IDL_DEBUG > 0 */
121 unsigned bdb_idl_search( ID *ids, ID id )
123 #define IDL_BINARY_SEARCH 1
124 #ifdef IDL_BINARY_SEARCH
126 * binary search of id in ids
127 * if found, returns position of id
128 * if not found, returns first postion greater than id
141 cursor = base + pivot;
142 val = IDL_CMP( id, ids[cursor + 1] );
147 } else if ( val > 0 ) {
163 /* (reverse) linear search */
170 for( i=ids[0]; i; i-- ) {
180 int bdb_idl_insert( ID *ids, ID id )
186 LDAP_LOG( INDEX, DETAIL1, "insert: %04lx at %d\n", (long) id, x, 0 );
188 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
195 if (BDB_IDL_IS_RANGE( ids )) {
196 /* if already in range, treat as a dup */
197 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
199 if (id < BDB_IDL_FIRST(ids))
201 else if (id > BDB_IDL_LAST(ids))
206 x = bdb_idl_search( ids, id );
214 if ( x <= ids[0] && ids[x] == id ) {
219 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
222 ids[2] = ids[ids[0]-1];
223 } else if ( ids[ids[0]-1] < id ) {
226 ids[2] = ids[ids[0]-1];
232 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
245 static int bdb_idl_delete( ID *ids, ID id )
251 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
253 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
260 if (BDB_IDL_IS_RANGE( ids )) {
261 /* if in range, treat as a noop */
262 if (id > BDB_IDL_FIRST(ids) && id < BDB_IDL_LAST(ids))
264 if (id == BDB_IDL_FIRST(ids))
266 else if (id == BDB_IDL_LAST(ids))
268 /* range collapsed to a single item */
269 if ( ids[1] == ids[2] )
274 x = bdb_idl_search( ids, id );
282 if( x > ids[0] || ids[x] != id ) {
286 } else if ( --ids[0] == 0 ) {
292 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
309 if ( key->size == sizeof( ID ) ) {
310 unsigned char *c = key->data;
311 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
318 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
319 * copy the cached IDL into it, otherwise just return the status.
323 struct bdb_info *bdb,
328 bdb_idl_cache_entry_t idl_tmp;
329 bdb_idl_cache_entry_t *matched_idl_entry;
331 DBT2bv( key, &idl_tmp.kstr );
333 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
334 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
336 if ( matched_idl_entry != NULL ) {
337 if ( matched_idl_entry->idl && ids )
338 BDB_IDL_CPY( ids, matched_idl_entry->idl );
339 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
340 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
341 IDL_LRU_DELETE( bdb, matched_idl_entry );
342 IDL_LRU_ADD( bdb, matched_idl_entry );
343 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
344 if ( matched_idl_entry->idl )
349 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
351 return LDAP_NO_SUCH_OBJECT;
356 struct bdb_info *bdb,
362 bdb_idl_cache_entry_t idl_tmp;
363 bdb_idl_cache_entry_t *ee;
365 DBT2bv( key, &idl_tmp.kstr );
367 ee = (bdb_idl_cache_entry_t *) ch_malloc(
368 sizeof( bdb_idl_cache_entry_t ) );
370 if ( rc == DB_NOTFOUND) {
373 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
374 BDB_IDL_CPY( ee->idl, ids );
376 ee->idl_lru_prev = NULL;
377 ee->idl_lru_next = NULL;
378 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
379 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
380 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
381 bdb_idl_entry_cmp, avl_dup_error ))
383 ch_free( ee->kstr.bv_val );
386 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
389 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
390 IDL_LRU_ADD( bdb, ee );
391 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
393 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
394 ee = bdb->bi_idl_lru_tail;
395 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
396 bdb_idl_entry_cmp ) == NULL ) {
398 LDAP_LOG( INDEX, ERR,
399 "bdb_idl_cache_put: AVL delete failed\n",
402 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
403 "AVL delete failed\n",
407 IDL_LRU_DELETE( bdb, ee );
409 --bdb->bi_idl_cache_size;
410 ch_free( ee->kstr.bv_val );
416 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
417 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
422 struct bdb_info *bdb,
426 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
427 DBT2bv( key, &idl_tmp.kstr );
429 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
430 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
432 if ( matched_idl_entry != NULL ) {
433 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
434 bdb_idl_entry_cmp ) == NULL ) {
436 LDAP_LOG( INDEX, ERR,
437 "bdb_idl_cache_del: AVL delete failed\n",
440 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
441 "AVL delete failed\n",
445 --bdb->bi_idl_cache_size;
446 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
447 IDL_LRU_DELETE( bdb, matched_idl_entry );
448 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
449 free( matched_idl_entry->kstr.bv_val );
450 if ( matched_idl_entry->idl )
451 free( matched_idl_entry->idl );
452 free( matched_idl_entry );
454 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
465 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
473 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
475 /* If using BerkeleyDB 4.0, the buf must be large enough to
476 * grab the entire IDL in one get(), otherwise BDB will leak
477 * resources on subsequent get's. We can safely call get()
478 * twice - once for the data, and once to get the DB_NOTFOUND
479 * result meaning there's no more data. See ITS#2040 for details.
480 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
481 * stack space is too limited.
483 * configure now requires Berkeley DB 4.1.
485 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
486 # define BDB_ENOUGH 5
488 # define BDB_ENOUGH 1
490 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
495 LDAP_LOG( INDEX, ARGS,
496 "bdb_idl_fetch_key: %s\n",
497 bdb_show_key( key, keybuf ), 0, 0 );
499 Debug( LDAP_DEBUG_ARGS,
500 "bdb_idl_fetch_key: %s\n",
501 bdb_show_key( key, keybuf ), 0, 0 );
504 assert( ids != NULL );
506 if ( bdb->bi_idl_cache_size ) {
507 rc = bdb_idl_cache_get( bdb, db, key, ids );
508 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
514 data.ulen = sizeof(buf);
515 data.flags = DB_DBT_USERMEM;
517 if ( tid ) flags |= DB_RMW;
519 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
522 LDAP_LOG( INDEX, ERR,
523 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
524 db_strerror(rc), rc, 0 );
526 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
527 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
532 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
538 DB_MULTIPLE_INIT( ptr, &data );
540 DB_MULTIPLE_NEXT(ptr, &data, j, len);
543 AC_MEMCPY( i, j, sizeof(ID) );
546 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
548 if ( rc == DB_NOTFOUND ) rc = 0;
550 /* On disk, a range is denoted by 0 in the first element */
552 if (ids[0] != BDB_IDL_RANGE_SIZE) {
554 LDAP_LOG( INDEX, ERR,
555 "=> bdb_idl_fetch_key: range size mismatch: "
556 "expected %ld, got %ld\n",
557 BDB_IDL_RANGE_SIZE, ids[0], 0 );
559 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
560 "range size mismatch: expected %d, got %ld\n",
561 BDB_IDL_RANGE_SIZE, ids[0], 0 );
563 cursor->c_close( cursor );
566 BDB_IDL_RANGE( ids, ids[2], ids[3] );
568 data.size = BDB_IDL_SIZEOF(ids);
571 rc2 = cursor->c_close( cursor );
574 LDAP_LOG( INDEX, ERR,
575 "bdb_idl_fetch_key: close failed: %s (%d)\n",
576 db_strerror(rc2), rc2, 0 );
578 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
579 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
584 if ( rc == DB_NOTFOUND ) {
586 } else if( rc != 0 ) {
588 LDAP_LOG( INDEX, ERR,
589 "bdb_idl_fetch_key: get failed: %s (%d)\n",
590 db_strerror(rc), rc, 0 );
592 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
593 "get failed: %s (%d)\n",
594 db_strerror(rc), rc, 0 );
598 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
599 /* size not multiple of ID size */
601 LDAP_LOG( INDEX, ERR,
602 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
603 (long) sizeof( ID ), (long) data.size, 0 );
605 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
606 "odd size: expected %ld multiple, got %ld\n",
607 (long) sizeof( ID ), (long) data.size, 0 );
611 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
614 LDAP_LOG( INDEX, ERR,
615 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
616 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
618 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
619 "get size mismatch: expected %ld, got %ld\n",
620 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
625 if ( bdb->bi_idl_cache_max_size ) {
626 bdb_idl_cache_put( bdb, db, key, ids, rc );
641 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
644 ID lo, hi, tmp, idl[BDB_IDL_DB_SIZE];
646 int wasrange = 0, isrange = 0;
651 LDAP_LOG( INDEX, ARGS,
652 "bdb_idl_insert_key: %lx %s\n",
653 (long) id, bdb_show_key( key, buf ), 0 );
655 Debug( LDAP_DEBUG_ARGS,
656 "bdb_idl_insert_key: %lx %s\n",
657 (long) id, bdb_show_key( key, buf ), 0 );
661 assert( id != NOID );
664 rc = bdb_idl_fetch_key( be, db, tid, key, idl );
665 if ( rc != DB_NOTFOUND ) {
666 wasrange = BDB_IDL_IS_RANGE( idl );
671 rc = bdb_idl_insert( idl, id );
673 /* Don't need to do anything */
674 if ( rc == -1 ) return 0;
676 isrange = BDB_IDL_IS_RANGE( idl );
680 data.size = sizeof( ID );
681 data.ulen = data.size;
682 data.flags = DB_DBT_USERMEM;
686 while ( !wasrange ) {
687 /* Delete vector, rewrite as range */
688 rc = db->del( db, tid, key, 0 );
694 rc = db->put( db, tid, key, &data, 0 );
700 rc = db->put( db, tid, key, &data, 0 );
706 rc = db->put( db, tid, key, &data, 0 );
715 /* Update range boundaries */
716 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
723 tmp = (id == idl[1]) ? lo : hi;
724 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
726 cursor->c_close( cursor );
730 rc = cursor->c_del( cursor, 0 );
732 cursor->c_close( cursor );
737 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
739 cursor->c_close( cursor );
743 rc = cursor->c_close( cursor );
752 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
757 if ( rc && rc != DB_KEYEXIST ) {
759 LDAP_LOG( INDEX, ERR,
760 "bdb_idl_insert_key: %s failed: %s (%d)\n",
761 err, db_strerror(rc), rc );
763 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
764 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
769 if ( bdb->bi_idl_cache_max_size ) {
770 bdb_idl_cache_del( bdb, db, key );
771 bdb_idl_cache_put( bdb, db, key, idl, 0 );
784 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
788 ID lo, hi, tmp, idl[BDB_IDL_DB_SIZE];
789 int wasrange, isrange;
795 LDAP_LOG( INDEX, ARGS,
796 "bdb_idl_delete_key: %lx %s\n",
797 (long) id, bdb_show_key( key, buf ), 0 );
799 Debug( LDAP_DEBUG_ARGS,
800 "bdb_idl_delete_key: %lx %s\n",
801 (long) id, bdb_show_key( key, buf ), 0 );
804 assert( id != NOID );
807 rc = bdb_idl_fetch_key( be, db, tid, key, idl );
808 if ( rc != DB_NOTFOUND ) {
809 wasrange = BDB_IDL_IS_RANGE( idl );
814 rc = bdb_idl_delete( idl, id );
816 /* Don't need to do anything */
817 if ( rc == -1 ) return 0;
819 isrange = BDB_IDL_IS_RANGE( idl );
824 data.size = sizeof( id );
825 data.ulen = data.size;
826 data.flags = DB_DBT_USERMEM;
828 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
831 LDAP_LOG( INDEX, ERR,
832 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
833 db_strerror(rc), rc, 0 );
835 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
836 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
841 if ( wasrange && !isrange ) {
842 rc = db->del( db, tid, key, 0 );
847 rc = db->put( db, tid, key, &data, 0 );
854 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
858 rc = cursor->c_del( cursor, 0 );
864 if ( isrange && rc == 0 ) {
865 tmp = ( id == lo ) ? idl[1] : idl[2];
866 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
871 if ( rc && rc != DB_NOTFOUND ) {
873 LDAP_LOG( INDEX, ERR,
874 "bdb_idl_delete_key: %s failed: %s (%d)\n",
875 err, db_strerror(rc), rc );
877 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
878 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
880 cursor->c_close( cursor );
883 rc = cursor->c_close( cursor );
886 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
887 db_strerror(rc), rc, 0 );
889 Debug( LDAP_DEBUG_ANY,
890 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
891 db_strerror(rc), rc, 0 );
895 if ( bdb->bi_idl_cache_max_size ) {
896 bdb_idl_cache_del( bdb, db, key );
897 bdb_idl_cache_put( bdb, db, key, idl, 0 );
905 * idl_intersection - return a = a intersection b
908 bdb_idl_intersection(
914 ID cursora = 0, cursorb = 0, cursorc;
917 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
922 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
923 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
924 if ( idmin > idmax ) {
927 } else if ( idmin == idmax ) {
933 if ( BDB_IDL_IS_RANGE( a ) ) {
934 if ( BDB_IDL_IS_RANGE(b) ) {
935 /* If both are ranges, just shrink the boundaries */
940 /* Else swap so that b is the range, a is a list */
948 /* If a range completely covers the list, the result is
949 * just the list. If idmin to idmax is contiguous, just
950 * turn it into a range.
952 if ( BDB_IDL_IS_RANGE( b )
953 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
954 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
955 if (idmax - idmin + 1 == a[0])
964 /* Fine, do the intersection one element at a time.
965 * First advance to idmin in both IDLs.
967 cursora = cursorb = idmin;
968 ida = bdb_idl_first( a, &cursora );
969 idb = bdb_idl_first( b, &cursorb );
972 while( ida <= idmax || idb <= idmax ) {
975 ida = bdb_idl_next( a, &cursora );
976 idb = bdb_idl_next( b, &cursorb );
977 } else if ( ida < idb ) {
978 ida = bdb_idl_next( a, &cursora );
980 idb = bdb_idl_next( b, &cursorb );
993 * idl_union - return a = a union b
1001 ID cursora = 0, cursorb = 0, cursorc;
1003 if ( BDB_IDL_IS_ZERO( b ) ) {
1007 if ( BDB_IDL_IS_ZERO( a ) ) {
1008 BDB_IDL_CPY( a, b );
1012 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1013 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1014 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1021 ida = bdb_idl_first( a, &cursora );
1022 idb = bdb_idl_first( b, &cursorb );
1026 /* The distinct elements of a are cat'd to b */
1027 while( ida != NOID || idb != NOID ) {
1029 if( ++cursorc > BDB_IDL_UM_MAX ) {
1033 ida = bdb_idl_next( a, &cursora );
1037 ida = bdb_idl_next( a, &cursora );
1038 idb = bdb_idl_next( b, &cursorb );
1042 /* b is copied back to a in sorted order */
1047 while (cursorb <= b[0] || cursorc <= a[0]) {
1052 if (cursorb <= b[0] && b[cursorb] < idb)
1053 a[cursora++] = b[cursorb++];
1066 * bdb_idl_notin - return a intersection ~b (or a minus b)
1075 ID cursora = 0, cursorb = 0;
1077 if( BDB_IDL_IS_ZERO( a ) ||
1078 BDB_IDL_IS_ZERO( b ) ||
1079 BDB_IDL_IS_RANGE( b ) )
1081 BDB_IDL_CPY( ids, a );
1085 if( BDB_IDL_IS_RANGE( a ) ) {
1086 BDB_IDL_CPY( ids, a );
1090 ida = bdb_idl_first( a, &cursora ),
1091 idb = bdb_idl_first( b, &cursorb );
1095 while( ida != NOID ) {
1096 if ( idb == NOID ) {
1097 /* we could shortcut this */
1098 ids[++ids[0]] = ida;
1099 ida = bdb_idl_next( a, &cursora );
1101 } else if ( ida < idb ) {
1102 ids[++ids[0]] = ida;
1103 ida = bdb_idl_next( a, &cursora );
1105 } else if ( ida > idb ) {
1106 idb = bdb_idl_next( b, &cursorb );
1109 ida = bdb_idl_next( a, &cursora );
1110 idb = bdb_idl_next( b, &cursorb );
1118 ID bdb_idl_first( ID *ids, ID *cursor )
1122 if ( ids[0] == 0 ) {
1127 if ( BDB_IDL_IS_RANGE( ids ) ) {
1128 if( *cursor < ids[1] ) {
1137 pos = bdb_idl_search( ids, *cursor );
1139 if( pos > ids[0] ) {
1147 ID bdb_idl_next( ID *ids, ID *cursor )
1149 if ( BDB_IDL_IS_RANGE( ids ) ) {
1150 if( ids[2] < ++(*cursor) ) {
1156 if ( ++(*cursor) <= ids[0] ) {
1157 return ids[*cursor];