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 )
68 #define IDL_BINARY_SEARCH 1
69 #ifdef IDL_BINARY_SEARCH
71 * binary search of id in ids
72 * if found, returns position of id
73 * 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 */
115 for( i=ids[0]; i; i-- ) {
125 static int idl_insert( ID *ids, ID id )
127 unsigned x = bdb_idl_search( ids, id );
130 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", id, x, 0 );
143 if ( x <= ids[0] && ids[x] == id ) {
148 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
151 ids[2] = ids[ids[0]-1];
152 } else if ( ids[ids[0]-1] < id ) {
155 ids[2] = ids[ids[0]-1];
161 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
174 static int idl_delete( ID *ids, ID id )
176 unsigned x = bdb_idl_search( ids, id );
179 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", id, x, 0 );
192 if( x > ids[0] || ids[x] != id ) {
196 } else if ( --ids[0] == 0 ) {
202 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
223 ID ids[BDB_IDL_DB_SIZE];
226 /* for printable keys only */
227 Debug( LDAP_DEBUG_ARGS,
228 "=> bdb_idl_insert_key: %s %ld\n",
229 key->data, (long) id, 0 );
231 assert( id != NOID );
234 data.ulen = sizeof( ids );
235 data.flags = DB_DBT_USERMEM;
237 /* fetch the key for read/modify/write */
238 rc = db->get( db, tid, key, &data, DB_RMW );
240 if( rc == DB_NOTFOUND ) {
243 data.size = 2 * sizeof( ID );
245 } else if ( rc != 0 ) {
246 Debug( LDAP_DEBUG_ANY,
247 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
248 db_strerror(rc), rc, 0 );
251 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
252 /* size not multiple of ID size */
253 Debug( LDAP_DEBUG_ANY,
254 "=> bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
255 (long) sizeof( ID ), (long) data.size, 0 );
258 } else if ( BDB_IDL_IS_RANGE(ids) ) {
261 } else if ( ids[2] > id ) {
267 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
269 Debug( LDAP_DEBUG_ANY,
270 "=> bdb_idl_insert_key: get size mismatch: expected %ld, got %ld\n",
271 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
275 rc = idl_insert( ids, id );
278 Debug( LDAP_DEBUG_ANY,
279 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
284 if( BDB_IDL_IS_RANGE( ids ) ) {
285 data.size = BDB_IDL_RANGE_SIZE;
287 data.size = (ids[0]+1) * sizeof( ID );
292 rc = db->put( db, tid, key, &data, 0 );
295 Debug( LDAP_DEBUG_ANY,
296 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
297 db_strerror(rc), rc, 0 );
311 ID ids[BDB_IDL_DB_SIZE];
314 /* for printable keys only */
315 Debug( LDAP_DEBUG_ARGS,
316 "=> bdb_idl_delete_key: %s %ld\n",
317 key->data, (long) id, 0 );
319 assert( id != NOID );
322 data.ulen = sizeof( ids );
323 data.flags = DB_DBT_USERMEM;
325 /* fetch the key for read/modify/write */
326 rc = db->get( db, tid, key, &data, DB_RMW );
329 Debug( LDAP_DEBUG_ANY,
330 "=> bdb_idl_delete_key: get failed: %s (%d)\n",
331 db_strerror(rc), rc, 0 );
334 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
335 /* size not multiple of ID size */
336 Debug( LDAP_DEBUG_ANY,
337 "=> bdb_idl_delete_key: odd size: expected %ld multiple, got %ld\n",
338 (long) sizeof( ID ), (long) data.size, 0 );
341 } else if ( BDB_IDL_IS_RANGE(ids) ) {
344 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
346 Debug( LDAP_DEBUG_ANY,
347 "=> bdb_idl_delete_key: get size mismatch: expected %ld, got %ld\n",
348 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
352 rc = idl_delete( ids, id );
355 Debug( LDAP_DEBUG_ANY,
356 "=> bdb_idl_delete_key: idl_delete failed (%d)\n",
363 rc = db->del( db, tid, key, 0 );
365 Debug( LDAP_DEBUG_ANY,
366 "=> bdb_idl_delete_key: delete failed: %s (%d)\n",
367 db_strerror(rc), rc, 0 );
372 data.size = (ids[0]+1) * sizeof( ID );
376 rc = db->put( db, tid, key, &data, 0 );
379 Debug( LDAP_DEBUG_ANY,
380 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
381 db_strerror(rc), rc, 0 );
389 * idl_intersection - return a intersection b
392 bdb_idl_intersection(
400 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
405 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
407 ids[1] = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
408 ids[2] = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
410 if ( ids[1] == ids[2] ) {
412 } else if( ids[1] > ids[2] ) {
418 if( BDB_IDL_IS_RANGE( a ) ) {
424 ida = bdb_idl_first( a, &cursora ),
425 idb = bdb_idl_first( b, &cursorb );
429 while( ida != NOID && idb != NOID ) {
432 ida = bdb_idl_next( a, &cursora );
433 idb = bdb_idl_next( b, &cursorb );
434 if( BDB_IDL_IS_RANGE( b ) && idb < ida ) {
435 if( ida > BDB_IDL_LAST( b ) ) {
442 } else if ( ida < idb ) {
443 ida = bdb_idl_next( a, &cursora );
445 idb = bdb_idl_next( b, &cursorb );
454 * idl_union - return a union b
465 if ( BDB_IDL_IS_ZERO( a ) ) {
466 BDB_IDL_CPY( ids, b );
470 if ( BDB_IDL_IS_ZERO( b ) ) {
471 BDB_IDL_CPY( ids, a );
475 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
477 ids[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
478 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
482 ida = bdb_idl_first( a, &cursora ),
483 idb = bdb_idl_first( b, &cursorb );
487 while( ida != NOID && idb != NOID ) {
488 if( ++ids[0] > BDB_IDL_MAX ) {
490 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
496 ida = bdb_idl_next( a, &cursora );
498 } else if ( ida > idb ) {
500 idb = bdb_idl_next( b, &cursorb );
504 ida = bdb_idl_next( a, &cursora );
505 idb = bdb_idl_next( b, &cursorb );
514 * bdb_idl_notin - return a intersection ~b (or a minus b)
525 if( BDB_IDL_IS_ZERO( a ) ||
526 BDB_IDL_IS_ZERO( b ) ||
527 BDB_IDL_IS_RANGE( b ) )
529 BDB_IDL_CPY( ids, a );
533 if( BDB_IDL_IS_RANGE( a ) ) {
534 BDB_IDL_CPY( ids, a );
538 ida = bdb_idl_first( a, &cursora ),
539 idb = bdb_idl_first( b, &cursorb );
543 while( ida != NOID ) {
545 /* we could shortcut this */
547 ida = bdb_idl_next( a, &cursora );
549 } else if ( ida < idb ) {
551 ida = bdb_idl_next( a, &cursora );
553 } else if ( ida > idb ) {
554 idb = bdb_idl_next( b, &cursorb );
557 ida = bdb_idl_next( a, &cursora );
558 idb = bdb_idl_next( b, &cursorb );
565 ID bdb_idl_first( ID *ids, ID *cursor )
574 if ( BDB_IDL_IS_RANGE( ids ) ) {
575 if( *cursor < ids[1] ) {
581 pos = bdb_idl_search( ids, *cursor );
591 ID bdb_idl_next( ID *ids, ID *cursor )
593 if ( BDB_IDL_IS_RANGE( ids ) ) {
594 if( ids[2] < ++(*cursor) ) {
600 if ( *cursor < ids[0] ) {
601 return ids[(*cursor)++];