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 if ( cache_entry->idl )
477 free( cache_entry->idl );
481 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
494 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
496 DBT data, key2, *kptr;
502 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
505 /* If using BerkeleyDB 4.0, the buf must be large enough to
506 * grab the entire IDL in one get(), otherwise BDB will leak
507 * resources on subsequent get's. We can safely call get()
508 * twice - once for the data, and once to get the DB_NOTFOUND
509 * result meaning there's no more data. See ITS#2040 for details.
510 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
511 * stack space is too limited.
513 * configure now requires Berkeley DB 4.1.
515 #if DB_VERSION_FULL < 0x04010000
516 # define BDB_ENOUGH 5
518 /* We sometimes test with tiny IDLs, and BDB always wants buffers
519 * that are at least one page in size.
521 # if BDB_IDL_DB_SIZE < 4096
522 # define BDB_ENOUGH 2048
524 # define BDB_ENOUGH 1
527 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
531 Debug( LDAP_DEBUG_ARGS,
532 "bdb_idl_fetch_key: %s\n",
533 bdb_show_key( key, keybuf ), 0, 0 );
535 assert( ids != NULL );
537 if ( saved_cursor && *saved_cursor ) {
539 } else if ( get_flag == LDAP_FILTER_GE ) {
540 opflag = DB_SET_RANGE;
541 } else if ( get_flag == LDAP_FILTER_LE ) {
547 /* only non-range lookups can use the IDL cache */
548 if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
549 rc = bdb_idl_cache_get( bdb, db, key, ids );
550 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
556 data.ulen = sizeof(buf);
557 data.flags = DB_DBT_USERMEM;
559 /* If we're not reusing an existing cursor, get a new one */
560 if( opflag != DB_NEXT ) {
561 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
563 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
564 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
568 cursor = *saved_cursor;
571 /* If this is a LE lookup, save original key so we can determine
572 * when to stop. If this is a GE lookup, save the key since it
573 * will be overwritten.
575 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
577 key2.flags = DB_DBT_USERMEM;
578 key2.ulen = sizeof(keybuf);
580 key2.size = key->size;
581 AC_MEMCPY( keybuf, key->data, key->size );
587 rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
589 /* skip presence key on range inequality lookups */
590 while (rc == 0 && kptr->size != len) {
591 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
593 /* If we're doing a LE compare and the new key is greater than
594 * our search key, we're done
596 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
597 key->data, key->size ) > 0 ) {
605 DB_MULTIPLE_INIT( ptr, &data );
607 DB_MULTIPLE_NEXT(ptr, &data, j, len);
613 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
615 if ( rc == DB_NOTFOUND ) rc = 0;
617 /* On disk, a range is denoted by 0 in the first element */
619 if (ids[0] != BDB_IDL_RANGE_SIZE) {
620 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
621 "range size mismatch: expected %d, got %ld\n",
622 BDB_IDL_RANGE_SIZE, ids[0], 0 );
623 cursor->c_close( cursor );
626 BDB_IDL_RANGE( ids, ids[2], ids[3] );
628 data.size = BDB_IDL_SIZEOF(ids);
631 if ( saved_cursor && rc == 0 ) {
632 if ( !*saved_cursor )
633 *saved_cursor = cursor;
637 rc2 = cursor->c_close( cursor );
639 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
640 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
644 if( rc == DB_NOTFOUND ) {
647 } else if( rc != 0 ) {
648 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
649 "get failed: %s (%d)\n",
650 db_strerror(rc), rc, 0 );
653 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
654 /* size not multiple of ID size */
655 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
656 "odd size: expected %ld multiple, got %ld\n",
657 (long) sizeof( ID ), (long) data.size, 0 );
660 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
662 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
663 "get size mismatch: expected %ld, got %ld\n",
664 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
668 if ( bdb->bi_idl_cache_max_size ) {
669 bdb_idl_cache_put( bdb, db, key, ids, rc );
684 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
688 ID lo, hi, nlo, nhi, nid;
693 Debug( LDAP_DEBUG_ARGS,
694 "bdb_idl_insert_key: %lx %s\n",
695 (long) id, bdb_show_key( key, buf ), 0 );
698 assert( id != NOID );
700 if ( bdb->bi_idl_cache_size ) {
701 bdb_idl_cache_del( bdb, db, key );
705 data.size = sizeof( ID );
706 data.ulen = data.size;
707 data.flags = DB_DBT_USERMEM;
709 BDB_ID2DISK( id, &nid );
711 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
713 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
714 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
718 /* Fetch the first data item for this key, to see if it
719 * exists and if it's a range.
721 rc = cursor->c_get( cursor, key, &data, DB_SET );
725 /* not a range, count the number of items */
727 rc = cursor->c_count( cursor, &count, 0 );
732 if ( count >= BDB_IDL_DB_MAX ) {
733 /* No room, convert to a range */
737 key2.dlen = key2.ulen;
738 key2.flags |= DB_DBT_PARTIAL;
740 BDB_DISK2ID( &nlo, &lo );
743 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
744 if ( rc != 0 && rc != DB_NOTFOUND ) {
745 err = "c_get next_nodup";
748 if ( rc == DB_NOTFOUND ) {
749 rc = cursor->c_get( cursor, key, &data, DB_LAST );
755 rc = cursor->c_get( cursor, key, &data, DB_PREV );
761 BDB_DISK2ID( &nhi, &hi );
762 /* Update hi/lo if needed, then delete all the items
768 } else if ( id > hi ) {
773 /* Don't fetch anything, just position cursor */
774 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
775 data.dlen = data.ulen = 0;
776 rc = cursor->c_get( cursor, key, &data, DB_SET );
781 rc = cursor->c_del( cursor, 0 );
783 err = "c_del range1";
786 /* Delete all the records */
787 for ( i=1; i<count; i++ ) {
788 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_DUP );
790 err = "c_get next_dup";
793 rc = cursor->c_del( cursor, 0 );
799 /* Store the range marker */
800 data.size = data.ulen = sizeof(ID);
801 data.flags = DB_DBT_USERMEM;
803 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
809 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
815 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
821 /* There's room, just store it */
825 /* It's a range, see if we need to rewrite
830 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
835 BDB_DISK2ID( &nlo, &lo );
838 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
843 BDB_DISK2ID( &nhi, &hi );
845 if ( id < lo || id > hi ) {
846 /* Delete the current lo/hi */
847 rc = cursor->c_del( cursor, 0 );
853 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
860 } else if ( rc == DB_NOTFOUND ) {
861 put1: data.data = &nid;
862 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
863 /* Don't worry if it's already there */
864 if ( rc != 0 && rc != DB_KEYEXIST ) {
869 /* initial c_get failed, nothing was done */
871 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
872 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
873 cursor->c_close( cursor );
876 rc = cursor->c_close( cursor );
878 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
879 "c_close failed: %s (%d)\n",
880 db_strerror(rc), rc, 0 );
893 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
897 ID lo, hi, tmp, nid, nlo, nhi;
902 Debug( LDAP_DEBUG_ARGS,
903 "bdb_idl_delete_key: %lx %s\n",
904 (long) id, bdb_show_key( key, buf ), 0 );
906 assert( id != NOID );
908 if ( bdb->bi_idl_cache_max_size ) {
909 bdb_idl_cache_del( bdb, db, key );
912 BDB_ID2DISK( id, &nid );
916 data.size = sizeof( id );
917 data.ulen = data.size;
918 data.flags = DB_DBT_USERMEM;
920 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
922 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
923 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
926 /* Fetch the first data item for this key, to see if it
927 * exists and if it's a range.
929 rc = cursor->c_get( cursor, key, &data, DB_SET );
933 /* Not a range, just delete it */
935 /* position to correct item */
937 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
943 rc = cursor->c_del( cursor, 0 );
949 /* It's a range, see if we need to rewrite
953 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
958 BDB_DISK2ID( &nlo, &lo );
960 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
965 BDB_DISK2ID( &nhi, &hi );
966 if ( id == lo || id == hi ) {
970 } else if ( id == hi ) {
975 /* The range has collapsed... */
976 rc = db->del( db, tid, key, 0 );
983 /* reposition on lo slot */
985 cursor->c_get( cursor, key, &data, DB_PREV );
987 rc = cursor->c_del( cursor, 0 );
994 BDB_ID2DISK( id, &nid );
996 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
1005 /* initial c_get failed, nothing was done */
1007 if ( rc != DB_NOTFOUND ) {
1008 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
1009 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
1011 cursor->c_close( cursor );
1014 rc = cursor->c_close( cursor );
1016 Debug( LDAP_DEBUG_ANY,
1017 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
1018 db_strerror(rc), rc, 0 );
1026 * idl_intersection - return a = a intersection b
1029 bdb_idl_intersection(
1035 ID cursora = 0, cursorb = 0, cursorc;
1038 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
1043 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1044 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1045 if ( idmin > idmax ) {
1048 } else if ( idmin == idmax ) {
1054 if ( BDB_IDL_IS_RANGE( a ) ) {
1055 if ( BDB_IDL_IS_RANGE(b) ) {
1056 /* If both are ranges, just shrink the boundaries */
1061 /* Else swap so that b is the range, a is a list */
1069 /* If a range completely covers the list, the result is
1070 * just the list. If idmin to idmax is contiguous, just
1071 * turn it into a range.
1073 if ( BDB_IDL_IS_RANGE( b )
1074 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1075 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1076 if (idmax - idmin + 1 == a[0])
1085 /* Fine, do the intersection one element at a time.
1086 * First advance to idmin in both IDLs.
1088 cursora = cursorb = idmin;
1089 ida = bdb_idl_first( a, &cursora );
1090 idb = bdb_idl_first( b, &cursorb );
1093 while( ida <= idmax || idb <= idmax ) {
1096 ida = bdb_idl_next( a, &cursora );
1097 idb = bdb_idl_next( b, &cursorb );
1098 } else if ( ida < idb ) {
1099 ida = bdb_idl_next( a, &cursora );
1101 idb = bdb_idl_next( b, &cursorb );
1107 BDB_IDL_CPY( b, a );
1114 * idl_union - return a = a union b
1122 ID cursora = 0, cursorb = 0, cursorc;
1124 if ( BDB_IDL_IS_ZERO( b ) ) {
1128 if ( BDB_IDL_IS_ZERO( a ) ) {
1129 BDB_IDL_CPY( a, b );
1133 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1134 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1135 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1142 ida = bdb_idl_first( a, &cursora );
1143 idb = bdb_idl_first( b, &cursorb );
1147 /* The distinct elements of a are cat'd to b */
1148 while( ida != NOID || idb != NOID ) {
1150 if( ++cursorc > BDB_IDL_UM_MAX ) {
1154 ida = bdb_idl_next( a, &cursora );
1158 ida = bdb_idl_next( a, &cursora );
1159 idb = bdb_idl_next( b, &cursorb );
1163 /* b is copied back to a in sorted order */
1168 while (cursorb <= b[0] || cursorc <= a[0]) {
1173 if (cursorb <= b[0] && b[cursorb] < idb)
1174 a[cursora++] = b[cursorb++];
1187 * bdb_idl_notin - return a intersection ~b (or a minus b)
1196 ID cursora = 0, cursorb = 0;
1198 if( BDB_IDL_IS_ZERO( a ) ||
1199 BDB_IDL_IS_ZERO( b ) ||
1200 BDB_IDL_IS_RANGE( b ) )
1202 BDB_IDL_CPY( ids, a );
1206 if( BDB_IDL_IS_RANGE( a ) ) {
1207 BDB_IDL_CPY( ids, a );
1211 ida = bdb_idl_first( a, &cursora ),
1212 idb = bdb_idl_first( b, &cursorb );
1216 while( ida != NOID ) {
1217 if ( idb == NOID ) {
1218 /* we could shortcut this */
1219 ids[++ids[0]] = ida;
1220 ida = bdb_idl_next( a, &cursora );
1222 } else if ( ida < idb ) {
1223 ids[++ids[0]] = ida;
1224 ida = bdb_idl_next( a, &cursora );
1226 } else if ( ida > idb ) {
1227 idb = bdb_idl_next( b, &cursorb );
1230 ida = bdb_idl_next( a, &cursora );
1231 idb = bdb_idl_next( b, &cursorb );
1239 ID bdb_idl_first( ID *ids, ID *cursor )
1243 if ( ids[0] == 0 ) {
1248 if ( BDB_IDL_IS_RANGE( ids ) ) {
1249 if( *cursor < ids[1] ) {
1258 pos = bdb_idl_search( ids, *cursor );
1260 if( pos > ids[0] ) {
1268 ID bdb_idl_next( ID *ids, ID *cursor )
1270 if ( BDB_IDL_IS_RANGE( ids ) ) {
1271 if( ids[2] < ++(*cursor) ) {
1277 if ( ++(*cursor) <= ids[0] ) {
1278 return ids[*cursor];
1286 /* Add one ID to an unsorted list. We ensure that the first element is the
1287 * minimum and the last element is the maximum, for fast range compaction.
1288 * this means IDLs up to length 3 are always sorted...
1290 int bdb_idl_append_one( ID *ids, ID id )
1292 if (BDB_IDL_IS_RANGE( ids )) {
1293 /* if already in range, treat as a dup */
1294 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
1296 if (id < BDB_IDL_FIRST(ids))
1298 else if (id > BDB_IDL_LAST(ids))
1310 if ( ids[0] > 1 && id < ids[ids[0]] ) {
1317 if ( ids[0] >= BDB_IDL_UM_MAX ) {
1326 /* Append sorted list b to sorted list a. The result is unsorted but
1327 * a[1] is the min of the result and a[a[0]] is the max.
1329 int bdb_idl_append( ID *a, ID *b )
1331 ID ida, idb, tmp, swap = 0;
1333 if ( BDB_IDL_IS_ZERO( b ) ) {
1337 if ( BDB_IDL_IS_ZERO( a ) ) {
1338 BDB_IDL_CPY( a, b );
1342 ida = BDB_IDL_LAST( a );
1343 idb = BDB_IDL_LAST( b );
1344 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ||
1345 a[0] + b[0] >= BDB_IDL_UM_MAX ) {
1346 a[2] = IDL_MAX( ida, idb );
1347 a[1] = IDL_MIN( a[1], b[1] );
1352 if ( b[0] > 1 && ida > idb ) {
1358 if ( b[1] < a[1] ) {
1369 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1380 /* Quicksort + Insertion sort for small arrays */
1383 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1386 bdb_idl_sort( ID *ids, ID *tmp )
1388 int *istack = (int *)tmp;
1389 int i,j,k,l,ir,jstack;
1392 if ( BDB_IDL_IS_RANGE( ids ))
1399 if (ir - l < SMALL) { /* Insertion sort */
1400 for (j=l+1;j<=ir;j++) {
1402 for (i=j-1;i>=1;i--) {
1403 if (ids[i] <= a) break;
1408 if (jstack == 0) break;
1409 ir = istack[jstack--];
1410 l = istack[jstack--];
1412 k = (l + ir) >> 1; /* Choose median of left, center, right */
1413 SWAP(ids[k], ids[l+1]);
1414 if (ids[l] > ids[ir]) {
1415 SWAP(ids[l], ids[ir]);
1417 if (ids[l+1] > ids[ir]) {
1418 SWAP(ids[l+1], ids[ir]);
1420 if (ids[l] > ids[l+1]) {
1421 SWAP(ids[l], ids[l+1]);
1427 do i++; while(ids[i] < a);
1428 do j--; while(ids[j] > a);
1430 SWAP(ids[i],ids[j]);
1435 if (ir-i+1 >= j-1) {
1436 istack[jstack] = ir;
1437 istack[jstack-1] = i;
1440 istack[jstack] = j-1;
1441 istack[jstack-1] = l;
1450 /* 8 bit Radix sort + insertion sort
1452 * based on code from http://www.cubic.org/docs/radix.htm
1453 * with improvements by mbackes@symas.com and hyc@symas.com
1455 * This code is O(n) but has a relatively high constant factor. For lists
1456 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1457 * Much faster than quicksort for lists longer than ~100. Insertion
1458 * sort is actually superior for lists <50.
1461 #define BUCKETS (1<<8)
1465 bdb_idl_sort( ID *ids, ID *tmp )
1467 int count, soft_limit, phase = 0, size = ids[0];
1469 unsigned char *maxv = (unsigned char *)&ids[size];
1471 if ( BDB_IDL_IS_RANGE( ids ))
1474 /* Use insertion sort for small lists */
1475 if ( size <= SMALL ) {
1479 for (j=1;j<=size;j++) {
1481 for (i=j-1;i>=1;i--) {
1482 if (ids[i] <= a) break;
1494 #if BYTE_ORDER == BIG_ENDIAN
1495 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1497 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1501 #if BYTE_ORDER == BIG_ENDIAN
1502 count = sizeof(ID)-1; count >= soft_limit; --count
1504 count = 0; count <= soft_limit; ++count
1507 unsigned int num[BUCKETS], * np, n, sum;
1509 ID *sp, *source, *dest;
1510 unsigned char *bp, *source_start;
1512 source = idls[phase]+1;
1513 dest = idls[phase^1]+1;
1514 source_start = ((unsigned char *) source) + count;
1517 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1519 /* count occurences of every byte value */
1521 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1524 /* transform count into index by summing elements and storing
1529 for ( i = BUCKETS; i > 0; --i ) {
1535 /* fill dest with the right values in the right place */
1538 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1546 /* copy back from temp if needed */
1549 for ( count = 0; count < size; ++count )
1553 #endif /* Quick vs Radix */
1555 #endif /* BDB_HIER */