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];
337 /* for printable keys only */
338 Debug( LDAP_DEBUG_ARGS,
339 "=> bdb_idl_insert_key: %s %ld\n",
340 (char *)key->data, (long) id, 0 );
343 assert( id != NOID );
348 ID buf[BDB_IDL_DB_SIZE];
353 data.size = sizeof( ID );
354 data.ulen = data.size;
355 data.flags = DB_DBT_USERMEM;
357 rc = bdb_idl_fetch_key( be, db, tid, key, buf );
358 if ( rc && rc != DB_NOTFOUND )
361 /* If it never existed, or there's room in the current key,
364 if ( rc == DB_NOTFOUND || ( !BDB_IDL_IS_RANGE(buf) &&
365 BDB_IDL_N(buf) < BDB_IDL_DB_MAX ) ) {
367 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
368 } else if ( BDB_IDL_IS_RANGE(buf) ) {
369 /* If it's a range and we're outside the boundaries,
370 * rewrite the range boundaries.
372 if ( id < BDB_IDL_RANGE_FIRST(buf) ||
373 id > BDB_IDL_RANGE_LAST(buf) ) {
374 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
376 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
377 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
380 if ( id < BDB_IDL_RANGE_FIRST(buf) ) {
385 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH | DB_RMW );
388 fail: Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
389 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
390 if ( cursor ) cursor->c_close( cursor );
394 /* We should have been able to just overwrite the old
395 * value with the new, but apparently we have to delete
398 rc = cursor->c_del( cursor, 0 );
403 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
408 rc = cursor->c_close( cursor );
415 } else { /* convert to a range */
416 lo = BDB_IDL_FIRST(buf);
417 hi = BDB_IDL_LAST(buf);
426 /* Delete all of the old IDL so we can replace with a range */
427 rc = db->del( db, tid, key, 0 );
433 /* Write the range */
436 rc = db->put( db, tid, key, &data, 0 );
442 rc = db->put( db, tid, key, &data, 0 );
448 rc = db->put( db, tid, key, &data, 0 );
457 data.ulen = sizeof ids;
458 data.flags = DB_DBT_USERMEM;
460 /* fetch the key for read/modify/write */
461 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
463 if( rc == DB_NOTFOUND ) {
466 data.size = 2 * sizeof( ID );
468 } else if ( rc != 0 ) {
469 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
470 "get failed: %s (%d)\n",
471 db_strerror(rc), rc, 0 );
474 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
475 /* size not multiple of ID size */
476 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
477 "odd size: expected %ld multiple, got %ld\n",
478 (long) sizeof( ID ), (long) data.size, 0 );
481 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
483 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
484 "get size mismatch: expected %ld, got %ld\n",
485 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
488 } else if ( BDB_IDL_IS_RANGE(ids) ) {
491 } else if ( ids[2] > id ) {
498 rc = bdb_idl_insert( ids, id );
501 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
506 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
507 "bdb_idl_insert failed (%d)\n",
513 data.size = BDB_IDL_SIZEOF( ids );
517 rc = db->put( db, tid, key, &data, 0 );
519 if( rc == DB_KEYEXIST ) rc = 0;
522 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
523 "put failed: %s (%d)\n",
524 db_strerror(rc), rc, 0 );
537 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
540 #ifndef BDB_IDL_MULTI
541 ID ids[BDB_IDL_DB_SIZE];
545 /* for printable keys only */
546 Debug( LDAP_DEBUG_ARGS,
547 "=> bdb_idl_delete_key: %s %ld\n",
548 (char *)key->data, (long) id, 0 );
551 assert( id != NOID );
559 data.size = sizeof( id );
560 data.ulen = data.size;
561 data.flags = DB_DBT_USERMEM;
563 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
564 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
565 DB_GET_BOTH | DB_RMW );
567 rc = cursor->c_del( cursor, 0 );
568 rc = cursor->c_close( cursor );
572 data.ulen = sizeof( ids );
573 data.flags = DB_DBT_USERMEM;
575 /* fetch the key for read/modify/write */
576 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
579 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
580 "get failed: %s (%d)\n",
581 db_strerror(rc), rc, 0 );
584 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
585 /* size not multiple of ID size */
586 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
587 "odd size: expected %ld multiple, got %ld\n",
588 (long) sizeof( ID ), (long) data.size, 0 );
591 } else if ( BDB_IDL_IS_RANGE(ids) ) {
594 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
596 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
597 "get size mismatch: expected %ld, got %ld\n",
598 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
602 rc = idl_delete( ids, id );
605 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
606 "idl_delete failed (%d)\n",
613 rc = db->del( db, tid, key, 0 );
615 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
616 "delete failed: %s (%d)\n",
617 db_strerror(rc), rc, 0 );
622 data.size = (ids[0]+1) * sizeof( ID );
626 rc = db->put( db, tid, key, &data, 0 );
628 #endif /* BDB_IDL_MULTI */
631 Debug( LDAP_DEBUG_ANY,
632 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
633 db_strerror(rc), rc, 0 );
641 * idl_intersection - return a = a intersection b
644 bdb_idl_intersection(
650 ID cursora = 0, cursorb = 0, cursorc;
653 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
658 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
659 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
660 if ( idmin > idmax ) {
663 } else if ( idmin == idmax ) {
669 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
675 if ( BDB_IDL_IS_RANGE( a ) ) {
682 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
683 BDB_IDL_LAST( b ) >= idmax) {
684 if (idmax - idmin + 1 == a[0])
693 ida = bdb_idl_first( a, &cursora );
694 idb = bdb_idl_first( b, &cursorb );
698 ida = bdb_idl_next( a, &cursora );
700 idb = bdb_idl_next( b, &cursorb );
702 while( ida <= idmax || idb <= idmax ) {
705 ida = bdb_idl_next( a, &cursora );
706 idb = bdb_idl_next( b, &cursorb );
707 } else if ( ida < idb ) {
708 ida = bdb_idl_next( a, &cursora );
710 idb = bdb_idl_next( b, &cursorb );
723 * idl_union - return a = a union b
731 ID cursora = 0, cursorb = 0, cursorc;
733 if ( BDB_IDL_IS_ZERO( b ) ) {
737 if ( BDB_IDL_IS_ZERO( a ) ) {
742 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
743 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
744 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
748 ida = bdb_idl_first( a, &cursora );
749 idb = bdb_idl_first( b, &cursorb );
753 /* The distinct elements of a are cat'd to b */
754 while( ida != NOID || idb != NOID ) {
756 if( ++cursorc > BDB_IDL_UM_MAX ) {
761 ida = bdb_idl_next( a, &cursora );
765 ida = bdb_idl_next( a, &cursora );
766 idb = bdb_idl_next( b, &cursorb );
770 /* b is copied back to a in sorted order */
775 while (cursorb <= b[0] || cursorc <= a[0]) {
780 if (b[cursorb] < idb)
781 a[cursora++] = b[cursorb++];
794 * bdb_idl_notin - return a intersection ~b (or a minus b)
803 ID cursora = 0, cursorb = 0;
805 if( BDB_IDL_IS_ZERO( a ) ||
806 BDB_IDL_IS_ZERO( b ) ||
807 BDB_IDL_IS_RANGE( b ) )
809 BDB_IDL_CPY( ids, a );
813 if( BDB_IDL_IS_RANGE( a ) ) {
814 BDB_IDL_CPY( ids, a );
818 ida = bdb_idl_first( a, &cursora ),
819 idb = bdb_idl_first( b, &cursorb );
823 while( ida != NOID ) {
825 /* we could shortcut this */
827 ida = bdb_idl_next( a, &cursora );
829 } else if ( ida < idb ) {
831 ida = bdb_idl_next( a, &cursora );
833 } else if ( ida > idb ) {
834 idb = bdb_idl_next( b, &cursorb );
837 ida = bdb_idl_next( a, &cursora );
838 idb = bdb_idl_next( b, &cursorb );
846 ID bdb_idl_first( ID *ids, ID *cursor )
855 if ( BDB_IDL_IS_RANGE( ids ) ) {
856 if( *cursor < ids[1] ) {
865 pos = bdb_idl_search( ids, *cursor );
875 ID bdb_idl_next( ID *ids, ID *cursor )
877 if ( BDB_IDL_IS_RANGE( ids ) ) {
878 if( ids[2] < ++(*cursor) ) {
884 if ( ++(*cursor) <= ids[0] ) {