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( ids[1] <= ids[2] );
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) ids[1], (long) idl[2], 0 );
49 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
51 for( i=1; i<=ids[0]; i++ ) {
53 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
55 Debug( LDAP_DEBUG_ANY, " %02lx", ids[i], 0, 0 );
58 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
63 #endif /* IDL_DEBUG > 1 */
64 #endif /* IDL_DEBUG > 0 */
66 unsigned bdb_idl_search( ID *ids, ID id )
72 #undef IDL_BINARY_SEARCH
73 #ifdef IDL_BINARY_SEARCH
75 * binary search of id in ids
76 * if found, returns position of id
77 * if not found, returns first postion greater than id
86 cursor = base + pivot;
87 val = IDL_CMP( id, ids[cursor + 1] );
92 } else if ( val > 0 ) {
108 /* (reverse) linear search */
112 for( i=ids[0]; i; i-- ) {
123 static int idl_insert( ID *ids, ID id )
125 unsigned x = bdb_idl_search( ids, id );
128 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", id, x, 0 );
141 if ( x <= ids[0] && ids[x] == id ) {
146 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
149 ids[2] = ids[ids[0]-1];
150 } else if ( ids[ids[0]-1] < id ) {
153 ids[2] = ids[ids[0]-1];
159 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
172 static int idl_delete( ID *ids, ID id )
174 unsigned x = bdb_idl_search( ids, id );
177 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", id, x, 0 );
190 if( x > ids[0] || ids[x] != id ) {
194 } else if ( --ids[0] == 0 ) {
200 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
221 ID ids[BDB_IDL_DB_SIZE];
224 /* for printable keys only */
225 Debug( LDAP_DEBUG_ARGS,
226 "=> bdb_idl_insert_key: %s %ld\n",
227 key->data, (long) id, 0 );
229 assert( id != NOID );
232 data.ulen = sizeof( ids );
233 data.flags = DB_DBT_USERMEM;
235 /* fetch the key for read/modify/write */
236 rc = db->get( db, tid, key, &data, DB_RMW );
238 if( rc == DB_NOTFOUND ) {
241 data.size = 2 * sizeof( ID );
243 } else if ( rc != 0 ) {
244 Debug( LDAP_DEBUG_ANY,
245 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
246 db_strerror(rc), rc, 0 );
249 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
250 /* size not multiple of ID size */
251 Debug( LDAP_DEBUG_ANY,
252 "=> bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
253 (long) sizeof( ID ), (long) data.size, 0 );
256 } else if ( BDB_IDL_IS_RANGE(ids) ) {
259 } else if ( ids[2] > id ) {
265 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
267 Debug( LDAP_DEBUG_ANY,
268 "=> bdb_idl_insert_key: get size mismatch: expected %ld, got %ld\n",
269 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
273 rc = idl_insert( ids, id );
276 Debug( LDAP_DEBUG_ANY,
277 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
282 if( BDB_IDL_IS_RANGE( ids ) ) {
283 data.size = BDB_IDL_RANGE_SIZE;
285 data.size = (ids[0]+1) * sizeof( ID );
290 rc = db->put( db, tid, key, &data, 0 );
293 Debug( LDAP_DEBUG_ANY,
294 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
295 db_strerror(rc), rc, 0 );
309 ID ids[BDB_IDL_DB_SIZE];
312 /* for printable keys only */
313 Debug( LDAP_DEBUG_ARGS,
314 "=> bdb_idl_delete_key: %s %ld\n",
315 key->data, (long) id, 0 );
317 assert( id != NOID );
320 data.ulen = sizeof( ids );
321 data.flags = DB_DBT_USERMEM;
323 /* fetch the key for read/modify/write */
324 rc = db->get( db, tid, key, &data, DB_RMW );
327 Debug( LDAP_DEBUG_ANY,
328 "=> bdb_idl_delete_key: get failed: %s (%d)\n",
329 db_strerror(rc), rc, 0 );
332 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
333 /* size not multiple of ID size */
334 Debug( LDAP_DEBUG_ANY,
335 "=> bdb_idl_delete_key: odd size: expected %ld multiple, got %ld\n",
336 (long) sizeof( ID ), (long) data.size, 0 );
339 } else if ( BDB_IDL_IS_RANGE(ids) ) {
342 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
344 Debug( LDAP_DEBUG_ANY,
345 "=> bdb_idl_delete_key: get size mismatch: expected %ld, got %ld\n",
346 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
350 rc = idl_delete( ids, id );
353 Debug( LDAP_DEBUG_ANY,
354 "=> bdb_idl_delete_key: idl_delete failed (%d)\n",
361 rc = db->del( db, tid, key, 0 );
363 Debug( LDAP_DEBUG_ANY,
364 "=> bdb_idl_delete_key: delete failed: %s (%d)\n",
365 db_strerror(rc), rc, 0 );
370 data.size = (ids[0]+1) * sizeof( ID );
374 rc = db->put( db, tid, key, &data, 0 );
377 Debug( LDAP_DEBUG_ANY,
378 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
379 db_strerror(rc), rc, 0 );
387 * idl_intersection - return a intersection b
390 bdb_idl_intersection(
398 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
403 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
405 ids[1] = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
406 ids[2] = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
408 if ( ids[1] == ids[2] ) {
410 } else if( ids[1] > ids[2] ) {
416 if( BDB_IDL_IS_RANGE( a ) ) {
422 ida = bdb_idl_first( a, &cursora ),
423 idb = bdb_idl_first( b, &cursorb );
427 while( ida != NOID && idb != NOID ) {
430 ida = bdb_idl_next( a, &cursora );
431 idb = bdb_idl_next( b, &cursorb );
432 if( BDB_IDL_IS_RANGE( b ) && idb < ida ) {
433 if( ida > BDB_IDL_LAST( b ) ) {
440 } else if ( ida < idb ) {
441 ida = bdb_idl_next( a, &cursora );
443 idb = bdb_idl_next( b, &cursorb );
452 * idl_union - return a union b
463 if ( BDB_IDL_IS_ZERO( a ) ) {
464 BDB_IDL_CPY( ids, b );
468 if ( BDB_IDL_IS_ZERO( b ) ) {
469 BDB_IDL_CPY( ids, a );
473 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
475 ids[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
476 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
480 ida = bdb_idl_first( a, &cursora ),
481 idb = bdb_idl_first( b, &cursorb );
485 while( ida != NOID && idb != NOID ) {
486 if( ++ids[0] > BDB_IDL_MAX ) {
488 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
494 ida = bdb_idl_next( a, &cursora );
496 } else if ( ida > idb ) {
498 idb = bdb_idl_next( b, &cursorb );
502 ida = bdb_idl_next( a, &cursora );
503 idb = bdb_idl_next( b, &cursorb );
512 * bdb_idl_notin - return a intersection ~b (or a minus b)
523 if( BDB_IDL_IS_ZERO( a ) ||
524 BDB_IDL_IS_ZERO( b ) ||
525 BDB_IDL_IS_RANGE( b ) )
527 BDB_IDL_CPY( ids, a );
531 if( BDB_IDL_IS_RANGE( a ) ) {
532 BDB_IDL_CPY( ids, a );
536 ida = bdb_idl_first( a, &cursora ),
537 idb = bdb_idl_first( b, &cursorb );
541 while( ida != NOID ) {
543 /* we could shortcut this */
545 ida = bdb_idl_next( a, &cursora );
547 } else if ( ida < idb ) {
549 ida = bdb_idl_next( a, &cursora );
551 } else if ( ida > idb ) {
552 idb = bdb_idl_next( b, &cursorb );
555 ida = bdb_idl_next( a, &cursora );
556 idb = bdb_idl_next( b, &cursorb );
563 ID bdb_idl_first( ID *ids, ID *cursor )
572 if ( BDB_IDL_IS_RANGE( ids ) ) {
573 if( *cursor < ids[1] ) {
579 pos = bdb_idl_search( ids, *cursor );
589 ID bdb_idl_next( ID *ids, ID *cursor )
591 if ( BDB_IDL_IS_RANGE( ids ) ) {
592 if( ids[2] < ++(*cursor) ) {
598 if ( *cursor < ids[0] ) {
599 return ids[(*cursor)++];