]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/cache.c
happy belated new year
[openldap] / servers / slapd / back-bdb / cache.c
index 5d36a2db29c45e72a8cc17e545443ab6632f4f26..f4de1cb9eab933420f065f6139e6dc20f1e52340 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2009 The OpenLDAP Foundation.
+ * Copyright 2000-2010 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -106,10 +106,14 @@ bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
 }
 
 #define LRU_DEL( c, e ) do { \
-       if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
-       if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
-       e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
-       e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
+       if ( e == e->bei_lruprev ) { \
+               (c)->c_lruhead = (c)->c_lrutail = NULL; \
+       } else { \
+               if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
+               if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
+               e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
+               e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
+       } \
        e->bei_lruprev = NULL; \
 } while ( 0 )
 
@@ -689,8 +693,9 @@ bdb_cache_lru_purge( struct bdb_info *bdb )
 {
        DB_LOCK         lock, *lockp;
        EntryInfo *elru, *elnext = NULL;
-       int count, islocked, eimax;
-       int efree = 0, eifree = 0, eicount, ecount;
+       int islocked;
+       ID eicount, ecount;
+       ID count, efree, eifree = 0;
 #ifdef LDAP_DEBUG
        int iter;
 #endif
@@ -698,24 +703,24 @@ bdb_cache_lru_purge( struct bdb_info *bdb )
        /* Wait for the mutex; we're the only one trying to purge. */
        ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
 
+       if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
+               efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
+               efree += bdb->bi_cache.c_minfree;
+       } else {
+               efree = 0;
+       }
+
        /* maximum number of EntryInfo leaves to cache. In slapcat
         * we always free all leaf nodes.
         */
-       if ( slapMode & SLAP_TOOL_READONLY )
-               eimax = 0;
-       else
-               eimax = bdb->bi_cache.c_eimax;
 
-       efree = bdb->bi_cache.c_cursize - bdb->bi_cache.c_maxsize;
-       if ( efree < 1 )
-               efree = 0;
-       else 
-               efree += bdb->bi_cache.c_minfree;
-
-       if ( bdb->bi_cache.c_leaves > eimax ) {
+       if ( slapMode & SLAP_TOOL_READONLY ) {
+               eifree = bdb->bi_cache.c_leaves;
+       } else if ( bdb->bi_cache.c_eimax &&
+               bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
                eifree = bdb->bi_cache.c_minfree * 10;
-               if ( eifree >= eimax )
-                       eifree = eimax / 2;
+               if ( eifree >= bdb->bi_cache.c_leaves )
+                       eifree /= 2;
        }
 
        if ( !efree && !eifree ) {
@@ -967,6 +972,9 @@ load1:
                                 */
                                if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
                                        (*eip)->bei_state &= ~CACHE_ENTRY_NOT_CACHED;
+                                       ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
+                                       ++bdb->bi_cache.c_cursize;
+                                       ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
                                }
                                flag &= ~ID_NOCACHE;
                        }
@@ -1064,20 +1072,17 @@ load1:
        if ( rc == 0 ) {
                int purge = 0;
 
-               if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ||
-                       bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
+               if (( load && !( flag & ID_NOCACHE )) || bdb->bi_cache.c_eimax ) {
                        ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
-                       if ( !bdb->bi_cache.c_purging ) {
-                               if ( load && !( flag & ID_NOCACHE )) {
-                                       bdb->bi_cache.c_cursize++;
-                                       if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
-                                               purge = 1;
-                                               bdb->bi_cache.c_purging = 1;
-                                       }
-                               } else if ( bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
+                       if ( load && !( flag & ID_NOCACHE )) {
+                               bdb->bi_cache.c_cursize++;
+                               if ( !bdb->bi_cache.c_purging && bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
                                        purge = 1;
                                        bdb->bi_cache.c_purging = 1;
                                }
+                       } else if ( !bdb->bi_cache.c_purging && bdb->bi_cache.c_eimax && bdb->bi_cache.c_leaves > bdb->bi_cache.c_eimax ) {
+                               purge = 1;
+                               bdb->bi_cache.c_purging = 1;
                        }
                        ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
                }