1 /* idl.c - ldap id list handling routines */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 #define IDL_MAX(x,y) ( x > y ? x : y )
17 #define IDL_MIN(x,y) ( x < y ? x : y )
19 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
22 static void idl_check( ID *ids )
24 if( BDB_IDL_IS_RANGE( ids ) ) {
25 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
28 for( i=1; i < ids[0]; i++ ) {
29 assert( ids[i+1] > ids[i] );
35 static void idl_dump( ID *ids )
37 if( BDB_IDL_IS_RANGE( ids ) ) {
39 LDAP_LOG( INDEX, INFO, "IDL: range (%ld - %ld)\n",
40 (long) BDB_IDL_RANGE_FIRST( ids ),
41 (long) BDB_IDL_RANGE_LAST( ids ), 0 );
43 Debug( LDAP_DEBUG_ANY,
44 "IDL: range ( %ld - %ld )\n",
45 (long) BDB_IDL_RANGE_FIRST( ids ),
46 (long) BDB_IDL_RANGE_LAST( ids ) );
52 LDAP_LOG( INDEX, INFO, "IDL: size %ld", (long) ids[0], 0, 0 );
54 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
57 for( i=1; i<=ids[0]; i++ ) {
59 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
62 LDAP_LOG( INDEX, INFO, "%02lx",(long)ids[i], 0, 0 );
64 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
68 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
73 #endif /* IDL_DEBUG > 1 */
74 #endif /* IDL_DEBUG > 0 */
76 unsigned bdb_idl_search( ID *ids, ID id )
78 #define IDL_BINARY_SEARCH 1
79 #ifdef IDL_BINARY_SEARCH
81 * binary search of id in ids
82 * if found, returns position of id
83 * if not found, returns first postion greater than id
96 cursor = base + pivot;
97 val = IDL_CMP( id, ids[cursor + 1] );
102 } else if ( val > 0 ) {
118 /* (reverse) linear search */
125 for( i=ids[0]; i; i-- ) {
135 int bdb_idl_insert( ID *ids, ID id )
137 unsigned x = bdb_idl_search( ids, id );
141 LDAP_LOG( INDEX, DETAIL1, "insert: %04lx at %d\n", (long) id, x, 0 );
143 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
157 if ( x <= ids[0] && ids[x] == id ) {
162 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
165 ids[2] = ids[ids[0]-1];
166 } else if ( ids[ids[0]-1] < id ) {
169 ids[2] = ids[ids[0]-1];
175 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
188 static int idl_delete( ID *ids, ID id )
190 unsigned x = bdb_idl_search( ids, id );
194 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
196 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
210 if( x > ids[0] || ids[x] != id ) {
214 } else if ( --ids[0] == 0 ) {
220 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
237 if ( key->size == sizeof( ID ) ) {
238 unsigned char *c = key->data;
239 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
254 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
262 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
264 /* buf must be large enough to grab the entire IDL in one
265 * get(), otherwise BDB 4 will leak resources on subsequent
266 * get's. We can safely call get() twice - once for the data,
267 * and once to get the DB_NOTFOUND result meaning there's
268 * no more data. See ITS#2040 for details. This bug is fixed
269 * in BDB 4.1 so a smaller buffer will work if stack space is
272 ID buf[BDB_IDL_DB_SIZE*5];
277 LDAP_LOG( INDEX, ARGS,
278 "bdb_idl_fetch_key: %s\n",
279 bdb_show_key( key, buf ), 0, 0 );
281 Debug( LDAP_DEBUG_ARGS,
282 "bdb_idl_fetch_key: %s\n",
283 bdb_show_key( key, buf ), 0, 0 );
286 assert( ids != NULL );
291 data.ulen = sizeof(buf);
292 data.flags = DB_DBT_USERMEM;
297 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
300 LDAP_LOG( INDEX, ERR,
301 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
302 db_strerror(rc), rc, 0 );
304 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
305 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
309 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
315 DB_MULTIPLE_INIT( ptr, &data );
317 DB_MULTIPLE_NEXT(ptr, &data, j, len);
320 AC_MEMCPY( i, j, sizeof(ID) );
323 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
325 if ( rc == DB_NOTFOUND ) rc = 0;
327 /* On disk, a range is denoted by 0 in the first element */
329 if (ids[0] != BDB_IDL_RANGE_SIZE) {
331 LDAP_LOG( INDEX, ERR,
332 "=> bdb_idl_fetch_key: range size mismatch: "
333 "expected %ld, got %ld\n",
334 BDB_IDL_RANGE_SIZE, ids[0], 0 );
336 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
337 "range size mismatch: expected %d, got %ld\n",
338 BDB_IDL_RANGE_SIZE, ids[0], 0 );
340 cursor->c_close( cursor );
343 BDB_IDL_RANGE( ids, ids[2], ids[3] );
345 data.size = BDB_IDL_SIZEOF(ids);
347 rc2 = cursor->c_close( cursor );
350 LDAP_LOG( INDEX, ERR,
351 "bdb_idl_fetch_key: close failed: %s (%d)\n",
352 db_strerror(rc2), rc2, 0 );
354 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
355 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
359 if( rc == DB_NOTFOUND ) {
362 } else if( rc != 0 ) {
364 LDAP_LOG( INDEX, ERR,
365 "bdb_idl_fetch_key: get failed: %s (%d)\n",
366 db_strerror(rc), rc, 0 );
368 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
369 "get failed: %s (%d)\n",
370 db_strerror(rc), rc, 0 );
374 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
375 /* size not multiple of ID size */
377 LDAP_LOG( INDEX, ERR,
378 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
379 (long) sizeof( ID ), (long) data.size, 0 );
381 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
382 "odd size: expected %ld multiple, got %ld\n",
383 (long) sizeof( ID ), (long) data.size, 0 );
387 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
390 LDAP_LOG( INDEX, ERR,
391 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
392 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
394 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
395 "get size mismatch: expected %ld, got %ld\n",
396 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
413 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
423 LDAP_LOG( INDEX, ARGS,
424 "bdb_idl_insert_key: %lx %s\n",
425 (long) id, bdb_show_key( key, buf ), 0 );
427 Debug( LDAP_DEBUG_ARGS,
428 "bdb_idl_insert_key: %lx %s\n",
429 (long) id, bdb_show_key( key, buf ), 0 );
433 assert( id != NOID );
436 data.size = sizeof( ID );
437 data.ulen = data.size;
438 data.flags = DB_DBT_USERMEM;
440 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
443 LDAP_LOG( INDEX, ERR,
444 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
445 db_strerror(rc), rc, 0 );
447 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
448 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
453 /* Fetch the first data item for this key, to see if it
454 * exists and if it's a range.
456 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
460 /* not a range, count the number of items */
462 rc = cursor->c_count( cursor, &count, 0 );
467 if ( count >= BDB_IDL_DB_MAX ) {
468 /* No room, convert to a range */
471 key2.dlen = key2.ulen;
472 key2.flags |= DB_DBT_PARTIAL;
476 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
477 if ( rc != 0 && rc != DB_NOTFOUND ) {
478 err = "c_get next_nodup";
481 if ( rc == DB_NOTFOUND ) {
482 rc = cursor->c_get( cursor, key, &data, DB_LAST );
488 rc = cursor->c_get( cursor, key, &data, DB_PREV );
498 rc = db->del( db, tid, key, 0 );
505 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
511 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
517 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
523 /* There's room, just store it */
527 /* It's a range, see if we need to rewrite
532 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
539 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
545 if ( id < lo || id > hi ) {
546 /* Delete the current lo/hi */
547 rc = cursor->c_del( cursor, 0 );
553 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
560 } else if ( rc == DB_NOTFOUND ) {
561 put1: data.data = &id;
562 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
563 /* Don't worry if it's already there */
564 if ( rc != 0 && rc != DB_KEYEXIST ) {
569 /* initial c_get failed, nothing was done */
572 LDAP_LOG( INDEX, ERR,
573 "bdb_idl_insert_key: %s failed: %s (%d)\n",
574 err, db_strerror(rc), rc );
576 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
577 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
579 cursor->c_close( cursor );
582 rc = cursor->c_close( cursor );
585 LDAP_LOG( INDEX, ERR,
586 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
587 db_strerror(rc), rc, 0 );
589 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
590 "c_close failed: %s (%d)\n",
591 db_strerror(rc), rc, 0 );
605 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
615 LDAP_LOG( INDEX, ARGS,
616 "bdb_idl_delete_key: %lx %s\n",
617 (long) id, bdb_show_key( key, buf ), 0 );
619 Debug( LDAP_DEBUG_ARGS,
620 "bdb_idl_delete_key: %lx %s\n",
621 (long) id, bdb_show_key( key, buf ), 0 );
624 assert( id != NOID );
628 data.size = sizeof( id );
629 data.ulen = data.size;
630 data.flags = DB_DBT_USERMEM;
632 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
635 LDAP_LOG( INDEX, ERR,
636 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
637 db_strerror(rc), rc, 0 );
639 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
640 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
644 /* Fetch the first data item for this key, to see if it
645 * exists and if it's a range.
647 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
651 /* Not a range, just delete it */
653 /* position to correct item */
655 rc = cursor->c_get( cursor, key, &data,
656 DB_GET_BOTH | DB_RMW );
662 rc = cursor->c_del( cursor, 0 );
668 /* It's a range, see if we need to rewrite
673 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
680 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
686 if ( id == lo || id == hi ) {
690 } else if ( id == hi ) {
695 /* The range has collapsed... */
696 rc = db->del( db, tid, key, 0 );
702 rc = cursor->c_del( cursor, 0 );
710 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
719 /* initial c_get failed, nothing was done */
721 if ( rc != DB_NOTFOUND ) {
723 LDAP_LOG( INDEX, ERR,
724 "bdb_idl_delete_key: %s failed: %s (%d)\n",
725 err, db_strerror(rc), rc );
727 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
728 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
731 cursor->c_close( cursor );
734 rc = cursor->c_close( cursor );
737 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
738 db_strerror(rc), rc, 0 );
740 Debug( LDAP_DEBUG_ANY,
741 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
742 db_strerror(rc), rc, 0 );
751 * idl_intersection - return a = a intersection b
754 bdb_idl_intersection(
760 ID cursora = 0, cursorb = 0, cursorc;
763 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
768 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
769 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
770 if ( idmin > idmax ) {
773 } else if ( idmin == idmax ) {
779 if ( BDB_IDL_IS_RANGE( a ) ) {
780 if ( BDB_IDL_IS_RANGE(b) ) {
781 /* If both are ranges, just shrink the boundaries */
786 /* Else swap so that b is the range, a is a list */
794 /* If a range completely covers the list, the result is
795 * just the list. If idmin to idmax is contiguous, just
796 * turn it into a range.
798 if ( BDB_IDL_IS_RANGE( b )
799 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
800 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
801 if (idmax - idmin + 1 == a[0])
810 /* Fine, do the intersection one element at a time.
811 * First advance to idmin in both IDLs.
813 cursora = cursorb = idmin;
814 ida = bdb_idl_first( a, &cursora );
815 idb = bdb_idl_first( b, &cursorb );
818 while( ida <= idmax || idb <= idmax ) {
821 ida = bdb_idl_next( a, &cursora );
822 idb = bdb_idl_next( b, &cursorb );
823 } else if ( ida < idb ) {
824 ida = bdb_idl_next( a, &cursora );
826 idb = bdb_idl_next( b, &cursorb );
839 * idl_union - return a = a union b
847 ID cursora = 0, cursorb = 0, cursorc;
849 if ( BDB_IDL_IS_ZERO( b ) ) {
853 if ( BDB_IDL_IS_ZERO( a ) ) {
858 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
859 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
860 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
867 ida = bdb_idl_first( a, &cursora );
868 idb = bdb_idl_first( b, &cursorb );
872 /* The distinct elements of a are cat'd to b */
873 while( ida != NOID || idb != NOID ) {
875 if( ++cursorc > BDB_IDL_UM_MAX ) {
879 ida = bdb_idl_next( a, &cursora );
883 ida = bdb_idl_next( a, &cursora );
884 idb = bdb_idl_next( b, &cursorb );
888 /* b is copied back to a in sorted order */
893 while (cursorb <= b[0] || cursorc <= a[0]) {
898 if (cursorb <= b[0] && b[cursorb] < idb)
899 a[cursora++] = b[cursorb++];
912 * bdb_idl_notin - return a intersection ~b (or a minus b)
921 ID cursora = 0, cursorb = 0;
923 if( BDB_IDL_IS_ZERO( a ) ||
924 BDB_IDL_IS_ZERO( b ) ||
925 BDB_IDL_IS_RANGE( b ) )
927 BDB_IDL_CPY( ids, a );
931 if( BDB_IDL_IS_RANGE( a ) ) {
932 BDB_IDL_CPY( ids, a );
936 ida = bdb_idl_first( a, &cursora ),
937 idb = bdb_idl_first( b, &cursorb );
941 while( ida != NOID ) {
943 /* we could shortcut this */
945 ida = bdb_idl_next( a, &cursora );
947 } else if ( ida < idb ) {
949 ida = bdb_idl_next( a, &cursora );
951 } else if ( ida > idb ) {
952 idb = bdb_idl_next( b, &cursorb );
955 ida = bdb_idl_next( a, &cursora );
956 idb = bdb_idl_next( b, &cursorb );
964 ID bdb_idl_first( ID *ids, ID *cursor )
973 if ( BDB_IDL_IS_RANGE( ids ) ) {
974 if( *cursor < ids[1] ) {
983 pos = bdb_idl_search( ids, *cursor );
993 ID bdb_idl_next( ID *ids, ID *cursor )
995 if ( BDB_IDL_IS_RANGE( ids ) ) {
996 if( ids[2] < ++(*cursor) ) {
1002 if ( ++(*cursor) <= ids[0] ) {
1003 return ids[*cursor];