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_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
356 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
358 if ( matched_idl_entry != NULL ) {
359 if ( matched_idl_entry->idl )
360 BDB_IDL_CPY( ids, matched_idl_entry->idl );
361 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
362 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
363 IDL_LRU_DELETE( bdb, matched_idl_entry );
364 IDL_LRU_ADD( bdb, matched_idl_entry );
365 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
366 if ( matched_idl_entry->idl )
371 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
378 data.ulen = sizeof(buf);
379 data.flags = DB_DBT_USERMEM;
381 if ( tid ) flags |= DB_RMW;
383 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
386 LDAP_LOG( INDEX, ERR,
387 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
388 db_strerror(rc), rc, 0 );
390 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
391 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
396 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
402 DB_MULTIPLE_INIT( ptr, &data );
404 DB_MULTIPLE_NEXT(ptr, &data, j, len);
407 AC_MEMCPY( i, j, sizeof(ID) );
410 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
412 if ( rc == DB_NOTFOUND ) rc = 0;
414 /* On disk, a range is denoted by 0 in the first element */
416 if (ids[0] != BDB_IDL_RANGE_SIZE) {
418 LDAP_LOG( INDEX, ERR,
419 "=> bdb_idl_fetch_key: range size mismatch: "
420 "expected %ld, got %ld\n",
421 BDB_IDL_RANGE_SIZE, ids[0], 0 );
423 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
424 "range size mismatch: expected %d, got %ld\n",
425 BDB_IDL_RANGE_SIZE, ids[0], 0 );
427 cursor->c_close( cursor );
430 BDB_IDL_RANGE( ids, ids[2], ids[3] );
432 data.size = BDB_IDL_SIZEOF(ids);
435 rc2 = cursor->c_close( cursor );
438 LDAP_LOG( INDEX, ERR,
439 "bdb_idl_fetch_key: close failed: %s (%d)\n",
440 db_strerror(rc2), rc2, 0 );
442 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
443 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
448 if( rc == DB_NOTFOUND ) {
449 #ifndef SLAP_IDL_CACHE
453 } else if( rc != 0 ) {
455 LDAP_LOG( INDEX, ERR,
456 "bdb_idl_fetch_key: get failed: %s (%d)\n",
457 db_strerror(rc), rc, 0 );
459 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
460 "get failed: %s (%d)\n",
461 db_strerror(rc), rc, 0 );
465 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
466 /* size not multiple of ID size */
468 LDAP_LOG( INDEX, ERR,
469 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
470 (long) sizeof( ID ), (long) data.size, 0 );
472 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
473 "odd size: expected %ld multiple, got %ld\n",
474 (long) sizeof( ID ), (long) data.size, 0 );
478 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
481 LDAP_LOG( INDEX, ERR,
482 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
483 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
485 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
486 "get size mismatch: expected %ld, got %ld\n",
487 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
492 #ifdef SLAP_IDL_CACHE
493 if ( bdb->bi_idl_cache_max_size ) {
494 bdb_idl_cache_entry_t *ee;
495 ee = (bdb_idl_cache_entry_t *) ch_malloc(
496 sizeof( bdb_idl_cache_entry_t ) );
498 if ( rc == DB_NOTFOUND) {
501 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
502 BDB_IDL_CPY( ee->idl, ids );
504 ee->idl_lru_prev = NULL;
505 ee->idl_lru_next = NULL;
506 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
507 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
508 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
509 bdb_idl_entry_cmp, avl_dup_error ))
511 ch_free( ee->kstr.bv_val );
514 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
517 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
518 IDL_LRU_ADD( bdb, ee );
519 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
521 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
522 ee = bdb->bi_idl_lru_tail;
523 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
524 bdb_idl_entry_cmp ) == NULL ) {
526 LDAP_LOG( INDEX, ERR,
527 "bdb_idl_fetch_key: AVL delete failed\n",
530 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
531 "AVL delete failed\n",
535 IDL_LRU_DELETE( bdb, ee );
537 --bdb->bi_idl_cache_size;
538 ch_free( ee->kstr.bv_val );
544 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
545 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
561 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
571 LDAP_LOG( INDEX, ARGS,
572 "bdb_idl_insert_key: %lx %s\n",
573 (long) id, bdb_show_key( key, buf ), 0 );
575 Debug( LDAP_DEBUG_ARGS,
576 "bdb_idl_insert_key: %lx %s\n",
577 (long) id, bdb_show_key( key, buf ), 0 );
581 assert( id != NOID );
583 #ifdef SLAP_IDL_CACHE
584 if ( bdb->bi_idl_cache_size ) {
585 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
586 DBT2bv( key, &idl_tmp.kstr );
588 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
589 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
591 if ( matched_idl_entry != NULL ) {
592 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
593 bdb_idl_entry_cmp ) == NULL ) {
595 LDAP_LOG( INDEX, ERR,
596 "bdb_idl_fetch_key: AVL delete failed\n",
599 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
600 "AVL delete failed\n",
604 --bdb->bi_idl_cache_size;
605 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
606 IDL_LRU_DELETE( bdb, matched_idl_entry );
607 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
608 free( matched_idl_entry->kstr.bv_val );
609 if ( matched_idl_entry->idl )
610 free( matched_idl_entry->idl );
611 free( matched_idl_entry );
613 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
618 data.size = sizeof( ID );
619 data.ulen = data.size;
620 data.flags = DB_DBT_USERMEM;
622 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
625 LDAP_LOG( INDEX, ERR,
626 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
627 db_strerror(rc), rc, 0 );
629 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
630 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
635 /* Fetch the first data item for this key, to see if it
636 * exists and if it's a range.
638 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
642 /* not a range, count the number of items */
644 rc = cursor->c_count( cursor, &count, 0 );
649 if ( count >= BDB_IDL_DB_MAX ) {
650 /* No room, convert to a range */
653 key2.dlen = key2.ulen;
654 key2.flags |= DB_DBT_PARTIAL;
658 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
659 if ( rc != 0 && rc != DB_NOTFOUND ) {
660 err = "c_get next_nodup";
663 if ( rc == DB_NOTFOUND ) {
664 rc = cursor->c_get( cursor, key, &data, DB_LAST );
670 rc = cursor->c_get( cursor, key, &data, DB_PREV );
680 rc = db->del( db, tid, key, 0 );
687 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
693 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
699 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
705 /* There's room, just store it */
709 /* It's a range, see if we need to rewrite
714 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
721 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
727 if ( id < lo || id > hi ) {
728 /* Delete the current lo/hi */
729 rc = cursor->c_del( cursor, 0 );
735 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
742 } else if ( rc == DB_NOTFOUND ) {
743 put1: data.data = &id;
744 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
745 /* Don't worry if it's already there */
746 if ( rc != 0 && rc != DB_KEYEXIST ) {
751 /* initial c_get failed, nothing was done */
754 LDAP_LOG( INDEX, ERR,
755 "bdb_idl_insert_key: %s failed: %s (%d)\n",
756 err, db_strerror(rc), rc );
758 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
759 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
761 cursor->c_close( cursor );
764 rc = cursor->c_close( cursor );
767 LDAP_LOG( INDEX, ERR,
768 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
769 db_strerror(rc), rc, 0 );
771 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
772 "c_close failed: %s (%d)\n",
773 db_strerror(rc), rc, 0 );
787 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
797 LDAP_LOG( INDEX, ARGS,
798 "bdb_idl_delete_key: %lx %s\n",
799 (long) id, bdb_show_key( key, buf ), 0 );
801 Debug( LDAP_DEBUG_ARGS,
802 "bdb_idl_delete_key: %lx %s\n",
803 (long) id, bdb_show_key( key, buf ), 0 );
806 assert( id != NOID );
808 #ifdef SLAP_IDL_CACHE
809 if ( bdb->bi_idl_cache_max_size ) {
810 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
811 DBT2bv( key, &idl_tmp.kstr );
813 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
814 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
816 if ( matched_idl_entry != NULL ) {
817 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
818 bdb_idl_entry_cmp ) == NULL ) {
820 LDAP_LOG( INDEX, ERR,
821 "bdb_idl_fetch_key: AVL delete failed\n",
824 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
825 "AVL delete failed\n",
829 --bdb->bi_idl_cache_size;
830 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
831 IDL_LRU_DELETE( bdb, matched_idl_entry );
832 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
833 free( matched_idl_entry->kstr.bv_val );
834 if ( matched_idl_entry->idl )
835 free( matched_idl_entry->idl );
836 free( matched_idl_entry );
838 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
844 data.size = sizeof( id );
845 data.ulen = data.size;
846 data.flags = DB_DBT_USERMEM;
848 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
851 LDAP_LOG( INDEX, ERR,
852 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
853 db_strerror(rc), rc, 0 );
855 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
856 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
860 /* Fetch the first data item for this key, to see if it
861 * exists and if it's a range.
863 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
867 /* Not a range, just delete it */
869 /* position to correct item */
871 rc = cursor->c_get( cursor, key, &data,
872 DB_GET_BOTH | DB_RMW );
878 rc = cursor->c_del( cursor, 0 );
884 /* It's a range, see if we need to rewrite
888 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
894 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
899 if ( id == lo || id == hi ) {
903 } else if ( id == hi ) {
908 /* The range has collapsed... */
909 rc = db->del( db, tid, key, 0 );
916 /* reposition on lo slot */
918 cursor->c_get( cursor, key, &data, DB_PREV );
921 rc = cursor->c_del( cursor, 0 );
929 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
938 /* initial c_get failed, nothing was done */
940 if ( rc != DB_NOTFOUND ) {
942 LDAP_LOG( INDEX, ERR,
943 "bdb_idl_delete_key: %s failed: %s (%d)\n",
944 err, db_strerror(rc), rc );
946 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
947 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
950 cursor->c_close( cursor );
953 rc = cursor->c_close( cursor );
956 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
957 db_strerror(rc), rc, 0 );
959 Debug( LDAP_DEBUG_ANY,
960 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
961 db_strerror(rc), rc, 0 );
970 * idl_intersection - return a = a intersection b
973 bdb_idl_intersection(
979 ID cursora = 0, cursorb = 0, cursorc;
982 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
987 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
988 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
989 if ( idmin > idmax ) {
992 } else if ( idmin == idmax ) {
998 if ( BDB_IDL_IS_RANGE( a ) ) {
999 if ( BDB_IDL_IS_RANGE(b) ) {
1000 /* If both are ranges, just shrink the boundaries */
1005 /* Else swap so that b is the range, a is a list */
1013 /* If a range completely covers the list, the result is
1014 * just the list. If idmin to idmax is contiguous, just
1015 * turn it into a range.
1017 if ( BDB_IDL_IS_RANGE( b )
1018 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1019 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1020 if (idmax - idmin + 1 == a[0])
1029 /* Fine, do the intersection one element at a time.
1030 * First advance to idmin in both IDLs.
1032 cursora = cursorb = idmin;
1033 ida = bdb_idl_first( a, &cursora );
1034 idb = bdb_idl_first( b, &cursorb );
1037 while( ida <= idmax || idb <= idmax ) {
1040 ida = bdb_idl_next( a, &cursora );
1041 idb = bdb_idl_next( b, &cursorb );
1042 } else if ( ida < idb ) {
1043 ida = bdb_idl_next( a, &cursora );
1045 idb = bdb_idl_next( b, &cursorb );
1051 BDB_IDL_CPY( b, a );
1058 * idl_union - return a = a union b
1066 ID cursora = 0, cursorb = 0, cursorc;
1068 if ( BDB_IDL_IS_ZERO( b ) ) {
1072 if ( BDB_IDL_IS_ZERO( a ) ) {
1073 BDB_IDL_CPY( a, b );
1077 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1078 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1079 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1086 ida = bdb_idl_first( a, &cursora );
1087 idb = bdb_idl_first( b, &cursorb );
1091 /* The distinct elements of a are cat'd to b */
1092 while( ida != NOID || idb != NOID ) {
1094 if( ++cursorc > BDB_IDL_UM_MAX ) {
1098 ida = bdb_idl_next( a, &cursora );
1102 ida = bdb_idl_next( a, &cursora );
1103 idb = bdb_idl_next( b, &cursorb );
1107 /* b is copied back to a in sorted order */
1112 while (cursorb <= b[0] || cursorc <= a[0]) {
1117 if (cursorb <= b[0] && b[cursorb] < idb)
1118 a[cursora++] = b[cursorb++];
1131 * bdb_idl_notin - return a intersection ~b (or a minus b)
1140 ID cursora = 0, cursorb = 0;
1142 if( BDB_IDL_IS_ZERO( a ) ||
1143 BDB_IDL_IS_ZERO( b ) ||
1144 BDB_IDL_IS_RANGE( b ) )
1146 BDB_IDL_CPY( ids, a );
1150 if( BDB_IDL_IS_RANGE( a ) ) {
1151 BDB_IDL_CPY( ids, a );
1155 ida = bdb_idl_first( a, &cursora ),
1156 idb = bdb_idl_first( b, &cursorb );
1160 while( ida != NOID ) {
1161 if ( idb == NOID ) {
1162 /* we could shortcut this */
1163 ids[++ids[0]] = ida;
1164 ida = bdb_idl_next( a, &cursora );
1166 } else if ( ida < idb ) {
1167 ids[++ids[0]] = ida;
1168 ida = bdb_idl_next( a, &cursora );
1170 } else if ( ida > idb ) {
1171 idb = bdb_idl_next( b, &cursorb );
1174 ida = bdb_idl_next( a, &cursora );
1175 idb = bdb_idl_next( b, &cursorb );
1183 ID bdb_idl_first( ID *ids, ID *cursor )
1187 if ( ids[0] == 0 ) {
1192 if ( BDB_IDL_IS_RANGE( ids ) ) {
1193 if( *cursor < ids[1] ) {
1202 pos = bdb_idl_search( ids, *cursor );
1204 if( pos > ids[0] ) {
1212 ID bdb_idl_next( ID *ids, ID *cursor )
1214 if ( BDB_IDL_IS_RANGE( ids ) ) {
1215 if( ids[2] < ++(*cursor) ) {
1221 if ( ++(*cursor) <= ids[0] ) {
1222 return ids[*cursor];