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 )
175 unsigned x = bdb_idl_search( ids, 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 );
195 if ( x <= ids[0] && ids[x] == id ) {
200 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
203 ids[2] = ids[ids[0]-1];
204 } else if ( ids[ids[0]-1] < id ) {
207 ids[2] = ids[ids[0]-1];
213 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
227 static int idl_delete( ID *ids, ID id )
229 unsigned x = bdb_idl_search( ids, id );
233 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
235 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
249 if( x > ids[0] || ids[x] != id ) {
253 } else if ( --ids[0] == 0 ) {
259 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
277 if ( key->size == sizeof( ID ) ) {
278 unsigned char *c = key->data;
279 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
294 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
302 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
303 #ifdef SLAP_IDL_CACHE
304 bdb_idl_cache_entry_t idl_tmp;
307 /* If using BerkeleyDB 4.0, the buf must be large enough to
308 * grab the entire IDL in one get(), otherwise BDB will leak
309 * resources on subsequent get's. We can safely call get()
310 * twice - once for the data, and once to get the DB_NOTFOUND
311 * result meaning there's no more data. See ITS#2040 for details.
312 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
313 * stack space is too limited.
315 * configure now requires Berkeley DB 4.1.
317 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
318 # define BDB_ENOUGH 5
320 # define BDB_ENOUGH 1
322 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
327 LDAP_LOG( INDEX, ARGS,
328 "bdb_idl_fetch_key: %s\n",
329 bdb_show_key( key, keybuf ), 0, 0 );
331 Debug( LDAP_DEBUG_ARGS,
332 "bdb_idl_fetch_key: %s\n",
333 bdb_show_key( key, keybuf ), 0, 0 );
336 assert( ids != NULL );
338 #ifdef SLAP_IDL_CACHE
339 if ( bdb->bi_idl_cache_max_size ) {
340 bdb_idl_cache_entry_t *matched_idl_entry;
341 DBT2bv( key, &idl_tmp.kstr );
343 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
344 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
346 if ( matched_idl_entry != NULL ) {
347 BDB_IDL_CPY( ids, matched_idl_entry->idl );
348 IDL_LRU_DELETE( bdb, matched_idl_entry );
349 IDL_LRU_ADD( bdb, matched_idl_entry );
350 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
353 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
360 data.ulen = sizeof(buf);
361 data.flags = DB_DBT_USERMEM;
363 if ( tid ) flags |= DB_RMW;
365 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
368 LDAP_LOG( INDEX, ERR,
369 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
370 db_strerror(rc), rc, 0 );
372 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
373 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
378 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
384 DB_MULTIPLE_INIT( ptr, &data );
386 DB_MULTIPLE_NEXT(ptr, &data, j, len);
389 AC_MEMCPY( i, j, sizeof(ID) );
392 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
394 if ( rc == DB_NOTFOUND ) rc = 0;
396 /* On disk, a range is denoted by 0 in the first element */
398 if (ids[0] != BDB_IDL_RANGE_SIZE) {
400 LDAP_LOG( INDEX, ERR,
401 "=> bdb_idl_fetch_key: range size mismatch: "
402 "expected %ld, got %ld\n",
403 BDB_IDL_RANGE_SIZE, ids[0], 0 );
405 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
406 "range size mismatch: expected %d, got %ld\n",
407 BDB_IDL_RANGE_SIZE, ids[0], 0 );
409 cursor->c_close( cursor );
412 BDB_IDL_RANGE( ids, ids[2], ids[3] );
414 data.size = BDB_IDL_SIZEOF(ids);
417 rc2 = cursor->c_close( cursor );
420 LDAP_LOG( INDEX, ERR,
421 "bdb_idl_fetch_key: close failed: %s (%d)\n",
422 db_strerror(rc2), rc2, 0 );
424 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
425 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
430 if( rc == DB_NOTFOUND ) {
433 } else if( rc != 0 ) {
435 LDAP_LOG( INDEX, ERR,
436 "bdb_idl_fetch_key: get failed: %s (%d)\n",
437 db_strerror(rc), rc, 0 );
439 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
440 "get failed: %s (%d)\n",
441 db_strerror(rc), rc, 0 );
445 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
446 /* size not multiple of ID size */
448 LDAP_LOG( INDEX, ERR,
449 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
450 (long) sizeof( ID ), (long) data.size, 0 );
452 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
453 "odd size: expected %ld multiple, got %ld\n",
454 (long) sizeof( ID ), (long) data.size, 0 );
458 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
461 LDAP_LOG( INDEX, ERR,
462 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
463 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
465 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
466 "get size mismatch: expected %ld, got %ld\n",
467 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
472 #ifdef SLAP_IDL_CACHE
473 if ( bdb->bi_idl_cache_max_size ) {
474 bdb_idl_cache_entry_t *ee;
475 ee = (bdb_idl_cache_entry_t *) ch_malloc(
476 sizeof( bdb_idl_cache_entry_t ) );
478 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
479 ee->idl_lru_prev = NULL;
480 ee->idl_lru_next = NULL;
481 BDB_IDL_CPY( ee->idl, ids );
482 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
483 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
484 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
485 bdb_idl_entry_cmp, avl_dup_error ))
487 ch_free( ee->kstr.bv_val );
491 IDL_LRU_ADD( bdb, ee );
492 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
494 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
495 ee = bdb->bi_idl_lru_tail;
496 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
497 bdb_idl_entry_cmp ) == NULL ) {
499 LDAP_LOG( INDEX, ERR,
500 "bdb_idl_fetch_key: AVL delete failed\n",
503 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
504 "AVL delete failed\n",
508 IDL_LRU_DELETE( bdb, ee );
510 --bdb->bi_idl_cache_size;
511 ch_free( ee->kstr.bv_val );
517 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
533 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
543 LDAP_LOG( INDEX, ARGS,
544 "bdb_idl_insert_key: %lx %s\n",
545 (long) id, bdb_show_key( key, buf ), 0 );
547 Debug( LDAP_DEBUG_ARGS,
548 "bdb_idl_insert_key: %lx %s\n",
549 (long) id, bdb_show_key( key, buf ), 0 );
553 assert( id != NOID );
555 #ifdef SLAP_IDL_CACHE
556 if ( bdb->bi_idl_cache_size ) {
557 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
558 DBT2bv( key, &idl_tmp.kstr );
560 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
561 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
563 if ( matched_idl_entry != NULL ) {
564 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
565 bdb_idl_entry_cmp ) == NULL ) {
567 LDAP_LOG( INDEX, ERR,
568 "bdb_idl_fetch_key: AVL delete failed\n",
571 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
572 "AVL delete failed\n",
576 --bdb->bi_idl_cache_size;
577 IDL_LRU_DELETE( bdb, matched_idl_entry );
578 free( matched_idl_entry->kstr.bv_val );
579 free( matched_idl_entry->idl );
580 free( matched_idl_entry );
582 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
587 data.size = sizeof( ID );
588 data.ulen = data.size;
589 data.flags = DB_DBT_USERMEM;
591 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
594 LDAP_LOG( INDEX, ERR,
595 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
596 db_strerror(rc), rc, 0 );
598 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
599 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
604 /* Fetch the first data item for this key, to see if it
605 * exists and if it's a range.
607 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
611 /* not a range, count the number of items */
613 rc = cursor->c_count( cursor, &count, 0 );
618 if ( count >= BDB_IDL_DB_MAX ) {
619 /* No room, convert to a range */
622 key2.dlen = key2.ulen;
623 key2.flags |= DB_DBT_PARTIAL;
627 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
628 if ( rc != 0 && rc != DB_NOTFOUND ) {
629 err = "c_get next_nodup";
632 if ( rc == DB_NOTFOUND ) {
633 rc = cursor->c_get( cursor, key, &data, DB_LAST );
639 rc = cursor->c_get( cursor, key, &data, DB_PREV );
649 rc = db->del( db, tid, key, 0 );
656 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
662 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
668 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
674 /* There's room, just store it */
678 /* It's a range, see if we need to rewrite
683 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
690 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
696 if ( id < lo || id > hi ) {
697 /* Delete the current lo/hi */
698 rc = cursor->c_del( cursor, 0 );
704 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
711 } else if ( rc == DB_NOTFOUND ) {
712 put1: data.data = &id;
713 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
714 /* Don't worry if it's already there */
715 if ( rc != 0 && rc != DB_KEYEXIST ) {
720 /* initial c_get failed, nothing was done */
723 LDAP_LOG( INDEX, ERR,
724 "bdb_idl_insert_key: %s failed: %s (%d)\n",
725 err, db_strerror(rc), rc );
727 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
728 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
730 cursor->c_close( cursor );
733 rc = cursor->c_close( cursor );
736 LDAP_LOG( INDEX, ERR,
737 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
738 db_strerror(rc), rc, 0 );
740 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
741 "c_close failed: %s (%d)\n",
742 db_strerror(rc), rc, 0 );
756 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
766 LDAP_LOG( INDEX, ARGS,
767 "bdb_idl_delete_key: %lx %s\n",
768 (long) id, bdb_show_key( key, buf ), 0 );
770 Debug( LDAP_DEBUG_ARGS,
771 "bdb_idl_delete_key: %lx %s\n",
772 (long) id, bdb_show_key( key, buf ), 0 );
775 assert( id != NOID );
777 #ifdef SLAP_IDL_CACHE
778 if ( bdb->bi_idl_cache_max_size ) {
779 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
780 DBT2bv( key, &idl_tmp.kstr );
782 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
783 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
785 if ( matched_idl_entry != NULL ) {
786 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
787 bdb_idl_entry_cmp ) == NULL ) {
789 LDAP_LOG( INDEX, ERR,
790 "bdb_idl_fetch_key: AVL delete failed\n",
793 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
794 "AVL delete failed\n",
798 --bdb->bi_idl_cache_size;
799 IDL_LRU_DELETE( bdb, matched_idl_entry );
800 free( matched_idl_entry->kstr.bv_val );
801 free( matched_idl_entry->idl );
802 free( matched_idl_entry );
804 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
810 data.size = sizeof( id );
811 data.ulen = data.size;
812 data.flags = DB_DBT_USERMEM;
814 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
817 LDAP_LOG( INDEX, ERR,
818 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
819 db_strerror(rc), rc, 0 );
821 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
822 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
826 /* Fetch the first data item for this key, to see if it
827 * exists and if it's a range.
829 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
833 /* Not a range, just delete it */
835 /* position to correct item */
837 rc = cursor->c_get( cursor, key, &data,
838 DB_GET_BOTH | DB_RMW );
844 rc = cursor->c_del( cursor, 0 );
850 /* It's a range, see if we need to rewrite
854 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
860 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
865 if ( id == lo || id == hi ) {
869 } else if ( id == hi ) {
874 /* The range has collapsed... */
875 rc = db->del( db, tid, key, 0 );
882 /* reposition on lo slot */
884 cursor->c_get( cursor, key, &data, DB_PREV );
887 rc = cursor->c_del( cursor, 0 );
895 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
904 /* initial c_get failed, nothing was done */
906 if ( rc != DB_NOTFOUND ) {
908 LDAP_LOG( INDEX, ERR,
909 "bdb_idl_delete_key: %s failed: %s (%d)\n",
910 err, db_strerror(rc), rc );
912 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
913 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
916 cursor->c_close( cursor );
919 rc = cursor->c_close( cursor );
922 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
923 db_strerror(rc), rc, 0 );
925 Debug( LDAP_DEBUG_ANY,
926 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
927 db_strerror(rc), rc, 0 );
936 * idl_intersection - return a = a intersection b
939 bdb_idl_intersection(
945 ID cursora = 0, cursorb = 0, cursorc;
948 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
953 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
954 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
955 if ( idmin > idmax ) {
958 } else if ( idmin == idmax ) {
964 if ( BDB_IDL_IS_RANGE( a ) ) {
965 if ( BDB_IDL_IS_RANGE(b) ) {
966 /* If both are ranges, just shrink the boundaries */
971 /* Else swap so that b is the range, a is a list */
979 /* If a range completely covers the list, the result is
980 * just the list. If idmin to idmax is contiguous, just
981 * turn it into a range.
983 if ( BDB_IDL_IS_RANGE( b )
984 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
985 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
986 if (idmax - idmin + 1 == a[0])
995 /* Fine, do the intersection one element at a time.
996 * First advance to idmin in both IDLs.
998 cursora = cursorb = idmin;
999 ida = bdb_idl_first( a, &cursora );
1000 idb = bdb_idl_first( b, &cursorb );
1003 while( ida <= idmax || idb <= idmax ) {
1006 ida = bdb_idl_next( a, &cursora );
1007 idb = bdb_idl_next( b, &cursorb );
1008 } else if ( ida < idb ) {
1009 ida = bdb_idl_next( a, &cursora );
1011 idb = bdb_idl_next( b, &cursorb );
1017 BDB_IDL_CPY( b, a );
1024 * idl_union - return a = a union b
1032 ID cursora = 0, cursorb = 0, cursorc;
1034 if ( BDB_IDL_IS_ZERO( b ) ) {
1038 if ( BDB_IDL_IS_ZERO( a ) ) {
1039 BDB_IDL_CPY( a, b );
1043 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1044 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1045 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1052 ida = bdb_idl_first( a, &cursora );
1053 idb = bdb_idl_first( b, &cursorb );
1057 /* The distinct elements of a are cat'd to b */
1058 while( ida != NOID || idb != NOID ) {
1060 if( ++cursorc > BDB_IDL_UM_MAX ) {
1064 ida = bdb_idl_next( a, &cursora );
1068 ida = bdb_idl_next( a, &cursora );
1069 idb = bdb_idl_next( b, &cursorb );
1073 /* b is copied back to a in sorted order */
1078 while (cursorb <= b[0] || cursorc <= a[0]) {
1083 if (cursorb <= b[0] && b[cursorb] < idb)
1084 a[cursora++] = b[cursorb++];
1097 * bdb_idl_notin - return a intersection ~b (or a minus b)
1106 ID cursora = 0, cursorb = 0;
1108 if( BDB_IDL_IS_ZERO( a ) ||
1109 BDB_IDL_IS_ZERO( b ) ||
1110 BDB_IDL_IS_RANGE( b ) )
1112 BDB_IDL_CPY( ids, a );
1116 if( BDB_IDL_IS_RANGE( a ) ) {
1117 BDB_IDL_CPY( ids, a );
1121 ida = bdb_idl_first( a, &cursora ),
1122 idb = bdb_idl_first( b, &cursorb );
1126 while( ida != NOID ) {
1127 if ( idb == NOID ) {
1128 /* we could shortcut this */
1129 ids[++ids[0]] = ida;
1130 ida = bdb_idl_next( a, &cursora );
1132 } else if ( ida < idb ) {
1133 ids[++ids[0]] = ida;
1134 ida = bdb_idl_next( a, &cursora );
1136 } else if ( ida > idb ) {
1137 idb = bdb_idl_next( b, &cursorb );
1140 ida = bdb_idl_next( a, &cursora );
1141 idb = bdb_idl_next( b, &cursorb );
1149 ID bdb_idl_first( ID *ids, ID *cursor )
1153 if ( ids[0] == 0 ) {
1158 if ( BDB_IDL_IS_RANGE( ids ) ) {
1159 if( *cursor < ids[1] ) {
1168 pos = bdb_idl_search( ids, *cursor );
1170 if( pos > ids[0] ) {
1178 ID bdb_idl_next( ID *ids, ID *cursor )
1180 if ( BDB_IDL_IS_RANGE( ids ) ) {
1181 if( ids[2] < ++(*cursor) ) {
1187 if ( ++(*cursor) <= ids[0] ) {
1188 return ids[*cursor];