1 /* idl.c - ldap id list handling routines */
4 * Copyright 1998-2000 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 static int 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 );
231 data.ulen = BDB_IDL_UM_SIZEOF;
232 data.flags = DB_DBT_USERMEM;
235 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
237 if( rc == DB_NOTFOUND ) {
240 } else if( rc != 0 ) {
241 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
242 "get failed: %s (%d)\n",
243 db_strerror(rc), rc, 0 );
246 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
247 /* size not multiple of ID size */
248 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
249 "odd size: expected %ld multiple, got %ld\n",
250 (long) sizeof( ID ), (long) data.size, 0 );
253 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
255 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
256 "get size mismatch: expected %ld, got %ld\n",
257 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
272 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
274 ID ids[BDB_IDL_DB_SIZE];
277 /* for printable keys only */
278 Debug( LDAP_DEBUG_ARGS,
279 "=> bdb_idl_insert_key: %s %ld\n",
280 (char *)key->data, (long) id, 0 );
282 assert( id != NOID );
285 data.ulen = sizeof ids;
286 data.flags = DB_DBT_USERMEM;
288 /* fetch the key for read/modify/write */
289 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
291 if( rc == DB_NOTFOUND ) {
294 data.size = 2 * sizeof( ID );
296 } else if ( rc != 0 ) {
297 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
298 "get failed: %s (%d)\n",
299 db_strerror(rc), rc, 0 );
302 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
303 /* size not multiple of ID size */
304 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
305 "odd size: expected %ld multiple, got %ld\n",
306 (long) sizeof( ID ), (long) data.size, 0 );
309 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
311 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
312 "get size mismatch: expected %ld, got %ld\n",
313 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
316 } else if ( BDB_IDL_IS_RANGE(ids) ) {
319 } else if ( ids[2] > id ) {
326 rc = idl_insert( ids, id );
329 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
334 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
335 "idl_insert failed (%d)\n",
341 data.size = BDB_IDL_SIZEOF( ids );
345 rc = db->put( db, tid, key, &data, 0 );
348 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
349 "put failed: %s (%d)\n",
350 db_strerror(rc), rc, 0 );
363 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
365 ID ids[BDB_IDL_DB_SIZE];
368 /* for printable keys only */
369 Debug( LDAP_DEBUG_ARGS,
370 "=> bdb_idl_delete_key: %s %ld\n",
371 (char *)key->data, (long) id, 0 );
373 assert( id != NOID );
376 data.ulen = sizeof( ids );
377 data.flags = DB_DBT_USERMEM;
379 /* fetch the key for read/modify/write */
380 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
383 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
384 "get failed: %s (%d)\n",
385 db_strerror(rc), rc, 0 );
388 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
389 /* size not multiple of ID size */
390 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
391 "odd size: expected %ld multiple, got %ld\n",
392 (long) sizeof( ID ), (long) data.size, 0 );
395 } else if ( BDB_IDL_IS_RANGE(ids) ) {
398 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
400 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
401 "get size mismatch: expected %ld, got %ld\n",
402 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
406 rc = idl_delete( ids, id );
409 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
410 "idl_delete failed (%d)\n",
417 rc = db->del( db, tid, key, 0 );
419 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
420 "delete failed: %s (%d)\n",
421 db_strerror(rc), rc, 0 );
426 data.size = (ids[0]+1) * sizeof( ID );
430 rc = db->put( db, tid, key, &data, 0 );
433 Debug( LDAP_DEBUG_ANY,
434 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
435 db_strerror(rc), rc, 0 );
443 * idl_intersection - return a = a intersection b
446 bdb_idl_intersection(
452 ID cursora = 0, cursorb = 0, cursorc;
455 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
460 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
461 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
462 if ( idmin > idmax ) {
465 } else if ( idmin == idmax ) {
471 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
477 if ( BDB_IDL_IS_RANGE( a ) ) {
484 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
485 BDB_IDL_LAST( b ) >= idmax) {
486 if (idmax - idmin + 1 == a[0])
495 ida = bdb_idl_first( a, &cursora );
496 idb = bdb_idl_first( b, &cursorb );
500 ida = bdb_idl_next( a, &cursora );
502 idb = bdb_idl_next( b, &cursorb );
504 while( ida <= idmax || idb <= idmax ) {
507 ida = bdb_idl_next( a, &cursora );
508 idb = bdb_idl_next( b, &cursorb );
509 } else if ( ida < idb ) {
510 ida = bdb_idl_next( a, &cursora );
512 idb = bdb_idl_next( b, &cursorb );
525 * idl_union - return a = a union b
533 ID cursora = 0, cursorb = 0, cursorc;
535 if ( BDB_IDL_IS_ZERO( b ) ) {
539 if ( BDB_IDL_IS_ZERO( a ) ) {
544 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
545 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
546 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
550 ida = bdb_idl_first( a, &cursora );
551 idb = bdb_idl_first( b, &cursorb );
555 /* The distinct elements of a are cat'd to b */
556 while( ida != NOID || idb != NOID ) {
558 if( ++cursorc > BDB_IDL_UM_MAX ) {
563 ida = bdb_idl_next( a, &cursora );
567 ida = bdb_idl_next( a, &cursora );
568 idb = bdb_idl_next( b, &cursorb );
572 /* b is copied back to a in sorted order */
577 while (cursorb <= b[0] || cursorc <= a[0]) {
582 if (b[cursorb] < idb)
583 a[cursora++] = b[cursorb++];
596 * bdb_idl_notin - return a intersection ~b (or a minus b)
605 ID cursora = 0, cursorb = 0;
607 if( BDB_IDL_IS_ZERO( a ) ||
608 BDB_IDL_IS_ZERO( b ) ||
609 BDB_IDL_IS_RANGE( b ) )
611 BDB_IDL_CPY( ids, a );
615 if( BDB_IDL_IS_RANGE( a ) ) {
616 BDB_IDL_CPY( ids, a );
620 ida = bdb_idl_first( a, &cursora ),
621 idb = bdb_idl_first( b, &cursorb );
625 while( ida != NOID ) {
627 /* we could shortcut this */
629 ida = bdb_idl_next( a, &cursora );
631 } else if ( ida < idb ) {
633 ida = bdb_idl_next( a, &cursora );
635 } else if ( ida > idb ) {
636 idb = bdb_idl_next( b, &cursorb );
639 ida = bdb_idl_next( a, &cursora );
640 idb = bdb_idl_next( b, &cursorb );
648 ID bdb_idl_first( ID *ids, ID *cursor )
657 if ( BDB_IDL_IS_RANGE( ids ) ) {
658 if( *cursor < ids[1] ) {
667 pos = bdb_idl_search( ids, *cursor );
677 ID bdb_idl_next( ID *ids, ID *cursor )
679 if ( BDB_IDL_IS_RANGE( ids ) ) {
680 if( ids[2] < ++(*cursor) ) {
686 if ( ++(*cursor) <= ids[0] ) {