/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 2000-2005 The OpenLDAP Foundation.
+ * Copyright 2000-2007 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
#define IDL_LRU_DELETE( bdb, e ) do { \
- if ( e->idl_lru_prev != NULL ) { \
- e->idl_lru_prev->idl_lru_next = e->idl_lru_next; \
- } else { \
- bdb->bi_idl_lru_head = e->idl_lru_next; \
- } \
- if ( e->idl_lru_next != NULL ) { \
- e->idl_lru_next->idl_lru_prev = e->idl_lru_prev; \
- } else { \
- bdb->bi_idl_lru_tail = e->idl_lru_prev; \
- } \
-} while ( 0 )
-
-#define IDL_LRU_ADD( bdb, e ) do { \
- e->idl_lru_next = bdb->bi_idl_lru_head; \
- if ( e->idl_lru_next != NULL ) { \
- e->idl_lru_next->idl_lru_prev = (e); \
- } \
- (bdb)->bi_idl_lru_head = (e); \
- e->idl_lru_prev = NULL; \
- if ( (bdb)->bi_idl_lru_tail == NULL ) { \
- (bdb)->bi_idl_lru_tail = (e); \
- } \
+ if ( e == bdb->bi_idl_lru_head ) bdb->bi_idl_lru_head = e->idl_lru_next; \
+ if ( e == bdb->bi_idl_lru_tail ) bdb->bi_idl_lru_tail = e->idl_lru_prev; \
+ e->idl_lru_next->idl_lru_prev = e->idl_lru_prev; \
+ e->idl_lru_prev->idl_lru_next = e->idl_lru_next; \
} while ( 0 )
static int
static int bdb_idl_delete( ID *ids, ID id )
{
- unsigned x = bdb_idl_search( ids, id );
+ unsigned x;
#if IDL_DEBUG > 1
Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 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 ) {
if ( matched_idl_entry != NULL ) {
if ( matched_idl_entry->idl && ids )
BDB_IDL_CPY( ids, matched_idl_entry->idl );
- ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
- IDL_LRU_DELETE( bdb, matched_idl_entry );
- IDL_LRU_ADD( bdb, matched_idl_entry );
- ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
+ matched_idl_entry->idl_flags |= CACHE_ENTRY_REFERENCED;
if ( matched_idl_entry->idl )
rc = LDAP_SUCCESS;
else
int rc )
{
bdb_idl_cache_entry_t idl_tmp;
- bdb_idl_cache_entry_t *ee;
+ bdb_idl_cache_entry_t *ee, *eprev;
+
+ if ( rc == DB_NOTFOUND || BDB_IDL_IS_ZERO( ids ))
+ return;
DBT2bv( key, &idl_tmp.kstr );
ee = (bdb_idl_cache_entry_t *) ch_malloc(
sizeof( bdb_idl_cache_entry_t ) );
ee->db = db;
- if ( rc == DB_NOTFOUND || BDB_IDL_IS_ZERO( ids )) {
- ee->idl = NULL;
- } else {
- ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
- BDB_IDL_CPY( ee->idl, ids );
- }
+ ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
+ BDB_IDL_CPY( ee->idl, ids );
+
ee->idl_lru_prev = NULL;
ee->idl_lru_next = NULL;
+ ee->idl_flags = 0;
ber_dupbv( &ee->kstr, &idl_tmp.kstr );
ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
return;
}
ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
- IDL_LRU_ADD( bdb, ee );
+ /* LRU_ADD */
+ if ( bdb->bi_idl_lru_head ) {
+ ee->idl_lru_next = bdb->bi_idl_lru_head;
+ ee->idl_lru_prev = bdb->bi_idl_lru_head->idl_lru_prev;
+ bdb->bi_idl_lru_head->idl_lru_prev->idl_lru_next = ee;
+ bdb->bi_idl_lru_head->idl_lru_prev = ee;
+ } else {
+ ee->idl_lru_next = ee->idl_lru_prev = ee;
+ bdb->bi_idl_lru_tail = ee;
+ }
+ bdb->bi_idl_lru_head = ee;
+
if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
- int i = 0;
- while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
- ee = bdb->bi_idl_lru_tail;
+ int i;
+ ee = bdb->bi_idl_lru_tail;
+ for ( i = 0; i < 10; i++, ee = eprev ) {
+ eprev = ee->idl_lru_prev;
+ if ( ee->idl_flags & CACHE_ENTRY_REFERENCED ) {
+ ee->idl_flags ^= CACHE_ENTRY_REFERENCED;
+ continue;
+ }
if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
bdb_idl_entry_cmp ) == NULL ) {
Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
ch_free( ee->idl );
ch_free( ee );
}
+ bdb->bi_idl_lru_tail = eprev;
}
-
ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
}
IDL_LRU_DELETE( bdb, cache_entry );
ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
free( cache_entry->kstr.bv_val );
- if ( cache_entry->idl )
- free( cache_entry->idl );
+ free( cache_entry->idl );
free( cache_entry );
}
}
bdb_idl_fetch_key(
BackendDB *be,
DB *db,
- DB_TXN *tid,
+ u_int32_t locker,
DBT *key,
ID *ids,
DBC **saved_cursor,
/* If we're not reusing an existing cursor, get a new one */
if( opflag != DB_NEXT ) {
- rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
+ rc = db->cursor( db, NULL, &cursor, bdb->bi_db_opflags );
if( rc != 0 ) {
Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
"cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
return rc;
}
+ cursor->locker = locker;
} else {
cursor = *saved_cursor;
}
return 0;
}
-#if 0
+#if 1
/* Quicksort + Insertion sort for small arrays */