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] );
298 #ifdef SLAP_IDL_CACHE
300 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
301 * copy the cached IDL into it, otherwise just return the status.
305 struct bdb_info *bdb,
310 bdb_idl_cache_entry_t idl_tmp;
311 bdb_idl_cache_entry_t *matched_idl_entry;
313 DBT2bv( key, &idl_tmp.kstr );
315 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
316 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
318 if ( matched_idl_entry != NULL ) {
319 if ( matched_idl_entry->idl && ids )
320 BDB_IDL_CPY( ids, matched_idl_entry->idl );
321 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
322 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
323 IDL_LRU_DELETE( bdb, matched_idl_entry );
324 IDL_LRU_ADD( bdb, matched_idl_entry );
325 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
326 if ( matched_idl_entry->idl )
331 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
333 return LDAP_NO_SUCH_OBJECT;
338 struct bdb_info *bdb,
344 bdb_idl_cache_entry_t idl_tmp;
345 bdb_idl_cache_entry_t *ee;
347 DBT2bv( key, &idl_tmp.kstr );
349 ee = (bdb_idl_cache_entry_t *) ch_malloc(
350 sizeof( bdb_idl_cache_entry_t ) );
352 if ( rc == DB_NOTFOUND) {
355 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
356 BDB_IDL_CPY( ee->idl, ids );
358 ee->idl_lru_prev = NULL;
359 ee->idl_lru_next = NULL;
360 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
361 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
362 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
363 bdb_idl_entry_cmp, avl_dup_error ))
365 ch_free( ee->kstr.bv_val );
368 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
371 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
372 IDL_LRU_ADD( bdb, ee );
373 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
375 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
376 ee = bdb->bi_idl_lru_tail;
377 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
378 bdb_idl_entry_cmp ) == NULL ) {
380 LDAP_LOG( INDEX, ERR,
381 "bdb_idl_cache_put: AVL delete failed\n",
384 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
385 "AVL delete failed\n",
389 IDL_LRU_DELETE( bdb, ee );
391 --bdb->bi_idl_cache_size;
392 ch_free( ee->kstr.bv_val );
398 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
399 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
404 struct bdb_info *bdb,
408 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
409 DBT2bv( key, &idl_tmp.kstr );
411 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
412 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
414 if ( matched_idl_entry != NULL ) {
415 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
416 bdb_idl_entry_cmp ) == NULL ) {
418 LDAP_LOG( INDEX, ERR,
419 "bdb_idl_cache_del: AVL delete failed\n",
422 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
423 "AVL delete failed\n",
427 --bdb->bi_idl_cache_size;
428 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
429 IDL_LRU_DELETE( bdb, matched_idl_entry );
430 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
431 free( matched_idl_entry->kstr.bv_val );
432 if ( matched_idl_entry->idl )
433 free( matched_idl_entry->idl );
434 free( matched_idl_entry );
436 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
448 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
456 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
458 /* If using BerkeleyDB 4.0, the buf must be large enough to
459 * grab the entire IDL in one get(), otherwise BDB will leak
460 * resources on subsequent get's. We can safely call get()
461 * twice - once for the data, and once to get the DB_NOTFOUND
462 * result meaning there's no more data. See ITS#2040 for details.
463 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
464 * stack space is too limited.
466 * configure now requires Berkeley DB 4.1.
468 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
469 # define BDB_ENOUGH 5
471 # define BDB_ENOUGH 1
473 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
478 LDAP_LOG( INDEX, ARGS,
479 "bdb_idl_fetch_key: %s\n",
480 bdb_show_key( key, keybuf ), 0, 0 );
482 Debug( LDAP_DEBUG_ARGS,
483 "bdb_idl_fetch_key: %s\n",
484 bdb_show_key( key, keybuf ), 0, 0 );
487 assert( ids != NULL );
489 #ifdef SLAP_IDL_CACHE
490 if ( bdb->bi_idl_cache_size ) {
491 rc = bdb_idl_cache_get( bdb, db, key, ids );
492 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
499 data.ulen = sizeof(buf);
500 data.flags = DB_DBT_USERMEM;
502 if ( tid ) flags |= DB_RMW;
504 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
507 LDAP_LOG( INDEX, ERR,
508 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
509 db_strerror(rc), rc, 0 );
511 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
512 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
517 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
523 DB_MULTIPLE_INIT( ptr, &data );
525 DB_MULTIPLE_NEXT(ptr, &data, j, len);
528 AC_MEMCPY( i, j, sizeof(ID) );
531 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
533 if ( rc == DB_NOTFOUND ) rc = 0;
535 /* On disk, a range is denoted by 0 in the first element */
537 if (ids[0] != BDB_IDL_RANGE_SIZE) {
539 LDAP_LOG( INDEX, ERR,
540 "=> bdb_idl_fetch_key: range size mismatch: "
541 "expected %ld, got %ld\n",
542 BDB_IDL_RANGE_SIZE, ids[0], 0 );
544 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
545 "range size mismatch: expected %d, got %ld\n",
546 BDB_IDL_RANGE_SIZE, ids[0], 0 );
548 cursor->c_close( cursor );
551 BDB_IDL_RANGE( ids, ids[2], ids[3] );
553 data.size = BDB_IDL_SIZEOF(ids);
556 rc2 = cursor->c_close( cursor );
559 LDAP_LOG( INDEX, ERR,
560 "bdb_idl_fetch_key: close failed: %s (%d)\n",
561 db_strerror(rc2), rc2, 0 );
563 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
564 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
569 if( rc == DB_NOTFOUND ) {
570 #ifndef SLAP_IDL_CACHE
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 #ifdef SLAP_IDL_CACHE
614 if ( bdb->bi_idl_cache_max_size ) {
615 bdb_idl_cache_put( bdb, db, key, ids, rc );
631 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
641 LDAP_LOG( INDEX, ARGS,
642 "bdb_idl_insert_key: %lx %s\n",
643 (long) id, bdb_show_key( key, buf ), 0 );
645 Debug( LDAP_DEBUG_ARGS,
646 "bdb_idl_insert_key: %lx %s\n",
647 (long) id, bdb_show_key( key, buf ), 0 );
651 assert( id != NOID );
653 #ifdef SLAP_IDL_CACHE
654 if ( bdb->bi_idl_cache_size ) {
655 bdb_idl_cache_del( bdb, db, key );
660 data.size = sizeof( ID );
661 data.ulen = data.size;
662 data.flags = DB_DBT_USERMEM;
664 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
667 LDAP_LOG( INDEX, ERR,
668 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
669 db_strerror(rc), rc, 0 );
671 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
672 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
677 /* Fetch the first data item for this key, to see if it
678 * exists and if it's a range.
680 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
684 /* not a range, count the number of items */
686 rc = cursor->c_count( cursor, &count, 0 );
691 if ( count >= BDB_IDL_DB_MAX ) {
692 /* No room, convert to a range */
695 key2.dlen = key2.ulen;
696 key2.flags |= DB_DBT_PARTIAL;
700 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
701 if ( rc != 0 && rc != DB_NOTFOUND ) {
702 err = "c_get next_nodup";
705 if ( rc == DB_NOTFOUND ) {
706 rc = cursor->c_get( cursor, key, &data, DB_LAST );
712 rc = cursor->c_get( cursor, key, &data, DB_PREV );
722 rc = db->del( db, tid, key, 0 );
729 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
735 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
741 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
747 /* There's room, just store it */
751 /* It's a range, see if we need to rewrite
756 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
763 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
769 if ( id < lo || id > hi ) {
770 /* Delete the current lo/hi */
771 rc = cursor->c_del( cursor, 0 );
777 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
784 } else if ( rc == DB_NOTFOUND ) {
785 put1: data.data = &id;
786 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
787 /* Don't worry if it's already there */
788 if ( rc != 0 && rc != DB_KEYEXIST ) {
793 /* initial c_get failed, nothing was done */
796 LDAP_LOG( INDEX, ERR,
797 "bdb_idl_insert_key: %s failed: %s (%d)\n",
798 err, db_strerror(rc), rc );
800 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
801 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
803 cursor->c_close( cursor );
806 rc = cursor->c_close( cursor );
809 LDAP_LOG( INDEX, ERR,
810 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
811 db_strerror(rc), rc, 0 );
813 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
814 "c_close failed: %s (%d)\n",
815 db_strerror(rc), rc, 0 );
829 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
839 LDAP_LOG( INDEX, ARGS,
840 "bdb_idl_delete_key: %lx %s\n",
841 (long) id, bdb_show_key( key, buf ), 0 );
843 Debug( LDAP_DEBUG_ARGS,
844 "bdb_idl_delete_key: %lx %s\n",
845 (long) id, bdb_show_key( key, buf ), 0 );
848 assert( id != NOID );
850 #ifdef SLAP_IDL_CACHE
851 if ( bdb->bi_idl_cache_max_size ) {
852 bdb_idl_cache_del( bdb, db, key );
858 data.size = sizeof( id );
859 data.ulen = data.size;
860 data.flags = DB_DBT_USERMEM;
862 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
865 LDAP_LOG( INDEX, ERR,
866 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
867 db_strerror(rc), rc, 0 );
869 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
870 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
874 /* Fetch the first data item for this key, to see if it
875 * exists and if it's a range.
877 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
881 /* Not a range, just delete it */
883 /* position to correct item */
885 rc = cursor->c_get( cursor, key, &data,
886 DB_GET_BOTH | DB_RMW );
892 rc = cursor->c_del( cursor, 0 );
898 /* It's a range, see if we need to rewrite
902 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
908 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
913 if ( id == lo || id == hi ) {
917 } else if ( id == hi ) {
922 /* The range has collapsed... */
923 rc = db->del( db, tid, key, 0 );
930 /* reposition on lo slot */
932 cursor->c_get( cursor, key, &data, DB_PREV );
935 rc = cursor->c_del( cursor, 0 );
943 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
952 /* initial c_get failed, nothing was done */
954 if ( rc != DB_NOTFOUND ) {
956 LDAP_LOG( INDEX, ERR,
957 "bdb_idl_delete_key: %s failed: %s (%d)\n",
958 err, db_strerror(rc), rc );
960 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
961 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
964 cursor->c_close( cursor );
967 rc = cursor->c_close( cursor );
970 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
971 db_strerror(rc), rc, 0 );
973 Debug( LDAP_DEBUG_ANY,
974 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
975 db_strerror(rc), rc, 0 );
984 * idl_intersection - return a = a intersection b
987 bdb_idl_intersection(
993 ID cursora = 0, cursorb = 0, cursorc;
996 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
1001 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1002 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1003 if ( idmin > idmax ) {
1006 } else if ( idmin == idmax ) {
1012 if ( BDB_IDL_IS_RANGE( a ) ) {
1013 if ( BDB_IDL_IS_RANGE(b) ) {
1014 /* If both are ranges, just shrink the boundaries */
1019 /* Else swap so that b is the range, a is a list */
1027 /* If a range completely covers the list, the result is
1028 * just the list. If idmin to idmax is contiguous, just
1029 * turn it into a range.
1031 if ( BDB_IDL_IS_RANGE( b )
1032 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1033 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1034 if (idmax - idmin + 1 == a[0])
1043 /* Fine, do the intersection one element at a time.
1044 * First advance to idmin in both IDLs.
1046 cursora = cursorb = idmin;
1047 ida = bdb_idl_first( a, &cursora );
1048 idb = bdb_idl_first( b, &cursorb );
1051 while( ida <= idmax || idb <= idmax ) {
1054 ida = bdb_idl_next( a, &cursora );
1055 idb = bdb_idl_next( b, &cursorb );
1056 } else if ( ida < idb ) {
1057 ida = bdb_idl_next( a, &cursora );
1059 idb = bdb_idl_next( b, &cursorb );
1065 BDB_IDL_CPY( b, a );
1072 * idl_union - return a = a union b
1080 ID cursora = 0, cursorb = 0, cursorc;
1082 if ( BDB_IDL_IS_ZERO( b ) ) {
1086 if ( BDB_IDL_IS_ZERO( a ) ) {
1087 BDB_IDL_CPY( a, b );
1091 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1092 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1093 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1100 ida = bdb_idl_first( a, &cursora );
1101 idb = bdb_idl_first( b, &cursorb );
1105 /* The distinct elements of a are cat'd to b */
1106 while( ida != NOID || idb != NOID ) {
1108 if( ++cursorc > BDB_IDL_UM_MAX ) {
1112 ida = bdb_idl_next( a, &cursora );
1116 ida = bdb_idl_next( a, &cursora );
1117 idb = bdb_idl_next( b, &cursorb );
1121 /* b is copied back to a in sorted order */
1126 while (cursorb <= b[0] || cursorc <= a[0]) {
1131 if (cursorb <= b[0] && b[cursorb] < idb)
1132 a[cursora++] = b[cursorb++];
1145 * bdb_idl_notin - return a intersection ~b (or a minus b)
1154 ID cursora = 0, cursorb = 0;
1156 if( BDB_IDL_IS_ZERO( a ) ||
1157 BDB_IDL_IS_ZERO( b ) ||
1158 BDB_IDL_IS_RANGE( b ) )
1160 BDB_IDL_CPY( ids, a );
1164 if( BDB_IDL_IS_RANGE( a ) ) {
1165 BDB_IDL_CPY( ids, a );
1169 ida = bdb_idl_first( a, &cursora ),
1170 idb = bdb_idl_first( b, &cursorb );
1174 while( ida != NOID ) {
1175 if ( idb == NOID ) {
1176 /* we could shortcut this */
1177 ids[++ids[0]] = ida;
1178 ida = bdb_idl_next( a, &cursora );
1180 } else if ( ida < idb ) {
1181 ids[++ids[0]] = ida;
1182 ida = bdb_idl_next( a, &cursora );
1184 } else if ( ida > idb ) {
1185 idb = bdb_idl_next( b, &cursorb );
1188 ida = bdb_idl_next( a, &cursora );
1189 idb = bdb_idl_next( b, &cursorb );
1197 ID bdb_idl_first( ID *ids, ID *cursor )
1201 if ( ids[0] == 0 ) {
1206 if ( BDB_IDL_IS_RANGE( ids ) ) {
1207 if( *cursor < ids[1] ) {
1216 pos = bdb_idl_search( ids, *cursor );
1218 if( pos > ids[0] ) {
1226 ID bdb_idl_next( ID *ids, ID *cursor )
1228 if ( BDB_IDL_IS_RANGE( ids ) ) {
1229 if( ids[2] < ++(*cursor) ) {
1235 if ( ++(*cursor) <= ids[0] ) {
1236 return ids[*cursor];