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) );
226 assert( ids != NULL );
230 data.ulen = BDB_IDL_UM_SIZEOF;
231 data.flags = DB_DBT_USERMEM;
234 rc = db->get( db, tid, key, &data, 0 );
236 if( rc == DB_NOTFOUND ) {
239 } else if( rc != 0 ) {
240 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
241 "get failed: %s (%d)\n",
242 db_strerror(rc), rc, 0 );
245 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
246 /* size not multiple of ID size */
247 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
248 "odd size: expected %ld multiple, got %ld\n",
249 (long) sizeof( ID ), (long) data.size, 0 );
252 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
254 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
255 "get size mismatch: expected %ld, got %ld\n",
256 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
272 ID ids[BDB_IDL_DB_SIZE];
275 /* for printable keys only */
276 Debug( LDAP_DEBUG_ARGS,
277 "=> bdb_idl_insert_key: %s %ld\n",
278 key->data, (long) id, 0 );
280 assert( id != NOID );
283 data.ulen = sizeof ids;
284 data.flags = DB_DBT_USERMEM;
286 /* fetch the key for read/modify/write */
287 rc = db->get( db, tid, key, &data, DB_RMW );
289 if( rc == DB_NOTFOUND ) {
292 data.size = 2 * sizeof( ID );
294 } else if ( rc != 0 ) {
295 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
296 "get failed: %s (%d)\n",
297 db_strerror(rc), rc, 0 );
300 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
301 /* size not multiple of ID size */
302 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
303 "odd size: expected %ld multiple, got %ld\n",
304 (long) sizeof( ID ), (long) data.size, 0 );
307 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
309 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
310 "get size mismatch: expected %ld, got %ld\n",
311 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
314 } else if ( BDB_IDL_IS_RANGE(ids) ) {
317 } else if ( ids[2] > id ) {
324 rc = idl_insert( ids, id );
327 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
328 "idl_insert failed (%d)\n",
333 data.size = BDB_IDL_SIZEOF( ids );
337 rc = db->put( db, tid, key, &data, 0 );
340 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
341 "put failed: %s (%d)\n",
342 db_strerror(rc), rc, 0 );
356 ID ids[BDB_IDL_DB_SIZE];
359 /* for printable keys only */
360 Debug( LDAP_DEBUG_ARGS,
361 "=> bdb_idl_delete_key: %s %ld\n",
362 key->data, (long) id, 0 );
364 assert( id != NOID );
367 data.ulen = sizeof( ids );
368 data.flags = DB_DBT_USERMEM;
370 /* fetch the key for read/modify/write */
371 rc = db->get( db, tid, key, &data, DB_RMW );
374 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
375 "get failed: %s (%d)\n",
376 db_strerror(rc), rc, 0 );
379 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
380 /* size not multiple of ID size */
381 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
382 "odd size: expected %ld multiple, got %ld\n",
383 (long) sizeof( ID ), (long) data.size, 0 );
386 } else if ( BDB_IDL_IS_RANGE(ids) ) {
389 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
391 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
392 "get size mismatch: expected %ld, got %ld\n",
393 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
397 rc = idl_delete( ids, id );
400 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
401 "idl_delete failed (%d)\n",
408 rc = db->del( db, tid, key, 0 );
410 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
411 "delete failed: %s (%d)\n",
412 db_strerror(rc), rc, 0 );
417 data.size = (ids[0]+1) * sizeof( ID );
421 rc = db->put( db, tid, key, &data, 0 );
424 Debug( LDAP_DEBUG_ANY,
425 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
426 db_strerror(rc), rc, 0 );
434 * idl_intersection - return a intersection b
437 bdb_idl_intersection(
445 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
450 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
452 ids[1] = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
453 ids[2] = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
455 if ( ids[1] == ids[2] ) {
457 } else if( ids[1] > ids[2] ) {
463 if( BDB_IDL_IS_RANGE( a ) ) {
469 ida = bdb_idl_first( a, &cursora ),
470 idb = bdb_idl_first( b, &cursorb );
474 while( ida != NOID && idb != NOID ) {
477 ida = bdb_idl_next( a, &cursora );
478 idb = bdb_idl_next( b, &cursorb );
479 if( BDB_IDL_IS_RANGE( b ) && idb < ida ) {
480 if( ida > BDB_IDL_LAST( b ) ) {
487 } else if ( ida < idb ) {
488 ida = bdb_idl_next( a, &cursora );
490 idb = bdb_idl_next( b, &cursorb );
499 * idl_union - return a union b
510 if ( BDB_IDL_IS_ZERO( a ) ) {
511 BDB_IDL_CPY( ids, b );
515 if ( BDB_IDL_IS_ZERO( b ) ) {
516 BDB_IDL_CPY( ids, a );
520 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
522 ids[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
523 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
527 ida = bdb_idl_first( a, &cursora ),
528 idb = bdb_idl_first( b, &cursorb );
532 while( ida != NOID && idb != NOID ) {
533 if( ++ids[0] > BDB_IDL_UM_MAX ) {
535 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
541 ida = bdb_idl_next( a, &cursora );
543 } else if ( ida > idb ) {
545 idb = bdb_idl_next( b, &cursorb );
549 ida = bdb_idl_next( a, &cursora );
550 idb = bdb_idl_next( b, &cursorb );
560 * bdb_idl_notin - return a intersection ~b (or a minus b)
571 if( BDB_IDL_IS_ZERO( a ) ||
572 BDB_IDL_IS_ZERO( b ) ||
573 BDB_IDL_IS_RANGE( b ) )
575 BDB_IDL_CPY( ids, a );
579 if( BDB_IDL_IS_RANGE( a ) ) {
580 BDB_IDL_CPY( ids, a );
584 ida = bdb_idl_first( a, &cursora ),
585 idb = bdb_idl_first( b, &cursorb );
589 while( ida != NOID ) {
591 /* we could shortcut this */
593 ida = bdb_idl_next( a, &cursora );
595 } else if ( ida < idb ) {
597 ida = bdb_idl_next( a, &cursora );
599 } else if ( ida > idb ) {
600 idb = bdb_idl_next( b, &cursorb );
603 ida = bdb_idl_next( a, &cursora );
604 idb = bdb_idl_next( b, &cursorb );
612 ID bdb_idl_first( ID *ids, ID *cursor )
621 if ( BDB_IDL_IS_RANGE( ids ) ) {
622 if( *cursor < ids[1] ) {
628 pos = bdb_idl_search( ids, *cursor );
638 ID bdb_idl_next( ID *ids, ID *cursor )
640 if ( BDB_IDL_IS_RANGE( ids ) ) {
641 if( ids[2] < ++(*cursor) ) {
647 if ( *cursor < ids[0] ) {
648 return ids[(*cursor)++];