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 );
257 DB_MULTIPLE_INIT( ptr, &data );
259 DB_MULTIPLE_NEXT(ptr, &data, j, len);
262 AC_MEMCPY( i, j, sizeof(ID) );
265 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
267 if ( rc == DB_NOTFOUND ) rc = 0;
269 /* On disk, a range is denoted by 0 in the first element */
271 if (ids[0] != BDB_IDL_RANGE_SIZE) {
272 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
273 "range size mismatch: expected %ld, got %ld\n",
274 BDB_IDL_RANGE_SIZE, ids[0], 0 );
275 cursor->c_close( cursor );
278 BDB_IDL_RANGE( ids, ids[2], ids[3] );
280 data.size = BDB_IDL_SIZEOF(ids);
282 rc2 = cursor->c_close( cursor );
284 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
285 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
291 data.ulen = BDB_IDL_UM_SIZEOF;
292 data.flags = DB_DBT_USERMEM;
294 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
297 if( rc == DB_NOTFOUND ) {
300 } else if( rc != 0 ) {
301 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
302 "get failed: %s (%d)\n",
303 db_strerror(rc), rc, 0 );
306 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
307 /* size not multiple of ID size */
308 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
309 "odd size: expected %ld multiple, got %ld\n",
310 (long) sizeof( ID ), (long) data.size, 0 );
313 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
315 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
316 "get size mismatch: expected %ld, got %ld\n",
317 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
332 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
335 #ifndef BDB_IDL_MULTI
336 ID ids[BDB_IDL_DB_SIZE];
339 /* for printable keys only */
340 Debug( LDAP_DEBUG_ARGS,
341 "=> bdb_idl_insert_key: %s %ld\n",
342 (char *)key->data, (long) id, 0 );
344 assert( id != NOID );
349 ID buf[BDB_IDL_DB_MAX];
354 data.size = sizeof( ID );
355 data.ulen = data.size;
356 data.flags = DB_DBT_USERMEM;
358 rc = bdb_idl_fetch_key( be, db, tid, key, buf );
359 if ( rc && rc != DB_NOTFOUND )
362 /* If it never existed, or there's room in the current key,
365 if ( rc == DB_NOTFOUND || ( !BDB_IDL_IS_RANGE(buf) &&
366 BDB_IDL_N(buf) < BDB_IDL_DB_MAX ) ) {
368 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
369 } else if ( BDB_IDL_IS_RANGE(buf) ) {
370 /* If it's a range and we're outside the boundaries,
371 * rewrite the range boundaries.
373 if ( id < BDB_IDL_RANGE_FIRST(buf) ||
374 id > BDB_IDL_RANGE_LAST(buf) ) {
375 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
377 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
378 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
381 if ( id < BDB_IDL_RANGE_FIRST(buf) ) {
386 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH | DB_RMW );
389 fail: Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
390 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
391 if ( cursor ) cursor->c_close( cursor );
395 /* We should have been able to just overwrite the old
396 * value with the new, but apparently we have to delete
399 rc = cursor->c_del( cursor, 0 );
404 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
409 rc = cursor->c_close( cursor );
416 } else { /* convert to a range */
417 lo = BDB_IDL_FIRST(buf);
418 hi = BDB_IDL_LAST(buf);
427 /* Delete all of the old IDL so we can replace with a range */
428 rc = db->del( db, tid, key, 0 );
434 /* Write the range */
437 rc = db->put( db, tid, key, &data, 0 );
443 rc = db->put( db, tid, key, &data, 0 );
449 rc = db->put( db, tid, key, &data, 0 );
458 data.ulen = sizeof ids;
459 data.flags = DB_DBT_USERMEM;
461 /* fetch the key for read/modify/write */
462 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
464 if( rc == DB_NOTFOUND ) {
467 data.size = 2 * sizeof( ID );
469 } else if ( rc != 0 ) {
470 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
471 "get failed: %s (%d)\n",
472 db_strerror(rc), rc, 0 );
475 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
476 /* size not multiple of ID size */
477 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
478 "odd size: expected %ld multiple, got %ld\n",
479 (long) sizeof( ID ), (long) data.size, 0 );
482 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
484 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
485 "get size mismatch: expected %ld, got %ld\n",
486 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
489 } else if ( BDB_IDL_IS_RANGE(ids) ) {
492 } else if ( ids[2] > id ) {
499 rc = bdb_idl_insert( ids, id );
502 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
507 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
508 "bdb_idl_insert failed (%d)\n",
514 data.size = BDB_IDL_SIZEOF( ids );
518 rc = db->put( db, tid, key, &data, 0 );
520 if( rc == DB_KEYEXIST ) rc = 0;
523 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
524 "put failed: %s (%d)\n",
525 db_strerror(rc), rc, 0 );
538 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
541 #ifndef BDB_IDL_MULTI
542 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 );
550 assert( id != NOID );
558 data.size = sizeof( id );
559 data.ulen = data.size;
560 data.flags = DB_DBT_USERMEM;
562 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
563 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
564 DB_GET_BOTH | DB_RMW );
566 rc = cursor->c_del( cursor, 0 );
567 rc = cursor->c_close( cursor );
571 data.ulen = sizeof( ids );
572 data.flags = DB_DBT_USERMEM;
574 /* fetch the key for read/modify/write */
575 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
578 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
579 "get failed: %s (%d)\n",
580 db_strerror(rc), rc, 0 );
583 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
584 /* size not multiple of ID size */
585 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
586 "odd size: expected %ld multiple, got %ld\n",
587 (long) sizeof( ID ), (long) data.size, 0 );
590 } else if ( BDB_IDL_IS_RANGE(ids) ) {
593 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
595 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
596 "get size mismatch: expected %ld, got %ld\n",
597 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
601 rc = idl_delete( ids, id );
604 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
605 "idl_delete failed (%d)\n",
612 rc = db->del( db, tid, key, 0 );
614 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
615 "delete failed: %s (%d)\n",
616 db_strerror(rc), rc, 0 );
621 data.size = (ids[0]+1) * sizeof( ID );
625 rc = db->put( db, tid, key, &data, 0 );
627 #endif /* BDB_IDL_MULTI */
630 Debug( LDAP_DEBUG_ANY,
631 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
632 db_strerror(rc), rc, 0 );
640 * idl_intersection - return a = a intersection b
643 bdb_idl_intersection(
649 ID cursora = 0, cursorb = 0, cursorc;
652 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
657 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
658 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
659 if ( idmin > idmax ) {
662 } else if ( idmin == idmax ) {
668 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
674 if ( BDB_IDL_IS_RANGE( a ) ) {
681 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
682 BDB_IDL_LAST( b ) >= idmax) {
683 if (idmax - idmin + 1 == a[0])
692 ida = bdb_idl_first( a, &cursora );
693 idb = bdb_idl_first( b, &cursorb );
697 ida = bdb_idl_next( a, &cursora );
699 idb = bdb_idl_next( b, &cursorb );
701 while( ida <= idmax || idb <= idmax ) {
704 ida = bdb_idl_next( a, &cursora );
705 idb = bdb_idl_next( b, &cursorb );
706 } else if ( ida < idb ) {
707 ida = bdb_idl_next( a, &cursora );
709 idb = bdb_idl_next( b, &cursorb );
722 * idl_union - return a = a union b
730 ID cursora = 0, cursorb = 0, cursorc;
732 if ( BDB_IDL_IS_ZERO( b ) ) {
736 if ( BDB_IDL_IS_ZERO( a ) ) {
741 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
742 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
743 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
747 ida = bdb_idl_first( a, &cursora );
748 idb = bdb_idl_first( b, &cursorb );
752 /* The distinct elements of a are cat'd to b */
753 while( ida != NOID || idb != NOID ) {
755 if( ++cursorc > BDB_IDL_UM_MAX ) {
760 ida = bdb_idl_next( a, &cursora );
764 ida = bdb_idl_next( a, &cursora );
765 idb = bdb_idl_next( b, &cursorb );
769 /* b is copied back to a in sorted order */
774 while (cursorb <= b[0] || cursorc <= a[0]) {
779 if (b[cursorb] < idb)
780 a[cursora++] = b[cursorb++];
793 * bdb_idl_notin - return a intersection ~b (or a minus b)
802 ID cursora = 0, cursorb = 0;
804 if( BDB_IDL_IS_ZERO( a ) ||
805 BDB_IDL_IS_ZERO( b ) ||
806 BDB_IDL_IS_RANGE( b ) )
808 BDB_IDL_CPY( ids, a );
812 if( BDB_IDL_IS_RANGE( a ) ) {
813 BDB_IDL_CPY( ids, a );
817 ida = bdb_idl_first( a, &cursora ),
818 idb = bdb_idl_first( b, &cursorb );
822 while( ida != NOID ) {
824 /* we could shortcut this */
826 ida = bdb_idl_next( a, &cursora );
828 } else if ( ida < idb ) {
830 ida = bdb_idl_next( a, &cursora );
832 } else if ( ida > idb ) {
833 idb = bdb_idl_next( b, &cursorb );
836 ida = bdb_idl_next( a, &cursora );
837 idb = bdb_idl_next( b, &cursorb );
845 ID bdb_idl_first( ID *ids, ID *cursor )
854 if ( BDB_IDL_IS_RANGE( ids ) ) {
855 if( *cursor < ids[1] ) {
864 pos = bdb_idl_search( ids, *cursor );
874 ID bdb_idl_next( ID *ids, ID *cursor )
876 if ( BDB_IDL_IS_RANGE( ids ) ) {
877 if( ids[2] < ++(*cursor) ) {
883 if ( ++(*cursor) <= ids[0] ) {