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 int bdb_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 );
319 /* FIXME: We really ought to count how many data items currently exist
320 * for this key, and cap off with a range when we hit the max. We need
321 * to use a 0 in the first slot to denote a range - since the data are
322 * sorted in ascending order, the only way to get a flag into the
323 * first slot is to use the smallest possible ID value. The fetch
324 * function above can turn it into a "memory-format" range. We also
325 * have to delete all of the existing data items when converting from
326 * a list to a range. And of course, if it's already a range, we need
327 * to read it in and process it instead of just doing the blind put
328 * that we do right now...
331 data.size = sizeof(id);
332 data.flags = DB_DBT_USERMEM;
334 rc = db->put( db, tid, key, &data, DB_NODUPDATA );
337 data.ulen = sizeof ids;
338 data.flags = DB_DBT_USERMEM;
340 /* fetch the key for read/modify/write */
341 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
343 if( rc == DB_NOTFOUND ) {
346 data.size = 2 * sizeof( ID );
348 } else if ( rc != 0 ) {
349 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
350 "get failed: %s (%d)\n",
351 db_strerror(rc), rc, 0 );
354 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
355 /* size not multiple of ID size */
356 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
357 "odd size: expected %ld multiple, got %ld\n",
358 (long) sizeof( ID ), (long) data.size, 0 );
361 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
363 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
364 "get size mismatch: expected %ld, got %ld\n",
365 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
368 } else if ( BDB_IDL_IS_RANGE(ids) ) {
371 } else if ( ids[2] > id ) {
378 rc = bdb_idl_insert( ids, id );
381 Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
386 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
387 "bdb_idl_insert failed (%d)\n",
393 data.size = BDB_IDL_SIZEOF( ids );
397 rc = db->put( db, tid, key, &data, 0 );
399 if( rc == DB_KEYEXIST ) rc = 0;
402 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
403 "put failed: %s (%d)\n",
404 db_strerror(rc), rc, 0 );
417 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
420 #ifndef BDB_IDL_MULTI
421 ID ids[BDB_IDL_DB_SIZE];
424 /* for printable keys only */
425 Debug( LDAP_DEBUG_ARGS,
426 "=> bdb_idl_delete_key: %s %ld\n",
427 (char *)key->data, (long) id, 0 );
429 assert( id != NOID );
437 data.size = sizeof( id );
438 data.ulen = data.size;
439 data.flags = DB_DBT_USERMEM;
441 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
442 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
443 DB_GET_BOTH | DB_RMW );
445 rc = cursor->c_del( cursor, 0 );
446 rc = cursor->c_close( cursor );
450 data.ulen = sizeof( ids );
451 data.flags = DB_DBT_USERMEM;
453 /* fetch the key for read/modify/write */
454 rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
457 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
458 "get failed: %s (%d)\n",
459 db_strerror(rc), rc, 0 );
462 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
463 /* size not multiple of ID size */
464 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
465 "odd size: expected %ld multiple, got %ld\n",
466 (long) sizeof( ID ), (long) data.size, 0 );
469 } else if ( BDB_IDL_IS_RANGE(ids) ) {
472 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
474 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
475 "get size mismatch: expected %ld, got %ld\n",
476 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
480 rc = idl_delete( ids, id );
483 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
484 "idl_delete failed (%d)\n",
491 rc = db->del( db, tid, key, 0 );
493 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
494 "delete failed: %s (%d)\n",
495 db_strerror(rc), rc, 0 );
500 data.size = (ids[0]+1) * sizeof( ID );
504 rc = db->put( db, tid, key, &data, 0 );
506 #endif /* BDB_IDL_MULTI */
509 Debug( LDAP_DEBUG_ANY,
510 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
511 db_strerror(rc), rc, 0 );
519 * idl_intersection - return a = a intersection b
522 bdb_idl_intersection(
528 ID cursora = 0, cursorb = 0, cursorc;
531 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
536 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
537 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
538 if ( idmin > idmax ) {
541 } else if ( idmin == idmax ) {
547 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
553 if ( BDB_IDL_IS_RANGE( a ) ) {
560 if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
561 BDB_IDL_LAST( b ) >= idmax) {
562 if (idmax - idmin + 1 == a[0])
571 ida = bdb_idl_first( a, &cursora );
572 idb = bdb_idl_first( b, &cursorb );
576 ida = bdb_idl_next( a, &cursora );
578 idb = bdb_idl_next( b, &cursorb );
580 while( ida <= idmax || idb <= idmax ) {
583 ida = bdb_idl_next( a, &cursora );
584 idb = bdb_idl_next( b, &cursorb );
585 } else if ( ida < idb ) {
586 ida = bdb_idl_next( a, &cursora );
588 idb = bdb_idl_next( b, &cursorb );
601 * idl_union - return a = a union b
609 ID cursora = 0, cursorb = 0, cursorc;
611 if ( BDB_IDL_IS_ZERO( b ) ) {
615 if ( BDB_IDL_IS_ZERO( a ) ) {
620 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
621 over: a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
622 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
626 ida = bdb_idl_first( a, &cursora );
627 idb = bdb_idl_first( b, &cursorb );
631 /* The distinct elements of a are cat'd to b */
632 while( ida != NOID || idb != NOID ) {
634 if( ++cursorc > BDB_IDL_UM_MAX ) {
639 ida = bdb_idl_next( a, &cursora );
643 ida = bdb_idl_next( a, &cursora );
644 idb = bdb_idl_next( b, &cursorb );
648 /* b is copied back to a in sorted order */
653 while (cursorb <= b[0] || cursorc <= a[0]) {
658 if (b[cursorb] < idb)
659 a[cursora++] = b[cursorb++];
672 * bdb_idl_notin - return a intersection ~b (or a minus b)
681 ID cursora = 0, cursorb = 0;
683 if( BDB_IDL_IS_ZERO( a ) ||
684 BDB_IDL_IS_ZERO( b ) ||
685 BDB_IDL_IS_RANGE( b ) )
687 BDB_IDL_CPY( ids, a );
691 if( BDB_IDL_IS_RANGE( a ) ) {
692 BDB_IDL_CPY( ids, a );
696 ida = bdb_idl_first( a, &cursora ),
697 idb = bdb_idl_first( b, &cursorb );
701 while( ida != NOID ) {
703 /* we could shortcut this */
705 ida = bdb_idl_next( a, &cursora );
707 } else if ( ida < idb ) {
709 ida = bdb_idl_next( a, &cursora );
711 } else if ( ida > idb ) {
712 idb = bdb_idl_next( b, &cursorb );
715 ida = bdb_idl_next( a, &cursora );
716 idb = bdb_idl_next( b, &cursorb );
724 ID bdb_idl_first( ID *ids, ID *cursor )
733 if ( BDB_IDL_IS_RANGE( ids ) ) {
734 if( *cursor < ids[1] ) {
743 pos = bdb_idl_search( ids, *cursor );
753 ID bdb_idl_next( ID *ids, ID *cursor )
755 if ( BDB_IDL_IS_RANGE( ids ) ) {
756 if( ids[2] < ++(*cursor) ) {
762 if ( ++(*cursor) <= ids[0] ) {