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 );
233 ID buf[BDB_IDL_UM_SIZE];
238 data.ulen = BDB_IDL_UM_SIZEOF;
239 data.flags = DB_DBT_USERMEM;
240 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags |
243 DB_MULTIPLE_INIT( ptr, &data );
246 DB_MULTIPLE_NEXT(ptr, &data, j, len);
249 AC_MEMCPY( i, j, sizeof(ID) );
253 BDB_IDL_RANGE( ids, ids[2], ids[3] );
257 data.size = BDB_IDL_SIZEOF(ids);
262 data.ulen = BDB_IDL_UM_SIZEOF;
263 data.flags = DB_DBT_USERMEM;
265 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
268 if( rc == DB_NOTFOUND ) {
271 } else if( rc != 0 ) {
272 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
273 "get failed: %s (%d)\n",
274 db_strerror(rc), rc, 0 );
277 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
278 /* size not multiple of ID size */
279 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
280 "odd size: expected %ld multiple, got %ld\n",
281 (long) sizeof( ID ), (long) data.size, 0 );
284 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
286 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
287 "get size mismatch: expected %ld, got %ld\n",
288 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
303 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
306 #ifndef BDB_IDL_MULTI
307 ID ids[BDB_IDL_DB_SIZE];
310 /* for printable keys only */
311 Debug( LDAP_DEBUG_ARGS,
312 "=> bdb_idl_insert_key: %s %ld\n",
313 (char *)key->data, (long) id, 0 );
315 assert( id != NOID );
320 data.size = sizeof(id);
321 data.flags = DB_DBT_USERMEM;
324 data.ulen = sizeof ids;
325 data.flags = DB_DBT_USERMEM;
327 /* fetch the key for read/modify/write */
328 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
330 if( rc == DB_NOTFOUND ) {
333 data.size = 2 * sizeof( ID );
335 } else if ( rc != 0 ) {
336 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
337 "get failed: %s (%d)\n",
338 db_strerror(rc), rc, 0 );
341 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
342 /* size not multiple of ID size */
343 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
344 "odd size: expected %ld multiple, got %ld\n",
345 (long) sizeof( ID ), (long) data.size, 0 );
348 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
350 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
351 "get size mismatch: expected %ld, got %ld\n",
352 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
355 } else if ( BDB_IDL_IS_RANGE(ids) ) {
358 } else if ( ids[2] > id ) {
365 rc = idl_insert( ids, id );
368 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
373 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
374 "idl_insert failed (%d)\n",
380 data.size = BDB_IDL_SIZEOF( ids );
385 rc = db->put( db, tid, key, &data, 0 );
387 if( rc == DB_KEYEXIST ) rc = 0;
390 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
391 "put failed: %s (%d)\n",
392 db_strerror(rc), rc, 0 );
405 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
408 #ifndef BDB_IDL_MULTI
409 ID ids[BDB_IDL_DB_SIZE];
412 /* for printable keys only */
413 Debug( LDAP_DEBUG_ARGS,
414 "=> bdb_idl_delete_key: %s %ld\n",
415 (char *)key->data, (long) id, 0 );
417 assert( id != NOID );
425 data.size = sizeof( id );
426 data.ulen = data.size;
427 data.flags = DB_DBT_USERMEM;
429 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
430 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
431 DB_GET_BOTH | DB_RMW );
432 rc = cursor->c_del( cursor, 0 );
433 rc = cursor->c_close( cursor );
437 data.ulen = sizeof( ids );
438 data.flags = DB_DBT_USERMEM;
440 /* fetch the key for read/modify/write */
441 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
444 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
445 "get failed: %s (%d)\n",
446 db_strerror(rc), rc, 0 );
449 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
450 /* size not multiple of ID size */
451 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
452 "odd size: expected %ld multiple, got %ld\n",
453 (long) sizeof( ID ), (long) data.size, 0 );
456 } else if ( BDB_IDL_IS_RANGE(ids) ) {
459 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
461 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
462 "get size mismatch: expected %ld, got %ld\n",
463 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
467 rc = idl_delete( ids, id );
470 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
471 "idl_delete failed (%d)\n",
478 rc = db->del( db, tid, key, 0 );
480 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
481 "delete failed: %s (%d)\n",
482 db_strerror(rc), rc, 0 );
487 data.size = (ids[0]+1) * sizeof( ID );
491 rc = db->put( db, tid, key, &data, 0 );
493 #endif /* BDB_IDL_MULTI */
496 Debug( LDAP_DEBUG_ANY,
497 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
498 db_strerror(rc), rc, 0 );
506 * idl_intersection - return a = a intersection b
509 bdb_idl_intersection(
515 ID cursora = 0, cursorb = 0, cursorc;
518 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
523 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
524 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
525 if ( idmin > idmax ) {
528 } else if ( idmin == idmax ) {
534 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
540 if ( BDB_IDL_IS_RANGE( a ) ) {
547 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
548 BDB_IDL_LAST( b ) >= idmax) {
549 if (idmax - idmin + 1 == a[0])
558 ida = bdb_idl_first( a, &cursora );
559 idb = bdb_idl_first( b, &cursorb );
563 ida = bdb_idl_next( a, &cursora );
565 idb = bdb_idl_next( b, &cursorb );
567 while( ida <= idmax || idb <= idmax ) {
570 ida = bdb_idl_next( a, &cursora );
571 idb = bdb_idl_next( b, &cursorb );
572 } else if ( ida < idb ) {
573 ida = bdb_idl_next( a, &cursora );
575 idb = bdb_idl_next( b, &cursorb );
588 * idl_union - return a = a union b
596 ID cursora = 0, cursorb = 0, cursorc;
598 if ( BDB_IDL_IS_ZERO( b ) ) {
602 if ( BDB_IDL_IS_ZERO( a ) ) {
607 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
608 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
609 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
613 ida = bdb_idl_first( a, &cursora );
614 idb = bdb_idl_first( b, &cursorb );
618 /* The distinct elements of a are cat'd to b */
619 while( ida != NOID || idb != NOID ) {
621 if( ++cursorc > BDB_IDL_UM_MAX ) {
626 ida = bdb_idl_next( a, &cursora );
630 ida = bdb_idl_next( a, &cursora );
631 idb = bdb_idl_next( b, &cursorb );
635 /* b is copied back to a in sorted order */
640 while (cursorb <= b[0] || cursorc <= a[0]) {
645 if (b[cursorb] < idb)
646 a[cursora++] = b[cursorb++];
659 * bdb_idl_notin - return a intersection ~b (or a minus b)
668 ID cursora = 0, cursorb = 0;
670 if( BDB_IDL_IS_ZERO( a ) ||
671 BDB_IDL_IS_ZERO( b ) ||
672 BDB_IDL_IS_RANGE( b ) )
674 BDB_IDL_CPY( ids, a );
678 if( BDB_IDL_IS_RANGE( a ) ) {
679 BDB_IDL_CPY( ids, a );
683 ida = bdb_idl_first( a, &cursora ),
684 idb = bdb_idl_first( b, &cursorb );
688 while( ida != NOID ) {
690 /* we could shortcut this */
692 ida = bdb_idl_next( a, &cursora );
694 } else if ( ida < idb ) {
696 ida = bdb_idl_next( a, &cursora );
698 } else if ( ida > idb ) {
699 idb = bdb_idl_next( b, &cursorb );
702 ida = bdb_idl_next( a, &cursora );
703 idb = bdb_idl_next( b, &cursorb );
711 ID bdb_idl_first( ID *ids, ID *cursor )
720 if ( BDB_IDL_IS_RANGE( ids ) ) {
721 if( *cursor < ids[1] ) {
730 pos = bdb_idl_search( ids, *cursor );
740 ID bdb_idl_next( ID *ids, ID *cursor )
742 if ( BDB_IDL_IS_RANGE( ids ) ) {
743 if( ids[2] < ++(*cursor) ) {
749 if ( ++(*cursor) <= ids[0] ) {