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;
323 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
326 data.ulen = sizeof ids;
327 data.flags = DB_DBT_USERMEM;
329 /* fetch the key for read/modify/write */
330 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
332 if( rc == DB_NOTFOUND ) {
335 data.size = 2 * sizeof( ID );
337 } else if ( rc != 0 ) {
338 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
339 "get failed: %s (%d)\n",
340 db_strerror(rc), rc, 0 );
343 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
344 /* size not multiple of ID size */
345 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
346 "odd size: expected %ld multiple, got %ld\n",
347 (long) sizeof( ID ), (long) data.size, 0 );
350 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
352 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
353 "get size mismatch: expected %ld, got %ld\n",
354 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
357 } else if ( BDB_IDL_IS_RANGE(ids) ) {
360 } else if ( ids[2] > id ) {
367 rc = idl_insert( ids, id );
370 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
375 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
376 "idl_insert failed (%d)\n",
382 data.size = BDB_IDL_SIZEOF( ids );
386 rc = db->put( db, tid, key, &data, 0 );
388 if( rc == DB_KEYEXIST ) rc = 0;
391 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
392 "put failed: %s (%d)\n",
393 db_strerror(rc), rc, 0 );
406 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
409 #ifndef BDB_IDL_MULTI
410 ID ids[BDB_IDL_DB_SIZE];
413 /* for printable keys only */
414 Debug( LDAP_DEBUG_ARGS,
415 "=> bdb_idl_delete_key: %s %ld\n",
416 (char *)key->data, (long) id, 0 );
418 assert( id != NOID );
426 data.size = sizeof( id );
427 data.ulen = data.size;
428 data.flags = DB_DBT_USERMEM;
430 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
431 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
432 DB_GET_BOTH | DB_RMW );
434 rc = cursor->c_del( cursor, 0 );
435 rc = cursor->c_close( cursor );
439 data.ulen = sizeof( ids );
440 data.flags = DB_DBT_USERMEM;
442 /* fetch the key for read/modify/write */
443 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
446 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
447 "get failed: %s (%d)\n",
448 db_strerror(rc), rc, 0 );
451 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
452 /* size not multiple of ID size */
453 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
454 "odd size: expected %ld multiple, got %ld\n",
455 (long) sizeof( ID ), (long) data.size, 0 );
458 } else if ( BDB_IDL_IS_RANGE(ids) ) {
461 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
463 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
464 "get size mismatch: expected %ld, got %ld\n",
465 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
469 rc = idl_delete( ids, id );
472 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
473 "idl_delete failed (%d)\n",
480 rc = db->del( db, tid, key, 0 );
482 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
483 "delete failed: %s (%d)\n",
484 db_strerror(rc), rc, 0 );
489 data.size = (ids[0]+1) * sizeof( ID );
493 rc = db->put( db, tid, key, &data, 0 );
495 #endif /* BDB_IDL_MULTI */
498 Debug( LDAP_DEBUG_ANY,
499 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
500 db_strerror(rc), rc, 0 );
508 * idl_intersection - return a = a intersection b
511 bdb_idl_intersection(
517 ID cursora = 0, cursorb = 0, cursorc;
520 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
525 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
526 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
527 if ( idmin > idmax ) {
530 } else if ( idmin == idmax ) {
536 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
542 if ( BDB_IDL_IS_RANGE( a ) ) {
549 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
550 BDB_IDL_LAST( b ) >= idmax) {
551 if (idmax - idmin + 1 == a[0])
560 ida = bdb_idl_first( a, &cursora );
561 idb = bdb_idl_first( b, &cursorb );
565 ida = bdb_idl_next( a, &cursora );
567 idb = bdb_idl_next( b, &cursorb );
569 while( ida <= idmax || idb <= idmax ) {
572 ida = bdb_idl_next( a, &cursora );
573 idb = bdb_idl_next( b, &cursorb );
574 } else if ( ida < idb ) {
575 ida = bdb_idl_next( a, &cursora );
577 idb = bdb_idl_next( b, &cursorb );
590 * idl_union - return a = a union b
598 ID cursora = 0, cursorb = 0, cursorc;
600 if ( BDB_IDL_IS_ZERO( b ) ) {
604 if ( BDB_IDL_IS_ZERO( a ) ) {
609 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
610 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
611 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
615 ida = bdb_idl_first( a, &cursora );
616 idb = bdb_idl_first( b, &cursorb );
620 /* The distinct elements of a are cat'd to b */
621 while( ida != NOID || idb != NOID ) {
623 if( ++cursorc > BDB_IDL_UM_MAX ) {
628 ida = bdb_idl_next( a, &cursora );
632 ida = bdb_idl_next( a, &cursora );
633 idb = bdb_idl_next( b, &cursorb );
637 /* b is copied back to a in sorted order */
642 while (cursorb <= b[0] || cursorc <= a[0]) {
647 if (b[cursorb] < idb)
648 a[cursora++] = b[cursorb++];
661 * bdb_idl_notin - return a intersection ~b (or a minus b)
670 ID cursora = 0, cursorb = 0;
672 if( BDB_IDL_IS_ZERO( a ) ||
673 BDB_IDL_IS_ZERO( b ) ||
674 BDB_IDL_IS_RANGE( b ) )
676 BDB_IDL_CPY( ids, a );
680 if( BDB_IDL_IS_RANGE( a ) ) {
681 BDB_IDL_CPY( ids, a );
685 ida = bdb_idl_first( a, &cursora ),
686 idb = bdb_idl_first( b, &cursorb );
690 while( ida != NOID ) {
692 /* we could shortcut this */
694 ida = bdb_idl_next( a, &cursora );
696 } else if ( ida < idb ) {
698 ida = bdb_idl_next( a, &cursora );
700 } else if ( ida > idb ) {
701 idb = bdb_idl_next( b, &cursorb );
704 ida = bdb_idl_next( a, &cursora );
705 idb = bdb_idl_next( b, &cursorb );
713 ID bdb_idl_first( ID *ids, ID *cursor )
722 if ( BDB_IDL_IS_RANGE( ids ) ) {
723 if( *cursor < ids[1] ) {
732 pos = bdb_idl_search( ids, *cursor );
742 ID bdb_idl_next( ID *ids, ID *cursor )
744 if ( BDB_IDL_IS_RANGE( ids ) ) {
745 if( ids[2] < ++(*cursor) ) {
751 if ( ++(*cursor) <= ids[0] ) {