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 /* position to correct item */
690 rc = cursor->c_get( cursor, key, &data,
691 DB_GET_BOTH | DB_RMW );
694 rc = cursor->c_del( cursor, 0 );
701 /* It's a range, see if we need to rewrite
706 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
713 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
719 if ( id == lo || id == hi ) {
723 } else if ( id == hi ) {
728 /* The range has collapsed... */
729 rc = db->del( db, tid, key, 0 );
735 rc = cursor->c_del( cursor, 0 );
743 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
752 /* initial c_get failed, nothing was done */
755 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: %s failed: %s (%d)\n", err, db_strerror(rc), rc ));
757 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
758 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
760 cursor->c_close( cursor );
763 rc = cursor->c_close( cursor );
767 data.ulen = sizeof( ids );
768 data.flags = DB_DBT_USERMEM;
770 /* fetch the key for read/modify/write */
771 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
775 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: get failed: %s (%d)\n", db_strerror(rc), rc ));
777 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
778 "get failed: %s (%d)\n",
779 db_strerror(rc), rc, 0 );
783 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
784 /* size not multiple of ID size */
786 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: odd size: expected: %ld multiple, got %ld\n", (long) sizeof( ID ), (long) data.size ));
788 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
789 "odd size: expected %ld multiple, got %ld\n",
790 (long) sizeof( ID ), (long) data.size, 0 );
794 } else if ( BDB_IDL_IS_RANGE(ids) ) {
797 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
800 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 ));
802 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
803 "get size mismatch: expected %ld, got %ld\n",
804 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
809 rc = idl_delete( ids, id );
813 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: delete failed: (%d)\n", rc ));
815 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
816 "idl_delete failed (%d)\n",
824 rc = db->del( db, tid, key, 0 );
827 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: delete failed: %s (%d)\n", db_strerror(rc), rc ));
829 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
830 "delete failed: %s (%d)\n",
831 db_strerror(rc), rc, 0 );
837 data.size = (ids[0]+1) * sizeof( ID );
841 rc = db->put( db, tid, key, &data, 0 );
843 #endif /* BDB_IDL_MULTI */
847 LDAP_LOG(( "idl", LDAP_LEVEL_ERR, "bdb_idl_delete_key: put failed: %s (%d)\n", db_strerror(rc), rc ));
849 Debug( LDAP_DEBUG_ANY,
850 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
851 db_strerror(rc), rc, 0 );
860 * idl_intersection - return a = a intersection b
863 bdb_idl_intersection(
869 ID cursora = 0, cursorb = 0, cursorc;
872 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
877 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
878 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
879 if ( idmin > idmax ) {
882 } else if ( idmin == idmax ) {
888 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
894 if ( BDB_IDL_IS_RANGE( a ) ) {
901 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
902 BDB_IDL_LAST( b ) >= idmax) {
903 if (idmax - idmin + 1 == a[0])
912 ida = bdb_idl_first( a, &cursora );
913 idb = bdb_idl_first( b, &cursorb );
917 ida = bdb_idl_next( a, &cursora );
919 idb = bdb_idl_next( b, &cursorb );
921 while( ida <= idmax || idb <= idmax ) {
924 ida = bdb_idl_next( a, &cursora );
925 idb = bdb_idl_next( b, &cursorb );
926 } else if ( ida < idb ) {
927 ida = bdb_idl_next( a, &cursora );
929 idb = bdb_idl_next( b, &cursorb );
942 * idl_union - return a = a union b
950 ID cursora = 0, cursorb = 0, cursorc;
952 if ( BDB_IDL_IS_ZERO( b ) ) {
956 if ( BDB_IDL_IS_ZERO( a ) ) {
961 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
962 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
963 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
967 ida = bdb_idl_first( a, &cursora );
968 idb = bdb_idl_first( b, &cursorb );
972 /* The distinct elements of a are cat'd to b */
973 while( ida != NOID || idb != NOID ) {
975 if( ++cursorc > BDB_IDL_UM_MAX ) {
980 ida = bdb_idl_next( a, &cursora );
984 ida = bdb_idl_next( a, &cursora );
985 idb = bdb_idl_next( b, &cursorb );
989 /* b is copied back to a in sorted order */
994 while (cursorb <= b[0] || cursorc <= a[0]) {
999 if (b[cursorb] < idb)
1000 a[cursora++] = b[cursorb++];
1013 * bdb_idl_notin - return a intersection ~b (or a minus b)
1022 ID cursora = 0, cursorb = 0;
1024 if( BDB_IDL_IS_ZERO( a ) ||
1025 BDB_IDL_IS_ZERO( b ) ||
1026 BDB_IDL_IS_RANGE( b ) )
1028 BDB_IDL_CPY( ids, a );
1032 if( BDB_IDL_IS_RANGE( a ) ) {
1033 BDB_IDL_CPY( ids, a );
1037 ida = bdb_idl_first( a, &cursora ),
1038 idb = bdb_idl_first( b, &cursorb );
1042 while( ida != NOID ) {
1043 if ( idb == NOID ) {
1044 /* we could shortcut this */
1045 ids[++ids[0]] = ida;
1046 ida = bdb_idl_next( a, &cursora );
1048 } else if ( ida < idb ) {
1049 ids[++ids[0]] = ida;
1050 ida = bdb_idl_next( a, &cursora );
1052 } else if ( ida > idb ) {
1053 idb = bdb_idl_next( b, &cursorb );
1056 ida = bdb_idl_next( a, &cursora );
1057 idb = bdb_idl_next( b, &cursorb );
1065 ID bdb_idl_first( ID *ids, ID *cursor )
1069 if ( ids[0] == 0 ) {
1074 if ( BDB_IDL_IS_RANGE( ids ) ) {
1075 if( *cursor < ids[1] ) {
1084 pos = bdb_idl_search( ids, *cursor );
1086 if( pos > ids[0] ) {
1094 ID bdb_idl_next( ID *ids, ID *cursor )
1096 if ( BDB_IDL_IS_RANGE( ids ) ) {
1097 if( ids[2] < ++(*cursor) ) {
1103 if ( ++(*cursor) <= ids[0] ) {
1104 return ids[*cursor];