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 ) )
23 void idl_dump( ID *ids )
25 if( BDB_IDL_IS_RANGE( ids ) ) {
26 Debug( LDAP_DEBUG_ANY,
27 "IDL: range %ld - %ld\n", (long) ids[0], 0, 0 );
31 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
33 for( i=1; i<=ids[0]; i++ ) {
35 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
37 Debug( LDAP_DEBUG_ANY, " %02lx", ids[i], 0, 0 );
40 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
45 unsigned bdb_idl_search( ID *ids, ID id )
47 #undef IDL_BINARY_SEARCH
48 #ifdef IDL_BINARY_SEARCH
50 * binary search of id in ids
51 * if found, returns position of id
52 * if not found, returns first postion greater than id
61 cursor = base + pivot;
62 val = IDL_CMP( id, ids[cursor + 1] );
67 } else if ( val > 0 ) {
83 /* (reverse) linear search */
85 for( i=ids[0]; i; i-- ) {
94 static int idl_insert( ID *ids, ID id )
96 unsigned x = bdb_idl_search( ids, id );
99 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", id, x, 0 );
110 if ( x <= ids[0] && ids[x] == id ) {
115 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
118 ids[2] = ids[ids[0]-1];
119 } else if ( ids[ids[0]-1] < id ) {
122 ids[2] = ids[ids[0]-1];
128 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
139 static int idl_delete( ID *ids, ID id )
141 unsigned x = bdb_idl_search( ids, id );
144 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", id, x, 0 );
155 if( x > ids[0] || ids[x] != id ) {
159 } else if ( --ids[0] == 0 ) {
165 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
184 ID ids[BDB_IDL_DB_SIZE];
187 /* for printable keys only */
188 Debug( LDAP_DEBUG_ARGS,
189 "=> bdb_idl_insert_key: %s %ld\n",
190 key->data, (long) id, 0 );
192 assert( id != NOID );
195 data.ulen = sizeof( ids );
196 data.flags = DB_DBT_USERMEM;
198 /* fetch the key for read/modify/write */
199 rc = db->get( db, tid, key, &data, DB_RMW );
201 if( rc == DB_NOTFOUND ) {
204 data.size = 2 * sizeof( ID );
206 } else if ( rc != 0 ) {
207 Debug( LDAP_DEBUG_ANY,
208 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
209 db_strerror(rc), rc, 0 );
212 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
213 /* size not multiple of ID size */
214 Debug( LDAP_DEBUG_ANY,
215 "=> bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
216 (long) sizeof( ID ), (long) data.size, 0 );
219 } else if ( BDB_IDL_IS_RANGE(ids) ) {
222 } else if ( ids[2] > id ) {
228 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
230 Debug( LDAP_DEBUG_ANY,
231 "=> bdb_idl_insert_key: get size mismatch: expected %ld, got %ld\n",
232 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
236 rc = idl_insert( ids, id );
239 Debug( LDAP_DEBUG_ANY,
240 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
245 if( BDB_IDL_IS_RANGE( ids ) ) {
246 data.size = BDB_IDL_RANGE_SIZE;
248 data.size = (ids[0]+1) * sizeof( ID );
253 rc = db->put( db, tid, key, &data, 0 );
256 Debug( LDAP_DEBUG_ANY,
257 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
258 db_strerror(rc), rc, 0 );
272 ID ids[BDB_IDL_DB_SIZE];
275 /* for printable keys only */
276 Debug( LDAP_DEBUG_ARGS,
277 "=> bdb_idl_delete_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 );
290 Debug( LDAP_DEBUG_ANY,
291 "=> bdb_idl_delete_key: get failed: %s (%d)\n",
292 db_strerror(rc), rc, 0 );
295 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
296 /* size not multiple of ID size */
297 Debug( LDAP_DEBUG_ANY,
298 "=> bdb_idl_delete_key: odd size: expected %ld multiple, got %ld\n",
299 (long) sizeof( ID ), (long) data.size, 0 );
302 } else if ( BDB_IDL_IS_RANGE(ids) ) {
305 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
307 Debug( LDAP_DEBUG_ANY,
308 "=> bdb_idl_delete_key: get size mismatch: expected %ld, got %ld\n",
309 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
313 rc = idl_delete( ids, id );
316 Debug( LDAP_DEBUG_ANY,
317 "=> bdb_idl_delete_key: idl_delete failed (%d)\n",
324 rc = db->del( db, tid, key, 0 );
326 Debug( LDAP_DEBUG_ANY,
327 "=> bdb_idl_delete_key: delete failed: %s (%d)\n",
328 db_strerror(rc), rc, 0 );
333 data.size = (ids[0]+1) * sizeof( ID );
337 rc = db->put( db, tid, key, &data, 0 );
340 Debug( LDAP_DEBUG_ANY,
341 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
342 db_strerror(rc), rc, 0 );
350 * idl_intersection - return a intersection b
353 bdb_idl_intersection(
361 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
366 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
368 ids[1] = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
369 ids[2] = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
371 if ( ids[1] == ids[2] ) {
373 } else if( ids[1] > ids[2] ) {
379 if( BDB_IDL_IS_RANGE( a ) ) {
385 ida = bdb_idl_first( a, &cursora ),
386 idb = bdb_idl_first( b, &cursorb );
390 while( ida != NOID && idb != NOID ) {
393 ida = bdb_idl_next( a, &cursora );
394 idb = bdb_idl_next( b, &cursorb );
395 if( BDB_IDL_IS_RANGE( b ) && idb < ida ) {
396 if( ida > BDB_IDL_LAST( b ) ) {
403 } else if ( ida < idb ) {
404 ida = bdb_idl_next( a, &cursora );
406 idb = bdb_idl_next( b, &cursorb );
415 * idl_union - return a union b
426 if ( BDB_IDL_IS_ZERO( a ) ) {
427 BDB_IDL_CPY( ids, b );
431 if ( BDB_IDL_IS_ZERO( b ) ) {
432 BDB_IDL_CPY( ids, a );
436 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
438 ids[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
439 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
443 ida = bdb_idl_first( a, &cursora ),
444 idb = bdb_idl_first( b, &cursorb );
448 while( ida != NOID && idb != NOID ) {
449 if( ++ids[0] > BDB_IDL_MAX ) {
451 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
457 ida = bdb_idl_next( a, &cursora );
459 } else if ( ida > idb ) {
461 idb = bdb_idl_next( b, &cursorb );
465 ida = bdb_idl_next( a, &cursora );
466 idb = bdb_idl_next( b, &cursorb );
475 * bdb_idl_notin - return a intersection ~b (or a minus b)
486 if( BDB_IDL_IS_ZERO( a ) ||
487 BDB_IDL_IS_ZERO( b ) ||
488 BDB_IDL_IS_RANGE( b ) )
490 BDB_IDL_CPY( ids, a );
494 if( BDB_IDL_IS_RANGE( a ) ) {
495 BDB_IDL_CPY( ids, a );
499 ida = bdb_idl_first( a, &cursora ),
500 idb = bdb_idl_first( b, &cursorb );
504 while( ida != NOID ) {
506 /* we could shortcut this */
508 ida = bdb_idl_next( a, &cursora );
510 } else if ( ida < idb ) {
512 ida = bdb_idl_next( a, &cursora );
514 } else if ( ida > idb ) {
515 idb = bdb_idl_next( b, &cursorb );
518 ida = bdb_idl_next( a, &cursora );
519 idb = bdb_idl_next( b, &cursorb );
526 ID bdb_idl_first( ID *ids, ID *cursor )
535 if ( BDB_IDL_IS_RANGE( ids ) ) {
536 if( *cursor < ids[1] ) {
542 pos = bdb_idl_search( ids, *cursor );
552 ID bdb_idl_next( ID *ids, ID *cursor )
554 if ( BDB_IDL_IS_RANGE( ids ) ) {
555 if( ids[2] < ++(*cursor) ) {
561 if ( *cursor < ids[0] ) {
562 return ids[(*cursor)++];