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) );
189 static int idl_delete( ID *ids, ID id )
191 unsigned x = bdb_idl_search( ids, id );
195 LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 );
197 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
211 if( x > ids[0] || ids[x] != id ) {
215 } else if ( --ids[0] == 0 ) {
221 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
239 if ( key->size == sizeof( ID ) ) {
240 unsigned char *c = key->data;
241 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
256 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
264 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
266 /* buf must be large enough to grab the entire IDL in one
267 * get(), otherwise BDB 4 will leak resources on subsequent
268 * get's. We can safely call get() twice - once for the data,
269 * and once to get the DB_NOTFOUND result meaning there's
270 * no more data. See ITS#2040 for details. This bug is fixed
271 * in BDB 4.1 so a smaller buffer will work if stack space is
274 ID buf[BDB_IDL_DB_SIZE*5];
279 LDAP_LOG( INDEX, ARGS,
280 "bdb_idl_fetch_key: %s\n",
281 bdb_show_key( key, buf ), 0, 0 );
283 Debug( LDAP_DEBUG_ARGS,
284 "bdb_idl_fetch_key: %s\n",
285 bdb_show_key( key, buf ), 0, 0 );
288 assert( ids != NULL );
293 data.ulen = sizeof(buf);
294 data.flags = DB_DBT_USERMEM;
299 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
302 LDAP_LOG( INDEX, ERR,
303 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
304 db_strerror(rc), rc, 0 );
306 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
307 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
311 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
317 DB_MULTIPLE_INIT( ptr, &data );
319 DB_MULTIPLE_NEXT(ptr, &data, j, len);
322 AC_MEMCPY( i, j, sizeof(ID) );
325 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
327 if ( rc == DB_NOTFOUND ) rc = 0;
329 /* On disk, a range is denoted by 0 in the first element */
331 if (ids[0] != BDB_IDL_RANGE_SIZE) {
333 LDAP_LOG( INDEX, ERR,
334 "=> bdb_idl_fetch_key: range size mismatch: "
335 "expected %ld, got %ld\n",
336 BDB_IDL_RANGE_SIZE, ids[0], 0 );
338 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
339 "range size mismatch: expected %d, got %ld\n",
340 BDB_IDL_RANGE_SIZE, ids[0], 0 );
342 cursor->c_close( cursor );
345 BDB_IDL_RANGE( ids, ids[2], ids[3] );
347 data.size = BDB_IDL_SIZEOF(ids);
349 rc2 = cursor->c_close( cursor );
352 LDAP_LOG( INDEX, ERR,
353 "bdb_idl_fetch_key: close failed: %s (%d)\n",
354 db_strerror(rc2), rc2, 0 );
356 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
357 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
361 if( rc == DB_NOTFOUND ) {
364 } else if( rc != 0 ) {
366 LDAP_LOG( INDEX, ERR,
367 "bdb_idl_fetch_key: get failed: %s (%d)\n",
368 db_strerror(rc), rc, 0 );
370 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
371 "get failed: %s (%d)\n",
372 db_strerror(rc), rc, 0 );
376 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
377 /* size not multiple of ID size */
379 LDAP_LOG( INDEX, ERR,
380 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
381 (long) sizeof( ID ), (long) data.size, 0 );
383 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
384 "odd size: expected %ld multiple, got %ld\n",
385 (long) sizeof( ID ), (long) data.size, 0 );
389 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
392 LDAP_LOG( INDEX, ERR,
393 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
394 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
396 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
397 "get size mismatch: expected %ld, got %ld\n",
398 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
415 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
425 LDAP_LOG( INDEX, ARGS,
426 "bdb_idl_insert_key: %lx %s\n",
427 (long) id, bdb_show_key( key, buf ), 0 );
429 Debug( LDAP_DEBUG_ARGS,
430 "bdb_idl_insert_key: %lx %s\n",
431 (long) id, bdb_show_key( key, buf ), 0 );
435 assert( id != NOID );
438 data.size = sizeof( ID );
439 data.ulen = data.size;
440 data.flags = DB_DBT_USERMEM;
442 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
445 LDAP_LOG( INDEX, ERR,
446 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
447 db_strerror(rc), rc, 0 );
449 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
450 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
455 /* Fetch the first data item for this key, to see if it
456 * exists and if it's a range.
458 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
462 /* not a range, count the number of items */
464 rc = cursor->c_count( cursor, &count, 0 );
469 if ( count >= BDB_IDL_DB_MAX ) {
470 /* No room, convert to a range */
473 key2.dlen = key2.ulen;
474 key2.flags |= DB_DBT_PARTIAL;
478 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
479 if ( rc != 0 && rc != DB_NOTFOUND ) {
480 err = "c_get next_nodup";
483 if ( rc == DB_NOTFOUND ) {
484 rc = cursor->c_get( cursor, key, &data, DB_LAST );
490 rc = cursor->c_get( cursor, key, &data, DB_PREV );
500 rc = db->del( db, tid, key, 0 );
507 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
513 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
519 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
525 /* There's room, just store it */
529 /* It's a range, see if we need to rewrite
534 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
541 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
547 if ( id < lo || id > hi ) {
548 /* Delete the current lo/hi */
549 rc = cursor->c_del( cursor, 0 );
555 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
562 } else if ( rc == DB_NOTFOUND ) {
563 put1: data.data = &id;
564 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
565 /* Don't worry if it's already there */
566 if ( rc != 0 && rc != DB_KEYEXIST ) {
571 /* initial c_get failed, nothing was done */
574 LDAP_LOG( INDEX, ERR,
575 "bdb_idl_insert_key: %s failed: %s (%d)\n",
576 err, db_strerror(rc), rc );
578 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
579 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
581 cursor->c_close( cursor );
584 rc = cursor->c_close( cursor );
587 LDAP_LOG( INDEX, ERR,
588 "bdb_idl_insert_key: c_close failed: %s (%d)\n",
589 db_strerror(rc), rc, 0 );
591 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
592 "c_close failed: %s (%d)\n",
593 db_strerror(rc), rc, 0 );
607 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
617 LDAP_LOG( INDEX, ARGS,
618 "bdb_idl_delete_key: %lx %s\n",
619 (long) id, bdb_show_key( key, buf ), 0 );
621 Debug( LDAP_DEBUG_ARGS,
622 "bdb_idl_delete_key: %lx %s\n",
623 (long) id, bdb_show_key( key, buf ), 0 );
626 assert( id != NOID );
630 data.size = sizeof( id );
631 data.ulen = data.size;
632 data.flags = DB_DBT_USERMEM;
634 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
637 LDAP_LOG( INDEX, ERR,
638 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
639 db_strerror(rc), rc, 0 );
641 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
642 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
646 /* Fetch the first data item for this key, to see if it
647 * exists and if it's a range.
649 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
653 /* Not a range, just delete it */
655 /* position to correct item */
657 rc = cursor->c_get( cursor, key, &data,
658 DB_GET_BOTH | DB_RMW );
664 rc = cursor->c_del( cursor, 0 );
670 /* It's a range, see if we need to rewrite
675 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
682 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
688 if ( id == lo || id == hi ) {
692 } else if ( id == hi ) {
697 /* The range has collapsed... */
698 rc = db->del( db, tid, key, 0 );
704 rc = cursor->c_del( cursor, 0 );
712 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
721 /* initial c_get failed, nothing was done */
723 if ( rc != DB_NOTFOUND ) {
725 LDAP_LOG( INDEX, ERR,
726 "bdb_idl_delete_key: %s failed: %s (%d)\n",
727 err, db_strerror(rc), rc );
729 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
730 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
733 cursor->c_close( cursor );
736 rc = cursor->c_close( cursor );
739 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: c_close failed: %s (%d)\n",
740 db_strerror(rc), rc, 0 );
742 Debug( LDAP_DEBUG_ANY,
743 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
744 db_strerror(rc), rc, 0 );
753 * idl_intersection - return a = a intersection b
756 bdb_idl_intersection(
762 ID cursora = 0, cursorb = 0, cursorc;
765 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
770 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
771 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
772 if ( idmin > idmax ) {
775 } else if ( idmin == idmax ) {
781 if ( BDB_IDL_IS_RANGE( a ) ) {
782 if ( BDB_IDL_IS_RANGE(b) ) {
783 /* If both are ranges, just shrink the boundaries */
788 /* Else swap so that b is the range, a is a list */
796 /* If a range completely covers the list, the result is
797 * just the list. If idmin to idmax is contiguous, just
798 * turn it into a range.
800 if ( BDB_IDL_IS_RANGE( b )
801 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
802 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
803 if (idmax - idmin + 1 == a[0])
812 /* Fine, do the intersection one element at a time.
813 * First advance to idmin in both IDLs.
815 cursora = cursorb = idmin;
816 ida = bdb_idl_first( a, &cursora );
817 idb = bdb_idl_first( b, &cursorb );
820 while( ida <= idmax || idb <= idmax ) {
823 ida = bdb_idl_next( a, &cursora );
824 idb = bdb_idl_next( b, &cursorb );
825 } else if ( ida < idb ) {
826 ida = bdb_idl_next( a, &cursora );
828 idb = bdb_idl_next( b, &cursorb );
841 * idl_union - return a = a union b
849 ID cursora = 0, cursorb = 0, cursorc;
851 if ( BDB_IDL_IS_ZERO( b ) ) {
855 if ( BDB_IDL_IS_ZERO( a ) ) {
860 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
861 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
862 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
869 ida = bdb_idl_first( a, &cursora );
870 idb = bdb_idl_first( b, &cursorb );
874 /* The distinct elements of a are cat'd to b */
875 while( ida != NOID || idb != NOID ) {
877 if( ++cursorc > BDB_IDL_UM_MAX ) {
881 ida = bdb_idl_next( a, &cursora );
885 ida = bdb_idl_next( a, &cursora );
886 idb = bdb_idl_next( b, &cursorb );
890 /* b is copied back to a in sorted order */
895 while (cursorb <= b[0] || cursorc <= a[0]) {
900 if (cursorb <= b[0] && b[cursorb] < idb)
901 a[cursora++] = b[cursorb++];
914 * bdb_idl_notin - return a intersection ~b (or a minus b)
923 ID cursora = 0, cursorb = 0;
925 if( BDB_IDL_IS_ZERO( a ) ||
926 BDB_IDL_IS_ZERO( b ) ||
927 BDB_IDL_IS_RANGE( b ) )
929 BDB_IDL_CPY( ids, a );
933 if( BDB_IDL_IS_RANGE( a ) ) {
934 BDB_IDL_CPY( ids, a );
938 ida = bdb_idl_first( a, &cursora ),
939 idb = bdb_idl_first( b, &cursorb );
943 while( ida != NOID ) {
945 /* we could shortcut this */
947 ida = bdb_idl_next( a, &cursora );
949 } else if ( ida < idb ) {
951 ida = bdb_idl_next( a, &cursora );
953 } else if ( ida > idb ) {
954 idb = bdb_idl_next( b, &cursorb );
957 ida = bdb_idl_next( a, &cursora );
958 idb = bdb_idl_next( b, &cursorb );
966 ID bdb_idl_first( ID *ids, ID *cursor )
975 if ( BDB_IDL_IS_RANGE( ids ) ) {
976 if( *cursor < ids[1] ) {
985 pos = bdb_idl_search( ids, *cursor );
995 ID bdb_idl_next( ID *ids, ID *cursor )
997 if ( BDB_IDL_IS_RANGE( ids ) ) {
998 if( ids[2] < ++(*cursor) ) {
1004 if ( ++(*cursor) <= ids[0] ) {
1005 return ids[*cursor];