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 ee = (bdb_idl_cache_entry_t *) ch_malloc(
348 sizeof( bdb_idl_cache_entry_t ) );
350 if ( rc == DB_NOTFOUND) {
353 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
354 BDB_IDL_CPY( ee->idl, ids );
356 ee->idl_lru_prev = NULL;
357 ee->idl_lru_next = NULL;
358 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
359 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
360 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
361 bdb_idl_entry_cmp, avl_dup_error ))
363 ch_free( ee->kstr.bv_val );
366 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
369 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
370 IDL_LRU_ADD( bdb, ee );
371 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
373 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
374 ee = bdb->bi_idl_lru_tail;
375 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
376 bdb_idl_entry_cmp ) == NULL ) {
378 LDAP_LOG( INDEX, ERR,
379 "bdb_idl_cache_put: AVL delete failed\n",
382 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
383 "AVL delete failed\n",
387 IDL_LRU_DELETE( bdb, ee );
389 --bdb->bi_idl_cache_size;
390 ch_free( ee->kstr.bv_val );
396 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
397 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
402 struct bdb_info *bdb,
406 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
407 DBT2bv( key, &idl_tmp.kstr );
409 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
410 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
412 if ( matched_idl_entry != NULL ) {
413 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
414 bdb_idl_entry_cmp ) == NULL ) {
416 LDAP_LOG( INDEX, ERR,
417 "bdb_idl_cache_del: AVL delete failed\n",
420 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
421 "AVL delete failed\n",
425 --bdb->bi_idl_cache_size;
426 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
427 IDL_LRU_DELETE( bdb, matched_idl_entry );
428 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
429 free( matched_idl_entry->kstr.bv_val );
430 if ( matched_idl_entry->idl )
431 free( matched_idl_entry->idl );
432 free( matched_idl_entry );
434 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
446 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
454 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
456 /* If using BerkeleyDB 4.0, the buf must be large enough to
457 * grab the entire IDL in one get(), otherwise BDB will leak
458 * resources on subsequent get's. We can safely call get()
459 * twice - once for the data, and once to get the DB_NOTFOUND
460 * result meaning there's no more data. See ITS#2040 for details.
461 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
462 * stack space is too limited.
464 * configure now requires Berkeley DB 4.1.
466 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 0)
467 # define BDB_ENOUGH 5
469 # define BDB_ENOUGH 1
471 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
476 LDAP_LOG( INDEX, ARGS,
477 "bdb_idl_fetch_key: %s\n",
478 bdb_show_key( key, keybuf ), 0, 0 );
480 Debug( LDAP_DEBUG_ARGS,
481 "bdb_idl_fetch_key: %s\n",
482 bdb_show_key( key, keybuf ), 0, 0 );
485 assert( ids != NULL );
487 #ifdef SLAP_IDL_CACHE
488 if ( bdb->bi_idl_cache_size ) {
489 rc = bdb_idl_cache_get( bdb, db, key, ids );
490 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
497 data.ulen = sizeof(buf);
498 data.flags = DB_DBT_USERMEM;
500 if ( tid ) flags |= DB_RMW;
502 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
505 LDAP_LOG( INDEX, ERR,
506 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
507 db_strerror(rc), rc, 0 );
509 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
510 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
515 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
521 DB_MULTIPLE_INIT( ptr, &data );
523 DB_MULTIPLE_NEXT(ptr, &data, j, len);
526 AC_MEMCPY( i, j, sizeof(ID) );
529 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
531 if ( rc == DB_NOTFOUND ) rc = 0;
533 /* On disk, a range is denoted by 0 in the first element */
535 if (ids[0] != BDB_IDL_RANGE_SIZE) {
537 LDAP_LOG( INDEX, ERR,
538 "=> bdb_idl_fetch_key: range size mismatch: "
539 "expected %ld, got %ld\n",
540 BDB_IDL_RANGE_SIZE, ids[0], 0 );
542 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
543 "range size mismatch: expected %d, got %ld\n",
544 BDB_IDL_RANGE_SIZE, ids[0], 0 );
546 cursor->c_close( cursor );
549 BDB_IDL_RANGE( ids, ids[2], ids[3] );
551 data.size = BDB_IDL_SIZEOF(ids);
554 rc2 = cursor->c_close( cursor );
557 LDAP_LOG( INDEX, ERR,
558 "bdb_idl_fetch_key: close failed: %s (%d)\n",
559 db_strerror(rc2), rc2, 0 );
561 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
562 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
567 if( rc == DB_NOTFOUND ) {
568 #ifndef SLAP_IDL_CACHE
572 } else if( rc != 0 ) {
574 LDAP_LOG( INDEX, ERR,
575 "bdb_idl_fetch_key: get failed: %s (%d)\n",
576 db_strerror(rc), rc, 0 );
578 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
579 "get failed: %s (%d)\n",
580 db_strerror(rc), rc, 0 );
584 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
585 /* size not multiple of ID size */
587 LDAP_LOG( INDEX, ERR,
588 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
589 (long) sizeof( ID ), (long) data.size, 0 );
591 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
592 "odd size: expected %ld multiple, got %ld\n",
593 (long) sizeof( ID ), (long) data.size, 0 );
597 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
600 LDAP_LOG( INDEX, ERR,
601 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
602 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
604 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
605 "get size mismatch: expected %ld, got %ld\n",
606 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
611 #ifdef SLAP_IDL_CACHE
612 if ( bdb->bi_idl_cache_max_size ) {
613 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 #ifdef SLAP_IDL_CACHE
652 if ( bdb->bi_idl_cache_size ) {
653 bdb_idl_cache_del( bdb, db, key );
658 data.size = sizeof( ID );
659 data.ulen = data.size;
660 data.flags = DB_DBT_USERMEM;
662 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
665 LDAP_LOG( INDEX, ERR,
666 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
667 db_strerror(rc), rc, 0 );
669 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
670 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
675 /* Fetch the first data item for this key, to see if it
676 * exists and if it's a range.
678 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
682 /* not a range, count the number of items */
684 rc = cursor->c_count( cursor, &count, 0 );
689 if ( count >= BDB_IDL_DB_MAX ) {
690 /* No room, convert to a range */
693 key2.dlen = key2.ulen;
694 key2.flags |= DB_DBT_PARTIAL;
698 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
699 if ( rc != 0 && rc != DB_NOTFOUND ) {
700 err = "c_get next_nodup";
703 if ( rc == DB_NOTFOUND ) {
704 rc = cursor->c_get( cursor, key, &data, DB_LAST );
710 rc = cursor->c_get( cursor, key, &data, DB_PREV );
720 rc = db->del( db, tid, key, 0 );
727 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
733 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
739 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
745 /* There's room, just store it */
749 /* It's a range, see if we need to rewrite
754 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
761 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
767 if ( id < lo || id > hi ) {
768 /* Delete the current lo/hi */
769 rc = cursor->c_del( cursor, 0 );
775 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
782 } else if ( rc == DB_NOTFOUND ) {
783 put1: data.data = &id;
784 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
785 /* Don't worry if it's already there */
786 if ( rc != 0 && rc != DB_KEYEXIST ) {
791 /* initial c_get failed, nothing was done */
794 LDAP_LOG( INDEX, ERR,
795 "bdb_idl_insert_key: %s failed: %s (%d)\n",
796 err, db_strerror(rc), rc );
798 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
799 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
801 cursor->c_close( cursor );
804 rc = cursor->c_close( cursor );
807 LDAP_LOG( INDEX, ERR,
808 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
809 db_strerror(rc), rc, 0 );
811 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
812 "c_close failed: %s (%d)\n",
813 db_strerror(rc), rc, 0 );
827 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
837 LDAP_LOG( INDEX, ARGS,
838 "bdb_idl_delete_key: %lx %s\n",
839 (long) id, bdb_show_key( key, buf ), 0 );
841 Debug( LDAP_DEBUG_ARGS,
842 "bdb_idl_delete_key: %lx %s\n",
843 (long) id, bdb_show_key( key, buf ), 0 );
846 assert( id != NOID );
848 #ifdef SLAP_IDL_CACHE
849 if ( bdb->bi_idl_cache_max_size ) {
850 bdb_idl_cache_del( bdb, db, key );
856 data.size = sizeof( id );
857 data.ulen = data.size;
858 data.flags = DB_DBT_USERMEM;
860 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
863 LDAP_LOG( INDEX, ERR,
864 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
865 db_strerror(rc), rc, 0 );
867 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
868 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
872 /* Fetch the first data item for this key, to see if it
873 * exists and if it's a range.
875 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
879 /* Not a range, just delete it */
881 /* position to correct item */
883 rc = cursor->c_get( cursor, key, &data,
884 DB_GET_BOTH | DB_RMW );
890 rc = cursor->c_del( cursor, 0 );
896 /* It's a range, see if we need to rewrite
900 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
906 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
911 if ( id == lo || id == hi ) {
915 } else if ( id == hi ) {
920 /* The range has collapsed... */
921 rc = db->del( db, tid, key, 0 );
928 /* reposition on lo slot */
930 cursor->c_get( cursor, key, &data, DB_PREV );
933 rc = cursor->c_del( cursor, 0 );
941 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
950 /* initial c_get failed, nothing was done */
952 if ( rc != DB_NOTFOUND ) {
954 LDAP_LOG( INDEX, ERR,
955 "bdb_idl_delete_key: %s failed: %s (%d)\n",
956 err, db_strerror(rc), rc );
958 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
959 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
962 cursor->c_close( cursor );
965 rc = cursor->c_close( cursor );
968 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
969 db_strerror(rc), rc, 0 );
971 Debug( LDAP_DEBUG_ANY,
972 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
973 db_strerror(rc), rc, 0 );
982 * idl_intersection - return a = a intersection b
985 bdb_idl_intersection(
991 ID cursora = 0, cursorb = 0, cursorc;
994 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
999 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1000 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1001 if ( idmin > idmax ) {
1004 } else if ( idmin == idmax ) {
1010 if ( BDB_IDL_IS_RANGE( a ) ) {
1011 if ( BDB_IDL_IS_RANGE(b) ) {
1012 /* If both are ranges, just shrink the boundaries */
1017 /* Else swap so that b is the range, a is a list */
1025 /* If a range completely covers the list, the result is
1026 * just the list. If idmin to idmax is contiguous, just
1027 * turn it into a range.
1029 if ( BDB_IDL_IS_RANGE( b )
1030 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1031 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1032 if (idmax - idmin + 1 == a[0])
1041 /* Fine, do the intersection one element at a time.
1042 * First advance to idmin in both IDLs.
1044 cursora = cursorb = idmin;
1045 ida = bdb_idl_first( a, &cursora );
1046 idb = bdb_idl_first( b, &cursorb );
1049 while( ida <= idmax || idb <= idmax ) {
1052 ida = bdb_idl_next( a, &cursora );
1053 idb = bdb_idl_next( b, &cursorb );
1054 } else if ( ida < idb ) {
1055 ida = bdb_idl_next( a, &cursora );
1057 idb = bdb_idl_next( b, &cursorb );
1063 BDB_IDL_CPY( b, a );
1070 * idl_union - return a = a union b
1078 ID cursora = 0, cursorb = 0, cursorc;
1080 if ( BDB_IDL_IS_ZERO( b ) ) {
1084 if ( BDB_IDL_IS_ZERO( a ) ) {
1085 BDB_IDL_CPY( a, b );
1089 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1090 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1091 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1098 ida = bdb_idl_first( a, &cursora );
1099 idb = bdb_idl_first( b, &cursorb );
1103 /* The distinct elements of a are cat'd to b */
1104 while( ida != NOID || idb != NOID ) {
1106 if( ++cursorc > BDB_IDL_UM_MAX ) {
1110 ida = bdb_idl_next( a, &cursora );
1114 ida = bdb_idl_next( a, &cursora );
1115 idb = bdb_idl_next( b, &cursorb );
1119 /* b is copied back to a in sorted order */
1124 while (cursorb <= b[0] || cursorc <= a[0]) {
1129 if (cursorb <= b[0] && b[cursorb] < idb)
1130 a[cursora++] = b[cursorb++];
1143 * bdb_idl_notin - return a intersection ~b (or a minus b)
1152 ID cursora = 0, cursorb = 0;
1154 if( BDB_IDL_IS_ZERO( a ) ||
1155 BDB_IDL_IS_ZERO( b ) ||
1156 BDB_IDL_IS_RANGE( b ) )
1158 BDB_IDL_CPY( ids, a );
1162 if( BDB_IDL_IS_RANGE( a ) ) {
1163 BDB_IDL_CPY( ids, a );
1167 ida = bdb_idl_first( a, &cursora ),
1168 idb = bdb_idl_first( b, &cursorb );
1172 while( ida != NOID ) {
1173 if ( idb == NOID ) {
1174 /* we could shortcut this */
1175 ids[++ids[0]] = ida;
1176 ida = bdb_idl_next( a, &cursora );
1178 } else if ( ida < idb ) {
1179 ids[++ids[0]] = ida;
1180 ida = bdb_idl_next( a, &cursora );
1182 } else if ( ida > idb ) {
1183 idb = bdb_idl_next( b, &cursorb );
1186 ida = bdb_idl_next( a, &cursora );
1187 idb = bdb_idl_next( b, &cursorb );
1195 ID bdb_idl_first( ID *ids, ID *cursor )
1199 if ( ids[0] == 0 ) {
1204 if ( BDB_IDL_IS_RANGE( ids ) ) {
1205 if( *cursor < ids[1] ) {
1214 pos = bdb_idl_search( ids, *cursor );
1216 if( pos > ids[0] ) {
1224 ID bdb_idl_next( ID *ids, ID *cursor )
1226 if ( BDB_IDL_IS_RANGE( ids ) ) {
1227 if( ids[2] < ++(*cursor) ) {
1233 if ( ++(*cursor) <= ids[0] ) {
1234 return ids[*cursor];