1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2014 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) )
27 #define IDL_CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
29 #define IDL_LRU_DELETE( bdb, e ) do { \
30 if ( (e) == (bdb)->bi_idl_lru_head ) { \
31 if ( (e)->idl_lru_next == (bdb)->bi_idl_lru_head ) { \
32 (bdb)->bi_idl_lru_head = NULL; \
34 (bdb)->bi_idl_lru_head = (e)->idl_lru_next; \
37 if ( (e) == (bdb)->bi_idl_lru_tail ) { \
38 if ( (e)->idl_lru_prev == (bdb)->bi_idl_lru_tail ) { \
39 assert( (bdb)->bi_idl_lru_head == NULL ); \
40 (bdb)->bi_idl_lru_tail = NULL; \
42 (bdb)->bi_idl_lru_tail = (e)->idl_lru_prev; \
45 (e)->idl_lru_next->idl_lru_prev = (e)->idl_lru_prev; \
46 (e)->idl_lru_prev->idl_lru_next = (e)->idl_lru_next; \
50 bdb_idl_entry_cmp( const void *v_idl1, const void *v_idl2 )
52 const bdb_idl_cache_entry_t *idl1 = v_idl1, *idl2 = v_idl2;
55 if ((rc = SLAP_PTRCMP( idl1->db, idl2->db ))) return rc;
56 if ((rc = idl1->kstr.bv_len - idl2->kstr.bv_len )) return rc;
57 return ( memcmp ( idl1->kstr.bv_val, idl2->kstr.bv_val , idl1->kstr.bv_len ) );
61 static void idl_check( ID *ids )
63 if( BDB_IDL_IS_RANGE( ids ) ) {
64 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
67 for( i=1; i < ids[0]; i++ ) {
68 assert( ids[i+1] > ids[i] );
74 static void idl_dump( ID *ids )
76 if( BDB_IDL_IS_RANGE( ids ) ) {
77 Debug( LDAP_DEBUG_ANY,
78 "IDL: range ( %ld - %ld )\n",
79 (long) BDB_IDL_RANGE_FIRST( ids ),
80 (long) BDB_IDL_RANGE_LAST( ids ) );
84 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
86 for( i=1; i<=ids[0]; i++ ) {
88 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
90 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
93 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
98 #endif /* IDL_DEBUG > 1 */
99 #endif /* IDL_DEBUG > 0 */
101 unsigned bdb_idl_search( ID *ids, ID id )
103 #define IDL_BINARY_SEARCH 1
104 #ifdef IDL_BINARY_SEARCH
106 * binary search of id in ids
107 * if found, returns position of id
108 * if not found, returns first postion greater than id
120 unsigned pivot = n >> 1;
121 cursor = base + pivot + 1;
122 val = IDL_CMP( id, ids[cursor] );
127 } else if ( val > 0 ) {
142 /* (reverse) linear search */
149 for( i=ids[0]; i; i-- ) {
159 int bdb_idl_insert( ID *ids, ID id )
164 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
170 if (BDB_IDL_IS_RANGE( ids )) {
171 /* if already in range, treat as a dup */
172 if (id >= BDB_IDL_RANGE_FIRST(ids) && id <= BDB_IDL_RANGE_LAST(ids))
174 if (id < BDB_IDL_RANGE_FIRST(ids))
176 else if (id > BDB_IDL_RANGE_LAST(ids))
181 x = bdb_idl_search( ids, id );
189 if ( x <= ids[0] && ids[x] == id ) {
194 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
197 ids[2] = ids[ids[0]-1];
198 } else if ( ids[ids[0]-1] < id ) {
201 ids[2] = ids[ids[0]-1];
207 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
220 int bdb_idl_delete( ID *ids, ID id )
225 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
231 if (BDB_IDL_IS_RANGE( ids )) {
232 /* If deleting a range boundary, adjust */
235 else if ( ids[2] == id )
237 /* deleting from inside a range is a no-op */
239 /* If the range has collapsed, re-adjust */
240 if ( ids[1] > ids[2] )
242 else if ( ids[1] == ids[2] )
247 x = bdb_idl_search( ids, id );
255 if( x > ids[0] || ids[x] != id ) {
259 } else if ( --ids[0] == 0 ) {
265 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
282 if ( key->size == 4 /* LUTIL_HASH_BYTES */ ) {
283 unsigned char *c = key->data;
284 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
291 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
292 * copy the cached IDL into it, otherwise just return the status.
296 struct bdb_info *bdb,
301 bdb_idl_cache_entry_t idl_tmp;
302 bdb_idl_cache_entry_t *matched_idl_entry;
303 int rc = LDAP_NO_SUCH_OBJECT;
305 DBT2bv( key, &idl_tmp.kstr );
307 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
308 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
310 if ( matched_idl_entry != NULL ) {
311 if ( matched_idl_entry->idl && ids )
312 BDB_IDL_CPY( ids, matched_idl_entry->idl );
313 matched_idl_entry->idl_flags |= CACHE_ENTRY_REFERENCED;
314 if ( matched_idl_entry->idl )
319 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
326 struct bdb_info *bdb,
332 bdb_idl_cache_entry_t idl_tmp;
333 bdb_idl_cache_entry_t *ee, *eprev;
335 if ( rc == DB_NOTFOUND || BDB_IDL_IS_ZERO( ids ))
338 DBT2bv( key, &idl_tmp.kstr );
340 ee = (bdb_idl_cache_entry_t *) ch_malloc(
341 sizeof( bdb_idl_cache_entry_t ) );
343 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
344 BDB_IDL_CPY( ee->idl, ids );
346 ee->idl_lru_prev = NULL;
347 ee->idl_lru_next = NULL;
349 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
350 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
351 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
352 bdb_idl_entry_cmp, avl_dup_error ))
354 ch_free( ee->kstr.bv_val );
357 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
360 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
362 if ( bdb->bi_idl_lru_head ) {
363 assert( bdb->bi_idl_lru_tail != NULL );
364 assert( bdb->bi_idl_lru_head->idl_lru_prev != NULL );
365 assert( bdb->bi_idl_lru_head->idl_lru_next != NULL );
367 ee->idl_lru_next = bdb->bi_idl_lru_head;
368 ee->idl_lru_prev = bdb->bi_idl_lru_head->idl_lru_prev;
369 bdb->bi_idl_lru_head->idl_lru_prev->idl_lru_next = ee;
370 bdb->bi_idl_lru_head->idl_lru_prev = ee;
372 ee->idl_lru_next = ee->idl_lru_prev = ee;
373 bdb->bi_idl_lru_tail = ee;
375 bdb->bi_idl_lru_head = ee;
377 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
379 eprev = bdb->bi_idl_lru_tail;
380 for ( i = 0; (ee = eprev) != NULL && i < 10; i++ ) {
381 eprev = ee->idl_lru_prev;
385 if ( ee->idl_flags & CACHE_ENTRY_REFERENCED ) {
386 ee->idl_flags ^= CACHE_ENTRY_REFERENCED;
389 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
390 bdb_idl_entry_cmp ) == NULL ) {
391 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
392 "AVL delete failed\n",
395 IDL_LRU_DELETE( bdb, ee );
397 --bdb->bi_idl_cache_size;
398 ch_free( ee->kstr.bv_val );
402 bdb->bi_idl_lru_tail = eprev;
403 assert( bdb->bi_idl_lru_tail != NULL
404 || bdb->bi_idl_lru_head == NULL );
406 bdb->bi_idl_cache_size++;
407 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
408 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
413 struct bdb_info *bdb,
417 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
418 DBT2bv( key, &idl_tmp.kstr );
420 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
421 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
423 if ( matched_idl_entry != NULL ) {
424 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
425 bdb_idl_entry_cmp ) == NULL ) {
426 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
427 "AVL delete failed\n",
430 --bdb->bi_idl_cache_size;
431 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
432 IDL_LRU_DELETE( bdb, matched_idl_entry );
433 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
434 free( matched_idl_entry->kstr.bv_val );
435 if ( matched_idl_entry->idl )
436 free( matched_idl_entry->idl );
437 free( matched_idl_entry );
439 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
443 bdb_idl_cache_add_id(
444 struct bdb_info *bdb,
449 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
450 DBT2bv( key, &idl_tmp.kstr );
452 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
453 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
455 if ( cache_entry != NULL ) {
456 if ( !BDB_IDL_IS_RANGE( cache_entry->idl ) &&
457 cache_entry->idl[0] < BDB_IDL_DB_MAX ) {
458 size_t s = BDB_IDL_SIZEOF( cache_entry->idl ) + sizeof(ID);
459 cache_entry->idl = ch_realloc( cache_entry->idl, s );
461 bdb_idl_insert( cache_entry->idl, id );
463 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
467 bdb_idl_cache_del_id(
468 struct bdb_info *bdb,
473 bdb_idl_cache_entry_t *cache_entry, idl_tmp;
474 DBT2bv( key, &idl_tmp.kstr );
476 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
477 cache_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
479 if ( cache_entry != NULL ) {
480 bdb_idl_delete( cache_entry->idl, id );
481 if ( cache_entry->idl[0] == 0 ) {
482 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) cache_entry,
483 bdb_idl_entry_cmp ) == NULL ) {
484 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
485 "AVL delete failed\n",
488 --bdb->bi_idl_cache_size;
489 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
490 IDL_LRU_DELETE( bdb, cache_entry );
491 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
492 free( cache_entry->kstr.bv_val );
493 free( cache_entry->idl );
497 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
510 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
512 DBT data, key2, *kptr;
518 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
521 /* If using BerkeleyDB 4.0, the buf must be large enough to
522 * grab the entire IDL in one get(), otherwise BDB will leak
523 * resources on subsequent get's. We can safely call get()
524 * twice - once for the data, and once to get the DB_NOTFOUND
525 * result meaning there's no more data. See ITS#2040 for details.
526 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
527 * stack space is too limited.
529 * configure now requires Berkeley DB 4.1.
531 #if DB_VERSION_FULL < 0x04010000
532 # define BDB_ENOUGH 5
534 /* We sometimes test with tiny IDLs, and BDB always wants buffers
535 * that are at least one page in size.
537 # if BDB_IDL_DB_SIZE < 4096
538 # define BDB_ENOUGH 2048
540 # define BDB_ENOUGH 1
543 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
547 Debug( LDAP_DEBUG_ARGS,
548 "bdb_idl_fetch_key: %s\n",
549 bdb_show_key( key, keybuf ), 0, 0 );
551 assert( ids != NULL );
553 if ( saved_cursor && *saved_cursor ) {
555 } else if ( get_flag == LDAP_FILTER_GE ) {
556 opflag = DB_SET_RANGE;
557 } else if ( get_flag == LDAP_FILTER_LE ) {
563 /* only non-range lookups can use the IDL cache */
564 if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
565 rc = bdb_idl_cache_get( bdb, db, key, ids );
566 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
572 data.ulen = sizeof(buf);
573 data.flags = DB_DBT_USERMEM;
575 /* If we're not reusing an existing cursor, get a new one */
576 if( opflag != DB_NEXT ) {
577 rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
579 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
580 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
584 cursor = *saved_cursor;
587 /* If this is a LE lookup, save original key so we can determine
588 * when to stop. If this is a GE lookup, save the key since it
589 * will be overwritten.
591 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
593 key2.flags = DB_DBT_USERMEM;
594 key2.ulen = sizeof(keybuf);
596 key2.size = key->size;
597 AC_MEMCPY( keybuf, key->data, key->size );
603 rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
605 /* skip presence key on range inequality lookups */
606 while (rc == 0 && kptr->size != len) {
607 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
609 /* If we're doing a LE compare and the new key is greater than
610 * our search key, we're done
612 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
613 key->data, key->size ) > 0 ) {
621 DB_MULTIPLE_INIT( ptr, &data );
623 DB_MULTIPLE_NEXT(ptr, &data, j, len);
629 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
631 if ( rc == DB_NOTFOUND ) rc = 0;
633 /* On disk, a range is denoted by 0 in the first element */
635 if (ids[0] != BDB_IDL_RANGE_SIZE) {
636 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
637 "range size mismatch: expected %d, got %ld\n",
638 BDB_IDL_RANGE_SIZE, ids[0], 0 );
639 cursor->c_close( cursor );
642 BDB_IDL_RANGE( ids, ids[2], ids[3] );
644 data.size = BDB_IDL_SIZEOF(ids);
647 if ( saved_cursor && rc == 0 ) {
648 if ( !*saved_cursor )
649 *saved_cursor = cursor;
653 rc2 = cursor->c_close( cursor );
655 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
656 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
660 if( rc == DB_NOTFOUND ) {
663 } else if( rc != 0 ) {
664 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
665 "get failed: %s (%d)\n",
666 db_strerror(rc), rc, 0 );
669 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
670 /* size not multiple of ID size */
671 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
672 "odd size: expected %ld multiple, got %ld\n",
673 (long) sizeof( ID ), (long) data.size, 0 );
676 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
678 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
679 "get size mismatch: expected %ld, got %ld\n",
680 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
684 if ( bdb->bi_idl_cache_max_size ) {
685 bdb_idl_cache_put( bdb, db, key, ids, rc );
700 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
704 ID lo, hi, nlo, nhi, nid;
709 Debug( LDAP_DEBUG_ARGS,
710 "bdb_idl_insert_key: %lx %s\n",
711 (long) id, bdb_show_key( key, buf ), 0 );
714 assert( id != NOID );
717 data.size = sizeof( ID );
718 data.ulen = data.size;
719 data.flags = DB_DBT_USERMEM;
721 BDB_ID2DISK( id, &nid );
723 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
725 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
726 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
730 /* Fetch the first data item for this key, to see if it
731 * exists and if it's a range.
733 rc = cursor->c_get( cursor, key, &data, DB_SET );
737 /* not a range, count the number of items */
739 rc = cursor->c_count( cursor, &count, 0 );
744 if ( count >= BDB_IDL_DB_MAX ) {
745 /* No room, convert to a range */
749 key2.dlen = key2.ulen;
750 key2.flags |= DB_DBT_PARTIAL;
752 BDB_DISK2ID( &nlo, &lo );
755 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
756 if ( rc != 0 && rc != DB_NOTFOUND ) {
757 err = "c_get next_nodup";
760 if ( rc == DB_NOTFOUND ) {
761 rc = cursor->c_get( cursor, key, &data, DB_LAST );
767 rc = cursor->c_get( cursor, key, &data, DB_PREV );
773 BDB_DISK2ID( &nhi, &hi );
774 /* Update hi/lo if needed, then delete all the items
780 } else if ( id > hi ) {
785 /* Don't fetch anything, just position cursor */
786 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
787 data.dlen = data.ulen = 0;
788 rc = cursor->c_get( cursor, key, &data, DB_SET );
793 rc = cursor->c_del( cursor, 0 );
795 err = "c_del range1";
798 /* Delete all the records */
799 for ( i=1; i<count; i++ ) {
800 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_DUP );
802 err = "c_get next_dup";
805 rc = cursor->c_del( cursor, 0 );
811 /* Store the range marker */
812 data.size = data.ulen = sizeof(ID);
813 data.flags = DB_DBT_USERMEM;
815 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
821 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
827 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
833 /* There's room, just store it */
837 /* It's a range, see if we need to rewrite
842 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
847 BDB_DISK2ID( &nlo, &lo );
850 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
855 BDB_DISK2ID( &nhi, &hi );
857 if ( id < lo || id > hi ) {
858 /* Delete the current lo/hi */
859 rc = cursor->c_del( cursor, 0 );
865 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
872 } else if ( rc == DB_NOTFOUND ) {
873 put1: data.data = &nid;
874 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
875 /* Don't worry if it's already there */
876 if ( rc != 0 && rc != DB_KEYEXIST ) {
881 /* initial c_get failed, nothing was done */
883 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
884 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
885 cursor->c_close( cursor );
888 /* If key was added (didn't already exist) and using IDL cache,
889 * update key in IDL cache.
891 if ( !rc && bdb->bi_idl_cache_max_size ) {
892 bdb_idl_cache_add_id( bdb, db, key, id );
894 rc = cursor->c_close( cursor );
896 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
897 "c_close failed: %s (%d)\n",
898 db_strerror(rc), rc, 0 );
911 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
915 ID lo, hi, tmp, nid, nlo, nhi;
920 Debug( LDAP_DEBUG_ARGS,
921 "bdb_idl_delete_key: %lx %s\n",
922 (long) id, bdb_show_key( key, buf ), 0 );
924 assert( id != NOID );
926 if ( bdb->bi_idl_cache_size ) {
927 bdb_idl_cache_del( bdb, db, key );
930 BDB_ID2DISK( id, &nid );
934 data.size = sizeof( id );
935 data.ulen = data.size;
936 data.flags = DB_DBT_USERMEM;
938 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
940 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
941 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
944 /* Fetch the first data item for this key, to see if it
945 * exists and if it's a range.
947 rc = cursor->c_get( cursor, key, &data, DB_SET );
951 /* Not a range, just delete it */
953 /* position to correct item */
955 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
961 rc = cursor->c_del( cursor, 0 );
967 /* It's a range, see if we need to rewrite
971 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
976 BDB_DISK2ID( &nlo, &lo );
978 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
983 BDB_DISK2ID( &nhi, &hi );
984 if ( id == lo || id == hi ) {
988 } else if ( id == hi ) {
993 /* The range has collapsed... */
994 rc = db->del( db, tid, key, 0 );
1001 /* reposition on lo slot */
1003 cursor->c_get( cursor, key, &data, DB_PREV );
1005 rc = cursor->c_del( cursor, 0 );
1012 BDB_ID2DISK( id, &nid );
1014 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
1016 err = "c_put lo/hi";
1023 /* initial c_get failed, nothing was done */
1025 if ( rc != DB_NOTFOUND ) {
1026 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
1027 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
1029 cursor->c_close( cursor );
1032 rc = cursor->c_close( cursor );
1034 Debug( LDAP_DEBUG_ANY,
1035 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
1036 db_strerror(rc), rc, 0 );
1044 * idl_intersection - return a = a intersection b
1047 bdb_idl_intersection(
1053 ID cursora = 0, cursorb = 0, cursorc;
1056 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
1061 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1062 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1063 if ( idmin > idmax ) {
1066 } else if ( idmin == idmax ) {
1072 if ( BDB_IDL_IS_RANGE( a ) ) {
1073 if ( BDB_IDL_IS_RANGE(b) ) {
1074 /* If both are ranges, just shrink the boundaries */
1079 /* Else swap so that b is the range, a is a list */
1087 /* If a range completely covers the list, the result is
1088 * just the list. If idmin to idmax is contiguous, just
1089 * turn it into a range.
1091 if ( BDB_IDL_IS_RANGE( b )
1092 && BDB_IDL_RANGE_FIRST( b ) <= BDB_IDL_FIRST( a )
1093 && BDB_IDL_RANGE_LAST( b ) >= BDB_IDL_LLAST( a ) ) {
1094 if (idmax - idmin + 1 == a[0])
1103 /* Fine, do the intersection one element at a time.
1104 * First advance to idmin in both IDLs.
1106 cursora = cursorb = idmin;
1107 ida = bdb_idl_first( a, &cursora );
1108 idb = bdb_idl_first( b, &cursorb );
1111 while( ida <= idmax || idb <= idmax ) {
1114 ida = bdb_idl_next( a, &cursora );
1115 idb = bdb_idl_next( b, &cursorb );
1116 } else if ( ida < idb ) {
1117 ida = bdb_idl_next( a, &cursora );
1119 idb = bdb_idl_next( b, &cursorb );
1125 BDB_IDL_CPY( b, a );
1132 * idl_union - return a = a union b
1140 ID cursora = 0, cursorb = 0, cursorc;
1142 if ( BDB_IDL_IS_ZERO( b ) ) {
1146 if ( BDB_IDL_IS_ZERO( a ) ) {
1147 BDB_IDL_CPY( a, b );
1151 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1152 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1153 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1160 ida = bdb_idl_first( a, &cursora );
1161 idb = bdb_idl_first( b, &cursorb );
1165 /* The distinct elements of a are cat'd to b */
1166 while( ida != NOID || idb != NOID ) {
1168 if( ++cursorc > BDB_IDL_UM_MAX ) {
1172 ida = bdb_idl_next( a, &cursora );
1176 ida = bdb_idl_next( a, &cursora );
1177 idb = bdb_idl_next( b, &cursorb );
1181 /* b is copied back to a in sorted order */
1186 while (cursorb <= b[0] || cursorc <= a[0]) {
1191 if (cursorb <= b[0] && b[cursorb] < idb)
1192 a[cursora++] = b[cursorb++];
1205 * bdb_idl_notin - return a intersection ~b (or a minus b)
1214 ID cursora = 0, cursorb = 0;
1216 if( BDB_IDL_IS_ZERO( a ) ||
1217 BDB_IDL_IS_ZERO( b ) ||
1218 BDB_IDL_IS_RANGE( b ) )
1220 BDB_IDL_CPY( ids, a );
1224 if( BDB_IDL_IS_RANGE( a ) ) {
1225 BDB_IDL_CPY( ids, a );
1229 ida = bdb_idl_first( a, &cursora ),
1230 idb = bdb_idl_first( b, &cursorb );
1234 while( ida != NOID ) {
1235 if ( idb == NOID ) {
1236 /* we could shortcut this */
1237 ids[++ids[0]] = ida;
1238 ida = bdb_idl_next( a, &cursora );
1240 } else if ( ida < idb ) {
1241 ids[++ids[0]] = ida;
1242 ida = bdb_idl_next( a, &cursora );
1244 } else if ( ida > idb ) {
1245 idb = bdb_idl_next( b, &cursorb );
1248 ida = bdb_idl_next( a, &cursora );
1249 idb = bdb_idl_next( b, &cursorb );
1257 ID bdb_idl_first( ID *ids, ID *cursor )
1261 if ( ids[0] == 0 ) {
1266 if ( BDB_IDL_IS_RANGE( ids ) ) {
1267 if( *cursor < ids[1] ) {
1276 pos = bdb_idl_search( ids, *cursor );
1278 if( pos > ids[0] ) {
1286 ID bdb_idl_next( ID *ids, ID *cursor )
1288 if ( BDB_IDL_IS_RANGE( ids ) ) {
1289 if( ids[2] < ++(*cursor) ) {
1295 if ( ++(*cursor) <= ids[0] ) {
1296 return ids[*cursor];
1304 /* Add one ID to an unsorted list. We ensure that the first element is the
1305 * minimum and the last element is the maximum, for fast range compaction.
1306 * this means IDLs up to length 3 are always sorted...
1308 int bdb_idl_append_one( ID *ids, ID id )
1310 if (BDB_IDL_IS_RANGE( ids )) {
1311 /* if already in range, treat as a dup */
1312 if (id >= BDB_IDL_RANGE_FIRST(ids) && id <= BDB_IDL_RANGE_LAST(ids))
1314 if (id < BDB_IDL_RANGE_FIRST(ids))
1316 else if (id > BDB_IDL_RANGE_LAST(ids))
1328 if ( ids[0] > 1 && id < ids[ids[0]] ) {
1335 if ( ids[0] >= BDB_IDL_UM_MAX ) {
1344 /* Append sorted list b to sorted list a. The result is unsorted but
1345 * a[1] is the min of the result and a[a[0]] is the max.
1347 int bdb_idl_append( ID *a, ID *b )
1349 ID ida, idb, tmp, swap = 0;
1351 if ( BDB_IDL_IS_ZERO( b ) ) {
1355 if ( BDB_IDL_IS_ZERO( a ) ) {
1356 BDB_IDL_CPY( a, b );
1361 return bdb_idl_append_one( a, BDB_IDL_FIRST( b ));
1364 ida = BDB_IDL_LAST( a );
1365 idb = BDB_IDL_LAST( b );
1366 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ||
1367 a[0] + b[0] >= BDB_IDL_UM_MAX ) {
1368 a[2] = IDL_MAX( ida, idb );
1369 a[1] = IDL_MIN( a[1], b[1] );
1380 if ( b[1] < a[1] ) {
1391 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1402 /* Quicksort + Insertion sort for small arrays */
1405 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1408 bdb_idl_sort( ID *ids, ID *tmp )
1410 int *istack = (int *)tmp;
1411 int i,j,k,l,ir,jstack;
1414 if ( BDB_IDL_IS_RANGE( ids ))
1421 if (ir - l < SMALL) { /* Insertion sort */
1422 for (j=l+1;j<=ir;j++) {
1424 for (i=j-1;i>=1;i--) {
1425 if (ids[i] <= a) break;
1430 if (jstack == 0) break;
1431 ir = istack[jstack--];
1432 l = istack[jstack--];
1434 k = (l + ir) >> 1; /* Choose median of left, center, right */
1435 SWAP(ids[k], ids[l+1]);
1436 if (ids[l] > ids[ir]) {
1437 SWAP(ids[l], ids[ir]);
1439 if (ids[l+1] > ids[ir]) {
1440 SWAP(ids[l+1], ids[ir]);
1442 if (ids[l] > ids[l+1]) {
1443 SWAP(ids[l], ids[l+1]);
1449 do i++; while(ids[i] < a);
1450 do j--; while(ids[j] > a);
1452 SWAP(ids[i],ids[j]);
1457 if (ir-i+1 >= j-l) {
1458 istack[jstack] = ir;
1459 istack[jstack-1] = i;
1462 istack[jstack] = j-1;
1463 istack[jstack-1] = l;
1472 /* 8 bit Radix sort + insertion sort
1474 * based on code from http://www.cubic.org/docs/radix.htm
1475 * with improvements by ebackes@symas.com and hyc@symas.com
1477 * This code is O(n) but has a relatively high constant factor. For lists
1478 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1479 * Much faster than quicksort for lists longer than ~100. Insertion
1480 * sort is actually superior for lists <50.
1483 #define BUCKETS (1<<8)
1487 bdb_idl_sort( ID *ids, ID *tmp )
1489 int count, soft_limit, phase = 0, size = ids[0];
1491 unsigned char *maxv = (unsigned char *)&ids[size];
1493 if ( BDB_IDL_IS_RANGE( ids ))
1496 /* Use insertion sort for small lists */
1497 if ( size <= SMALL ) {
1501 for (j=1;j<=size;j++) {
1503 for (i=j-1;i>=1;i--) {
1504 if (ids[i] <= a) break;
1516 #if BYTE_ORDER == BIG_ENDIAN
1517 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1519 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1523 #if BYTE_ORDER == BIG_ENDIAN
1524 count = sizeof(ID)-1; count >= soft_limit; --count
1526 count = 0; count <= soft_limit; ++count
1529 unsigned int num[BUCKETS], * np, n, sum;
1531 ID *sp, *source, *dest;
1532 unsigned char *bp, *source_start;
1534 source = idls[phase]+1;
1535 dest = idls[phase^1]+1;
1536 source_start = ((unsigned char *) source) + count;
1539 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1541 /* count occurences of every byte value */
1543 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1546 /* transform count into index by summing elements and storing
1551 for ( i = BUCKETS; i > 0; --i ) {
1557 /* fill dest with the right values in the right place */
1560 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1568 /* copy back from temp if needed */
1571 for ( count = 0; count < size; ++count )
1575 #endif /* Quick vs Radix */
1577 #endif /* BDB_HIER */