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) );
246 static int idl_delete( ID *ids, ID id )
248 unsigned x = bdb_idl_search( ids, id );
252 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
254 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
268 if( x > ids[0] || ids[x] != id ) {
272 } else if ( --ids[0] == 0 ) {
278 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
296 if ( key->size == sizeof( ID ) ) {
297 unsigned char *c = key->data;
298 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
305 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
306 * copy the cached IDL into it, otherwise just return the status.
310 struct bdb_info *bdb,
315 bdb_idl_cache_entry_t idl_tmp;
316 bdb_idl_cache_entry_t *matched_idl_entry;
318 DBT2bv( key, &idl_tmp.kstr );
320 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
321 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
323 if ( matched_idl_entry != NULL ) {
324 if ( matched_idl_entry->idl && ids )
325 BDB_IDL_CPY( ids, matched_idl_entry->idl );
326 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
327 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
328 IDL_LRU_DELETE( bdb, matched_idl_entry );
329 IDL_LRU_ADD( bdb, matched_idl_entry );
330 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
331 if ( matched_idl_entry->idl )
336 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
338 return LDAP_NO_SUCH_OBJECT;
343 struct bdb_info *bdb,
349 bdb_idl_cache_entry_t idl_tmp;
350 bdb_idl_cache_entry_t *ee;
352 DBT2bv( key, &idl_tmp.kstr );
354 ee = (bdb_idl_cache_entry_t *) ch_malloc(
355 sizeof( bdb_idl_cache_entry_t ) );
357 if ( rc == DB_NOTFOUND) {
360 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
361 BDB_IDL_CPY( ee->idl, ids );
363 ee->idl_lru_prev = NULL;
364 ee->idl_lru_next = NULL;
365 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
366 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
367 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
368 bdb_idl_entry_cmp, avl_dup_error ))
370 ch_free( ee->kstr.bv_val );
373 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
376 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
377 IDL_LRU_ADD( bdb, ee );
378 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
380 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
381 ee = bdb->bi_idl_lru_tail;
382 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
383 bdb_idl_entry_cmp ) == NULL ) {
385 LDAP_LOG( INDEX, ERR,
386 "bdb_idl_cache_put: AVL delete failed\n",
389 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
390 "AVL delete failed\n",
394 IDL_LRU_DELETE( bdb, ee );
396 --bdb->bi_idl_cache_size;
397 ch_free( ee->kstr.bv_val );
403 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
404 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
409 struct bdb_info *bdb,
413 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
414 DBT2bv( key, &idl_tmp.kstr );
416 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
417 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
419 if ( matched_idl_entry != NULL ) {
420 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
421 bdb_idl_entry_cmp ) == NULL ) {
423 LDAP_LOG( INDEX, ERR,
424 "bdb_idl_cache_del: AVL delete failed\n",
427 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
428 "AVL delete failed\n",
432 --bdb->bi_idl_cache_size;
433 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
434 IDL_LRU_DELETE( bdb, matched_idl_entry );
435 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
436 free( matched_idl_entry->kstr.bv_val );
437 if ( matched_idl_entry->idl )
438 free( matched_idl_entry->idl );
439 free( matched_idl_entry );
441 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
452 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
460 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
462 /* If using BerkeleyDB 4.0, the buf must be large enough to
463 * grab the entire IDL in one get(), otherwise BDB will leak
464 * resources on subsequent get's. We can safely call get()
465 * twice - once for the data, and once to get the DB_NOTFOUND
466 * result meaning there's no more data. See ITS#2040 for details.
467 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
468 * stack space is too limited.
470 * configure now requires Berkeley DB 4.1.
472 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
473 # define BDB_ENOUGH 5
475 # define BDB_ENOUGH 1
477 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
482 LDAP_LOG( INDEX, ARGS,
483 "bdb_idl_fetch_key: %s\n",
484 bdb_show_key( key, keybuf ), 0, 0 );
486 Debug( LDAP_DEBUG_ARGS,
487 "bdb_idl_fetch_key: %s\n",
488 bdb_show_key( key, keybuf ), 0, 0 );
491 assert( ids != NULL );
493 if ( bdb->bi_idl_cache_size ) {
494 rc = bdb_idl_cache_get( bdb, db, key, ids );
495 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
501 data.ulen = sizeof(buf);
502 data.flags = DB_DBT_USERMEM;
504 if ( tid ) flags |= DB_RMW;
506 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
509 LDAP_LOG( INDEX, ERR,
510 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
511 db_strerror(rc), rc, 0 );
513 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
514 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
519 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
525 DB_MULTIPLE_INIT( ptr, &data );
527 DB_MULTIPLE_NEXT(ptr, &data, j, len);
530 AC_MEMCPY( i, j, sizeof(ID) );
533 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
535 if ( rc == DB_NOTFOUND ) rc = 0;
537 /* On disk, a range is denoted by 0 in the first element */
539 if (ids[0] != BDB_IDL_RANGE_SIZE) {
541 LDAP_LOG( INDEX, ERR,
542 "=> bdb_idl_fetch_key: range size mismatch: "
543 "expected %ld, got %ld\n",
544 BDB_IDL_RANGE_SIZE, ids[0], 0 );
546 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
547 "range size mismatch: expected %d, got %ld\n",
548 BDB_IDL_RANGE_SIZE, ids[0], 0 );
550 cursor->c_close( cursor );
553 BDB_IDL_RANGE( ids, ids[2], ids[3] );
555 data.size = BDB_IDL_SIZEOF(ids);
558 rc2 = cursor->c_close( cursor );
561 LDAP_LOG( INDEX, ERR,
562 "bdb_idl_fetch_key: close failed: %s (%d)\n",
563 db_strerror(rc2), rc2, 0 );
565 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
566 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
571 if( rc == DB_NOTFOUND ) {
574 } else if( rc != 0 ) {
576 LDAP_LOG( INDEX, ERR,
577 "bdb_idl_fetch_key: get failed: %s (%d)\n",
578 db_strerror(rc), rc, 0 );
580 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
581 "get failed: %s (%d)\n",
582 db_strerror(rc), rc, 0 );
586 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
587 /* size not multiple of ID size */
589 LDAP_LOG( INDEX, ERR,
590 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
591 (long) sizeof( ID ), (long) data.size, 0 );
593 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
594 "odd size: expected %ld multiple, got %ld\n",
595 (long) sizeof( ID ), (long) data.size, 0 );
599 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
602 LDAP_LOG( INDEX, ERR,
603 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
604 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
606 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
607 "get size mismatch: expected %ld, got %ld\n",
608 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
613 if ( bdb->bi_idl_cache_max_size ) {
614 bdb_idl_cache_put( bdb, db, key, ids, rc );
629 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
639 LDAP_LOG( INDEX, ARGS,
640 "bdb_idl_insert_key: %lx %s\n",
641 (long) id, bdb_show_key( key, buf ), 0 );
643 Debug( LDAP_DEBUG_ARGS,
644 "bdb_idl_insert_key: %lx %s\n",
645 (long) id, bdb_show_key( key, buf ), 0 );
649 assert( id != NOID );
651 if ( bdb->bi_idl_cache_size ) {
652 bdb_idl_cache_del( bdb, db, key );
656 data.size = sizeof( ID );
657 data.ulen = data.size;
658 data.flags = DB_DBT_USERMEM;
660 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
663 LDAP_LOG( INDEX, ERR,
664 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
665 db_strerror(rc), rc, 0 );
667 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
668 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
673 /* Fetch the first data item for this key, to see if it
674 * exists and if it's a range.
676 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
680 /* not a range, count the number of items */
682 rc = cursor->c_count( cursor, &count, 0 );
687 if ( count >= BDB_IDL_DB_MAX ) {
688 /* No room, convert to a range */
691 key2.dlen = key2.ulen;
692 key2.flags |= DB_DBT_PARTIAL;
696 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
697 if ( rc != 0 && rc != DB_NOTFOUND ) {
698 err = "c_get next_nodup";
701 if ( rc == DB_NOTFOUND ) {
702 rc = cursor->c_get( cursor, key, &data, DB_LAST );
708 rc = cursor->c_get( cursor, key, &data, DB_PREV );
718 rc = db->del( db, tid, key, 0 );
725 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
731 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
737 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
743 /* There's room, just store it */
747 /* It's a range, see if we need to rewrite
752 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
759 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
765 if ( id < lo || id > hi ) {
766 /* Delete the current lo/hi */
767 rc = cursor->c_del( cursor, 0 );
773 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
780 } else if ( rc == DB_NOTFOUND ) {
781 put1: data.data = &id;
782 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
783 /* Don't worry if it's already there */
784 if ( rc != 0 && rc != DB_KEYEXIST ) {
789 /* initial c_get failed, nothing was done */
792 LDAP_LOG( INDEX, ERR,
793 "bdb_idl_insert_key: %s failed: %s (%d)\n",
794 err, db_strerror(rc), rc );
796 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
797 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
799 cursor->c_close( cursor );
802 rc = cursor->c_close( cursor );
805 LDAP_LOG( INDEX, ERR,
806 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
807 db_strerror(rc), rc, 0 );
809 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
810 "c_close failed: %s (%d)\n",
811 db_strerror(rc), rc, 0 );
825 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
835 LDAP_LOG( INDEX, ARGS,
836 "bdb_idl_delete_key: %lx %s\n",
837 (long) id, bdb_show_key( key, buf ), 0 );
839 Debug( LDAP_DEBUG_ARGS,
840 "bdb_idl_delete_key: %lx %s\n",
841 (long) id, bdb_show_key( key, buf ), 0 );
844 assert( id != NOID );
846 if ( bdb->bi_idl_cache_max_size ) {
847 bdb_idl_cache_del( bdb, db, key );
852 data.size = sizeof( id );
853 data.ulen = data.size;
854 data.flags = DB_DBT_USERMEM;
856 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
859 LDAP_LOG( INDEX, ERR,
860 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
861 db_strerror(rc), rc, 0 );
863 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
864 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
868 /* Fetch the first data item for this key, to see if it
869 * exists and if it's a range.
871 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
875 /* Not a range, just delete it */
877 /* position to correct item */
879 rc = cursor->c_get( cursor, key, &data,
880 DB_GET_BOTH | DB_RMW );
886 rc = cursor->c_del( cursor, 0 );
892 /* It's a range, see if we need to rewrite
896 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
902 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
907 if ( id == lo || id == hi ) {
911 } else if ( id == hi ) {
916 /* The range has collapsed... */
917 rc = db->del( db, tid, key, 0 );
924 /* reposition on lo slot */
926 cursor->c_get( cursor, key, &data, DB_PREV );
929 rc = cursor->c_del( cursor, 0 );
937 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
946 /* initial c_get failed, nothing was done */
948 if ( rc != DB_NOTFOUND ) {
950 LDAP_LOG( INDEX, ERR,
951 "bdb_idl_delete_key: %s failed: %s (%d)\n",
952 err, db_strerror(rc), rc );
954 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
955 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
958 cursor->c_close( cursor );
961 rc = cursor->c_close( cursor );
964 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
965 db_strerror(rc), rc, 0 );
967 Debug( LDAP_DEBUG_ANY,
968 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
969 db_strerror(rc), rc, 0 );
978 * idl_intersection - return a = a intersection b
981 bdb_idl_intersection(
987 ID cursora = 0, cursorb = 0, cursorc;
990 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
995 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
996 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
997 if ( idmin > idmax ) {
1000 } else if ( idmin == idmax ) {
1006 if ( BDB_IDL_IS_RANGE( a ) ) {
1007 if ( BDB_IDL_IS_RANGE(b) ) {
1008 /* If both are ranges, just shrink the boundaries */
1013 /* Else swap so that b is the range, a is a list */
1021 /* If a range completely covers the list, the result is
1022 * just the list. If idmin to idmax is contiguous, just
1023 * turn it into a range.
1025 if ( BDB_IDL_IS_RANGE( b )
1026 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1027 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1028 if (idmax - idmin + 1 == a[0])
1037 /* Fine, do the intersection one element at a time.
1038 * First advance to idmin in both IDLs.
1040 cursora = cursorb = idmin;
1041 ida = bdb_idl_first( a, &cursora );
1042 idb = bdb_idl_first( b, &cursorb );
1045 while( ida <= idmax || idb <= idmax ) {
1048 ida = bdb_idl_next( a, &cursora );
1049 idb = bdb_idl_next( b, &cursorb );
1050 } else if ( ida < idb ) {
1051 ida = bdb_idl_next( a, &cursora );
1053 idb = bdb_idl_next( b, &cursorb );
1059 BDB_IDL_CPY( b, a );
1066 * idl_union - return a = a union b
1074 ID cursora = 0, cursorb = 0, cursorc;
1076 if ( BDB_IDL_IS_ZERO( b ) ) {
1080 if ( BDB_IDL_IS_ZERO( a ) ) {
1081 BDB_IDL_CPY( a, b );
1085 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1086 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1087 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1094 ida = bdb_idl_first( a, &cursora );
1095 idb = bdb_idl_first( b, &cursorb );
1099 /* The distinct elements of a are cat'd to b */
1100 while( ida != NOID || idb != NOID ) {
1102 if( ++cursorc > BDB_IDL_UM_MAX ) {
1106 ida = bdb_idl_next( a, &cursora );
1110 ida = bdb_idl_next( a, &cursora );
1111 idb = bdb_idl_next( b, &cursorb );
1115 /* b is copied back to a in sorted order */
1120 while (cursorb <= b[0] || cursorc <= a[0]) {
1125 if (cursorb <= b[0] && b[cursorb] < idb)
1126 a[cursora++] = b[cursorb++];
1139 * bdb_idl_notin - return a intersection ~b (or a minus b)
1148 ID cursora = 0, cursorb = 0;
1150 if( BDB_IDL_IS_ZERO( a ) ||
1151 BDB_IDL_IS_ZERO( b ) ||
1152 BDB_IDL_IS_RANGE( b ) )
1154 BDB_IDL_CPY( ids, a );
1158 if( BDB_IDL_IS_RANGE( a ) ) {
1159 BDB_IDL_CPY( ids, a );
1163 ida = bdb_idl_first( a, &cursora ),
1164 idb = bdb_idl_first( b, &cursorb );
1168 while( ida != NOID ) {
1169 if ( idb == NOID ) {
1170 /* we could shortcut this */
1171 ids[++ids[0]] = ida;
1172 ida = bdb_idl_next( a, &cursora );
1174 } else if ( ida < idb ) {
1175 ids[++ids[0]] = ida;
1176 ida = bdb_idl_next( a, &cursora );
1178 } else if ( ida > idb ) {
1179 idb = bdb_idl_next( b, &cursorb );
1182 ida = bdb_idl_next( a, &cursora );
1183 idb = bdb_idl_next( b, &cursorb );
1191 ID bdb_idl_first( ID *ids, ID *cursor )
1195 if ( ids[0] == 0 ) {
1200 if ( BDB_IDL_IS_RANGE( ids ) ) {
1201 if( *cursor < ids[1] ) {
1210 pos = bdb_idl_search( ids, *cursor );
1212 if( pos > ids[0] ) {
1220 ID bdb_idl_next( ID *ids, ID *cursor )
1222 if ( BDB_IDL_IS_RANGE( ids ) ) {
1223 if( ids[2] < ++(*cursor) ) {
1229 if ( ++(*cursor) <= ids[0] ) {
1230 return ids[*cursor];