1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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 == bdb->bi_idl_lru_head ) bdb->bi_idl_lru_head = e->idl_lru_next; \
32 if ( e == bdb->bi_idl_lru_tail ) bdb->bi_idl_lru_tail = e->idl_lru_prev; \
33 e->idl_lru_next->idl_lru_prev = e->idl_lru_prev; \
34 e->idl_lru_prev->idl_lru_next = e->idl_lru_next; \
38 bdb_idl_entry_cmp( const void *v_idl1, const void *v_idl2 )
40 const bdb_idl_cache_entry_t *idl1 = v_idl1, *idl2 = v_idl2;
43 if ((rc = SLAP_PTRCMP( idl1->db, idl2->db ))) return rc;
44 if ((rc = idl1->kstr.bv_len - idl2->kstr.bv_len )) return rc;
45 return ( memcmp ( idl1->kstr.bv_val, idl2->kstr.bv_val , idl1->kstr.bv_len ) );
49 static void idl_check( ID *ids )
51 if( BDB_IDL_IS_RANGE( ids ) ) {
52 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
55 for( i=1; i < ids[0]; i++ ) {
56 assert( ids[i+1] > ids[i] );
62 static void idl_dump( ID *ids )
64 if( BDB_IDL_IS_RANGE( ids ) ) {
65 Debug( LDAP_DEBUG_ANY,
66 "IDL: range ( %ld - %ld )\n",
67 (long) BDB_IDL_RANGE_FIRST( ids ),
68 (long) BDB_IDL_RANGE_LAST( ids ) );
72 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
74 for( i=1; i<=ids[0]; i++ ) {
76 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
78 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
81 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
86 #endif /* IDL_DEBUG > 1 */
87 #endif /* IDL_DEBUG > 0 */
89 unsigned bdb_idl_search( ID *ids, ID id )
91 #define IDL_BINARY_SEARCH 1
92 #ifdef IDL_BINARY_SEARCH
94 * binary search of id in ids
95 * if found, returns position of id
96 * if not found, returns first postion greater than id
109 cursor = base + pivot;
110 val = IDL_CMP( id, ids[cursor + 1] );
115 } else if ( val > 0 ) {
131 /* (reverse) linear search */
138 for( i=ids[0]; i; i-- ) {
148 int bdb_idl_insert( ID *ids, ID id )
153 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
159 if (BDB_IDL_IS_RANGE( ids )) {
160 /* if already in range, treat as a dup */
161 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
163 if (id < BDB_IDL_FIRST(ids))
165 else if (id > BDB_IDL_LAST(ids))
170 x = bdb_idl_search( ids, id );
178 if ( x <= ids[0] && ids[x] == id ) {
183 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
186 ids[2] = ids[ids[0]-1];
187 } else if ( ids[ids[0]-1] < id ) {
190 ids[2] = ids[ids[0]-1];
196 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
209 static int bdb_idl_delete( ID *ids, ID id )
214 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
220 if (BDB_IDL_IS_RANGE( ids )) {
221 /* If deleting a range boundary, adjust */
224 else if ( ids[2] == id )
226 /* deleting from inside a range is a no-op */
228 /* If the range has collapsed, re-adjust */
229 if ( ids[1] > ids[2] )
231 else if ( ids[1] == ids[2] )
236 x = bdb_idl_search( ids, id );
244 if( x > ids[0] || ids[x] != id ) {
248 } else if ( --ids[0] == 0 ) {
254 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
271 if ( key->size == 4 /* LUTIL_HASH_BYTES */ ) {
272 unsigned char *c = key->data;
273 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
280 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
281 * copy the cached IDL into it, otherwise just return the status.
285 struct bdb_info *bdb,
290 bdb_idl_cache_entry_t idl_tmp;
291 bdb_idl_cache_entry_t *matched_idl_entry;
292 int rc = LDAP_NO_SUCH_OBJECT;
294 DBT2bv( key, &idl_tmp.kstr );
296 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
297 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
299 if ( matched_idl_entry != NULL ) {
300 if ( matched_idl_entry->idl && ids )
301 BDB_IDL_CPY( ids, matched_idl_entry->idl );
302 matched_idl_entry->idl_flags |= CACHE_ENTRY_REFERENCED;
303 if ( matched_idl_entry->idl )
308 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
315 struct bdb_info *bdb,
321 bdb_idl_cache_entry_t idl_tmp;
322 bdb_idl_cache_entry_t *ee, *eprev;
324 if ( rc == DB_NOTFOUND || BDB_IDL_IS_ZERO( ids ))
327 DBT2bv( key, &idl_tmp.kstr );
329 ee = (bdb_idl_cache_entry_t *) ch_malloc(
330 sizeof( bdb_idl_cache_entry_t ) );
332 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
333 BDB_IDL_CPY( ee->idl, ids );
335 ee->idl_lru_prev = NULL;
336 ee->idl_lru_next = NULL;
338 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
339 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
340 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
341 bdb_idl_entry_cmp, avl_dup_error ))
343 ch_free( ee->kstr.bv_val );
346 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
349 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
351 if ( bdb->bi_idl_lru_head ) {
352 ee->idl_lru_next = bdb->bi_idl_lru_head;
353 ee->idl_lru_prev = bdb->bi_idl_lru_head->idl_lru_prev;
354 bdb->bi_idl_lru_head->idl_lru_prev->idl_lru_next = ee;
355 bdb->bi_idl_lru_head->idl_lru_prev = ee;
357 ee->idl_lru_next = ee->idl_lru_prev = ee;
358 bdb->bi_idl_lru_tail = ee;
360 bdb->bi_idl_lru_head = ee;
362 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
364 ee = bdb->bi_idl_lru_tail;
365 for ( i = 0; i < 10; i++, ee = eprev ) {
366 eprev = ee->idl_lru_prev;
367 if ( ee->idl_flags & CACHE_ENTRY_REFERENCED ) {
368 ee->idl_flags ^= CACHE_ENTRY_REFERENCED;
371 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
372 bdb_idl_entry_cmp ) == NULL ) {
373 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
374 "AVL delete failed\n",
377 IDL_LRU_DELETE( bdb, ee );
379 --bdb->bi_idl_cache_size;
380 ch_free( ee->kstr.bv_val );
384 bdb->bi_idl_lru_tail = eprev;
386 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
387 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
392 struct bdb_info *bdb,
396 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
397 DBT2bv( key, &idl_tmp.kstr );
399 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
400 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
402 if ( matched_idl_entry != NULL ) {
403 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
404 bdb_idl_entry_cmp ) == NULL ) {
405 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
406 "AVL delete failed\n",
409 --bdb->bi_idl_cache_size;
410 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
411 IDL_LRU_DELETE( bdb, matched_idl_entry );
412 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
413 free( matched_idl_entry->kstr.bv_val );
414 if ( matched_idl_entry->idl )
415 free( matched_idl_entry->idl );
416 free( matched_idl_entry );
418 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
422 bdb_idl_cache_add_id(
423 struct bdb_info *bdb,
428 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
429 DBT2bv( key, &idl_tmp.kstr );
431 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
432 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
434 if ( cache_entry != NULL ) {
435 if ( !BDB_IDL_IS_RANGE( cache_entry->idl ) &&
436 cache_entry->idl[0] < BDB_IDL_DB_MAX ) {
437 size_t s = BDB_IDL_SIZEOF( cache_entry->idl ) + sizeof(ID);
438 cache_entry->idl = ch_realloc( cache_entry->idl, s );
440 bdb_idl_insert( cache_entry->idl, id );
442 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
446 bdb_idl_cache_del_id(
447 struct bdb_info *bdb,
452 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
453 DBT2bv( key, &idl_tmp.kstr );
455 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
456 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
458 if ( cache_entry != NULL ) {
459 bdb_idl_delete( cache_entry->idl, id );
460 if ( cache_entry->idl[0] == 0 ) {
461 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) cache_entry,
462 bdb_idl_entry_cmp ) == NULL ) {
463 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
464 "AVL delete failed\n",
467 --bdb->bi_idl_cache_size;
468 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
469 IDL_LRU_DELETE( bdb, cache_entry );
470 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
471 free( cache_entry->kstr.bv_val );
472 free( cache_entry->idl );
476 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
489 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
491 DBT data, key2, *kptr;
497 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
500 /* If using BerkeleyDB 4.0, the buf must be large enough to
501 * grab the entire IDL in one get(), otherwise BDB will leak
502 * resources on subsequent get's. We can safely call get()
503 * twice - once for the data, and once to get the DB_NOTFOUND
504 * result meaning there's no more data. See ITS#2040 for details.
505 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
506 * stack space is too limited.
508 * configure now requires Berkeley DB 4.1.
510 #if DB_VERSION_FULL < 0x04010000
511 # define BDB_ENOUGH 5
513 /* We sometimes test with tiny IDLs, and BDB always wants buffers
514 * that are at least one page in size.
516 # if BDB_IDL_DB_SIZE < 4096
517 # define BDB_ENOUGH 2048
519 # define BDB_ENOUGH 1
522 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
526 Debug( LDAP_DEBUG_ARGS,
527 "bdb_idl_fetch_key: %s\n",
528 bdb_show_key( key, keybuf ), 0, 0 );
530 assert( ids != NULL );
532 if ( saved_cursor && *saved_cursor ) {
534 } else if ( get_flag == LDAP_FILTER_GE ) {
535 opflag = DB_SET_RANGE;
536 } else if ( get_flag == LDAP_FILTER_LE ) {
542 /* only non-range lookups can use the IDL cache */
543 if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
544 rc = bdb_idl_cache_get( bdb, db, key, ids );
545 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
551 data.ulen = sizeof(buf);
552 data.flags = DB_DBT_USERMEM;
554 /* If we're not reusing an existing cursor, get a new one */
555 if( opflag != DB_NEXT ) {
556 rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
558 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
559 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
562 cursor->locker = locker;
564 cursor = *saved_cursor;
567 /* If this is a LE lookup, save original key so we can determine
568 * when to stop. If this is a GE lookup, save the key since it
569 * will be overwritten.
571 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
573 key2.flags = DB_DBT_USERMEM;
574 key2.ulen = sizeof(keybuf);
576 key2.size = key->size;
577 AC_MEMCPY( keybuf, key->data, key->size );
583 rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
585 /* skip presence key on range inequality lookups */
586 while (rc == 0 && kptr->size != len) {
587 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
589 /* If we're doing a LE compare and the new key is greater than
590 * our search key, we're done
592 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
593 key->data, key->size ) > 0 ) {
601 DB_MULTIPLE_INIT( ptr, &data );
603 DB_MULTIPLE_NEXT(ptr, &data, j, len);
609 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
611 if ( rc == DB_NOTFOUND ) rc = 0;
613 /* On disk, a range is denoted by 0 in the first element */
615 if (ids[0] != BDB_IDL_RANGE_SIZE) {
616 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
617 "range size mismatch: expected %d, got %ld\n",
618 BDB_IDL_RANGE_SIZE, ids[0], 0 );
619 cursor->c_close( cursor );
622 BDB_IDL_RANGE( ids, ids[2], ids[3] );
624 data.size = BDB_IDL_SIZEOF(ids);
627 if ( saved_cursor && rc == 0 ) {
628 if ( !*saved_cursor )
629 *saved_cursor = cursor;
633 rc2 = cursor->c_close( cursor );
635 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
636 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
640 if( rc == DB_NOTFOUND ) {
643 } else if( rc != 0 ) {
644 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
645 "get failed: %s (%d)\n",
646 db_strerror(rc), rc, 0 );
649 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
650 /* size not multiple of ID size */
651 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
652 "odd size: expected %ld multiple, got %ld\n",
653 (long) sizeof( ID ), (long) data.size, 0 );
656 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
658 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
659 "get size mismatch: expected %ld, got %ld\n",
660 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
664 if ( bdb->bi_idl_cache_max_size ) {
665 bdb_idl_cache_put( bdb, db, key, ids, rc );
680 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
684 ID lo, hi, nlo, nhi, nid;
689 Debug( LDAP_DEBUG_ARGS,
690 "bdb_idl_insert_key: %lx %s\n",
691 (long) id, bdb_show_key( key, buf ), 0 );
694 assert( id != NOID );
696 if ( bdb->bi_idl_cache_size ) {
697 bdb_idl_cache_del( bdb, db, key );
701 data.size = sizeof( ID );
702 data.ulen = data.size;
703 data.flags = DB_DBT_USERMEM;
705 BDB_ID2DISK( id, &nid );
707 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
709 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
710 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
714 /* Fetch the first data item for this key, to see if it
715 * exists and if it's a range.
717 rc = cursor->c_get( cursor, key, &data, DB_SET );
721 /* not a range, count the number of items */
723 rc = cursor->c_count( cursor, &count, 0 );
728 if ( count >= BDB_IDL_DB_MAX ) {
729 /* No room, convert to a range */
733 key2.dlen = key2.ulen;
734 key2.flags |= DB_DBT_PARTIAL;
736 BDB_DISK2ID( &nlo, &lo );
739 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
740 if ( rc != 0 && rc != DB_NOTFOUND ) {
741 err = "c_get next_nodup";
744 if ( rc == DB_NOTFOUND ) {
745 rc = cursor->c_get( cursor, key, &data, DB_LAST );
751 rc = cursor->c_get( cursor, key, &data, DB_PREV );
757 BDB_DISK2ID( &nhi, &hi );
758 /* Update hi/lo if needed, then delete all the items
764 } else if ( id > hi ) {
769 /* Don't fetch anything, just position cursor */
770 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
771 data.dlen = data.ulen = 0;
772 rc = cursor->c_get( cursor, key, &data, DB_SET );
777 rc = cursor->c_del( cursor, 0 );
779 err = "c_del range1";
782 /* Delete all the records */
783 for ( i=1; i<count; i++ ) {
784 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_DUP );
786 err = "c_get next_dup";
789 rc = cursor->c_del( cursor, 0 );
795 /* Store the range marker */
796 data.size = data.ulen = sizeof(ID);
797 data.flags = DB_DBT_USERMEM;
799 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
805 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
811 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
817 /* There's room, just store it */
821 /* It's a range, see if we need to rewrite
826 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
831 BDB_DISK2ID( &nlo, &lo );
834 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
839 BDB_DISK2ID( &nhi, &hi );
841 if ( id < lo || id > hi ) {
842 /* Delete the current lo/hi */
843 rc = cursor->c_del( cursor, 0 );
849 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
856 } else if ( rc == DB_NOTFOUND ) {
857 put1: data.data = &nid;
858 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
859 /* Don't worry if it's already there */
860 if ( rc != 0 && rc != DB_KEYEXIST ) {
865 /* initial c_get failed, nothing was done */
867 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
868 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
869 cursor->c_close( cursor );
872 rc = cursor->c_close( cursor );
874 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
875 "c_close failed: %s (%d)\n",
876 db_strerror(rc), rc, 0 );
889 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
893 ID lo, hi, tmp, nid, nlo, nhi;
898 Debug( LDAP_DEBUG_ARGS,
899 "bdb_idl_delete_key: %lx %s\n",
900 (long) id, bdb_show_key( key, buf ), 0 );
902 assert( id != NOID );
904 if ( bdb->bi_idl_cache_max_size ) {
905 bdb_idl_cache_del( bdb, db, key );
908 BDB_ID2DISK( id, &nid );
912 data.size = sizeof( id );
913 data.ulen = data.size;
914 data.flags = DB_DBT_USERMEM;
916 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
918 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
919 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
922 /* Fetch the first data item for this key, to see if it
923 * exists and if it's a range.
925 rc = cursor->c_get( cursor, key, &data, DB_SET );
929 /* Not a range, just delete it */
931 /* position to correct item */
933 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
939 rc = cursor->c_del( cursor, 0 );
945 /* It's a range, see if we need to rewrite
949 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
954 BDB_DISK2ID( &nlo, &lo );
956 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
961 BDB_DISK2ID( &nhi, &hi );
962 if ( id == lo || id == hi ) {
966 } else if ( id == hi ) {
971 /* The range has collapsed... */
972 rc = db->del( db, tid, key, 0 );
979 /* reposition on lo slot */
981 cursor->c_get( cursor, key, &data, DB_PREV );
983 rc = cursor->c_del( cursor, 0 );
990 BDB_ID2DISK( id, &nid );
992 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
1001 /* initial c_get failed, nothing was done */
1003 if ( rc != DB_NOTFOUND ) {
1004 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
1005 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
1007 cursor->c_close( cursor );
1010 rc = cursor->c_close( cursor );
1012 Debug( LDAP_DEBUG_ANY,
1013 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
1014 db_strerror(rc), rc, 0 );
1022 * idl_intersection - return a = a intersection b
1025 bdb_idl_intersection(
1031 ID cursora = 0, cursorb = 0, cursorc;
1034 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
1039 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1040 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1041 if ( idmin > idmax ) {
1044 } else if ( idmin == idmax ) {
1050 if ( BDB_IDL_IS_RANGE( a ) ) {
1051 if ( BDB_IDL_IS_RANGE(b) ) {
1052 /* If both are ranges, just shrink the boundaries */
1057 /* Else swap so that b is the range, a is a list */
1065 /* If a range completely covers the list, the result is
1066 * just the list. If idmin to idmax is contiguous, just
1067 * turn it into a range.
1069 if ( BDB_IDL_IS_RANGE( b )
1070 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1071 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1072 if (idmax - idmin + 1 == a[0])
1081 /* Fine, do the intersection one element at a time.
1082 * First advance to idmin in both IDLs.
1084 cursora = cursorb = idmin;
1085 ida = bdb_idl_first( a, &cursora );
1086 idb = bdb_idl_first( b, &cursorb );
1089 while( ida <= idmax || idb <= idmax ) {
1092 ida = bdb_idl_next( a, &cursora );
1093 idb = bdb_idl_next( b, &cursorb );
1094 } else if ( ida < idb ) {
1095 ida = bdb_idl_next( a, &cursora );
1097 idb = bdb_idl_next( b, &cursorb );
1103 BDB_IDL_CPY( b, a );
1110 * idl_union - return a = a union b
1118 ID cursora = 0, cursorb = 0, cursorc;
1120 if ( BDB_IDL_IS_ZERO( b ) ) {
1124 if ( BDB_IDL_IS_ZERO( a ) ) {
1125 BDB_IDL_CPY( a, b );
1129 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1130 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1131 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1138 ida = bdb_idl_first( a, &cursora );
1139 idb = bdb_idl_first( b, &cursorb );
1143 /* The distinct elements of a are cat'd to b */
1144 while( ida != NOID || idb != NOID ) {
1146 if( ++cursorc > BDB_IDL_UM_MAX ) {
1150 ida = bdb_idl_next( a, &cursora );
1154 ida = bdb_idl_next( a, &cursora );
1155 idb = bdb_idl_next( b, &cursorb );
1159 /* b is copied back to a in sorted order */
1164 while (cursorb <= b[0] || cursorc <= a[0]) {
1169 if (cursorb <= b[0] && b[cursorb] < idb)
1170 a[cursora++] = b[cursorb++];
1183 * bdb_idl_notin - return a intersection ~b (or a minus b)
1192 ID cursora = 0, cursorb = 0;
1194 if( BDB_IDL_IS_ZERO( a ) ||
1195 BDB_IDL_IS_ZERO( b ) ||
1196 BDB_IDL_IS_RANGE( b ) )
1198 BDB_IDL_CPY( ids, a );
1202 if( BDB_IDL_IS_RANGE( a ) ) {
1203 BDB_IDL_CPY( ids, a );
1207 ida = bdb_idl_first( a, &cursora ),
1208 idb = bdb_idl_first( b, &cursorb );
1212 while( ida != NOID ) {
1213 if ( idb == NOID ) {
1214 /* we could shortcut this */
1215 ids[++ids[0]] = ida;
1216 ida = bdb_idl_next( a, &cursora );
1218 } else if ( ida < idb ) {
1219 ids[++ids[0]] = ida;
1220 ida = bdb_idl_next( a, &cursora );
1222 } else if ( ida > idb ) {
1223 idb = bdb_idl_next( b, &cursorb );
1226 ida = bdb_idl_next( a, &cursora );
1227 idb = bdb_idl_next( b, &cursorb );
1235 ID bdb_idl_first( ID *ids, ID *cursor )
1239 if ( ids[0] == 0 ) {
1244 if ( BDB_IDL_IS_RANGE( ids ) ) {
1245 if( *cursor < ids[1] ) {
1254 pos = bdb_idl_search( ids, *cursor );
1256 if( pos > ids[0] ) {
1264 ID bdb_idl_next( ID *ids, ID *cursor )
1266 if ( BDB_IDL_IS_RANGE( ids ) ) {
1267 if( ids[2] < ++(*cursor) ) {
1273 if ( ++(*cursor) <= ids[0] ) {
1274 return ids[*cursor];
1282 /* Add one ID to an unsorted list. We ensure that the first element is the
1283 * minimum and the last element is the maximum, for fast range compaction.
1284 * this means IDLs up to length 3 are always sorted...
1286 int bdb_idl_append_one( ID *ids, ID id )
1288 if (BDB_IDL_IS_RANGE( ids )) {
1289 /* if already in range, treat as a dup */
1290 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
1292 if (id < BDB_IDL_FIRST(ids))
1294 else if (id > BDB_IDL_LAST(ids))
1306 if ( ids[0] > 1 && id < ids[ids[0]] ) {
1313 if ( ids[0] >= BDB_IDL_UM_MAX ) {
1322 /* Append sorted list b to sorted list a. The result is unsorted but
1323 * a[1] is the min of the result and a[a[0]] is the max.
1325 int bdb_idl_append( ID *a, ID *b )
1327 ID ida, idb, tmp, swap = 0;
1329 if ( BDB_IDL_IS_ZERO( b ) ) {
1333 if ( BDB_IDL_IS_ZERO( a ) ) {
1334 BDB_IDL_CPY( a, b );
1338 ida = BDB_IDL_LAST( a );
1339 idb = BDB_IDL_LAST( b );
1340 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ||
1341 a[0] + b[0] >= BDB_IDL_UM_MAX ) {
1342 a[2] = IDL_MAX( ida, idb );
1343 a[1] = IDL_MIN( a[1], b[1] );
1348 if ( b[0] > 1 && ida > idb ) {
1354 if ( b[1] < a[1] ) {
1365 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1376 /* Quicksort + Insertion sort for small arrays */
1379 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1382 bdb_idl_sort( ID *ids, ID *tmp )
1384 int *istack = (int *)tmp;
1385 int i,j,k,l,ir,jstack;
1388 if ( BDB_IDL_IS_RANGE( ids ))
1395 if (ir - l < SMALL) { /* Insertion sort */
1396 for (j=l+1;j<=ir;j++) {
1398 for (i=j-1;i>=1;i--) {
1399 if (ids[i] <= a) break;
1404 if (jstack == 0) break;
1405 ir = istack[jstack--];
1406 l = istack[jstack--];
1408 k = (l + ir) >> 1; /* Choose median of left, center, right */
1409 SWAP(ids[k], ids[l+1]);
1410 if (ids[l] > ids[ir]) {
1411 SWAP(ids[l], ids[ir]);
1413 if (ids[l+1] > ids[ir]) {
1414 SWAP(ids[l+1], ids[ir]);
1416 if (ids[l] > ids[l+1]) {
1417 SWAP(ids[l], ids[l+1]);
1423 do i++; while(ids[i] < a);
1424 do j--; while(ids[j] > a);
1426 SWAP(ids[i],ids[j]);
1431 if (ir-i+1 >= j-1) {
1432 istack[jstack] = ir;
1433 istack[jstack-1] = i;
1436 istack[jstack] = j-1;
1437 istack[jstack-1] = l;
1446 /* 8 bit Radix sort + insertion sort
1448 * based on code from http://www.cubic.org/docs/radix.htm
1449 * with improvements by mbackes@symas.com and hyc@symas.com
1451 * This code is O(n) but has a relatively high constant factor. For lists
1452 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1453 * Much faster than quicksort for lists longer than ~100. Insertion
1454 * sort is actually superior for lists <50.
1457 #define BUCKETS (1<<8)
1461 bdb_idl_sort( ID *ids, ID *tmp )
1463 int count, soft_limit, phase = 0, size = ids[0];
1465 unsigned char *maxv = (unsigned char *)&ids[size];
1467 if ( BDB_IDL_IS_RANGE( ids ))
1470 /* Use insertion sort for small lists */
1471 if ( size <= SMALL ) {
1475 for (j=1;j<=size;j++) {
1477 for (i=j-1;i>=1;i--) {
1478 if (ids[i] <= a) break;
1490 #if BYTE_ORDER == BIG_ENDIAN
1491 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1493 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1497 #if BYTE_ORDER == BIG_ENDIAN
1498 count = sizeof(ID)-1; count >= soft_limit; --count
1500 count = 0; count <= soft_limit; ++count
1503 unsigned int num[BUCKETS], * np, n, sum;
1505 ID *sp, *source, *dest;
1506 unsigned char *bp, *source_start;
1508 source = idls[phase]+1;
1509 dest = idls[phase^1]+1;
1510 source_start = ((unsigned char *) source) + count;
1513 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1515 /* count occurences of every byte value */
1517 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1520 /* transform count into index by summing elements and storing
1525 for ( i = BUCKETS; i > 0; --i ) {
1531 /* fill dest with the right values in the right place */
1534 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1542 /* copy back from temp if needed */
1545 for ( count = 0; count < size; ++count )
1549 #endif /* Quick vs Radix */
1551 #endif /* BDB_HIER */