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 */
110 for( i=ids[0]; i; i-- ) {
119 static int idl_insert( ID *ids, ID id )
121 unsigned x = bdb_idl_search( ids, id );
124 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", id, x, 0 );
137 if ( x <= ids[0] && ids[x] == id ) {
142 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
145 ids[2] = ids[ids[0]-1];
146 } else if ( ids[ids[0]-1] < id ) {
149 ids[2] = ids[ids[0]-1];
155 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
168 static int idl_delete( ID *ids, ID id )
170 unsigned x = bdb_idl_search( ids, id );
173 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", id, x, 0 );
186 if( x > ids[0] || ids[x] != id ) {
190 } else if ( --ids[0] == 0 ) {
196 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
217 ID ids[BDB_IDL_DB_SIZE];
220 /* for printable keys only */
221 Debug( LDAP_DEBUG_ARGS,
222 "=> bdb_idl_insert_key: %s %ld\n",
223 key->data, (long) id, 0 );
225 assert( id != NOID );
228 data.ulen = sizeof( ids );
229 data.flags = DB_DBT_USERMEM;
231 /* fetch the key for read/modify/write */
232 rc = db->get( db, tid, key, &data, DB_RMW );
234 if( rc == DB_NOTFOUND ) {
237 data.size = 2 * sizeof( ID );
239 } else if ( rc != 0 ) {
240 Debug( LDAP_DEBUG_ANY,
241 "=> bdb_idl_insert_key: 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,
248 "=> bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
249 (long) sizeof( ID ), (long) data.size, 0 );
252 } else if ( BDB_IDL_IS_RANGE(ids) ) {
255 } else if ( ids[2] > id ) {
261 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
263 Debug( LDAP_DEBUG_ANY,
264 "=> bdb_idl_insert_key: get size mismatch: expected %ld, got %ld\n",
265 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
269 rc = idl_insert( ids, id );
272 Debug( LDAP_DEBUG_ANY,
273 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
278 if( BDB_IDL_IS_RANGE( ids ) ) {
279 data.size = BDB_IDL_RANGE_SIZE;
281 data.size = (ids[0]+1) * sizeof( ID );
286 rc = db->put( db, tid, key, &data, 0 );
289 Debug( LDAP_DEBUG_ANY,
290 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
291 db_strerror(rc), rc, 0 );
305 ID ids[BDB_IDL_DB_SIZE];
308 /* for printable keys only */
309 Debug( LDAP_DEBUG_ARGS,
310 "=> bdb_idl_delete_key: %s %ld\n",
311 key->data, (long) id, 0 );
313 assert( id != NOID );
316 data.ulen = sizeof( ids );
317 data.flags = DB_DBT_USERMEM;
319 /* fetch the key for read/modify/write */
320 rc = db->get( db, tid, key, &data, DB_RMW );
323 Debug( LDAP_DEBUG_ANY,
324 "=> bdb_idl_delete_key: get failed: %s (%d)\n",
325 db_strerror(rc), rc, 0 );
328 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
329 /* size not multiple of ID size */
330 Debug( LDAP_DEBUG_ANY,
331 "=> bdb_idl_delete_key: odd size: expected %ld multiple, got %ld\n",
332 (long) sizeof( ID ), (long) data.size, 0 );
335 } else if ( BDB_IDL_IS_RANGE(ids) ) {
338 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
340 Debug( LDAP_DEBUG_ANY,
341 "=> bdb_idl_delete_key: get size mismatch: expected %ld, got %ld\n",
342 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
346 rc = idl_delete( ids, id );
349 Debug( LDAP_DEBUG_ANY,
350 "=> bdb_idl_delete_key: idl_delete failed (%d)\n",
357 rc = db->del( db, tid, key, 0 );
359 Debug( LDAP_DEBUG_ANY,
360 "=> bdb_idl_delete_key: delete failed: %s (%d)\n",
361 db_strerror(rc), rc, 0 );
366 data.size = (ids[0]+1) * sizeof( ID );
370 rc = db->put( db, tid, key, &data, 0 );
373 Debug( LDAP_DEBUG_ANY,
374 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
375 db_strerror(rc), rc, 0 );
383 * idl_intersection - return a intersection b
386 bdb_idl_intersection(
394 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
399 if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
401 ids[1] = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
402 ids[2] = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
404 if ( ids[1] == ids[2] ) {
406 } else if( ids[1] > ids[2] ) {
412 if( BDB_IDL_IS_RANGE( a ) ) {
418 ida = bdb_idl_first( a, &cursora ),
419 idb = bdb_idl_first( b, &cursorb );
423 while( ida != NOID && idb != NOID ) {
426 ida = bdb_idl_next( a, &cursora );
427 idb = bdb_idl_next( b, &cursorb );
428 if( BDB_IDL_IS_RANGE( b ) && idb < ida ) {
429 if( ida > BDB_IDL_LAST( b ) ) {
436 } else if ( ida < idb ) {
437 ida = bdb_idl_next( a, &cursora );
439 idb = bdb_idl_next( b, &cursorb );
448 * idl_union - return a union b
459 if ( BDB_IDL_IS_ZERO( a ) ) {
460 BDB_IDL_CPY( ids, b );
464 if ( BDB_IDL_IS_ZERO( b ) ) {
465 BDB_IDL_CPY( ids, a );
469 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
471 ids[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
472 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_FIRST(b) );
476 ida = bdb_idl_first( a, &cursora ),
477 idb = bdb_idl_first( b, &cursorb );
481 while( ida != NOID && idb != NOID ) {
482 if( ++ids[0] > BDB_IDL_MAX ) {
484 ids[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
490 ida = bdb_idl_next( a, &cursora );
492 } else if ( ida > idb ) {
494 idb = bdb_idl_next( b, &cursorb );
498 ida = bdb_idl_next( a, &cursora );
499 idb = bdb_idl_next( b, &cursorb );
508 * bdb_idl_notin - return a intersection ~b (or a minus b)
519 if( BDB_IDL_IS_ZERO( a ) ||
520 BDB_IDL_IS_ZERO( b ) ||
521 BDB_IDL_IS_RANGE( b ) )
523 BDB_IDL_CPY( ids, a );
527 if( BDB_IDL_IS_RANGE( a ) ) {
528 BDB_IDL_CPY( ids, a );
532 ida = bdb_idl_first( a, &cursora ),
533 idb = bdb_idl_first( b, &cursorb );
537 while( ida != NOID ) {
539 /* we could shortcut this */
541 ida = bdb_idl_next( a, &cursora );
543 } else if ( ida < idb ) {
545 ida = bdb_idl_next( a, &cursora );
547 } else if ( ida > idb ) {
548 idb = bdb_idl_next( b, &cursorb );
551 ida = bdb_idl_next( a, &cursora );
552 idb = bdb_idl_next( b, &cursorb );
559 ID bdb_idl_first( ID *ids, ID *cursor )
568 if ( BDB_IDL_IS_RANGE( ids ) ) {
569 if( *cursor < ids[1] ) {
575 pos = bdb_idl_search( ids, *cursor );
585 ID bdb_idl_next( ID *ids, ID *cursor )
587 if ( BDB_IDL_IS_RANGE( ids ) ) {
588 if( ids[2] < ++(*cursor) ) {
594 if ( *cursor < ids[0] ) {
595 return ids[(*cursor)++];