int bdb_idl_insert( ID *ids, ID id ) { unsigned x; #if IDL_DEBUG > 1 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 ); idl_dump( ids ); #elif IDL_DEBUG > 0 idl_check( ids ); #endif if (BDB_IDL_IS_RANGE( ids )) { /* if already in range, treat as a dup */ if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids)) return -1; if (id < BDB_IDL_FIRST(ids)) ids[1] = id; else if (id > BDB_IDL_LAST(ids)) ids[2] = id; return 0; } x = bdb_idl_search( ids, id ); assert( x > 0 ); if( x < 1 ) { /* internal error */ return -2; } if ( x <= ids[0] && ids[x] == id ) { /* duplicate */ return -1; } if ( ++ids[0] >= BDB_IDL_DB_MAX ) { if( id < ids[1] ) { ids[1] = id; ids[2] = ids[ids[0]-1]; } else if ( ids[ids[0]-1] < id ) { ids[2] = id; } else { ids[2] = ids[ids[0]-1]; } ids[0] = NOID; } else { /* insert id */ AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) ); ids[x] = id; } #if IDL_DEBUG > 1 idl_dump( ids ); #elif IDL_DEBUG > 0 idl_check( ids ); #endif return 0; }
unsigned bdb_idl_search( ID *ids, ID id ) { #define IDL_BINARY_SEARCH 1 #ifdef IDL_BINARY_SEARCH /* * binary search of id in ids * if found, returns position of id * if not found, returns first postion greater than id */ unsigned base = 0; unsigned cursor = 0; int val = 0; unsigned n = ids[0]; #if IDL_DEBUG > 0 idl_check( ids ); #endif while( 0 < n ) { int pivot = n >> 1; cursor = base + pivot; val = IDL_CMP( id, ids[cursor + 1] ); if( val < 0 ) { n = pivot; } else if ( val > 0 ) { base = cursor + 1; n -= pivot + 1; } else { return cursor + 1; } } if( val > 0 ) { return cursor + 2; } else { return cursor + 1; } #else /* (reverse) linear search */ int i; #if IDL_DEBUG > 0 idl_check( ids ); #endif for( i=ids[0]; i; i-- ) { if( id > ids[i] ) { break; } } return i+1; #endif }
static int bdb_idl_delete( ID *ids, ID id ) { unsigned x; #if IDL_DEBUG > 1 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 ); idl_dump( ids ); #elif IDL_DEBUG > 0 idl_check( ids ); #endif if (BDB_IDL_IS_RANGE( ids )) { /* If deleting a range boundary, adjust */ if ( ids[1] == id ) ids[1]++; else if ( ids[2] == id ) ids[2]--; /* deleting from inside a range is a no-op */ /* If the range has collapsed, re-adjust */ if ( ids[1] > ids[2] ) ids[0] = 0; else if ( ids[1] == ids[2] ) ids[1] = 1; return 0; } x = bdb_idl_search( ids, id ); assert( x > 0 ); if( x <= 0 ) { /* internal error */ return -2; } if( x > ids[0] || ids[x] != id ) { /* not found */ return -1; } else if ( --ids[0] == 0 ) { if( x != 1 ) { return -3; } } else { AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) ); } #if IDL_DEBUG > 1 idl_dump( ids ); #elif IDL_DEBUG > 0 idl_check( ids ); #endif return 0; }
int bdb_idl_insert( ID *ids, ID id ) { unsigned x = bdb_idl_search( ids, id ); #if IDL_DEBUG > 1 #ifdef NEW_LOGGING LDAP_LOG( INDEX, DETAIL1, "insert: %04lx at %d\n", (long) id, x, 0 ); #else Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 ); idl_dump( ids ); #endif #elif IDL_DEBUG > 0 idl_check( ids ); #endif assert( x > 0 ); if( x < 1 ) { /* internal error */ return -2; } if ( x <= ids[0] && ids[x] == id ) { /* duplicate */ return -1; } if ( ++ids[0] >= BDB_IDL_DB_MAX ) { if( id < ids[1] ) { ids[1] = id; ids[2] = ids[ids[0]-1]; } else if ( ids[ids[0]-1] < id ) { ids[2] = id; } else { ids[2] = ids[ids[0]-1]; } ids[0] = NOID; } else { /* insert id */ AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) ); ids[x] = id; } #if IDL_DEBUG > 1 idl_dump( ids ); #elif IDL_DEBUG > 0 idl_check( ids ); #endif return 0; }
static void idl_dump( ID *ids ) { if( WT_IDL_IS_RANGE( ids ) ) { Debug( LDAP_DEBUG_ANY, "IDL: range ( %ld - %ld )\n", (long) WT_IDL_RANGE_FIRST( ids ), (long) WT_IDL_RANGE_LAST( ids ), 0); } else { ID i; Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 ); for( i=1; i<=ids[0]; i++ ) { if( i % 16 == 1 ) { Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 ); } Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 ); } Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 ); } idl_check( ids ); }
/* insert id */ AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) ); ids[x] = id; } #if IDL_DEBUG > 1 idl_dump( ids ); #elif IDL_DEBUG > 0 idl_check( ids ); #endif return 0; } #if 0 /* unused */ static int idl_delete( ID *ids, ID id ) { unsigned x = bdb_idl_search( ids, id ); #if IDL_DEBUG > 1 #ifdef NEW_LOGGING LDAP_LOG( INDEX, DETAIL1, "delete: %04lx at %d\n", (long) id, x, 0 ); #else Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 ); idl_dump( ids ); #endif #elif IDL_DEBUG > 0 idl_check( ids ); #endif assert( x > 0 ); if( x <= 0 ) { /* internal error */ return -2; } if( x > ids[0] || ids[x] != id ) { /* not found */ return -1; } else if ( --ids[0] == 0 ) { if( x != 1 ) { return -3; } } else { AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) ); } #if IDL_DEBUG > 1 idl_dump( ids ); #elif IDL_DEBUG > 0 idl_check( ids ); #endif return 0; }
static void idl_dump( ID *ids ) { if( BDB_IDL_IS_RANGE( ids ) ) { #ifdef NEW_LOGGING LDAP_LOG( INDEX, INFO, "IDL: range (%ld - %ld)\n", (long) BDB_IDL_RANGE_FIRST( ids ), (long) BDB_IDL_RANGE_LAST( ids ), 0 ); #else Debug( LDAP_DEBUG_ANY, "IDL: range ( %ld - %ld )\n", (long) BDB_IDL_RANGE_FIRST( ids ), (long) BDB_IDL_RANGE_LAST( ids ) ); #endif } else { ID i; #ifdef NEW_LOGGING LDAP_LOG( INDEX, INFO, "IDL: size %ld", (long) ids[0], 0, 0 ); #else Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 ); #endif for( i=1; i<=ids[0]; i++ ) { if( i % 16 == 1 ) { Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 ); } #ifdef NEW_LOGGING LDAP_LOG( INDEX, INFO, "%02lx",(long)ids[i], 0, 0 ); #else Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 ); #endif } Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 ); } idl_check( ids ); }