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 ) ) {
38 Debug( LDAP_DEBUG_ANY,
39 "IDL: range ( %ld - %ld )\n",
40 (long) BDB_IDL_RANGE_FIRST( ids ),
41 (long) BDB_IDL_RANGE_LAST( ids ) );
45 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
47 for( i=1; i<=ids[0]; i++ ) {
49 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
51 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
54 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
59 #endif /* IDL_DEBUG > 1 */
60 #endif /* IDL_DEBUG > 0 */
62 unsigned bdb_idl_search( ID *ids, ID id )
64 #define IDL_BINARY_SEARCH 1
65 #ifdef IDL_BINARY_SEARCH
67 * binary search of id in ids
68 * if found, returns position of id
69 * if not found, returns first postion greater than id
82 cursor = base + pivot;
83 val = IDL_CMP( id, ids[cursor + 1] );
88 } else if ( val > 0 ) {
104 /* (reverse) linear search */
111 for( i=ids[0]; i; i-- ) {
121 int bdb_idl_insert( ID *ids, ID id )
123 unsigned x = bdb_idl_search( ids, id );
126 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
139 if ( x <= ids[0] && ids[x] == id ) {
144 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
147 ids[2] = ids[ids[0]-1];
148 } else if ( ids[ids[0]-1] < id ) {
151 ids[2] = ids[ids[0]-1];
157 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
170 static int idl_delete( ID *ids, ID id )
172 unsigned x = bdb_idl_search( ids, id );
175 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
188 if( x > ids[0] || ids[x] != id ) {
192 } else if ( --ids[0] == 0 ) {
198 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
218 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
222 assert( ids != NULL );
229 ID buf[BDB_IDL_DB_SIZE];
234 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
236 data.ulen = sizeof(buf);
237 data.flags = DB_DBT_USERMEM;
242 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
244 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
245 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
248 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
254 DB_MULTIPLE_INIT( ptr, &data );
256 DB_MULTIPLE_NEXT(ptr, &data, j, len);
259 AC_MEMCPY( i, j, sizeof(ID) );
262 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
264 if ( rc == DB_NOTFOUND ) rc = 0;
266 /* On disk, a range is denoted by 0 in the first element */
268 if (ids[0] != BDB_IDL_RANGE_SIZE) {
269 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
270 "range size mismatch: expected %ld, got %ld\n",
271 BDB_IDL_RANGE_SIZE, ids[0], 0 );
272 cursor->c_close( cursor );
275 BDB_IDL_RANGE( ids, ids[2], ids[3] );
277 data.size = BDB_IDL_SIZEOF(ids);
279 rc2 = cursor->c_close( cursor );
281 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
282 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
288 data.ulen = BDB_IDL_UM_SIZEOF;
289 data.flags = DB_DBT_USERMEM;
291 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
294 if( rc == DB_NOTFOUND ) {
297 } else if( rc != 0 ) {
298 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
299 "get failed: %s (%d)\n",
300 db_strerror(rc), rc, 0 );
303 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
304 /* size not multiple of ID size */
305 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
306 "odd size: expected %ld multiple, got %ld\n",
307 (long) sizeof( ID ), (long) data.size, 0 );
310 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
312 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
313 "get size mismatch: expected %ld, got %ld\n",
314 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
329 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
332 #ifndef BDB_IDL_MULTI
333 ID ids[BDB_IDL_DB_SIZE];
336 /* for printable keys only */
337 Debug( LDAP_DEBUG_ARGS,
338 "=> bdb_idl_insert_key: %s %ld\n",
339 (char *)key->data, (long) id, 0 );
341 assert( id != NOID );
346 ID buf[BDB_IDL_DB_SIZE];
351 data.size = sizeof( ID );
352 data.ulen = data.size;
353 data.flags = DB_DBT_USERMEM;
355 rc = bdb_idl_fetch_key( be, db, tid, key, buf );
356 if ( rc && rc != DB_NOTFOUND )
359 /* If it never existed, or there's room in the current key,
362 if ( rc == DB_NOTFOUND || ( !BDB_IDL_IS_RANGE(buf) &&
363 BDB_IDL_N(buf) < BDB_IDL_DB_MAX ) ) {
365 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
366 } else if ( BDB_IDL_IS_RANGE(buf) ) {
367 /* If it's a range and we're outside the boundaries,
368 * rewrite the range boundaries.
370 if ( id < BDB_IDL_RANGE_FIRST(buf) ||
371 id > BDB_IDL_RANGE_LAST(buf) ) {
372 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
374 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
375 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
378 if ( id < BDB_IDL_RANGE_FIRST(buf) ) {
383 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH | DB_RMW );
386 fail: Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
387 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
388 if ( cursor ) cursor->c_close( cursor );
392 /* We should have been able to just overwrite the old
393 * value with the new, but apparently we have to delete
396 rc = cursor->c_del( cursor, 0 );
401 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
406 rc = cursor->c_close( cursor );
413 } else { /* convert to a range */
414 lo = BDB_IDL_FIRST(buf);
415 hi = BDB_IDL_LAST(buf);
424 /* Delete all of the old IDL so we can replace with a range */
425 rc = db->del( db, tid, key, 0 );
431 /* Write the range */
434 rc = db->put( db, tid, key, &data, 0 );
440 rc = db->put( db, tid, key, &data, 0 );
446 rc = db->put( db, tid, key, &data, 0 );
455 data.ulen = sizeof ids;
456 data.flags = DB_DBT_USERMEM;
458 /* fetch the key for read/modify/write */
459 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
461 if( rc == DB_NOTFOUND ) {
464 data.size = 2 * sizeof( ID );
466 } else if ( rc != 0 ) {
467 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
468 "get failed: %s (%d)\n",
469 db_strerror(rc), rc, 0 );
472 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
473 /* size not multiple of ID size */
474 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
475 "odd size: expected %ld multiple, got %ld\n",
476 (long) sizeof( ID ), (long) data.size, 0 );
479 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
481 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
482 "get size mismatch: expected %ld, got %ld\n",
483 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
486 } else if ( BDB_IDL_IS_RANGE(ids) ) {
489 } else if ( ids[2] > id ) {
496 rc = bdb_idl_insert( ids, id );
499 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
504 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
505 "bdb_idl_insert failed (%d)\n",
511 data.size = BDB_IDL_SIZEOF( ids );
515 rc = db->put( db, tid, key, &data, 0 );
517 if( rc == DB_KEYEXIST ) rc = 0;
520 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
521 "put failed: %s (%d)\n",
522 db_strerror(rc), rc, 0 );
535 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
538 #ifndef BDB_IDL_MULTI
539 ID ids[BDB_IDL_DB_SIZE];
542 /* for printable keys only */
543 Debug( LDAP_DEBUG_ARGS,
544 "=> bdb_idl_delete_key: %s %ld\n",
545 (char *)key->data, (long) id, 0 );
547 assert( id != NOID );
555 data.size = sizeof( id );
556 data.ulen = data.size;
557 data.flags = DB_DBT_USERMEM;
559 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
560 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
561 DB_GET_BOTH | DB_RMW );
563 rc = cursor->c_del( cursor, 0 );
564 rc = cursor->c_close( cursor );
568 data.ulen = sizeof( ids );
569 data.flags = DB_DBT_USERMEM;
571 /* fetch the key for read/modify/write */
572 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
575 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
576 "get failed: %s (%d)\n",
577 db_strerror(rc), rc, 0 );
580 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
581 /* size not multiple of ID size */
582 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
583 "odd size: expected %ld multiple, got %ld\n",
584 (long) sizeof( ID ), (long) data.size, 0 );
587 } else if ( BDB_IDL_IS_RANGE(ids) ) {
590 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
592 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
593 "get size mismatch: expected %ld, got %ld\n",
594 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
598 rc = idl_delete( ids, id );
601 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
602 "idl_delete failed (%d)\n",
609 rc = db->del( db, tid, key, 0 );
611 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
612 "delete failed: %s (%d)\n",
613 db_strerror(rc), rc, 0 );
618 data.size = (ids[0]+1) * sizeof( ID );
622 rc = db->put( db, tid, key, &data, 0 );
624 #endif /* BDB_IDL_MULTI */
627 Debug( LDAP_DEBUG_ANY,
628 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
629 db_strerror(rc), rc, 0 );
637 * idl_intersection - return a = a intersection b
640 bdb_idl_intersection(
646 ID cursora = 0, cursorb = 0, cursorc;
649 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
654 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
655 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
656 if ( idmin > idmax ) {
659 } else if ( idmin == idmax ) {
665 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
671 if ( BDB_IDL_IS_RANGE( a ) ) {
678 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
679 BDB_IDL_LAST( b ) >= idmax) {
680 if (idmax - idmin + 1 == a[0])
689 ida = bdb_idl_first( a, &cursora );
690 idb = bdb_idl_first( b, &cursorb );
694 ida = bdb_idl_next( a, &cursora );
696 idb = bdb_idl_next( b, &cursorb );
698 while( ida <= idmax || idb <= idmax ) {
701 ida = bdb_idl_next( a, &cursora );
702 idb = bdb_idl_next( b, &cursorb );
703 } else if ( ida < idb ) {
704 ida = bdb_idl_next( a, &cursora );
706 idb = bdb_idl_next( b, &cursorb );
719 * idl_union - return a = a union b
727 ID cursora = 0, cursorb = 0, cursorc;
729 if ( BDB_IDL_IS_ZERO( b ) ) {
733 if ( BDB_IDL_IS_ZERO( a ) ) {
738 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
739 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
740 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
744 ida = bdb_idl_first( a, &cursora );
745 idb = bdb_idl_first( b, &cursorb );
749 /* The distinct elements of a are cat'd to b */
750 while( ida != NOID || idb != NOID ) {
752 if( ++cursorc > BDB_IDL_UM_MAX ) {
757 ida = bdb_idl_next( a, &cursora );
761 ida = bdb_idl_next( a, &cursora );
762 idb = bdb_idl_next( b, &cursorb );
766 /* b is copied back to a in sorted order */
771 while (cursorb <= b[0] || cursorc <= a[0]) {
776 if (b[cursorb] < idb)
777 a[cursora++] = b[cursorb++];
790 * bdb_idl_notin - return a intersection ~b (or a minus b)
799 ID cursora = 0, cursorb = 0;
801 if( BDB_IDL_IS_ZERO( a ) ||
802 BDB_IDL_IS_ZERO( b ) ||
803 BDB_IDL_IS_RANGE( b ) )
805 BDB_IDL_CPY( ids, a );
809 if( BDB_IDL_IS_RANGE( a ) ) {
810 BDB_IDL_CPY( ids, a );
814 ida = bdb_idl_first( a, &cursora ),
815 idb = bdb_idl_first( b, &cursorb );
819 while( ida != NOID ) {
821 /* we could shortcut this */
823 ida = bdb_idl_next( a, &cursora );
825 } else if ( ida < idb ) {
827 ida = bdb_idl_next( a, &cursora );
829 } else if ( ida > idb ) {
830 idb = bdb_idl_next( b, &cursorb );
833 ida = bdb_idl_next( a, &cursora );
834 idb = bdb_idl_next( b, &cursorb );
842 ID bdb_idl_first( ID *ids, ID *cursor )
851 if ( BDB_IDL_IS_RANGE( ids ) ) {
852 if( *cursor < ids[1] ) {
861 pos = bdb_idl_search( ids, *cursor );
871 ID bdb_idl_next( ID *ids, ID *cursor )
873 if ( BDB_IDL_IS_RANGE( ids ) ) {
874 if( ids[2] < ++(*cursor) ) {
880 if ( ++(*cursor) <= ids[0] ) {