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 (char *)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_TRACE, "=> bdb_idl_insert_key: dup\n",
332 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
333 "idl_insert failed (%d)\n",
339 data.size = BDB_IDL_SIZEOF( ids );
343 rc = db->put( db, tid, key, &data, 0 );
346 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
347 "put failed: %s (%d)\n",
348 db_strerror(rc), rc, 0 );
362 ID ids[BDB_IDL_DB_SIZE];
365 /* for printable keys only */
366 Debug( LDAP_DEBUG_ARGS,
367 "=> bdb_idl_delete_key: %s %ld\n",
368 (char *)key->data, (long) id, 0 );
370 assert( id != NOID );
373 data.ulen = sizeof( ids );
374 data.flags = DB_DBT_USERMEM;
376 /* fetch the key for read/modify/write */
377 rc = db->get( db, tid, key, &data, DB_RMW );
380 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
381 "get failed: %s (%d)\n",
382 db_strerror(rc), rc, 0 );
385 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
386 /* size not multiple of ID size */
387 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
388 "odd size: expected %ld multiple, got %ld\n",
389 (long) sizeof( ID ), (long) data.size, 0 );
392 } else if ( BDB_IDL_IS_RANGE(ids) ) {
395 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
397 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
398 "get size mismatch: expected %ld, got %ld\n",
399 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
403 rc = idl_delete( ids, id );
406 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
407 "idl_delete failed (%d)\n",
414 rc = db->del( db, tid, key, 0 );
416 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
417 "delete failed: %s (%d)\n",
418 db_strerror(rc), rc, 0 );
423 data.size = (ids[0]+1) * sizeof( ID );
427 rc = db->put( db, tid, key, &data, 0 );
430 Debug( LDAP_DEBUG_ANY,
431 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
432 db_strerror(rc), rc, 0 );
440 * idl_intersection - return a = a intersection b
443 bdb_idl_intersection(
449 ID cursora = 0, cursorb = 0, cursorc;
452 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
457 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
458 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
459 if ( idmin > idmax ) {
462 } else if ( idmin == idmax ) {
468 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
474 if ( BDB_IDL_IS_RANGE( a ) ) {
481 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
482 BDB_IDL_LAST( b ) >= idmax) {
483 if (idmax - idmin + 1 == a[0])
492 ida = bdb_idl_first( a, &cursora );
493 idb = bdb_idl_first( b, &cursorb );
497 ida = bdb_idl_next( a, &cursora );
499 idb = bdb_idl_next( b, &cursorb );
501 while( ida <= idmax || idb <= idmax ) {
504 ida = bdb_idl_next( a, &cursora );
505 idb = bdb_idl_next( b, &cursorb );
506 } else if ( ida < idb ) {
507 ida = bdb_idl_next( a, &cursora );
509 idb = bdb_idl_next( b, &cursorb );
522 * idl_union - return a = a union b
530 ID cursora = 0, cursorb = 0, cursorc;
532 if ( BDB_IDL_IS_ZERO( b ) ) {
536 if ( BDB_IDL_IS_ZERO( a ) ) {
541 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
542 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
543 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
547 ida = bdb_idl_first( a, &cursora );
548 idb = bdb_idl_first( b, &cursorb );
552 /* The distinct elements of a are cat'd to b */
553 while( ida != NOID || idb != NOID ) {
555 if( ++cursorc > BDB_IDL_UM_MAX ) {
560 ida = bdb_idl_next( a, &cursora );
564 ida = bdb_idl_next( a, &cursora );
565 idb = bdb_idl_next( b, &cursorb );
569 /* b is copied back to a in sorted order */
574 while (cursorb <= b[0] || cursorc <= a[0]) {
579 if (b[cursorb] < idb)
580 a[cursora++] = b[cursorb++];
593 * bdb_idl_notin - return a intersection ~b (or a minus b)
602 ID cursora = 0, cursorb = 0;
604 if( BDB_IDL_IS_ZERO( a ) ||
605 BDB_IDL_IS_ZERO( b ) ||
606 BDB_IDL_IS_RANGE( b ) )
608 BDB_IDL_CPY( ids, a );
612 if( BDB_IDL_IS_RANGE( a ) ) {
613 BDB_IDL_CPY( ids, a );
617 ida = bdb_idl_first( a, &cursora ),
618 idb = bdb_idl_first( b, &cursorb );
622 while( ida != NOID ) {
624 /* we could shortcut this */
626 ida = bdb_idl_next( a, &cursora );
628 } else if ( ida < idb ) {
630 ida = bdb_idl_next( a, &cursora );
632 } else if ( ida > idb ) {
633 idb = bdb_idl_next( b, &cursorb );
636 ida = bdb_idl_next( a, &cursora );
637 idb = bdb_idl_next( b, &cursorb );
645 ID bdb_idl_first( ID *ids, ID *cursor )
654 if ( BDB_IDL_IS_RANGE( ids ) ) {
655 if( *cursor < ids[1] ) {
664 pos = bdb_idl_search( ids, *cursor );
674 ID bdb_idl_next( ID *ids, ID *cursor )
676 if ( BDB_IDL_IS_RANGE( ids ) ) {
677 if( ids[2] < ++(*cursor) ) {
683 if ( ++(*cursor) <= ids[0] ) {