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) );
245 static int bdb_idl_delete( ID *ids, ID id )
251 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
253 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
260 if (BDB_IDL_IS_RANGE( ids )) {
261 /* if in range, treat as a noop */
262 if (id > BDB_IDL_FIRST(ids) && id < BDB_IDL_LAST(ids))
264 if (id == BDB_IDL_FIRST(ids))
266 else if (id == BDB_IDL_LAST(ids))
268 /* range collapsed to a single item */
269 if ( ids[1] == ids[2] )
274 x = bdb_idl_search( ids, id );
282 if( x > ids[0] || ids[x] != id ) {
286 } else if ( --ids[0] == 0 ) {
292 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
309 if ( key->size == sizeof( ID ) ) {
310 unsigned char *c = key->data;
311 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
318 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
319 * copy the cached IDL into it, otherwise just return the status.
323 struct bdb_info *bdb,
328 bdb_idl_cache_entry_t idl_tmp;
329 bdb_idl_cache_entry_t *matched_idl_entry;
331 DBT2bv( key, &idl_tmp.kstr );
333 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
334 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
336 if ( matched_idl_entry != NULL ) {
337 if ( matched_idl_entry->idl && ids )
338 BDB_IDL_CPY( ids, matched_idl_entry->idl );
339 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
340 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
341 IDL_LRU_DELETE( bdb, matched_idl_entry );
342 IDL_LRU_ADD( bdb, matched_idl_entry );
343 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
344 if ( matched_idl_entry->idl )
349 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
351 return LDAP_NO_SUCH_OBJECT;
354 /* Replace existing cached IDL with new one, tell caller it was a dup */
356 idl_cache_dup( void *left, void *right )
358 bdb_idl_cache_entry_t *le = left, *re = right;
366 /* add or replace cache entries */
369 struct bdb_info *bdb,
375 bdb_idl_cache_entry_t idl_tmp;
376 bdb_idl_cache_entry_t *ee;
378 DBT2bv( key, &idl_tmp.kstr );
380 ee = (bdb_idl_cache_entry_t *) ch_malloc(
381 sizeof( bdb_idl_cache_entry_t ) );
383 if ( rc == DB_NOTFOUND) {
386 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
387 BDB_IDL_CPY( ee->idl, ids );
389 ee->idl_lru_prev = NULL;
390 ee->idl_lru_next = NULL;
391 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
392 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
393 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
394 bdb_idl_entry_cmp, idl_cache_dup ))
396 /* ee->idl has been stored in the tree */
397 ch_free( ee->kstr.bv_val );
399 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
402 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
403 IDL_LRU_ADD( bdb, ee );
404 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
406 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
407 ee = bdb->bi_idl_lru_tail;
408 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
409 bdb_idl_entry_cmp ) == NULL ) {
411 LDAP_LOG( INDEX, ERR,
412 "bdb_idl_cache_put: AVL delete failed\n",
415 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
416 "AVL delete failed\n",
420 IDL_LRU_DELETE( bdb, ee );
422 --bdb->bi_idl_cache_size;
423 ch_free( ee->kstr.bv_val );
429 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
430 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
435 struct bdb_info *bdb,
439 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
440 DBT2bv( key, &idl_tmp.kstr );
442 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
443 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
445 if ( matched_idl_entry != NULL ) {
446 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
447 bdb_idl_entry_cmp ) == NULL ) {
449 LDAP_LOG( INDEX, ERR,
450 "bdb_idl_cache_del: AVL delete failed\n",
453 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
454 "AVL delete failed\n",
458 --bdb->bi_idl_cache_size;
459 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
460 IDL_LRU_DELETE( bdb, matched_idl_entry );
461 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
462 free( matched_idl_entry->kstr.bv_val );
463 if ( matched_idl_entry->idl )
464 free( matched_idl_entry->idl );
465 free( matched_idl_entry );
467 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
478 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
486 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
488 /* If using BerkeleyDB 4.0, the buf must be large enough to
489 * grab the entire IDL in one get(), otherwise BDB will leak
490 * resources on subsequent get's. We can safely call get()
491 * twice - once for the data, and once to get the DB_NOTFOUND
492 * result meaning there's no more data. See ITS#2040 for details.
493 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
494 * stack space is too limited.
496 * configure now requires Berkeley DB 4.1.
498 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
499 # define BDB_ENOUGH 5
501 # define BDB_ENOUGH 1
503 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
508 LDAP_LOG( INDEX, ARGS,
509 "bdb_idl_fetch_key: %s\n",
510 bdb_show_key( key, keybuf ), 0, 0 );
512 Debug( LDAP_DEBUG_ARGS,
513 "bdb_idl_fetch_key: %s\n",
514 bdb_show_key( key, keybuf ), 0, 0 );
517 assert( ids != NULL );
519 if ( bdb->bi_idl_cache_size ) {
520 rc = bdb_idl_cache_get( bdb, db, key, ids );
521 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
527 data.ulen = sizeof(buf);
528 data.flags = DB_DBT_USERMEM;
530 if ( tid ) flags |= DB_RMW;
532 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
535 LDAP_LOG( INDEX, ERR,
536 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
537 db_strerror(rc), rc, 0 );
539 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
540 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
545 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
551 DB_MULTIPLE_INIT( ptr, &data );
553 DB_MULTIPLE_NEXT(ptr, &data, j, len);
557 for (k=0; k<sizeof(ID); k++) {
564 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
566 if ( rc == DB_NOTFOUND ) rc = 0;
568 /* On disk, a range is denoted by 0 in the first element */
570 if (ids[0] != BDB_IDL_RANGE_SIZE) {
572 LDAP_LOG( INDEX, ERR,
573 "=> bdb_idl_fetch_key: range size mismatch: "
574 "expected %ld, got %ld\n",
575 BDB_IDL_RANGE_SIZE, ids[0], 0 );
577 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
578 "range size mismatch: expected %d, got %ld\n",
579 BDB_IDL_RANGE_SIZE, ids[0], 0 );
581 cursor->c_close( cursor );
584 BDB_IDL_RANGE( ids, ids[2], ids[3] );
586 data.size = BDB_IDL_SIZEOF(ids);
589 rc2 = cursor->c_close( cursor );
592 LDAP_LOG( INDEX, ERR,
593 "bdb_idl_fetch_key: close failed: %s (%d)\n",
594 db_strerror(rc2), rc2, 0 );
596 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
597 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
602 if ( rc == DB_NOTFOUND ) {
604 } else if( rc != 0 ) {
606 LDAP_LOG( INDEX, ERR,
607 "bdb_idl_fetch_key: get failed: %s (%d)\n",
608 db_strerror(rc), rc, 0 );
610 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
611 "get failed: %s (%d)\n",
612 db_strerror(rc), rc, 0 );
616 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
617 /* size not multiple of ID size */
619 LDAP_LOG( INDEX, ERR,
620 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
621 (long) sizeof( ID ), (long) data.size, 0 );
623 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
624 "odd size: expected %ld multiple, got %ld\n",
625 (long) sizeof( ID ), (long) data.size, 0 );
629 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
632 LDAP_LOG( INDEX, ERR,
633 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
634 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
636 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
637 "get size mismatch: expected %ld, got %ld\n",
638 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
643 if ( bdb->bi_idl_cache_max_size ) {
644 bdb_idl_cache_put( bdb, db, key, ids, rc );
659 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
662 ID lo, hi, tmp, idl[BDB_IDL_DB_SIZE];
663 char *err, buf[sizeof(ID)];
664 int wasrange = 0, isrange = 0;
669 LDAP_LOG( INDEX, ARGS,
670 "bdb_idl_insert_key: %lx %s\n",
671 (long) id, bdb_show_key( key, buf ), 0 );
673 Debug( LDAP_DEBUG_ARGS,
674 "bdb_idl_insert_key: %lx %s\n",
675 (long) id, bdb_show_key( key, buf ), 0 );
679 assert( id != NOID );
682 rc = bdb_idl_fetch_key( be, db, tid, key, idl );
683 if ( rc != DB_NOTFOUND ) {
684 wasrange = BDB_IDL_IS_RANGE( idl );
689 rc = bdb_idl_insert( idl, id );
691 /* Don't need to do anything */
692 if ( rc == -1 ) return 0;
694 isrange = BDB_IDL_IS_RANGE( idl );
698 data.size = sizeof( ID );
699 data.ulen = data.size;
700 data.flags = DB_DBT_USERMEM;
704 while ( !wasrange ) {
705 /* Delete vector, rewrite as range */
706 rc = db->del( db, tid, key, 0 );
712 for (i=sizeof(ID)-1; i>=0; i--) {
715 rc = db->put( db, tid, key, &data, 0 );
721 for (i=sizeof(ID)-1; i>=0; i--) {
725 rc = db->put( db, tid, key, &data, 0 );
731 for (i=sizeof(ID)-1; i>=0; i--) {
735 rc = db->put( db, tid, key, &data, 0 );
744 /* Update range boundaries */
745 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
750 tmp = (id == idl[1]) ? lo : hi;
751 for (i=sizeof(ID)-1; i>=0; i--) {
755 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
757 cursor->c_close( cursor );
761 rc = cursor->c_del( cursor, 0 );
763 cursor->c_close( cursor );
768 for (i=sizeof(ID)-1; i>=0; i--) {
772 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
774 cursor->c_close( cursor );
778 rc = cursor->c_close( cursor );
787 for (i=sizeof(ID)-1; i>=0; i--) {
791 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
796 if ( rc && rc != DB_KEYEXIST ) {
798 LDAP_LOG( INDEX, ERR,
799 "bdb_idl_insert_key: %s failed: %s (%d)\n",
800 err, db_strerror(rc), rc );
802 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
803 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
808 if ( bdb->bi_idl_cache_max_size ) {
809 bdb_idl_cache_put( bdb, db, key, idl, 0 );
822 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
826 ID lo, hi, tmp, idl[BDB_IDL_DB_SIZE];
827 int wasrange, isrange;
828 char *err, buf[sizeof(ID)];
833 LDAP_LOG( INDEX, ARGS,
834 "bdb_idl_delete_key: %lx %s\n",
835 (long) id, bdb_show_key( key, buf ), 0 );
837 Debug( LDAP_DEBUG_ARGS,
838 "bdb_idl_delete_key: %lx %s\n",
839 (long) id, bdb_show_key( key, buf ), 0 );
842 assert( id != NOID );
845 rc = bdb_idl_fetch_key( be, db, tid, key, idl );
846 if ( rc != DB_NOTFOUND ) {
847 wasrange = BDB_IDL_IS_RANGE( idl );
852 rc = bdb_idl_delete( idl, id );
854 /* Don't need to do anything */
855 if ( rc == -1 ) return 0;
857 isrange = BDB_IDL_IS_RANGE( idl );
862 data.size = sizeof( id );
863 data.ulen = data.size;
864 data.flags = DB_DBT_USERMEM;
866 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
869 LDAP_LOG( INDEX, ERR,
870 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
871 db_strerror(rc), rc, 0 );
873 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
874 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
879 if ( wasrange && !isrange ) {
880 rc = db->del( db, tid, key, 0 );
885 for (i=sizeof(ID)-1; i>=0; i--) {
889 rc = db->put( db, tid, key, &data, 0 );
896 for (i=sizeof(ID)-1; i>=0; i--) {
900 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
904 rc = cursor->c_del( cursor, 0 );
910 if ( isrange && rc == 0 ) {
911 tmp = ( id == lo ) ? idl[1] : idl[2];
912 for (i=sizeof(ID)-1; i>=0; i--) {
916 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
921 if ( rc && rc != DB_NOTFOUND ) {
923 LDAP_LOG( INDEX, ERR,
924 "bdb_idl_delete_key: %s failed: %s (%d)\n",
925 err, db_strerror(rc), rc );
927 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
928 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
930 cursor->c_close( cursor );
933 rc = cursor->c_close( cursor );
936 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
937 db_strerror(rc), rc, 0 );
939 Debug( LDAP_DEBUG_ANY,
940 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
941 db_strerror(rc), rc, 0 );
945 if ( bdb->bi_idl_cache_max_size ) {
946 bdb_idl_cache_put( bdb, db, key, idl, 0 );
954 * idl_intersection - return a = a intersection b
957 bdb_idl_intersection(
963 ID cursora = 0, cursorb = 0, cursorc;
966 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
971 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
972 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
973 if ( idmin > idmax ) {
976 } else if ( idmin == idmax ) {
982 if ( BDB_IDL_IS_RANGE( a ) ) {
983 if ( BDB_IDL_IS_RANGE(b) ) {
984 /* If both are ranges, just shrink the boundaries */
989 /* Else swap so that b is the range, a is a list */
997 /* If a range completely covers the list, the result is
998 * just the list. If idmin to idmax is contiguous, just
999 * turn it into a range.
1001 if ( BDB_IDL_IS_RANGE( b )
1002 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1003 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1004 if (idmax - idmin + 1 == a[0])
1013 /* Fine, do the intersection one element at a time.
1014 * First advance to idmin in both IDLs.
1016 cursora = cursorb = idmin;
1017 ida = bdb_idl_first( a, &cursora );
1018 idb = bdb_idl_first( b, &cursorb );
1021 while( ida <= idmax || idb <= idmax ) {
1024 ida = bdb_idl_next( a, &cursora );
1025 idb = bdb_idl_next( b, &cursorb );
1026 } else if ( ida < idb ) {
1027 ida = bdb_idl_next( a, &cursora );
1029 idb = bdb_idl_next( b, &cursorb );
1035 BDB_IDL_CPY( b, a );
1042 * idl_union - return a = a union b
1050 ID cursora = 0, cursorb = 0, cursorc;
1052 if ( BDB_IDL_IS_ZERO( b ) ) {
1056 if ( BDB_IDL_IS_ZERO( a ) ) {
1057 BDB_IDL_CPY( a, b );
1061 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1062 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1063 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1070 ida = bdb_idl_first( a, &cursora );
1071 idb = bdb_idl_first( b, &cursorb );
1075 /* The distinct elements of a are cat'd to b */
1076 while( ida != NOID || idb != NOID ) {
1078 if( ++cursorc > BDB_IDL_UM_MAX ) {
1082 ida = bdb_idl_next( a, &cursora );
1086 ida = bdb_idl_next( a, &cursora );
1087 idb = bdb_idl_next( b, &cursorb );
1091 /* b is copied back to a in sorted order */
1096 while (cursorb <= b[0] || cursorc <= a[0]) {
1101 if (cursorb <= b[0] && b[cursorb] < idb)
1102 a[cursora++] = b[cursorb++];
1115 * bdb_idl_notin - return a intersection ~b (or a minus b)
1124 ID cursora = 0, cursorb = 0;
1126 if( BDB_IDL_IS_ZERO( a ) ||
1127 BDB_IDL_IS_ZERO( b ) ||
1128 BDB_IDL_IS_RANGE( b ) )
1130 BDB_IDL_CPY( ids, a );
1134 if( BDB_IDL_IS_RANGE( a ) ) {
1135 BDB_IDL_CPY( ids, a );
1139 ida = bdb_idl_first( a, &cursora ),
1140 idb = bdb_idl_first( b, &cursorb );
1144 while( ida != NOID ) {
1145 if ( idb == NOID ) {
1146 /* we could shortcut this */
1147 ids[++ids[0]] = ida;
1148 ida = bdb_idl_next( a, &cursora );
1150 } else if ( ida < idb ) {
1151 ids[++ids[0]] = ida;
1152 ida = bdb_idl_next( a, &cursora );
1154 } else if ( ida > idb ) {
1155 idb = bdb_idl_next( b, &cursorb );
1158 ida = bdb_idl_next( a, &cursora );
1159 idb = bdb_idl_next( b, &cursorb );
1167 ID bdb_idl_first( ID *ids, ID *cursor )
1171 if ( ids[0] == 0 ) {
1176 if ( BDB_IDL_IS_RANGE( ids ) ) {
1177 if( *cursor < ids[1] ) {
1186 pos = bdb_idl_search( ids, *cursor );
1188 if( pos > ids[0] ) {
1196 ID bdb_idl_next( ID *ids, ID *cursor )
1198 if ( BDB_IDL_IS_RANGE( ids ) ) {
1199 if( ids[2] < ++(*cursor) ) {
1205 if ( ++(*cursor) <= ids[0] ) {
1206 return ids[*cursor];