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(( "idl", LDAP_LEVEL_INFO, "IDL: range (%ld - %ld)\n",
40 (long) BDB_IDL_RANGE_FIRST( ids ),
41 (long) BDB_IDL_RANGE_LAST( ids ) ));
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(( "idl", LDAP_LEVEL_INFO, "IDL: size %ld",
55 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
58 for( i=1; i<=ids[0]; i++ ) {
60 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
63 LDAP_LOG(( "idl", LDAP_LEVEL_INFO, "%02lx",(long)ids[i] ));
65 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
69 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
74 #endif /* IDL_DEBUG > 1 */
75 #endif /* IDL_DEBUG > 0 */
77 unsigned bdb_idl_search( ID *ids, ID id )
79 #define IDL_BINARY_SEARCH 1
80 #ifdef IDL_BINARY_SEARCH
82 * binary search of id in ids
83 * if found, returns position of id
84 * if not found, returns first postion greater than id
97 cursor = base + pivot;
98 val = IDL_CMP( id, ids[cursor + 1] );
103 } else if ( val > 0 ) {
119 /* (reverse) linear search */
126 for( i=ids[0]; i; i-- ) {
136 int bdb_idl_insert( ID *ids, ID id )
138 unsigned x = bdb_idl_search( ids, id );
142 LDAP_LOG(( "idl", LDAP_LEVEL_DETAIL1, "insert: %04lx at %d\n",
145 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
159 if ( x <= ids[0] && ids[x] == id ) {
164 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
167 ids[2] = ids[ids[0]-1];
168 } else if ( ids[ids[0]-1] < id ) {
171 ids[2] = ids[ids[0]-1];
177 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
190 static int idl_delete( ID *ids, ID id )
192 unsigned x = bdb_idl_search( ids, id );
196 LDAP_LOG(( "idl", LDAP_LEVEL_DETAIL1, "delete: %04lx at %d\n",
199 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
213 if( x > ids[0] || ids[x] != id ) {
217 } else if ( --ids[0] == 0 ) {
223 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
243 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
247 assert( ids != NULL );
254 ID buf[BDB_PAGESIZE*4];
259 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
261 data.ulen = sizeof(buf);
262 data.flags = DB_DBT_USERMEM;
267 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
270 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_fetch_key: cursor failed: %s (%d)\n", db_strerror(rc), rc ));
272 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
273 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
277 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
283 DB_MULTIPLE_INIT( ptr, &data );
285 DB_MULTIPLE_NEXT(ptr, &data, j, len);
288 AC_MEMCPY( i, j, sizeof(ID) );
291 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
293 if ( rc == DB_NOTFOUND ) rc = 0;
295 /* On disk, a range is denoted by 0 in the first element */
297 if (ids[0] != BDB_IDL_RANGE_SIZE) {
299 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "=> bdb_idl_fetch_key: range size mismatch: expected %ld, got %ld\n", BDB_IDL_RANGE_SIZE, ids[0] ));
301 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
302 "range size mismatch: expected %d, got %ld\n",
303 BDB_IDL_RANGE_SIZE, ids[0], 0 );
305 cursor->c_close( cursor );
308 BDB_IDL_RANGE( ids, ids[2], ids[3] );
310 data.size = BDB_IDL_SIZEOF(ids);
312 rc2 = cursor->c_close( cursor );
315 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_fetch_key: close failed: %s (%d)\n", db_strerror(rc2), rc2 ));
317 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
318 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
325 data.ulen = BDB_IDL_UM_SIZEOF;
326 data.flags = DB_DBT_USERMEM;
328 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
331 if( rc == DB_NOTFOUND ) {
334 } else if( rc != 0 ) {
336 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_fetch_key: get failed: %s (%d)\n", db_strerror(rc), rc ));
338 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
339 "get failed: %s (%d)\n",
340 db_strerror(rc), rc, 0 );
344 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
345 /* size not multiple of ID size */
347 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n", (long) sizeof( ID ), (long) data.size ));
349 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
350 "odd size: expected %ld multiple, got %ld\n",
351 (long) sizeof( ID ), (long) data.size, 0 );
355 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
358 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n", (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size ));
360 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
361 "get size mismatch: expected %ld, got %ld\n",
362 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
378 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
381 #ifndef BDB_IDL_MULTI
382 ID ids[BDB_IDL_DB_SIZE];
386 /* for printable keys only */
388 LDAP_LOG(( "idl", LDAP_LEVEL_ARGS, "bdb_idl_insert_key: %s %ld\n", (char *)key->data, (long) id ));
390 Debug( LDAP_DEBUG_ARGS,
391 "=> bdb_idl_insert_key: %s %ld\n",
392 (char *)key->data, (long) id, 0 );
396 assert( id != NOID );
405 data.size = sizeof( ID );
406 data.ulen = data.size;
407 data.flags = DB_DBT_USERMEM;
409 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
412 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_insert_key: cursor failed: %s (%d)\n", db_strerror(rc), rc ));
414 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
415 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
420 /* Fetch the first data item for this key, to see if it
421 * exists and if it's a range.
423 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
427 /* not a range, count the number of items */
429 rc = cursor->c_count( cursor, &count, 0 );
434 if ( count >= BDB_IDL_DB_SIZE ) {
435 /* No room, convert to a range */
438 rc = cursor->c_get( cursor, key, &data, DB_LAST );
447 rc = db->del( db, tid, key, 0 );
454 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
460 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
466 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
472 /* There's room, just store it */
476 /* It's a range, see if we need to rewrite
481 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
488 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
494 if ( id < lo || id > hi ) {
495 /* Delete the current lo/hi */
496 rc = cursor->c_del( cursor, 0 );
502 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
509 } else if ( rc == DB_NOTFOUND ) {
510 put1: data.data = &id;
511 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
512 /* Don't worry if it's already there */
513 if ( rc != 0 && rc != DB_KEYEXIST ) {
518 /* initial c_get failed, nothing was done */
521 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_insert_key: %s failed: %s (%d)\n", err, db_strerror(rc), rc ));
523 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
524 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
526 cursor->c_close( cursor );
529 rc = cursor->c_close( cursor );
531 #else /* !BDB_IDL_MULTI */
533 data.ulen = sizeof ids;
534 data.flags = DB_DBT_USERMEM;
536 /* fetch the key for read/modify/write */
537 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
539 if( rc == DB_NOTFOUND ) {
542 data.size = 2 * sizeof( ID );
544 } else if ( rc != 0 ) {
546 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_insert_key: get failed: %s (%d)\n", db_strerror(rc), rc ));
548 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
549 "get failed: %s (%d)\n",
550 db_strerror(rc), rc, 0 );
554 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
555 /* size not multiple of ID size */
557 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n", (long) sizeof( ID ), (long) data.size ));
559 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
560 "odd size: expected %ld multiple, got %ld\n",
561 (long) sizeof( ID ), (long) data.size, 0 );
565 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
568 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n", (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size ));
570 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
571 "get size mismatch: expected %ld, got %ld\n",
572 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
576 } else if ( BDB_IDL_IS_RANGE(ids) ) {
579 } else if ( ids[2] > id ) {
586 rc = bdb_idl_insert( ids, id );
590 LDAP_LOG(( "idl", LDAP_LEVEL_DETAIL1, "bdb_idl_insert_key: dup\n" ));
592 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
599 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_insert_key: insert failed: (%d)\n", rc ));
601 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
602 "bdb_idl_insert failed (%d)\n",
609 data.size = BDB_IDL_SIZEOF( ids );
613 rc = db->put( db, tid, key, &data, 0 );
615 if( rc == DB_KEYEXIST ) rc = 0;
619 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_insert_key: put failed: (%d)\n", db_strerror(rc), rc ));
621 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
622 "put failed: %s (%d)\n",
623 db_strerror(rc), rc, 0 );
637 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
640 #ifndef BDB_IDL_MULTI
641 ID ids[BDB_IDL_DB_SIZE];
645 /* for printable keys only */
647 LDAP_LOG(( "idl", LDAP_LEVEL_ARGS, "bdb_idl_delete_key: %s %ld\n", (char *)key->data, (long) id ));
649 Debug( LDAP_DEBUG_ARGS,
650 "=> bdb_idl_delete_key: %s %ld\n",
651 (char *)key->data, (long) id, 0 );
655 assert( id != NOID );
665 data.size = sizeof( id );
666 data.ulen = data.size;
667 data.flags = DB_DBT_USERMEM;
669 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
672 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: cursor failed: %s (%d)\n", db_strerror(rc), rc ));
674 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
675 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
679 /* Fetch the first data item for this key, to see if it
680 * exists and if it's a range.
682 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
686 /* Not a range, just delete it */
688 rc = cursor->c_get( cursor, key, &data,
689 DB_GET_BOTH | DB_RMW );
691 rc = cursor->c_del( cursor, 0 );
698 /* It's a range, see if we need to rewrite
703 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
710 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
716 if ( id == lo || id == hi ) {
720 } else if ( id == hi ) {
725 /* The range has collapsed... */
726 rc = db->del( db, tid, key, 0 );
732 rc = cursor->c_del( cursor, 0 );
740 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
749 /* initial c_get failed, nothing was done */
752 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: %s failed: %s (%d)\n", err, db_strerror(rc), rc ));
754 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
755 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
757 cursor->c_close( cursor );
760 rc = cursor->c_close( cursor );
764 data.ulen = sizeof( ids );
765 data.flags = DB_DBT_USERMEM;
767 /* fetch the key for read/modify/write */
768 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
772 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: get failed: %s (%d)\n", db_strerror(rc), rc ));
774 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
775 "get failed: %s (%d)\n",
776 db_strerror(rc), rc, 0 );
780 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
781 /* size not multiple of ID size */
783 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: odd size: expected: %ld multiple, got %ld\n", (long) sizeof( ID ), (long) data.size ));
785 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
786 "odd size: expected %ld multiple, got %ld\n",
787 (long) sizeof( ID ), (long) data.size, 0 );
791 } else if ( BDB_IDL_IS_RANGE(ids) ) {
794 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
797 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: get size mismatch: expected: %ld, got %ld\n", (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size ));
799 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
800 "get size mismatch: expected %ld, got %ld\n",
801 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
806 rc = idl_delete( ids, id );
810 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: delete failed: (%d)\n", rc ));
812 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
813 "idl_delete failed (%d)\n",
821 rc = db->del( db, tid, key, 0 );
824 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: delete failed: %s (%d)\n", db_strerror(rc), rc ));
826 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
827 "delete failed: %s (%d)\n",
828 db_strerror(rc), rc, 0 );
834 data.size = (ids[0]+1) * sizeof( ID );
838 rc = db->put( db, tid, key, &data, 0 );
840 #endif /* BDB_IDL_MULTI */
844 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: put failed: %s (%d)\n", db_strerror(rc), rc ));
846 Debug( LDAP_DEBUG_ANY,
847 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
848 db_strerror(rc), rc, 0 );
857 * idl_intersection - return a = a intersection b
860 bdb_idl_intersection(
866 ID cursora = 0, cursorb = 0, cursorc;
869 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
874 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
875 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
876 if ( idmin > idmax ) {
879 } else if ( idmin == idmax ) {
885 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
891 if ( BDB_IDL_IS_RANGE( a ) ) {
898 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
899 BDB_IDL_LAST( b ) >= idmax) {
900 if (idmax - idmin + 1 == a[0])
909 ida = bdb_idl_first( a, &cursora );
910 idb = bdb_idl_first( b, &cursorb );
914 ida = bdb_idl_next( a, &cursora );
916 idb = bdb_idl_next( b, &cursorb );
918 while( ida <= idmax || idb <= idmax ) {
921 ida = bdb_idl_next( a, &cursora );
922 idb = bdb_idl_next( b, &cursorb );
923 } else if ( ida < idb ) {
924 ida = bdb_idl_next( a, &cursora );
926 idb = bdb_idl_next( b, &cursorb );
939 * idl_union - return a = a union b
947 ID cursora = 0, cursorb = 0, cursorc;
949 if ( BDB_IDL_IS_ZERO( b ) ) {
953 if ( BDB_IDL_IS_ZERO( a ) ) {
958 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
959 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
960 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
964 ida = bdb_idl_first( a, &cursora );
965 idb = bdb_idl_first( b, &cursorb );
969 /* The distinct elements of a are cat'd to b */
970 while( ida != NOID || idb != NOID ) {
972 if( ++cursorc > BDB_IDL_UM_MAX ) {
977 ida = bdb_idl_next( a, &cursora );
981 ida = bdb_idl_next( a, &cursora );
982 idb = bdb_idl_next( b, &cursorb );
986 /* b is copied back to a in sorted order */
991 while (cursorb <= b[0] || cursorc <= a[0]) {
996 if (b[cursorb] < idb)
997 a[cursora++] = b[cursorb++];
1010 * bdb_idl_notin - return a intersection ~b (or a minus b)
1019 ID cursora = 0, cursorb = 0;
1021 if( BDB_IDL_IS_ZERO( a ) ||
1022 BDB_IDL_IS_ZERO( b ) ||
1023 BDB_IDL_IS_RANGE( b ) )
1025 BDB_IDL_CPY( ids, a );
1029 if( BDB_IDL_IS_RANGE( a ) ) {
1030 BDB_IDL_CPY( ids, a );
1034 ida = bdb_idl_first( a, &cursora ),
1035 idb = bdb_idl_first( b, &cursorb );
1039 while( ida != NOID ) {
1040 if ( idb == NOID ) {
1041 /* we could shortcut this */
1042 ids[++ids[0]] = ida;
1043 ida = bdb_idl_next( a, &cursora );
1045 } else if ( ida < idb ) {
1046 ids[++ids[0]] = ida;
1047 ida = bdb_idl_next( a, &cursora );
1049 } else if ( ida > idb ) {
1050 idb = bdb_idl_next( b, &cursorb );
1053 ida = bdb_idl_next( a, &cursora );
1054 idb = bdb_idl_next( b, &cursorb );
1062 ID bdb_idl_first( ID *ids, ID *cursor )
1066 if ( ids[0] == 0 ) {
1071 if ( BDB_IDL_IS_RANGE( ids ) ) {
1072 if( *cursor < ids[1] ) {
1081 pos = bdb_idl_search( ids, *cursor );
1083 if( pos > ids[0] ) {
1091 ID bdb_idl_next( ID *ids, ID *cursor )
1093 if ( BDB_IDL_IS_RANGE( ids ) ) {
1094 if( ids[2] < ++(*cursor) ) {
1100 if ( ++(*cursor) <= ids[0] ) {
1101 return ids[*cursor];