1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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 ) ) {
83 Debug( LDAP_DEBUG_ANY,
84 "IDL: range ( %ld - %ld )\n",
85 (long) BDB_IDL_RANGE_FIRST( ids ),
86 (long) BDB_IDL_RANGE_LAST( ids ) );
90 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
92 for( i=1; i<=ids[0]; i++ ) {
94 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
96 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
99 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
104 #endif /* IDL_DEBUG > 1 */
105 #endif /* IDL_DEBUG > 0 */
107 unsigned bdb_idl_search( ID *ids, ID id )
109 #define IDL_BINARY_SEARCH 1
110 #ifdef IDL_BINARY_SEARCH
112 * binary search of id in ids
113 * if found, returns position of id
114 * if not found, returns first postion greater than id
127 cursor = base + pivot;
128 val = IDL_CMP( id, ids[cursor + 1] );
133 } else if ( val > 0 ) {
149 /* (reverse) linear search */
156 for( i=ids[0]; i; i-- ) {
166 int bdb_idl_insert( ID *ids, ID id )
171 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
177 if (BDB_IDL_IS_RANGE( ids )) {
178 /* if already in range, treat as a dup */
179 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
181 if (id < BDB_IDL_FIRST(ids))
183 else if (id > BDB_IDL_LAST(ids))
188 x = bdb_idl_search( ids, id );
196 if ( x <= ids[0] && ids[x] == id ) {
201 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
204 ids[2] = ids[ids[0]-1];
205 } else if ( ids[ids[0]-1] < id ) {
208 ids[2] = ids[ids[0]-1];
214 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
227 static int bdb_idl_delete( ID *ids, ID id )
229 unsigned x = bdb_idl_search( ids, id );
232 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
245 if( x > ids[0] || ids[x] != id ) {
249 } else if ( --ids[0] == 0 ) {
255 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
272 if ( key->size == 4 /* LUTIL_HASH_BYTES */ ) {
273 unsigned char *c = key->data;
274 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
281 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
282 * copy the cached IDL into it, otherwise just return the status.
286 struct bdb_info *bdb,
291 bdb_idl_cache_entry_t idl_tmp;
292 bdb_idl_cache_entry_t *matched_idl_entry;
293 int rc = LDAP_NO_SUCH_OBJECT;
295 DBT2bv( key, &idl_tmp.kstr );
297 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
298 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
300 if ( matched_idl_entry != NULL ) {
301 if ( matched_idl_entry->idl && ids )
302 BDB_IDL_CPY( ids, matched_idl_entry->idl );
303 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
304 IDL_LRU_DELETE( bdb, matched_idl_entry );
305 IDL_LRU_ADD( bdb, matched_idl_entry );
306 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
307 if ( matched_idl_entry->idl )
312 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
319 struct bdb_info *bdb,
325 bdb_idl_cache_entry_t idl_tmp;
326 bdb_idl_cache_entry_t *ee;
328 DBT2bv( key, &idl_tmp.kstr );
330 ee = (bdb_idl_cache_entry_t *) ch_malloc(
331 sizeof( bdb_idl_cache_entry_t ) );
333 if ( rc == DB_NOTFOUND || BDB_IDL_IS_ZERO( ids )) {
336 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
337 BDB_IDL_CPY( ee->idl, ids );
339 ee->idl_lru_prev = NULL;
340 ee->idl_lru_next = NULL;
341 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
342 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
343 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
344 bdb_idl_entry_cmp, avl_dup_error ))
346 ch_free( ee->kstr.bv_val );
349 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
352 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
353 IDL_LRU_ADD( bdb, ee );
354 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
356 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
357 ee = bdb->bi_idl_lru_tail;
358 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
359 bdb_idl_entry_cmp ) == NULL ) {
360 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
361 "AVL delete failed\n",
364 IDL_LRU_DELETE( bdb, ee );
366 --bdb->bi_idl_cache_size;
367 ch_free( ee->kstr.bv_val );
373 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
374 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
379 struct bdb_info *bdb,
383 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
384 DBT2bv( key, &idl_tmp.kstr );
386 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
387 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
389 if ( matched_idl_entry != NULL ) {
390 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
391 bdb_idl_entry_cmp ) == NULL ) {
392 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
393 "AVL delete failed\n",
396 --bdb->bi_idl_cache_size;
397 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
398 IDL_LRU_DELETE( bdb, matched_idl_entry );
399 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
400 free( matched_idl_entry->kstr.bv_val );
401 if ( matched_idl_entry->idl )
402 free( matched_idl_entry->idl );
403 free( matched_idl_entry );
405 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
409 bdb_idl_cache_add_id(
410 struct bdb_info *bdb,
415 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
416 DBT2bv( key, &idl_tmp.kstr );
418 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
419 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
421 if ( cache_entry != NULL ) {
422 if ( !BDB_IDL_IS_RANGE( cache_entry->idl ) &&
423 cache_entry->idl[0] < BDB_IDL_DB_MAX ) {
424 size_t s = BDB_IDL_SIZEOF( cache_entry->idl ) + sizeof(ID);
425 cache_entry->idl = ch_realloc( cache_entry->idl, s );
427 bdb_idl_insert( cache_entry->idl, id );
429 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
433 bdb_idl_cache_del_id(
434 struct bdb_info *bdb,
439 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
440 DBT2bv( key, &idl_tmp.kstr );
442 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
443 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
445 if ( cache_entry != NULL ) {
446 bdb_idl_delete( cache_entry->idl, id );
447 if ( cache_entry->idl[0] == 0 ) {
448 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) cache_entry,
449 bdb_idl_entry_cmp ) == NULL ) {
450 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
451 "AVL delete failed\n",
454 --bdb->bi_idl_cache_size;
455 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
456 IDL_LRU_DELETE( bdb, cache_entry );
457 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
458 free( cache_entry->kstr.bv_val );
459 if ( cache_entry->idl )
460 free( cache_entry->idl );
464 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
477 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
479 DBT data, key2, *kptr;
485 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_FULL < 0x04010000
499 # define BDB_ENOUGH 5
501 /* We sometimes test with tiny IDLs, and BDB always wants buffers
502 * that are at least one page in size.
504 # if BDB_IDL_DB_SIZE < 4096
505 # define BDB_ENOUGH 2048
507 # define BDB_ENOUGH 1
510 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
514 Debug( LDAP_DEBUG_ARGS,
515 "bdb_idl_fetch_key: %s\n",
516 bdb_show_key( key, keybuf ), 0, 0 );
518 assert( ids != NULL );
520 if ( saved_cursor && *saved_cursor ) {
522 } else if ( get_flag == LDAP_FILTER_GE ) {
523 opflag = DB_SET_RANGE;
524 } else if ( get_flag == LDAP_FILTER_LE ) {
530 /* only non-range lookups can use the IDL cache */
531 if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
532 rc = bdb_idl_cache_get( bdb, db, key, ids );
533 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
539 data.ulen = sizeof(buf);
540 data.flags = DB_DBT_USERMEM;
542 /* If we're not reusing an existing cursor, get a new one */
543 if( opflag != DB_NEXT ) {
544 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
546 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
547 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
551 cursor = *saved_cursor;
554 /* If this is a LE lookup, save original key so we can determine
555 * when to stop. If this is a GE lookup, save the key since it
556 * will be overwritten.
558 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
560 key2.flags = DB_DBT_USERMEM;
561 key2.ulen = sizeof(keybuf);
563 key2.size = key->size;
564 AC_MEMCPY( keybuf, key->data, key->size );
570 rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
572 /* skip presence key on range inequality lookups */
573 while (rc == 0 && kptr->size != len) {
574 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
576 /* If we're doing a LE compare and the new key is greater than
577 * our search key, we're done
579 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
580 key->data, key->size ) > 0 ) {
588 DB_MULTIPLE_INIT( ptr, &data );
590 DB_MULTIPLE_NEXT(ptr, &data, j, len);
596 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
598 if ( rc == DB_NOTFOUND ) rc = 0;
600 /* On disk, a range is denoted by 0 in the first element */
602 if (ids[0] != BDB_IDL_RANGE_SIZE) {
603 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
604 "range size mismatch: expected %d, got %ld\n",
605 BDB_IDL_RANGE_SIZE, ids[0], 0 );
606 cursor->c_close( cursor );
609 BDB_IDL_RANGE( ids, ids[2], ids[3] );
611 data.size = BDB_IDL_SIZEOF(ids);
614 if ( saved_cursor && rc == 0 ) {
615 if ( !*saved_cursor )
616 *saved_cursor = cursor;
620 rc2 = cursor->c_close( cursor );
622 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
623 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
627 if( rc == DB_NOTFOUND ) {
630 } else if( rc != 0 ) {
631 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
632 "get failed: %s (%d)\n",
633 db_strerror(rc), rc, 0 );
636 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
637 /* size not multiple of ID size */
638 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
639 "odd size: expected %ld multiple, got %ld\n",
640 (long) sizeof( ID ), (long) data.size, 0 );
643 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
645 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
646 "get size mismatch: expected %ld, got %ld\n",
647 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
651 if ( bdb->bi_idl_cache_max_size ) {
652 bdb_idl_cache_put( bdb, db, key, ids, rc );
667 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
671 ID lo, hi, nlo, nhi, nid;
676 Debug( LDAP_DEBUG_ARGS,
677 "bdb_idl_insert_key: %lx %s\n",
678 (long) id, bdb_show_key( key, buf ), 0 );
681 assert( id != NOID );
683 if ( bdb->bi_idl_cache_size ) {
684 bdb_idl_cache_del( bdb, db, key );
688 data.size = sizeof( ID );
689 data.ulen = data.size;
690 data.flags = DB_DBT_USERMEM;
692 BDB_ID2DISK( id, &nid );
694 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
696 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
697 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
701 /* Fetch the first data item for this key, to see if it
702 * exists and if it's a range.
704 rc = cursor->c_get( cursor, key, &data, DB_SET );
708 /* not a range, count the number of items */
710 rc = cursor->c_count( cursor, &count, 0 );
715 if ( count >= BDB_IDL_DB_MAX ) {
716 /* No room, convert to a range */
720 key2.dlen = key2.ulen;
721 key2.flags |= DB_DBT_PARTIAL;
723 BDB_DISK2ID( &nlo, &lo );
726 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
727 if ( rc != 0 && rc != DB_NOTFOUND ) {
728 err = "c_get next_nodup";
731 if ( rc == DB_NOTFOUND ) {
732 rc = cursor->c_get( cursor, key, &data, DB_LAST );
738 rc = cursor->c_get( cursor, key, &data, DB_PREV );
744 BDB_DISK2ID( &nhi, &hi );
745 /* Update hi/lo if needed, then delete all the items
751 } else if ( id > hi ) {
756 /* Don't fetch anything, just position cursor */
757 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
758 data.dlen = data.ulen = 0;
759 rc = cursor->c_get( cursor, key, &data, DB_SET );
764 rc = cursor->c_del( cursor, 0 );
766 err = "c_del range1";
769 /* Delete all the records */
770 for ( i=1; i<count; i++ ) {
771 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_DUP );
773 err = "c_get next_dup";
776 rc = cursor->c_del( cursor, 0 );
782 /* Store the range marker */
783 data.size = data.ulen = sizeof(ID);
784 data.flags = DB_DBT_USERMEM;
786 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
792 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
798 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
804 /* There's room, just store it */
808 /* It's a range, see if we need to rewrite
813 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
818 BDB_DISK2ID( &nlo, &lo );
821 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
826 BDB_DISK2ID( &nhi, &hi );
828 if ( id < lo || id > hi ) {
829 /* Delete the current lo/hi */
830 rc = cursor->c_del( cursor, 0 );
836 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
843 } else if ( rc == DB_NOTFOUND ) {
844 put1: data.data = &nid;
845 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
846 /* Don't worry if it's already there */
847 if ( rc != 0 && rc != DB_KEYEXIST ) {
852 /* initial c_get failed, nothing was done */
854 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
855 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
856 cursor->c_close( cursor );
859 rc = cursor->c_close( cursor );
861 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
862 "c_close failed: %s (%d)\n",
863 db_strerror(rc), rc, 0 );
876 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
880 ID lo, hi, tmp, nid, nlo, nhi;
885 Debug( LDAP_DEBUG_ARGS,
886 "bdb_idl_delete_key: %lx %s\n",
887 (long) id, bdb_show_key( key, buf ), 0 );
889 assert( id != NOID );
891 if ( bdb->bi_idl_cache_max_size ) {
892 bdb_idl_cache_del( bdb, db, key );
895 BDB_ID2DISK( id, &nid );
899 data.size = sizeof( id );
900 data.ulen = data.size;
901 data.flags = DB_DBT_USERMEM;
903 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
905 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
906 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
909 /* Fetch the first data item for this key, to see if it
910 * exists and if it's a range.
912 rc = cursor->c_get( cursor, key, &data, DB_SET );
916 /* Not a range, just delete it */
918 /* position to correct item */
920 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
926 rc = cursor->c_del( cursor, 0 );
932 /* It's a range, see if we need to rewrite
936 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
941 BDB_DISK2ID( &nlo, &lo );
943 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
948 BDB_DISK2ID( &nhi, &hi );
949 if ( id == lo || id == hi ) {
953 } else if ( id == hi ) {
958 /* The range has collapsed... */
959 rc = db->del( db, tid, key, 0 );
966 /* reposition on lo slot */
968 cursor->c_get( cursor, key, &data, DB_PREV );
970 rc = cursor->c_del( cursor, 0 );
977 BDB_ID2DISK( id, &nid );
979 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
988 /* initial c_get failed, nothing was done */
990 if ( rc != DB_NOTFOUND ) {
991 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
992 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
994 cursor->c_close( cursor );
997 rc = cursor->c_close( cursor );
999 Debug( LDAP_DEBUG_ANY,
1000 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
1001 db_strerror(rc), rc, 0 );
1009 * idl_intersection - return a = a intersection b
1012 bdb_idl_intersection(
1018 ID cursora = 0, cursorb = 0, cursorc;
1021 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
1026 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1027 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1028 if ( idmin > idmax ) {
1031 } else if ( idmin == idmax ) {
1037 if ( BDB_IDL_IS_RANGE( a ) ) {
1038 if ( BDB_IDL_IS_RANGE(b) ) {
1039 /* If both are ranges, just shrink the boundaries */
1044 /* Else swap so that b is the range, a is a list */
1052 /* If a range completely covers the list, the result is
1053 * just the list. If idmin to idmax is contiguous, just
1054 * turn it into a range.
1056 if ( BDB_IDL_IS_RANGE( b )
1057 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1058 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1059 if (idmax - idmin + 1 == a[0])
1068 /* Fine, do the intersection one element at a time.
1069 * First advance to idmin in both IDLs.
1071 cursora = cursorb = idmin;
1072 ida = bdb_idl_first( a, &cursora );
1073 idb = bdb_idl_first( b, &cursorb );
1076 while( ida <= idmax || idb <= idmax ) {
1079 ida = bdb_idl_next( a, &cursora );
1080 idb = bdb_idl_next( b, &cursorb );
1081 } else if ( ida < idb ) {
1082 ida = bdb_idl_next( a, &cursora );
1084 idb = bdb_idl_next( b, &cursorb );
1090 BDB_IDL_CPY( b, a );
1097 * idl_union - return a = a union b
1105 ID cursora = 0, cursorb = 0, cursorc;
1107 if ( BDB_IDL_IS_ZERO( b ) ) {
1111 if ( BDB_IDL_IS_ZERO( a ) ) {
1112 BDB_IDL_CPY( a, b );
1116 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1117 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1118 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1125 ida = bdb_idl_first( a, &cursora );
1126 idb = bdb_idl_first( b, &cursorb );
1130 /* The distinct elements of a are cat'd to b */
1131 while( ida != NOID || idb != NOID ) {
1133 if( ++cursorc > BDB_IDL_UM_MAX ) {
1137 ida = bdb_idl_next( a, &cursora );
1141 ida = bdb_idl_next( a, &cursora );
1142 idb = bdb_idl_next( b, &cursorb );
1146 /* b is copied back to a in sorted order */
1151 while (cursorb <= b[0] || cursorc <= a[0]) {
1156 if (cursorb <= b[0] && b[cursorb] < idb)
1157 a[cursora++] = b[cursorb++];
1170 * bdb_idl_notin - return a intersection ~b (or a minus b)
1179 ID cursora = 0, cursorb = 0;
1181 if( BDB_IDL_IS_ZERO( a ) ||
1182 BDB_IDL_IS_ZERO( b ) ||
1183 BDB_IDL_IS_RANGE( b ) )
1185 BDB_IDL_CPY( ids, a );
1189 if( BDB_IDL_IS_RANGE( a ) ) {
1190 BDB_IDL_CPY( ids, a );
1194 ida = bdb_idl_first( a, &cursora ),
1195 idb = bdb_idl_first( b, &cursorb );
1199 while( ida != NOID ) {
1200 if ( idb == NOID ) {
1201 /* we could shortcut this */
1202 ids[++ids[0]] = ida;
1203 ida = bdb_idl_next( a, &cursora );
1205 } else if ( ida < idb ) {
1206 ids[++ids[0]] = ida;
1207 ida = bdb_idl_next( a, &cursora );
1209 } else if ( ida > idb ) {
1210 idb = bdb_idl_next( b, &cursorb );
1213 ida = bdb_idl_next( a, &cursora );
1214 idb = bdb_idl_next( b, &cursorb );
1222 ID bdb_idl_first( ID *ids, ID *cursor )
1226 if ( ids[0] == 0 ) {
1231 if ( BDB_IDL_IS_RANGE( ids ) ) {
1232 if( *cursor < ids[1] ) {
1241 pos = bdb_idl_search( ids, *cursor );
1243 if( pos > ids[0] ) {
1251 ID bdb_idl_next( ID *ids, ID *cursor )
1253 if ( BDB_IDL_IS_RANGE( ids ) ) {
1254 if( ids[2] < ++(*cursor) ) {
1260 if ( ++(*cursor) <= ids[0] ) {
1261 return ids[*cursor];
1269 /* Add one ID to an unsorted list. We ensure that the first element is the
1270 * minimum and the last element is the maximum, for fast range compaction.
1271 * this means IDLs up to length 3 are always sorted...
1273 int bdb_idl_append_one( ID *ids, ID id )
1275 if (BDB_IDL_IS_RANGE( ids )) {
1276 /* if already in range, treat as a dup */
1277 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
1279 if (id < BDB_IDL_FIRST(ids))
1281 else if (id > BDB_IDL_LAST(ids))
1293 if ( ids[0] > 1 && id < ids[ids[0]] ) {
1300 if ( ids[0] >= BDB_IDL_UM_MAX ) {
1309 /* Append sorted list b to sorted list a. The result is unsorted but
1310 * a[1] is the min of the result and a[a[0]] is the max.
1312 int bdb_idl_append( ID *a, ID *b )
1314 ID ida, idb, tmp, swap = 0;
1316 if ( BDB_IDL_IS_ZERO( b ) ) {
1320 if ( BDB_IDL_IS_ZERO( a ) ) {
1321 BDB_IDL_CPY( a, b );
1325 ida = BDB_IDL_LAST( a );
1326 idb = BDB_IDL_LAST( b );
1327 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ||
1328 a[0] + b[0] >= BDB_IDL_UM_MAX ) {
1329 a[2] = IDL_MAX( ida, idb );
1330 a[1] = IDL_MIN( a[1], b[1] );
1335 if ( b[0] > 1 && ida > idb ) {
1341 if ( b[1] < a[1] ) {
1352 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1363 /* Quicksort + Insertion sort for small arrays */
1366 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1369 bdb_idl_sort( ID *ids, ID *tmp )
1371 int *istack = (int *)tmp;
1372 int i,j,k,l,ir,jstack;
1375 if ( BDB_IDL_IS_RANGE( ids ))
1382 if (ir - l < SMALL) { /* Insertion sort */
1383 for (j=l+1;j<=ir;j++) {
1385 for (i=j-1;i>=1;i--) {
1386 if (ids[i] <= a) break;
1391 if (jstack == 0) break;
1392 ir = istack[jstack--];
1393 l = istack[jstack--];
1395 k = (l + ir) >> 1; /* Choose median of left, center, right */
1396 SWAP(ids[k], ids[l+1]);
1397 if (ids[l] > ids[ir]) {
1398 SWAP(ids[l], ids[ir]);
1400 if (ids[l+1] > ids[ir]) {
1401 SWAP(ids[l+1], ids[ir]);
1403 if (ids[l] > ids[l+1]) {
1404 SWAP(ids[l], ids[l+1]);
1410 do i++; while(ids[i] < a);
1411 do j--; while(ids[j] > a);
1413 SWAP(ids[i],ids[j]);
1418 if (ir-i+1 >= j-1) {
1419 istack[jstack] = ir;
1420 istack[jstack-1] = i;
1423 istack[jstack] = j-1;
1424 istack[jstack-1] = l;
1433 /* 8 bit Radix sort + insertion sort
1435 * based on code from http://www.cubic.org/docs/radix.htm
1436 * with improvements by mbackes@symas.com and hyc@symas.com
1438 * This code is O(n) but has a relatively high constant factor. For lists
1439 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1440 * Much faster than quicksort for lists longer than ~100. Insertion
1441 * sort is actually superior for lists <50.
1444 #define BUCKETS (1<<8)
1448 bdb_idl_sort( ID *ids, ID *tmp )
1450 int count, soft_limit, phase = 0, size = ids[0];
1452 unsigned char *maxv = (unsigned char *)&ids[size];
1454 if ( BDB_IDL_IS_RANGE( ids ))
1457 /* Use insertion sort for small lists */
1458 if ( size <= SMALL ) {
1462 for (j=1;j<=size;j++) {
1464 for (i=j-1;i>=1;i--) {
1465 if (ids[i] <= a) break;
1477 #if BYTE_ORDER == BIG_ENDIAN
1478 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1480 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1484 #if BYTE_ORDER == BIG_ENDIAN
1485 count = sizeof(ID)-1; count >= soft_limit; --count
1487 count = 0; count <= soft_limit; ++count
1490 unsigned int num[BUCKETS], * np, n, sum;
1492 ID *sp, *source, *dest;
1493 unsigned char *bp, *source_start;
1495 source = idls[phase]+1;
1496 dest = idls[phase^1]+1;
1497 source_start = ((unsigned char *) source) + count;
1500 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1502 /* count occurences of every byte value */
1504 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1507 /* transform count into index by summing elements and storing
1512 for ( i = BUCKETS; i > 0; --i ) {
1518 /* fill dest with the right values in the right place */
1521 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1529 /* copy back from temp if needed */
1532 for ( count = 0; count < size; ++count )
1536 #endif /* Quick vs Radix */
1538 #endif /* BDB_HIER */