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 /* enable basic checks for now */
27 static void idl_check( ID *ids )
29 if( BDB_IDL_IS_RANGE( ids ) ) {
30 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
33 for( i=1; i < ids[0]; i++ ) {
34 assert( ids[i+1] > ids[i] );
40 static void idl_dump( ID *ids )
42 if( BDB_IDL_IS_RANGE( 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 ) );
50 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
52 for( i=1; i<=ids[0]; i++ ) {
54 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
56 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
59 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
64 #endif /* IDL_DEBUG > 1 */
65 #endif /* IDL_DEBUG > 0 */
67 unsigned bdb_idl_search( ID *ids, ID id )
69 #define IDL_BINARY_SEARCH 1
70 #ifdef IDL_BINARY_SEARCH
72 * binary search of id in ids
73 * if found, returns position of id
74 * if not found, returns first postion greater than id
87 cursor = base + pivot;
88 val = IDL_CMP( id, ids[cursor + 1] );
93 } else if ( val > 0 ) {
109 /* (reverse) linear search */
116 for( i=ids[0]; i; i-- ) {
126 int bdb_idl_insert( ID *ids, ID id )
128 unsigned x = bdb_idl_search( ids, id );
131 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
144 if ( x <= ids[0] && ids[x] == id ) {
149 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
152 ids[2] = ids[ids[0]-1];
153 } else if ( ids[ids[0]-1] < id ) {
156 ids[2] = ids[ids[0]-1];
162 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
175 static int idl_delete( ID *ids, ID id )
177 unsigned x = bdb_idl_search( ids, id );
180 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
193 if( x > ids[0] || ids[x] != id ) {
197 } else if ( --ids[0] == 0 ) {
203 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
223 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
227 assert( ids != NULL );
234 ID buf[BDB_IDL_UM_SIZE];
239 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
241 data.ulen = BDB_IDL_UM_SIZEOF;
242 data.flags = DB_DBT_USERMEM;
247 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
249 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
250 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
253 rc = cursor->c_get( cursor, key, &data, flags | DB_SET );
259 DB_MULTIPLE_INIT( ptr, &data );
261 DB_MULTIPLE_NEXT(ptr, &data, j, len);
264 AC_MEMCPY( i, j, sizeof(ID) );
267 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
269 if ( rc == DB_NOTFOUND ) rc = 0;
271 /* On disk, a range is denoted by 0 in the first element */
273 if (ids[0] != BDB_IDL_RANGE_SIZE) {
274 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
275 "range size mismatch: expected %ld, got %ld\n",
276 BDB_IDL_RANGE_SIZE, ids[0], 0 );
277 cursor->c_close( cursor );
280 BDB_IDL_RANGE( ids, ids[2], ids[3] );
282 data.size = BDB_IDL_SIZEOF(ids);
284 rc2 = cursor->c_close( cursor );
286 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
287 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
293 data.ulen = BDB_IDL_UM_SIZEOF;
294 data.flags = DB_DBT_USERMEM;
296 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
299 if( rc == DB_NOTFOUND ) {
302 } else if( rc != 0 ) {
303 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
304 "get failed: %s (%d)\n",
305 db_strerror(rc), rc, 0 );
308 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
309 /* size not multiple of ID size */
310 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
311 "odd size: expected %ld multiple, got %ld\n",
312 (long) sizeof( ID ), (long) data.size, 0 );
315 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
317 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
318 "get size mismatch: expected %ld, got %ld\n",
319 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
334 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
337 #ifndef BDB_IDL_MULTI
338 ID ids[BDB_IDL_DB_SIZE];
341 /* for printable keys only */
342 Debug( LDAP_DEBUG_ARGS,
343 "=> bdb_idl_insert_key: %s %ld\n",
344 (char *)key->data, (long) id, 0 );
346 assert( id != NOID );
351 ID buf[BDB_IDL_DB_MAX];
356 data.size = sizeof( ID );
357 data.ulen = data.size;
358 data.flags = DB_DBT_USERMEM;
360 rc = bdb_idl_fetch_key( be, db, tid, key, buf );
361 if ( rc && rc != DB_NOTFOUND )
364 /* If it never existed, or there's room in the current key,
367 if ( rc == DB_NOTFOUND || ( !BDB_IDL_IS_RANGE(buf) &&
368 BDB_IDL_N(buf) < BDB_IDL_DB_MAX ) ) {
370 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
371 } else if ( BDB_IDL_IS_RANGE(buf) ) {
372 /* If it's a range and we're outside the boundaries,
373 * rewrite the range boundaries.
375 if ( id < BDB_IDL_RANGE_FIRST(buf) ||
376 id > BDB_IDL_RANGE_LAST(buf) ) {
377 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
379 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
380 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
383 if ( id < BDB_IDL_RANGE_FIRST(buf) ) {
388 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH | DB_RMW );
391 fail: Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
392 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
393 if ( cursor ) cursor->c_close( cursor );
397 /* We should have been able to just overwrite the old
398 * value with the new, but apparently we have to delete
401 rc = cursor->c_del( cursor, 0 );
406 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
411 rc = cursor->c_close( cursor );
418 } else { /* convert to a range */
419 lo = BDB_IDL_FIRST(buf);
420 hi = BDB_IDL_LAST(buf);
429 /* Delete all of the old IDL so we can replace with a range */
430 rc = db->del( db, tid, key, 0 );
436 /* Write the range */
439 rc = db->put( db, tid, key, &data, 0 );
445 rc = db->put( db, tid, key, &data, 0 );
451 rc = db->put( db, tid, key, &data, 0 );
460 data.ulen = sizeof ids;
461 data.flags = DB_DBT_USERMEM;
463 /* fetch the key for read/modify/write */
464 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
466 if( rc == DB_NOTFOUND ) {
469 data.size = 2 * sizeof( ID );
471 } else if ( rc != 0 ) {
472 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
473 "get failed: %s (%d)\n",
474 db_strerror(rc), rc, 0 );
477 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
478 /* size not multiple of ID size */
479 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
480 "odd size: expected %ld multiple, got %ld\n",
481 (long) sizeof( ID ), (long) data.size, 0 );
484 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
486 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
487 "get size mismatch: expected %ld, got %ld\n",
488 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
491 } else if ( BDB_IDL_IS_RANGE(ids) ) {
494 } else if ( ids[2] > id ) {
501 rc = bdb_idl_insert( ids, id );
504 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
509 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
510 "bdb_idl_insert failed (%d)\n",
516 data.size = BDB_IDL_SIZEOF( ids );
520 rc = db->put( db, tid, key, &data, 0 );
522 if( rc == DB_KEYEXIST ) rc = 0;
525 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
526 "put failed: %s (%d)\n",
527 db_strerror(rc), rc, 0 );
540 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
543 #ifndef BDB_IDL_MULTI
544 ID ids[BDB_IDL_DB_SIZE];
547 /* for printable keys only */
548 Debug( LDAP_DEBUG_ARGS,
549 "=> bdb_idl_delete_key: %s %ld\n",
550 (char *)key->data, (long) id, 0 );
552 assert( id != NOID );
560 data.size = sizeof( id );
561 data.ulen = data.size;
562 data.flags = DB_DBT_USERMEM;
564 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
565 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
566 DB_GET_BOTH | DB_RMW );
568 rc = cursor->c_del( cursor, 0 );
569 rc = cursor->c_close( cursor );
573 data.ulen = sizeof( ids );
574 data.flags = DB_DBT_USERMEM;
576 /* fetch the key for read/modify/write */
577 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
580 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
581 "get failed: %s (%d)\n",
582 db_strerror(rc), rc, 0 );
585 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
586 /* size not multiple of ID size */
587 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
588 "odd size: expected %ld multiple, got %ld\n",
589 (long) sizeof( ID ), (long) data.size, 0 );
592 } else if ( BDB_IDL_IS_RANGE(ids) ) {
595 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
597 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
598 "get size mismatch: expected %ld, got %ld\n",
599 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
603 rc = idl_delete( ids, id );
606 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
607 "idl_delete failed (%d)\n",
614 rc = db->del( db, tid, key, 0 );
616 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
617 "delete failed: %s (%d)\n",
618 db_strerror(rc), rc, 0 );
623 data.size = (ids[0]+1) * sizeof( ID );
627 rc = db->put( db, tid, key, &data, 0 );
629 #endif /* BDB_IDL_MULTI */
632 Debug( LDAP_DEBUG_ANY,
633 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
634 db_strerror(rc), rc, 0 );
642 * idl_intersection - return a = a intersection b
645 bdb_idl_intersection(
651 ID cursora = 0, cursorb = 0, cursorc;
654 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
659 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
660 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
661 if ( idmin > idmax ) {
664 } else if ( idmin == idmax ) {
670 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
676 if ( BDB_IDL_IS_RANGE( a ) ) {
683 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
684 BDB_IDL_LAST( b ) >= idmax) {
685 if (idmax - idmin + 1 == a[0])
694 ida = bdb_idl_first( a, &cursora );
695 idb = bdb_idl_first( b, &cursorb );
699 ida = bdb_idl_next( a, &cursora );
701 idb = bdb_idl_next( b, &cursorb );
703 while( ida <= idmax || idb <= idmax ) {
706 ida = bdb_idl_next( a, &cursora );
707 idb = bdb_idl_next( b, &cursorb );
708 } else if ( ida < idb ) {
709 ida = bdb_idl_next( a, &cursora );
711 idb = bdb_idl_next( b, &cursorb );
724 * idl_union - return a = a union b
732 ID cursora = 0, cursorb = 0, cursorc;
734 if ( BDB_IDL_IS_ZERO( b ) ) {
738 if ( BDB_IDL_IS_ZERO( a ) ) {
743 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
744 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
745 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
749 ida = bdb_idl_first( a, &cursora );
750 idb = bdb_idl_first( b, &cursorb );
754 /* The distinct elements of a are cat'd to b */
755 while( ida != NOID || idb != NOID ) {
757 if( ++cursorc > BDB_IDL_UM_MAX ) {
762 ida = bdb_idl_next( a, &cursora );
766 ida = bdb_idl_next( a, &cursora );
767 idb = bdb_idl_next( b, &cursorb );
771 /* b is copied back to a in sorted order */
776 while (cursorb <= b[0] || cursorc <= a[0]) {
781 if (b[cursorb] < idb)
782 a[cursora++] = b[cursorb++];
795 * bdb_idl_notin - return a intersection ~b (or a minus b)
804 ID cursora = 0, cursorb = 0;
806 if( BDB_IDL_IS_ZERO( a ) ||
807 BDB_IDL_IS_ZERO( b ) ||
808 BDB_IDL_IS_RANGE( b ) )
810 BDB_IDL_CPY( ids, a );
814 if( BDB_IDL_IS_RANGE( a ) ) {
815 BDB_IDL_CPY( ids, a );
819 ida = bdb_idl_first( a, &cursora ),
820 idb = bdb_idl_first( b, &cursorb );
824 while( ida != NOID ) {
826 /* we could shortcut this */
828 ida = bdb_idl_next( a, &cursora );
830 } else if ( ida < idb ) {
832 ida = bdb_idl_next( a, &cursora );
834 } else if ( ida > idb ) {
835 idb = bdb_idl_next( b, &cursorb );
838 ida = bdb_idl_next( a, &cursora );
839 idb = bdb_idl_next( b, &cursorb );
847 ID bdb_idl_first( ID *ids, ID *cursor )
856 if ( BDB_IDL_IS_RANGE( ids ) ) {
857 if( *cursor < ids[1] ) {
866 pos = bdb_idl_search( ids, *cursor );
876 ID bdb_idl_next( ID *ids, ID *cursor )
878 if ( BDB_IDL_IS_RANGE( ids ) ) {
879 if( ids[2] < ++(*cursor) ) {
885 if ( ++(*cursor) <= ids[0] ) {