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_mutex_lock( &bdb->bi_idl_tree_mutex );
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 IDL_LRU_DELETE( bdb, matched_idl_entry );
361 IDL_LRU_ADD( bdb, matched_idl_entry );
362 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
365 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
372 data.ulen = sizeof(buf);
373 data.flags = DB_DBT_USERMEM;
375 if ( tid ) flags |= DB_RMW;
377 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
380 LDAP_LOG( INDEX, ERR,
381 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
382 db_strerror(rc), rc, 0 );
384 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
385 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
390 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
396 DB_MULTIPLE_INIT( ptr, &data );
398 DB_MULTIPLE_NEXT(ptr, &data, j, len);
401 AC_MEMCPY( i, j, sizeof(ID) );
404 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
406 if ( rc == DB_NOTFOUND ) rc = 0;
408 /* On disk, a range is denoted by 0 in the first element */
410 if (ids[0] != BDB_IDL_RANGE_SIZE) {
412 LDAP_LOG( INDEX, ERR,
413 "=> bdb_idl_fetch_key: range size mismatch: "
414 "expected %ld, got %ld\n",
415 BDB_IDL_RANGE_SIZE, ids[0], 0 );
417 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
418 "range size mismatch: expected %d, got %ld\n",
419 BDB_IDL_RANGE_SIZE, ids[0], 0 );
421 cursor->c_close( cursor );
424 BDB_IDL_RANGE( ids, ids[2], ids[3] );
426 data.size = BDB_IDL_SIZEOF(ids);
429 rc2 = cursor->c_close( cursor );
432 LDAP_LOG( INDEX, ERR,
433 "bdb_idl_fetch_key: close failed: %s (%d)\n",
434 db_strerror(rc2), rc2, 0 );
436 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
437 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
442 if( rc == DB_NOTFOUND ) {
445 } else if( rc != 0 ) {
447 LDAP_LOG( INDEX, ERR,
448 "bdb_idl_fetch_key: get failed: %s (%d)\n",
449 db_strerror(rc), rc, 0 );
451 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
452 "get failed: %s (%d)\n",
453 db_strerror(rc), rc, 0 );
457 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
458 /* size not multiple of ID size */
460 LDAP_LOG( INDEX, ERR,
461 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
462 (long) sizeof( ID ), (long) data.size, 0 );
464 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
465 "odd size: expected %ld multiple, got %ld\n",
466 (long) sizeof( ID ), (long) data.size, 0 );
470 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
473 LDAP_LOG( INDEX, ERR,
474 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
475 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
477 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
478 "get size mismatch: expected %ld, got %ld\n",
479 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
484 #ifdef SLAP_IDL_CACHE
485 if ( bdb->bi_idl_cache_max_size ) {
486 bdb_idl_cache_entry_t *ee;
487 ee = (bdb_idl_cache_entry_t *) ch_malloc(
488 sizeof( bdb_idl_cache_entry_t ) );
490 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
491 ee->idl_lru_prev = NULL;
492 ee->idl_lru_next = NULL;
493 BDB_IDL_CPY( ee->idl, ids );
494 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
495 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
496 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
497 bdb_idl_entry_cmp, avl_dup_error ))
499 ch_free( ee->kstr.bv_val );
503 IDL_LRU_ADD( bdb, ee );
504 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
506 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
507 ee = bdb->bi_idl_lru_tail;
508 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
509 bdb_idl_entry_cmp ) == NULL ) {
511 LDAP_LOG( INDEX, ERR,
512 "bdb_idl_fetch_key: AVL delete failed\n",
515 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
516 "AVL delete failed\n",
520 IDL_LRU_DELETE( bdb, ee );
522 --bdb->bi_idl_cache_size;
523 ch_free( ee->kstr.bv_val );
529 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
545 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
555 LDAP_LOG( INDEX, ARGS,
556 "bdb_idl_insert_key: %lx %s\n",
557 (long) id, bdb_show_key( key, buf ), 0 );
559 Debug( LDAP_DEBUG_ARGS,
560 "bdb_idl_insert_key: %lx %s\n",
561 (long) id, bdb_show_key( key, buf ), 0 );
565 assert( id != NOID );
567 #ifdef SLAP_IDL_CACHE
568 if ( bdb->bi_idl_cache_size ) {
569 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
570 DBT2bv( key, &idl_tmp.kstr );
572 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
573 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
575 if ( matched_idl_entry != NULL ) {
576 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
577 bdb_idl_entry_cmp ) == NULL ) {
579 LDAP_LOG( INDEX, ERR,
580 "bdb_idl_fetch_key: AVL delete failed\n",
583 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
584 "AVL delete failed\n",
588 --bdb->bi_idl_cache_size;
589 IDL_LRU_DELETE( bdb, matched_idl_entry );
590 free( matched_idl_entry->kstr.bv_val );
591 free( matched_idl_entry->idl );
592 free( matched_idl_entry );
594 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
599 data.size = sizeof( ID );
600 data.ulen = data.size;
601 data.flags = DB_DBT_USERMEM;
603 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
606 LDAP_LOG( INDEX, ERR,
607 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
608 db_strerror(rc), rc, 0 );
610 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
611 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
616 /* Fetch the first data item for this key, to see if it
617 * exists and if it's a range.
619 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
623 /* not a range, count the number of items */
625 rc = cursor->c_count( cursor, &count, 0 );
630 if ( count >= BDB_IDL_DB_MAX ) {
631 /* No room, convert to a range */
634 key2.dlen = key2.ulen;
635 key2.flags |= DB_DBT_PARTIAL;
639 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
640 if ( rc != 0 && rc != DB_NOTFOUND ) {
641 err = "c_get next_nodup";
644 if ( rc == DB_NOTFOUND ) {
645 rc = cursor->c_get( cursor, key, &data, DB_LAST );
651 rc = cursor->c_get( cursor, key, &data, DB_PREV );
661 rc = db->del( db, tid, key, 0 );
668 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
674 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
680 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
686 /* There's room, just store it */
690 /* It's a range, see if we need to rewrite
695 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
702 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
708 if ( id < lo || id > hi ) {
709 /* Delete the current lo/hi */
710 rc = cursor->c_del( cursor, 0 );
716 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
723 } else if ( rc == DB_NOTFOUND ) {
724 put1: data.data = &id;
725 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
726 /* Don't worry if it's already there */
727 if ( rc != 0 && rc != DB_KEYEXIST ) {
732 /* initial c_get failed, nothing was done */
735 LDAP_LOG( INDEX, ERR,
736 "bdb_idl_insert_key: %s failed: %s (%d)\n",
737 err, db_strerror(rc), rc );
739 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
740 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
742 cursor->c_close( cursor );
745 rc = cursor->c_close( cursor );
748 LDAP_LOG( INDEX, ERR,
749 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
750 db_strerror(rc), rc, 0 );
752 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
753 "c_close failed: %s (%d)\n",
754 db_strerror(rc), rc, 0 );
768 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
778 LDAP_LOG( INDEX, ARGS,
779 "bdb_idl_delete_key: %lx %s\n",
780 (long) id, bdb_show_key( key, buf ), 0 );
782 Debug( LDAP_DEBUG_ARGS,
783 "bdb_idl_delete_key: %lx %s\n",
784 (long) id, bdb_show_key( key, buf ), 0 );
787 assert( id != NOID );
789 #ifdef SLAP_IDL_CACHE
790 if ( bdb->bi_idl_cache_max_size ) {
791 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
792 DBT2bv( key, &idl_tmp.kstr );
794 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_mutex );
795 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
797 if ( matched_idl_entry != NULL ) {
798 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
799 bdb_idl_entry_cmp ) == NULL ) {
801 LDAP_LOG( INDEX, ERR,
802 "bdb_idl_fetch_key: AVL delete failed\n",
805 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
806 "AVL delete failed\n",
810 --bdb->bi_idl_cache_size;
811 IDL_LRU_DELETE( bdb, matched_idl_entry );
812 free( matched_idl_entry->kstr.bv_val );
813 free( matched_idl_entry->idl );
814 free( matched_idl_entry );
816 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_mutex );
822 data.size = sizeof( id );
823 data.ulen = data.size;
824 data.flags = DB_DBT_USERMEM;
826 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
829 LDAP_LOG( INDEX, ERR,
830 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
831 db_strerror(rc), rc, 0 );
833 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
834 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
838 /* Fetch the first data item for this key, to see if it
839 * exists and if it's a range.
841 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
845 /* Not a range, just delete it */
847 /* position to correct item */
849 rc = cursor->c_get( cursor, key, &data,
850 DB_GET_BOTH | DB_RMW );
856 rc = cursor->c_del( cursor, 0 );
862 /* It's a range, see if we need to rewrite
866 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
872 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
877 if ( id == lo || id == hi ) {
881 } else if ( id == hi ) {
886 /* The range has collapsed... */
887 rc = db->del( db, tid, key, 0 );
894 /* reposition on lo slot */
896 cursor->c_get( cursor, key, &data, DB_PREV );
899 rc = cursor->c_del( cursor, 0 );
907 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
916 /* initial c_get failed, nothing was done */
918 if ( rc != DB_NOTFOUND ) {
920 LDAP_LOG( INDEX, ERR,
921 "bdb_idl_delete_key: %s failed: %s (%d)\n",
922 err, db_strerror(rc), rc );
924 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
925 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
928 cursor->c_close( cursor );
931 rc = cursor->c_close( cursor );
934 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
935 db_strerror(rc), rc, 0 );
937 Debug( LDAP_DEBUG_ANY,
938 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
939 db_strerror(rc), rc, 0 );
948 * idl_intersection - return a = a intersection b
951 bdb_idl_intersection(
957 ID cursora = 0, cursorb = 0, cursorc;
960 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
965 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
966 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
967 if ( idmin > idmax ) {
970 } else if ( idmin == idmax ) {
976 if ( BDB_IDL_IS_RANGE( a ) ) {
977 if ( BDB_IDL_IS_RANGE(b) ) {
978 /* If both are ranges, just shrink the boundaries */
983 /* Else swap so that b is the range, a is a list */
991 /* If a range completely covers the list, the result is
992 * just the list. If idmin to idmax is contiguous, just
993 * turn it into a range.
995 if ( BDB_IDL_IS_RANGE( b )
996 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
997 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
998 if (idmax - idmin + 1 == a[0])
1007 /* Fine, do the intersection one element at a time.
1008 * First advance to idmin in both IDLs.
1010 cursora = cursorb = idmin;
1011 ida = bdb_idl_first( a, &cursora );
1012 idb = bdb_idl_first( b, &cursorb );
1015 while( ida <= idmax || idb <= idmax ) {
1018 ida = bdb_idl_next( a, &cursora );
1019 idb = bdb_idl_next( b, &cursorb );
1020 } else if ( ida < idb ) {
1021 ida = bdb_idl_next( a, &cursora );
1023 idb = bdb_idl_next( b, &cursorb );
1029 BDB_IDL_CPY( b, a );
1036 * idl_union - return a = a union b
1044 ID cursora = 0, cursorb = 0, cursorc;
1046 if ( BDB_IDL_IS_ZERO( b ) ) {
1050 if ( BDB_IDL_IS_ZERO( a ) ) {
1051 BDB_IDL_CPY( a, b );
1055 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1056 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1057 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1064 ida = bdb_idl_first( a, &cursora );
1065 idb = bdb_idl_first( b, &cursorb );
1069 /* The distinct elements of a are cat'd to b */
1070 while( ida != NOID || idb != NOID ) {
1072 if( ++cursorc > BDB_IDL_UM_MAX ) {
1076 ida = bdb_idl_next( a, &cursora );
1080 ida = bdb_idl_next( a, &cursora );
1081 idb = bdb_idl_next( b, &cursorb );
1085 /* b is copied back to a in sorted order */
1090 while (cursorb <= b[0] || cursorc <= a[0]) {
1095 if (cursorb <= b[0] && b[cursorb] < idb)
1096 a[cursora++] = b[cursorb++];
1109 * bdb_idl_notin - return a intersection ~b (or a minus b)
1118 ID cursora = 0, cursorb = 0;
1120 if( BDB_IDL_IS_ZERO( a ) ||
1121 BDB_IDL_IS_ZERO( b ) ||
1122 BDB_IDL_IS_RANGE( b ) )
1124 BDB_IDL_CPY( ids, a );
1128 if( BDB_IDL_IS_RANGE( a ) ) {
1129 BDB_IDL_CPY( ids, a );
1133 ida = bdb_idl_first( a, &cursora ),
1134 idb = bdb_idl_first( b, &cursorb );
1138 while( ida != NOID ) {
1139 if ( idb == NOID ) {
1140 /* we could shortcut this */
1141 ids[++ids[0]] = ida;
1142 ida = bdb_idl_next( a, &cursora );
1144 } else if ( ida < idb ) {
1145 ids[++ids[0]] = ida;
1146 ida = bdb_idl_next( a, &cursora );
1148 } else if ( ida > idb ) {
1149 idb = bdb_idl_next( b, &cursorb );
1152 ida = bdb_idl_next( a, &cursora );
1153 idb = bdb_idl_next( b, &cursorb );
1161 ID bdb_idl_first( ID *ids, ID *cursor )
1165 if ( ids[0] == 0 ) {
1170 if ( BDB_IDL_IS_RANGE( ids ) ) {
1171 if( *cursor < ids[1] ) {
1180 pos = bdb_idl_search( ids, *cursor );
1182 if( pos > ids[0] ) {
1190 ID bdb_idl_next( ID *ids, ID *cursor )
1192 if ( BDB_IDL_IS_RANGE( ids ) ) {
1193 if( ids[2] < ++(*cursor) ) {
1199 if ( ++(*cursor) <= ids[0] ) {
1200 return ids[*cursor];