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) );
240 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
244 assert( ids != NULL );
251 ID buf[BDB_PAGESIZE*4];
256 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
258 data.ulen = sizeof(buf);
259 data.flags = DB_DBT_USERMEM;
264 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
267 LDAP_LOG( INDEX, ERR,
268 "bdb_idl_fetch_key: cursor failed: %s (%d)\n",
269 db_strerror(rc), rc, 0 );
271 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
272 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
276 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
282 DB_MULTIPLE_INIT( ptr, &data );
284 DB_MULTIPLE_NEXT(ptr, &data, j, len);
287 AC_MEMCPY( i, j, sizeof(ID) );
290 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
292 if ( rc == DB_NOTFOUND ) rc = 0;
294 /* On disk, a range is denoted by 0 in the first element */
296 if (ids[0] != BDB_IDL_RANGE_SIZE) {
298 LDAP_LOG( INDEX, ERR,
299 "=> bdb_idl_fetch_key: range size mismatch: "
300 "expected %ld, got %ld\n",
301 BDB_IDL_RANGE_SIZE, ids[0], 0 );
303 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
304 "range size mismatch: expected %d, got %ld\n",
305 BDB_IDL_RANGE_SIZE, ids[0], 0 );
307 cursor->c_close( cursor );
310 BDB_IDL_RANGE( ids, ids[2], ids[3] );
312 data.size = BDB_IDL_SIZEOF(ids);
314 rc2 = cursor->c_close( cursor );
317 LDAP_LOG( INDEX, ERR,
318 "bdb_idl_fetch_key: close failed: %s (%d)\n",
319 db_strerror(rc2), rc2, 0 );
321 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
322 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
329 data.ulen = BDB_IDL_UM_SIZEOF;
330 data.flags = DB_DBT_USERMEM;
332 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
335 if( rc == DB_NOTFOUND ) {
338 } else if( rc != 0 ) {
340 LDAP_LOG( INDEX, ERR,
341 "bdb_idl_fetch_key: get failed: %s (%d)\n",
342 db_strerror(rc), rc, 0 );
344 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
345 "get failed: %s (%d)\n",
346 db_strerror(rc), rc, 0 );
350 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
351 /* size not multiple of ID size */
353 LDAP_LOG( INDEX, ERR,
354 "bdb_idl_fetch_key: odd size: expected %ld multiple, got %ld\n",
355 (long) sizeof( ID ), (long) data.size, 0 );
357 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
358 "odd size: expected %ld multiple, got %ld\n",
359 (long) sizeof( ID ), (long) data.size, 0 );
363 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
366 LDAP_LOG( INDEX, ERR,
367 "bdb_idl_fetch_key: get size mismatch: expected %ld, got %ld\n",
368 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
370 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
371 "get size mismatch: expected %ld, got %ld\n",
372 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
388 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
391 #ifndef BDB_IDL_MULTI
392 ID ids[BDB_IDL_DB_SIZE];
396 /* for printable keys only */
398 LDAP_LOG( INDEX, ARGS, "bdb_idl_insert_key: %s %ld\n",
399 (char *)key->data, (long) id, 0 );
401 Debug( LDAP_DEBUG_ARGS,
402 "=> bdb_idl_insert_key: %s %ld\n",
403 (char *)key->data, (long) id, 0 );
407 assert( id != NOID );
416 data.size = sizeof( ID );
417 data.ulen = data.size;
418 data.flags = DB_DBT_USERMEM;
420 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
423 LDAP_LOG( INDEX, ERR,
424 "bdb_idl_insert_key: cursor failed: %s (%d)\n",
425 db_strerror(rc), rc, 0 );
427 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
428 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
433 /* Fetch the first data item for this key, to see if it
434 * exists and if it's a range.
436 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
440 /* not a range, count the number of items */
442 rc = cursor->c_count( cursor, &count, 0 );
447 if ( count >= BDB_IDL_DB_SIZE ) {
448 /* No room, convert to a range */
451 rc = cursor->c_get( cursor, key, &data, DB_LAST );
460 rc = db->del( db, tid, key, 0 );
467 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
473 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
479 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
485 /* There's room, just store it */
489 /* It's a range, see if we need to rewrite
494 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
501 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
507 if ( id < lo || id > hi ) {
508 /* Delete the current lo/hi */
509 rc = cursor->c_del( cursor, 0 );
515 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
522 } else if ( rc == DB_NOTFOUND ) {
523 put1: data.data = &id;
524 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
525 /* Don't worry if it's already there */
526 if ( rc != 0 && rc != DB_KEYEXIST ) {
531 /* initial c_get failed, nothing was done */
534 LDAP_LOG( INDEX, ERR,
535 "bdb_idl_insert_key: %s failed: %s (%d)\n",
536 err, db_strerror(rc), rc );
538 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
539 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
541 cursor->c_close( cursor );
544 rc = cursor->c_close( cursor );
546 #else /* !BDB_IDL_MULTI */
548 data.ulen = sizeof ids;
549 data.flags = DB_DBT_USERMEM;
551 /* fetch the key for read/modify/write */
552 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
554 if( rc == DB_NOTFOUND ) {
557 data.size = 2 * sizeof( ID );
559 } else if ( rc != 0 ) {
561 LDAP_LOG( INDEX, ERR, "bdb_idl_insert_key: get failed: %s (%d)\n",
562 db_strerror(rc), rc, 0 );
564 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
565 "get failed: %s (%d)\n",
566 db_strerror(rc), rc, 0 );
570 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
571 /* size not multiple of ID size */
573 LDAP_LOG( INDEX, ERR,
574 "bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
575 (long) sizeof( ID ), (long) data.size, 0 );
577 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
578 "odd size: expected %ld multiple, got %ld\n",
579 (long) sizeof( ID ), (long) data.size, 0 );
583 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
586 LDAP_LOG( INDEX, ERR,
587 "bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
588 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
590 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
591 "get size mismatch: expected %ld, got %ld\n",
592 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
596 } else if ( BDB_IDL_IS_RANGE(ids) ) {
599 } else if ( ids[2] > id ) {
606 rc = bdb_idl_insert( ids, id );
610 LDAP_LOG( INDEX, DETAIL1, "bdb_idl_insert_key: dup\n", 0, 0, 0 );
612 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
619 LDAP_LOG( INDEX, ERR,
620 "bdb_idl_insert_key: insert failed: (%d)\n", rc, 0, 0 );
622 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
623 "bdb_idl_insert failed (%d)\n",
630 data.size = BDB_IDL_SIZEOF( ids );
634 rc = db->put( db, tid, key, &data, 0 );
636 if( rc == DB_KEYEXIST ) rc = 0;
640 LDAP_LOG( INDEX, ERR,
641 "bdb_idl_insert_key: put failed: %s (%d)\n",
642 db_strerror(rc), rc, 0 );
644 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
645 "put failed: %s (%d)\n",
646 db_strerror(rc), rc, 0 );
660 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
663 #ifndef BDB_IDL_MULTI
664 ID ids[BDB_IDL_DB_SIZE];
668 /* for printable keys only */
670 LDAP_LOG( INDEX, ARGS, "bdb_idl_delete_key: %s %ld\n",
671 (char *)key->data, (long) id, 0 );
673 Debug( LDAP_DEBUG_ARGS,
674 "=> bdb_idl_delete_key: %s %ld\n",
675 (char *)key->data, (long) id, 0 );
679 assert( id != NOID );
689 data.size = sizeof( id );
690 data.ulen = data.size;
691 data.flags = DB_DBT_USERMEM;
693 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
696 LDAP_LOG( INDEX, ERR,
697 "bdb_idl_delete_key: cursor failed: %s (%d)\n",
698 db_strerror(rc), rc, 0 );
700 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
701 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
705 /* Fetch the first data item for this key, to see if it
706 * exists and if it's a range.
708 rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
712 /* Not a range, just delete it */
714 /* position to correct item */
716 rc = cursor->c_get( cursor, key, &data,
717 DB_GET_BOTH | DB_RMW );
720 rc = cursor->c_del( cursor, 0 );
727 /* It's a range, see if we need to rewrite
732 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
739 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
745 if ( id == lo || id == hi ) {
749 } else if ( id == hi ) {
754 /* The range has collapsed... */
755 rc = db->del( db, tid, key, 0 );
761 rc = cursor->c_del( cursor, 0 );
769 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
778 /* initial c_get failed, nothing was done */
781 LDAP_LOG( INDEX, ERR,
782 "bdb_idl_delete_key: %s failed: %s (%d)\n",
783 err, db_strerror(rc), rc );
785 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
786 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
788 cursor->c_close( cursor );
791 rc = cursor->c_close( cursor );
795 data.ulen = sizeof( ids );
796 data.flags = DB_DBT_USERMEM;
798 /* fetch the key for read/modify/write */
799 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
803 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: get failed: %s (%d)\n",
804 db_strerror(rc), rc, 0 );
806 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
807 "get failed: %s (%d)\n",
808 db_strerror(rc), rc, 0 );
812 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
813 /* size not multiple of ID size */
815 LDAP_LOG( INDEX, ERR,
816 "bdb_idl_delete_key: odd size: expected: %ld multiple, got %ld\n",
817 (long) sizeof( ID ), (long) data.size, 0 );
819 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
820 "odd size: expected %ld multiple, got %ld\n",
821 (long) sizeof( ID ), (long) data.size, 0 );
825 } else if ( BDB_IDL_IS_RANGE(ids) ) {
828 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
831 LDAP_LOG( INDEX, ERR,
832 "bdb_idl_delete_key: get size mismatch: expected: %ld, got %ld\n",
833 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
835 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
836 "get size mismatch: expected %ld, got %ld\n",
837 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
842 rc = idl_delete( ids, id );
846 LDAP_LOG( INDEX, ERR,
847 "bdb_idl_delete_key: delete failed: (%d)\n", rc, 0, 0 );
849 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
850 "idl_delete failed (%d)\n",
858 rc = db->del( db, tid, key, 0 );
861 LDAP_LOG( INDEX, ERR,
862 "bdb_idl_delete_key: delete failed: %s (%d)\n",
863 db_strerror(rc), rc, 0 );
865 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
866 "delete failed: %s (%d)\n",
867 db_strerror(rc), rc, 0 );
873 data.size = (ids[0]+1) * sizeof( ID );
877 rc = db->put( db, tid, key, &data, 0 );
879 #endif /* BDB_IDL_MULTI */
883 LDAP_LOG( INDEX, ERR, "bdb_idl_delete_key: put failed: %s (%d)\n",
884 db_strerror(rc), rc, 0 );
886 Debug( LDAP_DEBUG_ANY,
887 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
888 db_strerror(rc), rc, 0 );
897 * idl_intersection - return a = a intersection b
900 bdb_idl_intersection(
906 ID cursora = 0, cursorb = 0, cursorc;
909 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
914 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
915 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
916 if ( idmin > idmax ) {
919 } else if ( idmin == idmax ) {
925 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
931 if ( BDB_IDL_IS_RANGE( a ) ) {
938 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
939 BDB_IDL_LAST( b ) >= idmax) {
940 if (idmax - idmin + 1 == a[0])
949 ida = bdb_idl_first( a, &cursora );
950 idb = bdb_idl_first( b, &cursorb );
954 ida = bdb_idl_next( a, &cursora );
956 idb = bdb_idl_next( b, &cursorb );
958 while( ida <= idmax || idb <= idmax ) {
961 ida = bdb_idl_next( a, &cursora );
962 idb = bdb_idl_next( b, &cursorb );
963 } else if ( ida < idb ) {
964 ida = bdb_idl_next( a, &cursora );
966 idb = bdb_idl_next( b, &cursorb );
979 * idl_union - return a = a union b
987 ID cursora = 0, cursorb = 0, cursorc;
989 if ( BDB_IDL_IS_ZERO( b ) ) {
993 if ( BDB_IDL_IS_ZERO( a ) ) {
998 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
999 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1000 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1004 ida = bdb_idl_first( a, &cursora );
1005 idb = bdb_idl_first( b, &cursorb );
1009 /* The distinct elements of a are cat'd to b */
1010 while( ida != NOID || idb != NOID ) {
1012 if( ++cursorc > BDB_IDL_UM_MAX ) {
1017 ida = bdb_idl_next( a, &cursora );
1021 ida = bdb_idl_next( a, &cursora );
1022 idb = bdb_idl_next( b, &cursorb );
1026 /* b is copied back to a in sorted order */
1031 while (cursorb <= b[0] || cursorc <= a[0]) {
1036 if (b[cursorb] < idb)
1037 a[cursora++] = b[cursorb++];
1050 * bdb_idl_notin - return a intersection ~b (or a minus b)
1059 ID cursora = 0, cursorb = 0;
1061 if( BDB_IDL_IS_ZERO( a ) ||
1062 BDB_IDL_IS_ZERO( b ) ||
1063 BDB_IDL_IS_RANGE( b ) )
1065 BDB_IDL_CPY( ids, a );
1069 if( BDB_IDL_IS_RANGE( a ) ) {
1070 BDB_IDL_CPY( ids, a );
1074 ida = bdb_idl_first( a, &cursora ),
1075 idb = bdb_idl_first( b, &cursorb );
1079 while( ida != NOID ) {
1080 if ( idb == NOID ) {
1081 /* we could shortcut this */
1082 ids[++ids[0]] = ida;
1083 ida = bdb_idl_next( a, &cursora );
1085 } else if ( ida < idb ) {
1086 ids[++ids[0]] = ida;
1087 ida = bdb_idl_next( a, &cursora );
1089 } else if ( ida > idb ) {
1090 idb = bdb_idl_next( b, &cursorb );
1093 ida = bdb_idl_next( a, &cursora );
1094 idb = bdb_idl_next( b, &cursorb );
1102 ID bdb_idl_first( ID *ids, ID *cursor )
1106 if ( ids[0] == 0 ) {
1111 if ( BDB_IDL_IS_RANGE( ids ) ) {
1112 if( *cursor < ids[1] ) {
1121 pos = bdb_idl_search( ids, *cursor );
1123 if( pos > ids[0] ) {
1131 ID bdb_idl_next( ID *ids, ID *cursor )
1133 if ( BDB_IDL_IS_RANGE( ids ) ) {
1134 if( ids[2] < ++(*cursor) ) {
1140 if ( ++(*cursor) <= ids[0] ) {
1141 return ids[*cursor];