1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2006 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 )
232 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
238 if (BDB_IDL_IS_RANGE( ids )) {
239 /* If deleting a range boundary, adjust */
242 else if ( ids[2] == id )
244 /* deleting from inside a range is a no-op */
246 /* If the range has collapsed, re-adjust */
247 if ( ids[1] > ids[2] )
249 else if ( ids[1] == ids[2] )
254 x = bdb_idl_search( ids, id );
262 if( x > ids[0] || ids[x] != id ) {
266 } else if ( --ids[0] == 0 ) {
272 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
289 if ( key->size == 4 /* LUTIL_HASH_BYTES */ ) {
290 unsigned char *c = key->data;
291 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
298 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
299 * copy the cached IDL into it, otherwise just return the status.
303 struct bdb_info *bdb,
308 bdb_idl_cache_entry_t idl_tmp;
309 bdb_idl_cache_entry_t *matched_idl_entry;
310 int rc = LDAP_NO_SUCH_OBJECT;
312 DBT2bv( key, &idl_tmp.kstr );
314 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
315 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
317 if ( matched_idl_entry != NULL ) {
318 if ( matched_idl_entry->idl && ids )
319 BDB_IDL_CPY( ids, matched_idl_entry->idl );
320 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
321 IDL_LRU_DELETE( bdb, matched_idl_entry );
322 IDL_LRU_ADD( bdb, matched_idl_entry );
323 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
324 if ( matched_idl_entry->idl )
329 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
336 struct bdb_info *bdb,
342 bdb_idl_cache_entry_t idl_tmp;
343 bdb_idl_cache_entry_t *ee;
345 if ( rc == DB_NOTFOUND || BDB_IDL_IS_ZERO( ids ))
348 DBT2bv( key, &idl_tmp.kstr );
350 ee = (bdb_idl_cache_entry_t *) ch_malloc(
351 sizeof( bdb_idl_cache_entry_t ) );
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 ) {
377 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
378 "AVL delete failed\n",
381 IDL_LRU_DELETE( bdb, ee );
383 --bdb->bi_idl_cache_size;
384 ch_free( ee->kstr.bv_val );
390 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
391 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
396 struct bdb_info *bdb,
400 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
401 DBT2bv( key, &idl_tmp.kstr );
403 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
404 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
406 if ( matched_idl_entry != NULL ) {
407 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
408 bdb_idl_entry_cmp ) == NULL ) {
409 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
410 "AVL delete failed\n",
413 --bdb->bi_idl_cache_size;
414 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
415 IDL_LRU_DELETE( bdb, matched_idl_entry );
416 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
417 free( matched_idl_entry->kstr.bv_val );
418 if ( matched_idl_entry->idl )
419 free( matched_idl_entry->idl );
420 free( matched_idl_entry );
422 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
426 bdb_idl_cache_add_id(
427 struct bdb_info *bdb,
432 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
433 DBT2bv( key, &idl_tmp.kstr );
435 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
436 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
438 if ( cache_entry != NULL ) {
439 if ( !BDB_IDL_IS_RANGE( cache_entry->idl ) &&
440 cache_entry->idl[0] < BDB_IDL_DB_MAX ) {
441 size_t s = BDB_IDL_SIZEOF( cache_entry->idl ) + sizeof(ID);
442 cache_entry->idl = ch_realloc( cache_entry->idl, s );
444 bdb_idl_insert( cache_entry->idl, id );
446 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
450 bdb_idl_cache_del_id(
451 struct bdb_info *bdb,
456 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
457 DBT2bv( key, &idl_tmp.kstr );
459 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
460 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
462 if ( cache_entry != NULL ) {
463 bdb_idl_delete( cache_entry->idl, id );
464 if ( cache_entry->idl[0] == 0 ) {
465 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) cache_entry,
466 bdb_idl_entry_cmp ) == NULL ) {
467 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
468 "AVL delete failed\n",
471 --bdb->bi_idl_cache_size;
472 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
473 IDL_LRU_DELETE( bdb, cache_entry );
474 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
475 free( cache_entry->kstr.bv_val );
476 free( cache_entry->idl );
480 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
493 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
495 DBT data, key2, *kptr;
501 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
504 /* If using BerkeleyDB 4.0, the buf must be large enough to
505 * grab the entire IDL in one get(), otherwise BDB will leak
506 * resources on subsequent get's. We can safely call get()
507 * twice - once for the data, and once to get the DB_NOTFOUND
508 * result meaning there's no more data. See ITS#2040 for details.
509 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
510 * stack space is too limited.
512 * configure now requires Berkeley DB 4.1.
514 #if DB_VERSION_FULL < 0x04010000
515 # define BDB_ENOUGH 5
517 /* We sometimes test with tiny IDLs, and BDB always wants buffers
518 * that are at least one page in size.
520 # if BDB_IDL_DB_SIZE < 4096
521 # define BDB_ENOUGH 2048
523 # define BDB_ENOUGH 1
526 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
530 Debug( LDAP_DEBUG_ARGS,
531 "bdb_idl_fetch_key: %s\n",
532 bdb_show_key( key, keybuf ), 0, 0 );
534 assert( ids != NULL );
536 if ( saved_cursor && *saved_cursor ) {
538 } else if ( get_flag == LDAP_FILTER_GE ) {
539 opflag = DB_SET_RANGE;
540 } else if ( get_flag == LDAP_FILTER_LE ) {
546 /* only non-range lookups can use the IDL cache */
547 if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
548 rc = bdb_idl_cache_get( bdb, db, key, ids );
549 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
555 data.ulen = sizeof(buf);
556 data.flags = DB_DBT_USERMEM;
558 /* If we're not reusing an existing cursor, get a new one */
559 if( opflag != DB_NEXT ) {
560 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
562 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
563 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
567 cursor = *saved_cursor;
570 /* If this is a LE lookup, save original key so we can determine
571 * when to stop. If this is a GE lookup, save the key since it
572 * will be overwritten.
574 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
576 key2.flags = DB_DBT_USERMEM;
577 key2.ulen = sizeof(keybuf);
579 key2.size = key->size;
580 AC_MEMCPY( keybuf, key->data, key->size );
586 rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
588 /* skip presence key on range inequality lookups */
589 while (rc == 0 && kptr->size != len) {
590 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
592 /* If we're doing a LE compare and the new key is greater than
593 * our search key, we're done
595 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
596 key->data, key->size ) > 0 ) {
604 DB_MULTIPLE_INIT( ptr, &data );
606 DB_MULTIPLE_NEXT(ptr, &data, j, len);
612 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
614 if ( rc == DB_NOTFOUND ) rc = 0;
616 /* On disk, a range is denoted by 0 in the first element */
618 if (ids[0] != BDB_IDL_RANGE_SIZE) {
619 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
620 "range size mismatch: expected %d, got %ld\n",
621 BDB_IDL_RANGE_SIZE, ids[0], 0 );
622 cursor->c_close( cursor );
625 BDB_IDL_RANGE( ids, ids[2], ids[3] );
627 data.size = BDB_IDL_SIZEOF(ids);
630 if ( saved_cursor && rc == 0 ) {
631 if ( !*saved_cursor )
632 *saved_cursor = cursor;
636 rc2 = cursor->c_close( cursor );
638 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
639 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
643 if( rc == DB_NOTFOUND ) {
646 } else if( rc != 0 ) {
647 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
648 "get failed: %s (%d)\n",
649 db_strerror(rc), rc, 0 );
652 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
653 /* size not multiple of ID size */
654 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
655 "odd size: expected %ld multiple, got %ld\n",
656 (long) sizeof( ID ), (long) data.size, 0 );
659 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
661 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
662 "get size mismatch: expected %ld, got %ld\n",
663 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
667 if ( bdb->bi_idl_cache_max_size ) {
668 bdb_idl_cache_put( bdb, db, key, ids, rc );
683 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
687 ID lo, hi, nlo, nhi, nid;
692 Debug( LDAP_DEBUG_ARGS,
693 "bdb_idl_insert_key: %lx %s\n",
694 (long) id, bdb_show_key( key, buf ), 0 );
697 assert( id != NOID );
699 if ( bdb->bi_idl_cache_size ) {
700 bdb_idl_cache_del( bdb, db, key );
704 data.size = sizeof( ID );
705 data.ulen = data.size;
706 data.flags = DB_DBT_USERMEM;
708 BDB_ID2DISK( id, &nid );
710 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
712 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
713 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
717 /* Fetch the first data item for this key, to see if it
718 * exists and if it's a range.
720 rc = cursor->c_get( cursor, key, &data, DB_SET );
724 /* not a range, count the number of items */
726 rc = cursor->c_count( cursor, &count, 0 );
731 if ( count >= BDB_IDL_DB_MAX ) {
732 /* No room, convert to a range */
736 key2.dlen = key2.ulen;
737 key2.flags |= DB_DBT_PARTIAL;
739 BDB_DISK2ID( &nlo, &lo );
742 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
743 if ( rc != 0 && rc != DB_NOTFOUND ) {
744 err = "c_get next_nodup";
747 if ( rc == DB_NOTFOUND ) {
748 rc = cursor->c_get( cursor, key, &data, DB_LAST );
754 rc = cursor->c_get( cursor, key, &data, DB_PREV );
760 BDB_DISK2ID( &nhi, &hi );
761 /* Update hi/lo if needed, then delete all the items
767 } else if ( id > hi ) {
772 /* Don't fetch anything, just position cursor */
773 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
774 data.dlen = data.ulen = 0;
775 rc = cursor->c_get( cursor, key, &data, DB_SET );
780 rc = cursor->c_del( cursor, 0 );
782 err = "c_del range1";
785 /* Delete all the records */
786 for ( i=1; i<count; i++ ) {
787 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_DUP );
789 err = "c_get next_dup";
792 rc = cursor->c_del( cursor, 0 );
798 /* Store the range marker */
799 data.size = data.ulen = sizeof(ID);
800 data.flags = DB_DBT_USERMEM;
802 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
808 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
814 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
820 /* There's room, just store it */
824 /* It's a range, see if we need to rewrite
829 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
834 BDB_DISK2ID( &nlo, &lo );
837 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
842 BDB_DISK2ID( &nhi, &hi );
844 if ( id < lo || id > hi ) {
845 /* Delete the current lo/hi */
846 rc = cursor->c_del( cursor, 0 );
852 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
859 } else if ( rc == DB_NOTFOUND ) {
860 put1: data.data = &nid;
861 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
862 /* Don't worry if it's already there */
863 if ( rc != 0 && rc != DB_KEYEXIST ) {
868 /* initial c_get failed, nothing was done */
870 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
871 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
872 cursor->c_close( cursor );
875 rc = cursor->c_close( cursor );
877 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
878 "c_close failed: %s (%d)\n",
879 db_strerror(rc), rc, 0 );
892 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
896 ID lo, hi, tmp, nid, nlo, nhi;
901 Debug( LDAP_DEBUG_ARGS,
902 "bdb_idl_delete_key: %lx %s\n",
903 (long) id, bdb_show_key( key, buf ), 0 );
905 assert( id != NOID );
907 if ( bdb->bi_idl_cache_max_size ) {
908 bdb_idl_cache_del( bdb, db, key );
911 BDB_ID2DISK( id, &nid );
915 data.size = sizeof( id );
916 data.ulen = data.size;
917 data.flags = DB_DBT_USERMEM;
919 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
921 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
922 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
925 /* Fetch the first data item for this key, to see if it
926 * exists and if it's a range.
928 rc = cursor->c_get( cursor, key, &data, DB_SET );
932 /* Not a range, just delete it */
934 /* position to correct item */
936 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
942 rc = cursor->c_del( cursor, 0 );
948 /* It's a range, see if we need to rewrite
952 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
957 BDB_DISK2ID( &nlo, &lo );
959 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
964 BDB_DISK2ID( &nhi, &hi );
965 if ( id == lo || id == hi ) {
969 } else if ( id == hi ) {
974 /* The range has collapsed... */
975 rc = db->del( db, tid, key, 0 );
982 /* reposition on lo slot */
984 cursor->c_get( cursor, key, &data, DB_PREV );
986 rc = cursor->c_del( cursor, 0 );
993 BDB_ID2DISK( id, &nid );
995 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
1004 /* initial c_get failed, nothing was done */
1006 if ( rc != DB_NOTFOUND ) {
1007 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
1008 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
1010 cursor->c_close( cursor );
1013 rc = cursor->c_close( cursor );
1015 Debug( LDAP_DEBUG_ANY,
1016 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
1017 db_strerror(rc), rc, 0 );
1025 * idl_intersection - return a = a intersection b
1028 bdb_idl_intersection(
1034 ID cursora = 0, cursorb = 0, cursorc;
1037 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
1042 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1043 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1044 if ( idmin > idmax ) {
1047 } else if ( idmin == idmax ) {
1053 if ( BDB_IDL_IS_RANGE( a ) ) {
1054 if ( BDB_IDL_IS_RANGE(b) ) {
1055 /* If both are ranges, just shrink the boundaries */
1060 /* Else swap so that b is the range, a is a list */
1068 /* If a range completely covers the list, the result is
1069 * just the list. If idmin to idmax is contiguous, just
1070 * turn it into a range.
1072 if ( BDB_IDL_IS_RANGE( b )
1073 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1074 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1075 if (idmax - idmin + 1 == a[0])
1084 /* Fine, do the intersection one element at a time.
1085 * First advance to idmin in both IDLs.
1087 cursora = cursorb = idmin;
1088 ida = bdb_idl_first( a, &cursora );
1089 idb = bdb_idl_first( b, &cursorb );
1092 while( ida <= idmax || idb <= idmax ) {
1095 ida = bdb_idl_next( a, &cursora );
1096 idb = bdb_idl_next( b, &cursorb );
1097 } else if ( ida < idb ) {
1098 ida = bdb_idl_next( a, &cursora );
1100 idb = bdb_idl_next( b, &cursorb );
1106 BDB_IDL_CPY( b, a );
1113 * idl_union - return a = a union b
1121 ID cursora = 0, cursorb = 0, cursorc;
1123 if ( BDB_IDL_IS_ZERO( b ) ) {
1127 if ( BDB_IDL_IS_ZERO( a ) ) {
1128 BDB_IDL_CPY( a, b );
1132 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1133 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1134 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1141 ida = bdb_idl_first( a, &cursora );
1142 idb = bdb_idl_first( b, &cursorb );
1146 /* The distinct elements of a are cat'd to b */
1147 while( ida != NOID || idb != NOID ) {
1149 if( ++cursorc > BDB_IDL_UM_MAX ) {
1153 ida = bdb_idl_next( a, &cursora );
1157 ida = bdb_idl_next( a, &cursora );
1158 idb = bdb_idl_next( b, &cursorb );
1162 /* b is copied back to a in sorted order */
1167 while (cursorb <= b[0] || cursorc <= a[0]) {
1172 if (cursorb <= b[0] && b[cursorb] < idb)
1173 a[cursora++] = b[cursorb++];
1186 * bdb_idl_notin - return a intersection ~b (or a minus b)
1195 ID cursora = 0, cursorb = 0;
1197 if( BDB_IDL_IS_ZERO( a ) ||
1198 BDB_IDL_IS_ZERO( b ) ||
1199 BDB_IDL_IS_RANGE( b ) )
1201 BDB_IDL_CPY( ids, a );
1205 if( BDB_IDL_IS_RANGE( a ) ) {
1206 BDB_IDL_CPY( ids, a );
1210 ida = bdb_idl_first( a, &cursora ),
1211 idb = bdb_idl_first( b, &cursorb );
1215 while( ida != NOID ) {
1216 if ( idb == NOID ) {
1217 /* we could shortcut this */
1218 ids[++ids[0]] = ida;
1219 ida = bdb_idl_next( a, &cursora );
1221 } else if ( ida < idb ) {
1222 ids[++ids[0]] = ida;
1223 ida = bdb_idl_next( a, &cursora );
1225 } else if ( ida > idb ) {
1226 idb = bdb_idl_next( b, &cursorb );
1229 ida = bdb_idl_next( a, &cursora );
1230 idb = bdb_idl_next( b, &cursorb );
1238 ID bdb_idl_first( ID *ids, ID *cursor )
1242 if ( ids[0] == 0 ) {
1247 if ( BDB_IDL_IS_RANGE( ids ) ) {
1248 if( *cursor < ids[1] ) {
1257 pos = bdb_idl_search( ids, *cursor );
1259 if( pos > ids[0] ) {
1267 ID bdb_idl_next( ID *ids, ID *cursor )
1269 if ( BDB_IDL_IS_RANGE( ids ) ) {
1270 if( ids[2] < ++(*cursor) ) {
1276 if ( ++(*cursor) <= ids[0] ) {
1277 return ids[*cursor];
1285 /* Add one ID to an unsorted list. We ensure that the first element is the
1286 * minimum and the last element is the maximum, for fast range compaction.
1287 * this means IDLs up to length 3 are always sorted...
1289 int bdb_idl_append_one( ID *ids, ID id )
1291 if (BDB_IDL_IS_RANGE( ids )) {
1292 /* if already in range, treat as a dup */
1293 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
1295 if (id < BDB_IDL_FIRST(ids))
1297 else if (id > BDB_IDL_LAST(ids))
1309 if ( ids[0] > 1 && id < ids[ids[0]] ) {
1316 if ( ids[0] >= BDB_IDL_UM_MAX ) {
1325 /* Append sorted list b to sorted list a. The result is unsorted but
1326 * a[1] is the min of the result and a[a[0]] is the max.
1328 int bdb_idl_append( ID *a, ID *b )
1330 ID ida, idb, tmp, swap = 0;
1332 if ( BDB_IDL_IS_ZERO( b ) ) {
1336 if ( BDB_IDL_IS_ZERO( a ) ) {
1337 BDB_IDL_CPY( a, b );
1341 ida = BDB_IDL_LAST( a );
1342 idb = BDB_IDL_LAST( b );
1343 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ||
1344 a[0] + b[0] >= BDB_IDL_UM_MAX ) {
1345 a[2] = IDL_MAX( ida, idb );
1346 a[1] = IDL_MIN( a[1], b[1] );
1351 if ( b[0] > 1 && ida > idb ) {
1357 if ( b[1] < a[1] ) {
1368 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1379 /* Quicksort + Insertion sort for small arrays */
1382 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1385 bdb_idl_sort( ID *ids, ID *tmp )
1387 int *istack = (int *)tmp;
1388 int i,j,k,l,ir,jstack;
1391 if ( BDB_IDL_IS_RANGE( ids ))
1398 if (ir - l < SMALL) { /* Insertion sort */
1399 for (j=l+1;j<=ir;j++) {
1401 for (i=j-1;i>=1;i--) {
1402 if (ids[i] <= a) break;
1407 if (jstack == 0) break;
1408 ir = istack[jstack--];
1409 l = istack[jstack--];
1411 k = (l + ir) >> 1; /* Choose median of left, center, right */
1412 SWAP(ids[k], ids[l+1]);
1413 if (ids[l] > ids[ir]) {
1414 SWAP(ids[l], ids[ir]);
1416 if (ids[l+1] > ids[ir]) {
1417 SWAP(ids[l+1], ids[ir]);
1419 if (ids[l] > ids[l+1]) {
1420 SWAP(ids[l], ids[l+1]);
1426 do i++; while(ids[i] < a);
1427 do j--; while(ids[j] > a);
1429 SWAP(ids[i],ids[j]);
1434 if (ir-i+1 >= j-1) {
1435 istack[jstack] = ir;
1436 istack[jstack-1] = i;
1439 istack[jstack] = j-1;
1440 istack[jstack-1] = l;
1449 /* 8 bit Radix sort + insertion sort
1451 * based on code from http://www.cubic.org/docs/radix.htm
1452 * with improvements by mbackes@symas.com and hyc@symas.com
1454 * This code is O(n) but has a relatively high constant factor. For lists
1455 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1456 * Much faster than quicksort for lists longer than ~100. Insertion
1457 * sort is actually superior for lists <50.
1460 #define BUCKETS (1<<8)
1464 bdb_idl_sort( ID *ids, ID *tmp )
1466 int count, soft_limit, phase = 0, size = ids[0];
1468 unsigned char *maxv = (unsigned char *)&ids[size];
1470 if ( BDB_IDL_IS_RANGE( ids ))
1473 /* Use insertion sort for small lists */
1474 if ( size <= SMALL ) {
1478 for (j=1;j<=size;j++) {
1480 for (i=j-1;i>=1;i--) {
1481 if (ids[i] <= a) break;
1493 #if BYTE_ORDER == BIG_ENDIAN
1494 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1496 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1500 #if BYTE_ORDER == BIG_ENDIAN
1501 count = sizeof(ID)-1; count >= soft_limit; --count
1503 count = 0; count <= soft_limit; ++count
1506 unsigned int num[BUCKETS], * np, n, sum;
1508 ID *sp, *source, *dest;
1509 unsigned char *bp, *source_start;
1511 source = idls[phase]+1;
1512 dest = idls[phase^1]+1;
1513 source_start = ((unsigned char *) source) + count;
1516 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1518 /* count occurences of every byte value */
1520 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1523 /* transform count into index by summing elements and storing
1528 for ( i = BUCKETS; i > 0; --i ) {
1534 /* fill dest with the right values in the right place */
1537 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1545 /* copy back from temp if needed */
1548 for ( count = 0; count < size; ++count )
1552 #endif /* Quick vs Radix */
1554 #endif /* BDB_HIER */