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 (id < BDB_IDL_FIRST(ids))
191 else if (id > BDB_IDL_LAST(ids))
196 x = bdb_idl_search( ids, id );
204 if ( x <= ids[0] && ids[x] == id ) {
209 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
212 ids[2] = ids[ids[0]-1];
213 } else if ( ids[ids[0]-1] < id ) {
216 ids[2] = ids[ids[0]-1];
222 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
236 static int idl_delete( ID *ids, ID id )
238 unsigned x = bdb_idl_search( ids, id );
242 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
244 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
258 if( x > ids[0] || ids[x] != id ) {
262 } else if ( --ids[0] == 0 ) {
268 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
286 if ( key->size == sizeof( ID ) ) {
287 unsigned char *c = key->data;
288 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
303 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
311 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
312 #ifdef SLAP_IDL_CACHE
313 bdb_idl_cache_entry_t idl_tmp;
316 /* If using BerkeleyDB 4.0, the buf must be large enough to
317 * grab the entire IDL in one get(), otherwise BDB will leak
318 * resources on subsequent get's. We can safely call get()
319 * twice - once for the data, and once to get the DB_NOTFOUND
320 * result meaning there's no more data. See ITS#2040 for details.
321 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
322 * stack space is too limited.
324 * configure now requires Berkeley DB 4.1.
326 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
327 # define BDB_ENOUGH 5
329 # define BDB_ENOUGH 1
331 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
336 LDAP_LOG( INDEX, ARGS,
337 "bdb_idl_fetch_key: %s\n",
338 bdb_show_key( key, keybuf ), 0, 0 );
340 Debug( LDAP_DEBUG_ARGS,
341 "bdb_idl_fetch_key: %s\n",
342 bdb_show_key( key, keybuf ), 0, 0 );
345 assert( ids != NULL );
347 #ifdef SLAP_IDL_CACHE
348 if ( bdb->bi_idl_cache_max_size ) {
349 bdb_idl_cache_entry_t *matched_idl_entry;
350 DBT2bv( key, &idl_tmp.kstr );
352 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
353 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
355 if ( matched_idl_entry != NULL ) {
356 BDB_IDL_CPY( ids, matched_idl_entry->idl );
357 IDL_LRU_DELETE( bdb, matched_idl_entry );
358 IDL_LRU_ADD( bdb, matched_idl_entry );
359 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
362 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
369 data.ulen = sizeof(buf);
370 data.flags = DB_DBT_USERMEM;
372 if ( tid ) flags |= DB_RMW;
374 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
377 LDAP_LOG( INDEX, ERR,
378 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
379 db_strerror(rc), rc, 0 );
381 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
382 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
387 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
393 DB_MULTIPLE_INIT( ptr, &data );
395 DB_MULTIPLE_NEXT(ptr, &data, j, len);
398 AC_MEMCPY( i, j, sizeof(ID) );
401 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
403 if ( rc == DB_NOTFOUND ) rc = 0;
405 /* On disk, a range is denoted by 0 in the first element */
407 if (ids[0] != BDB_IDL_RANGE_SIZE) {
409 LDAP_LOG( INDEX, ERR,
410 "=> bdb_idl_fetch_key: range size mismatch: "
411 "expected %ld, got %ld\n",
412 BDB_IDL_RANGE_SIZE, ids[0], 0 );
414 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
415 "range size mismatch: expected %d, got %ld\n",
416 BDB_IDL_RANGE_SIZE, ids[0], 0 );
418 cursor->c_close( cursor );
421 BDB_IDL_RANGE( ids, ids[2], ids[3] );
423 data.size = BDB_IDL_SIZEOF(ids);
426 rc2 = cursor->c_close( cursor );
429 LDAP_LOG( INDEX, ERR,
430 "bdb_idl_fetch_key: close failed: %s (%d)\n",
431 db_strerror(rc2), rc2, 0 );
433 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
434 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
439 if( rc == DB_NOTFOUND ) {
442 } else if( rc != 0 ) {
444 LDAP_LOG( INDEX, ERR,
445 "bdb_idl_fetch_key: get failed: %s (%d)\n",
446 db_strerror(rc), rc, 0 );
448 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
449 "get failed: %s (%d)\n",
450 db_strerror(rc), rc, 0 );
454 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
455 /* size not multiple of ID size */
457 LDAP_LOG( INDEX, ERR,
458 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
459 (long) sizeof( ID ), (long) data.size, 0 );
461 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
462 "odd size: expected %ld multiple, got %ld\n",
463 (long) sizeof( ID ), (long) data.size, 0 );
467 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
470 LDAP_LOG( INDEX, ERR,
471 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
472 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
474 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
475 "get size mismatch: expected %ld, got %ld\n",
476 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
481 #ifdef SLAP_IDL_CACHE
482 if ( bdb->bi_idl_cache_max_size ) {
483 bdb_idl_cache_entry_t *ee;
484 ee = (bdb_idl_cache_entry_t *) ch_malloc(
485 sizeof( bdb_idl_cache_entry_t ) );
487 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
488 ee->idl_lru_prev = NULL;
489 ee->idl_lru_next = NULL;
490 BDB_IDL_CPY( ee->idl, ids );
491 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
492 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
493 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
494 bdb_idl_entry_cmp, avl_dup_error ))
496 ch_free( ee->kstr.bv_val );
500 IDL_LRU_ADD( bdb, ee );
501 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
503 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
504 ee = bdb->bi_idl_lru_tail;
505 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
506 bdb_idl_entry_cmp ) == NULL ) {
508 LDAP_LOG( INDEX, ERR,
509 "bdb_idl_fetch_key: AVL delete failed\n",
512 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
513 "AVL delete failed\n",
517 IDL_LRU_DELETE( bdb, ee );
519 --bdb->bi_idl_cache_size;
520 ch_free( ee->kstr.bv_val );
526 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
542 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
552 LDAP_LOG( INDEX, ARGS,
553 "bdb_idl_insert_key: %lx %s\n",
554 (long) id, bdb_show_key( key, buf ), 0 );
556 Debug( LDAP_DEBUG_ARGS,
557 "bdb_idl_insert_key: %lx %s\n",
558 (long) id, bdb_show_key( key, buf ), 0 );
562 assert( id != NOID );
564 #ifdef SLAP_IDL_CACHE
565 if ( bdb->bi_idl_cache_size ) {
566 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
567 DBT2bv( key, &idl_tmp.kstr );
569 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
570 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
572 if ( matched_idl_entry != NULL ) {
573 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
574 bdb_idl_entry_cmp ) == NULL ) {
576 LDAP_LOG( INDEX, ERR,
577 "bdb_idl_fetch_key: AVL delete failed\n",
580 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
581 "AVL delete failed\n",
585 --bdb->bi_idl_cache_size;
586 IDL_LRU_DELETE( bdb, matched_idl_entry );
587 free( matched_idl_entry->kstr.bv_val );
588 free( matched_idl_entry->idl );
589 free( matched_idl_entry );
591 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
596 data.size = sizeof( ID );
597 data.ulen = data.size;
598 data.flags = DB_DBT_USERMEM;
600 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
603 LDAP_LOG( INDEX, ERR,
604 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
605 db_strerror(rc), rc, 0 );
607 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
608 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
613 /* Fetch the first data item for this key, to see if it
614 * exists and if it's a range.
616 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
620 /* not a range, count the number of items */
622 rc = cursor->c_count( cursor, &count, 0 );
627 if ( count >= BDB_IDL_DB_MAX ) {
628 /* No room, convert to a range */
631 key2.dlen = key2.ulen;
632 key2.flags |= DB_DBT_PARTIAL;
636 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
637 if ( rc != 0 && rc != DB_NOTFOUND ) {
638 err = "c_get next_nodup";
641 if ( rc == DB_NOTFOUND ) {
642 rc = cursor->c_get( cursor, key, &data, DB_LAST );
648 rc = cursor->c_get( cursor, key, &data, DB_PREV );
658 rc = db->del( db, tid, key, 0 );
665 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
671 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
677 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
683 /* There's room, just store it */
687 /* It's a range, see if we need to rewrite
692 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
699 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
705 if ( id < lo || id > hi ) {
706 /* Delete the current lo/hi */
707 rc = cursor->c_del( cursor, 0 );
713 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
720 } else if ( rc == DB_NOTFOUND ) {
721 put1: data.data = &id;
722 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
723 /* Don't worry if it's already there */
724 if ( rc != 0 && rc != DB_KEYEXIST ) {
729 /* initial c_get failed, nothing was done */
732 LDAP_LOG( INDEX, ERR,
733 "bdb_idl_insert_key: %s failed: %s (%d)\n",
734 err, db_strerror(rc), rc );
736 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
737 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
739 cursor->c_close( cursor );
742 rc = cursor->c_close( cursor );
745 LDAP_LOG( INDEX, ERR,
746 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
747 db_strerror(rc), rc, 0 );
749 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
750 "c_close failed: %s (%d)\n",
751 db_strerror(rc), rc, 0 );
765 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
775 LDAP_LOG( INDEX, ARGS,
776 "bdb_idl_delete_key: %lx %s\n",
777 (long) id, bdb_show_key( key, buf ), 0 );
779 Debug( LDAP_DEBUG_ARGS,
780 "bdb_idl_delete_key: %lx %s\n",
781 (long) id, bdb_show_key( key, buf ), 0 );
784 assert( id != NOID );
786 #ifdef SLAP_IDL_CACHE
787 if ( bdb->bi_idl_cache_max_size ) {
788 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
789 DBT2bv( key, &idl_tmp.kstr );
791 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
792 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
794 if ( matched_idl_entry != NULL ) {
795 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
796 bdb_idl_entry_cmp ) == NULL ) {
798 LDAP_LOG( INDEX, ERR,
799 "bdb_idl_fetch_key: AVL delete failed\n",
802 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
803 "AVL delete failed\n",
807 --bdb->bi_idl_cache_size;
808 IDL_LRU_DELETE( bdb, matched_idl_entry );
809 free( matched_idl_entry->kstr.bv_val );
810 free( matched_idl_entry->idl );
811 free( matched_idl_entry );
813 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
819 data.size = sizeof( id );
820 data.ulen = data.size;
821 data.flags = DB_DBT_USERMEM;
823 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
826 LDAP_LOG( INDEX, ERR,
827 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
828 db_strerror(rc), rc, 0 );
830 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
831 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
835 /* Fetch the first data item for this key, to see if it
836 * exists and if it's a range.
838 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
842 /* Not a range, just delete it */
844 /* position to correct item */
846 rc = cursor->c_get( cursor, key, &data,
847 DB_GET_BOTH | DB_RMW );
853 rc = cursor->c_del( cursor, 0 );
859 /* It's a range, see if we need to rewrite
863 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
869 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
874 if ( id == lo || id == hi ) {
878 } else if ( id == hi ) {
883 /* The range has collapsed... */
884 rc = db->del( db, tid, key, 0 );
891 /* reposition on lo slot */
893 cursor->c_get( cursor, key, &data, DB_PREV );
896 rc = cursor->c_del( cursor, 0 );
904 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
913 /* initial c_get failed, nothing was done */
915 if ( rc != DB_NOTFOUND ) {
917 LDAP_LOG( INDEX, ERR,
918 "bdb_idl_delete_key: %s failed: %s (%d)\n",
919 err, db_strerror(rc), rc );
921 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
922 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
925 cursor->c_close( cursor );
928 rc = cursor->c_close( cursor );
931 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
932 db_strerror(rc), rc, 0 );
934 Debug( LDAP_DEBUG_ANY,
935 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
936 db_strerror(rc), rc, 0 );
945 * idl_intersection - return a = a intersection b
948 bdb_idl_intersection(
954 ID cursora = 0, cursorb = 0, cursorc;
957 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
962 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
963 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
964 if ( idmin > idmax ) {
967 } else if ( idmin == idmax ) {
973 if ( BDB_IDL_IS_RANGE( a ) ) {
974 if ( BDB_IDL_IS_RANGE(b) ) {
975 /* If both are ranges, just shrink the boundaries */
980 /* Else swap so that b is the range, a is a list */
988 /* If a range completely covers the list, the result is
989 * just the list. If idmin to idmax is contiguous, just
990 * turn it into a range.
992 if ( BDB_IDL_IS_RANGE( b )
993 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
994 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
995 if (idmax - idmin + 1 == a[0])
1004 /* Fine, do the intersection one element at a time.
1005 * First advance to idmin in both IDLs.
1007 cursora = cursorb = idmin;
1008 ida = bdb_idl_first( a, &cursora );
1009 idb = bdb_idl_first( b, &cursorb );
1012 while( ida <= idmax || idb <= idmax ) {
1015 ida = bdb_idl_next( a, &cursora );
1016 idb = bdb_idl_next( b, &cursorb );
1017 } else if ( ida < idb ) {
1018 ida = bdb_idl_next( a, &cursora );
1020 idb = bdb_idl_next( b, &cursorb );
1026 BDB_IDL_CPY( b, a );
1033 * idl_union - return a = a union b
1041 ID cursora = 0, cursorb = 0, cursorc;
1043 if ( BDB_IDL_IS_ZERO( b ) ) {
1047 if ( BDB_IDL_IS_ZERO( a ) ) {
1048 BDB_IDL_CPY( a, b );
1052 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1053 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1054 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1061 ida = bdb_idl_first( a, &cursora );
1062 idb = bdb_idl_first( b, &cursorb );
1066 /* The distinct elements of a are cat'd to b */
1067 while( ida != NOID || idb != NOID ) {
1069 if( ++cursorc > BDB_IDL_UM_MAX ) {
1073 ida = bdb_idl_next( a, &cursora );
1077 ida = bdb_idl_next( a, &cursora );
1078 idb = bdb_idl_next( b, &cursorb );
1082 /* b is copied back to a in sorted order */
1087 while (cursorb <= b[0] || cursorc <= a[0]) {
1092 if (cursorb <= b[0] && b[cursorb] < idb)
1093 a[cursora++] = b[cursorb++];
1106 * bdb_idl_notin - return a intersection ~b (or a minus b)
1115 ID cursora = 0, cursorb = 0;
1117 if( BDB_IDL_IS_ZERO( a ) ||
1118 BDB_IDL_IS_ZERO( b ) ||
1119 BDB_IDL_IS_RANGE( b ) )
1121 BDB_IDL_CPY( ids, a );
1125 if( BDB_IDL_IS_RANGE( a ) ) {
1126 BDB_IDL_CPY( ids, a );
1130 ida = bdb_idl_first( a, &cursora ),
1131 idb = bdb_idl_first( b, &cursorb );
1135 while( ida != NOID ) {
1136 if ( idb == NOID ) {
1137 /* we could shortcut this */
1138 ids[++ids[0]] = ida;
1139 ida = bdb_idl_next( a, &cursora );
1141 } else if ( ida < idb ) {
1142 ids[++ids[0]] = ida;
1143 ida = bdb_idl_next( a, &cursora );
1145 } else if ( ida > idb ) {
1146 idb = bdb_idl_next( b, &cursorb );
1149 ida = bdb_idl_next( a, &cursora );
1150 idb = bdb_idl_next( b, &cursorb );
1158 ID bdb_idl_first( ID *ids, ID *cursor )
1162 if ( ids[0] == 0 ) {
1167 if ( BDB_IDL_IS_RANGE( ids ) ) {
1168 if( *cursor < ids[1] ) {
1177 pos = bdb_idl_search( ids, *cursor );
1179 if( pos > ids[0] ) {
1187 ID bdb_idl_next( ID *ids, ID *cursor )
1189 if ( BDB_IDL_IS_RANGE( ids ) ) {
1190 if( ids[2] < ++(*cursor) ) {
1196 if ( ++(*cursor) <= ids[0] ) {
1197 return ids[*cursor];