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];
240 data.ulen = BDB_IDL_UM_SIZEOF;
241 data.flags = DB_DBT_USERMEM;
243 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
245 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
246 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
249 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
250 DB_SET | DB_MULTIPLE );
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, bdb->bi_db_opflags |
263 DB_NEXT_DUP | DB_MULTIPLE );
265 if ( rc == DB_NOTFOUND ) rc = 0;
267 /* On disk, a range is denoted by 0 in the first element */
269 if (ids[0] != BDB_IDL_RANGE_SIZE) {
270 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
271 "range size mismatch: expected %ld, got %ld\n",
272 BDB_IDL_RANGE_SIZE, ids[0], 0 );
273 cursor->c_close( cursor );
276 BDB_IDL_RANGE( ids, ids[2], ids[3] );
278 data.size = BDB_IDL_SIZEOF(ids);
280 rc2 = cursor->c_close( cursor );
282 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
283 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
289 data.ulen = BDB_IDL_UM_SIZEOF;
290 data.flags = DB_DBT_USERMEM;
292 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
295 if( rc == DB_NOTFOUND ) {
298 } else if( rc != 0 ) {
299 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
300 "get failed: %s (%d)\n",
301 db_strerror(rc), rc, 0 );
304 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
305 /* size not multiple of ID size */
306 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
307 "odd size: expected %ld multiple, got %ld\n",
308 (long) sizeof( ID ), (long) data.size, 0 );
311 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
313 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
314 "get size mismatch: expected %ld, got %ld\n",
315 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
330 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
333 #ifndef BDB_IDL_MULTI
334 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 );
342 assert( id != NOID );
347 ID buf[BDB_IDL_DB_MAX];
352 data.size = sizeof( ID );
353 data.ulen = data.size;
354 data.flags = DB_DBT_USERMEM;
356 rc = bdb_idl_fetch_key( be, db, tid, key, buf );
357 if ( rc && rc != DB_NOTFOUND )
360 /* If it never existed, or there's room in the current key,
363 if ( rc == DB_NOTFOUND || ( !BDB_IDL_IS_RANGE(buf) &&
364 BDB_IDL_N(buf) < BDB_IDL_DB_MAX ) ) {
366 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
367 } else if ( BDB_IDL_IS_RANGE(buf) ) {
368 /* If it's a range and we're outside the boundaries,
369 * rewrite the range boundaries.
371 if ( id < BDB_IDL_RANGE_FIRST(buf) ||
372 id > BDB_IDL_RANGE_LAST(buf) ) {
373 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
375 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
376 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
379 if ( id < BDB_IDL_RANGE_FIRST(buf) ) {
384 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
387 fail: Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
388 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
389 if ( cursor ) cursor->c_close( cursor );
393 /* We should have been able to just overwrite the old
394 * value with the new, but apparently we have to delete
397 rc = cursor->c_del( cursor, 0 );
402 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
407 rc = cursor->c_close( cursor );
414 } else { /* convert to a range */
415 lo = BDB_IDL_FIRST(buf);
416 hi = BDB_IDL_LAST(buf);
425 /* Delete all of the old IDL so we can replace with a range */
426 rc = db->del( db, tid, key, 0 );
432 /* Write the range */
435 rc = db->put( db, tid, key, &data, 0 );
441 rc = db->put( db, tid, key, &data, 0 );
447 rc = db->put( db, tid, key, &data, 0 );
456 data.ulen = sizeof ids;
457 data.flags = DB_DBT_USERMEM;
459 /* fetch the key for read/modify/write */
460 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
462 if( rc == DB_NOTFOUND ) {
465 data.size = 2 * sizeof( ID );
467 } else if ( rc != 0 ) {
468 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
469 "get failed: %s (%d)\n",
470 db_strerror(rc), rc, 0 );
473 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
474 /* size not multiple of ID size */
475 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
476 "odd size: expected %ld multiple, got %ld\n",
477 (long) sizeof( ID ), (long) data.size, 0 );
480 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
482 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
483 "get size mismatch: expected %ld, got %ld\n",
484 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
487 } else if ( BDB_IDL_IS_RANGE(ids) ) {
490 } else if ( ids[2] > id ) {
497 rc = bdb_idl_insert( ids, id );
500 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
505 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
506 "bdb_idl_insert failed (%d)\n",
512 data.size = BDB_IDL_SIZEOF( ids );
516 rc = db->put( db, tid, key, &data, 0 );
518 if( rc == DB_KEYEXIST ) rc = 0;
521 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
522 "put failed: %s (%d)\n",
523 db_strerror(rc), rc, 0 );
536 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
539 #ifndef BDB_IDL_MULTI
540 ID ids[BDB_IDL_DB_SIZE];
543 /* for printable keys only */
544 Debug( LDAP_DEBUG_ARGS,
545 "=> bdb_idl_delete_key: %s %ld\n",
546 (char *)key->data, (long) id, 0 );
548 assert( id != NOID );
556 data.size = sizeof( id );
557 data.ulen = data.size;
558 data.flags = DB_DBT_USERMEM;
560 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
561 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
562 DB_GET_BOTH | DB_RMW );
564 rc = cursor->c_del( cursor, 0 );
565 rc = cursor->c_close( cursor );
569 data.ulen = sizeof( ids );
570 data.flags = DB_DBT_USERMEM;
572 /* fetch the key for read/modify/write */
573 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
576 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
577 "get failed: %s (%d)\n",
578 db_strerror(rc), rc, 0 );
581 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
582 /* size not multiple of ID size */
583 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
584 "odd size: expected %ld multiple, got %ld\n",
585 (long) sizeof( ID ), (long) data.size, 0 );
588 } else if ( BDB_IDL_IS_RANGE(ids) ) {
591 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
593 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
594 "get size mismatch: expected %ld, got %ld\n",
595 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
599 rc = idl_delete( ids, id );
602 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
603 "idl_delete failed (%d)\n",
610 rc = db->del( db, tid, key, 0 );
612 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
613 "delete failed: %s (%d)\n",
614 db_strerror(rc), rc, 0 );
619 data.size = (ids[0]+1) * sizeof( ID );
623 rc = db->put( db, tid, key, &data, 0 );
625 #endif /* BDB_IDL_MULTI */
628 Debug( LDAP_DEBUG_ANY,
629 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
630 db_strerror(rc), rc, 0 );
638 * idl_intersection - return a = a intersection b
641 bdb_idl_intersection(
647 ID cursora = 0, cursorb = 0, cursorc;
650 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
655 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
656 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
657 if ( idmin > idmax ) {
660 } else if ( idmin == idmax ) {
666 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
672 if ( BDB_IDL_IS_RANGE( a ) ) {
679 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
680 BDB_IDL_LAST( b ) >= idmax) {
681 if (idmax - idmin + 1 == a[0])
690 ida = bdb_idl_first( a, &cursora );
691 idb = bdb_idl_first( b, &cursorb );
695 ida = bdb_idl_next( a, &cursora );
697 idb = bdb_idl_next( b, &cursorb );
699 while( ida <= idmax || idb <= idmax ) {
702 ida = bdb_idl_next( a, &cursora );
703 idb = bdb_idl_next( b, &cursorb );
704 } else if ( ida < idb ) {
705 ida = bdb_idl_next( a, &cursora );
707 idb = bdb_idl_next( b, &cursorb );
720 * idl_union - return a = a union b
728 ID cursora = 0, cursorb = 0, cursorc;
730 if ( BDB_IDL_IS_ZERO( b ) ) {
734 if ( BDB_IDL_IS_ZERO( a ) ) {
739 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
740 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
741 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
745 ida = bdb_idl_first( a, &cursora );
746 idb = bdb_idl_first( b, &cursorb );
750 /* The distinct elements of a are cat'd to b */
751 while( ida != NOID || idb != NOID ) {
753 if( ++cursorc > BDB_IDL_UM_MAX ) {
758 ida = bdb_idl_next( a, &cursora );
762 ida = bdb_idl_next( a, &cursora );
763 idb = bdb_idl_next( b, &cursorb );
767 /* b is copied back to a in sorted order */
772 while (cursorb <= b[0] || cursorc <= a[0]) {
777 if (b[cursorb] < idb)
778 a[cursora++] = b[cursorb++];
791 * bdb_idl_notin - return a intersection ~b (or a minus b)
800 ID cursora = 0, cursorb = 0;
802 if( BDB_IDL_IS_ZERO( a ) ||
803 BDB_IDL_IS_ZERO( b ) ||
804 BDB_IDL_IS_RANGE( b ) )
806 BDB_IDL_CPY( ids, a );
810 if( BDB_IDL_IS_RANGE( a ) ) {
811 BDB_IDL_CPY( ids, a );
815 ida = bdb_idl_first( a, &cursora ),
816 idb = bdb_idl_first( b, &cursorb );
820 while( ida != NOID ) {
822 /* we could shortcut this */
824 ida = bdb_idl_next( a, &cursora );
826 } else if ( ida < idb ) {
828 ida = bdb_idl_next( a, &cursora );
830 } else if ( ida > idb ) {
831 idb = bdb_idl_next( b, &cursorb );
834 ida = bdb_idl_next( a, &cursora );
835 idb = bdb_idl_next( b, &cursorb );
843 ID bdb_idl_first( ID *ids, ID *cursor )
852 if ( BDB_IDL_IS_RANGE( ids ) ) {
853 if( *cursor < ids[1] ) {
862 pos = bdb_idl_search( ids, *cursor );
872 ID bdb_idl_next( ID *ids, ID *cursor )
874 if ( BDB_IDL_IS_RANGE( ids ) ) {
875 if( ids[2] < ++(*cursor) ) {
881 if ( ++(*cursor) <= ids[0] ) {