]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/cache.c
Removed unnecessary definition that is already in core.schema.
[openldap] / servers / slapd / back-ldbm / cache.c
index a1877e073867e33a2729b52c3704d2342f112fcc..1e6d1bbd3ef66bccf17efbf03697c8acda051a8a 100644 (file)
@@ -38,24 +38,10 @@ static int  cache_delete_entry_internal(struct cache *cache, Entry *e);
 static void    lru_print(struct cache *cache);
 #endif
 
-/*
- * the cache has three entry points (ways to find things):
- *
- *     by entry        e.g., if you already have an entry from the cache
- *                     and want to delete it. (really by entry ptr)
- *     by dn           e.g., when looking for the base object of a search
- *     by id           e.g., for search candidates
- *
- * these correspond to three different avl trees that are maintained.
- */
-#define cache_entry_cmp entry_cmp
-#define cache_entrydn_cmp entry_dn_cmp
-#define cache_entryid_cmp entry_id_cmp
-
 static int
 cache_entry_rdwr_lock(Entry *e, int rw)
 {
-       Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %lu\n",
+       Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
                rw ? "w" : "r", e->e_id, 0);
 
        if (rw)
@@ -67,7 +53,7 @@ cache_entry_rdwr_lock(Entry *e, int rw)
 static int
 cache_entry_rdwr_trylock(Entry *e, int rw)
 {
-       Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %lu\n",
+       Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
                rw ? "w" : "r", e->e_id, 0);
 
        if (rw)
@@ -79,7 +65,7 @@ cache_entry_rdwr_trylock(Entry *e, int rw)
 static int
 cache_entry_rdwr_unlock(Entry *e, int rw)
 {
-       Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %lu\n",
+       Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
                rw ? "w" : "r", e->e_id, 0);
 
        if (rw)
@@ -103,11 +89,7 @@ cache_entry_rdwr_destroy(Entry *e)
 static int
 cache_entry_private_init( Entry*e )
 {
-       struct ldbm_entry_info *lei;
-
-#ifdef LDAP_DEBUG
        assert( e->e_private == NULL );
-#endif
 
        if( e->e_private != NULL ) {
                /* this should never happen */
@@ -128,11 +110,7 @@ cache_entry_private_init( Entry*e )
 static int
 cache_entry_private_destroy( Entry*e )
 {
-       struct ldbm_entry_info *lei;
-
-#ifdef LDAP_DEBUG
        assert( e->e_private );
-#endif
 
        cache_entry_rdwr_destroy( e );
 
@@ -144,74 +122,84 @@ cache_entry_private_destroy( Entry*e )
 void
 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
 {
+       ID id;
+       int refcnt;
+
        /* set cache mutex */
        ldap_pvt_thread_mutex_lock( &cache->c_mutex );
 
-#ifdef LDAP_DEBUG
        assert( e->e_private );
-#endif
 
        cache_entry_rdwr_unlock(e, rw);
 
-       LEI(e)->lei_refcnt--;
+       id = e->e_id;
+       refcnt = --LEI(e)->lei_refcnt;
 
        if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
-               Debug( LDAP_DEBUG_TRACE,
-                       "====> cache_return_entry_%s( %lu ): created (%d)\n",
-                       rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
-
                LEI(e)->lei_state = CACHE_ENTRY_READY;
 
+               /* free cache mutex */
+               ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+
+               Debug( LDAP_DEBUG_TRACE,
+                       "====> cache_return_entry_%s( %ld ): created (%d)\n",
+                       rw ? "w" : "r", id, refcnt );
+
        } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
-               if( LEI(e)->lei_refcnt > 0 ) {
-                       Debug( LDAP_DEBUG_TRACE,
-                               "====> cache_return_entry_%s( %lu ): delete pending (%d)\n",
-                               rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
+               if( refcnt > 0 ) {
+                       /* free cache mutex */
+                       ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
 
-               } else {
                        Debug( LDAP_DEBUG_TRACE,
-                               "====> cache_return_entry_%s( %lu ): deleted (%d)\n",
-                               rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
+                               "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
+                               rw ? "w" : "r", id, refcnt );
 
+               } else {
                        cache_entry_private_destroy( e );
                        entry_free( e );
+
+                       /* free cache mutex */
+                       ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+
+                       Debug( LDAP_DEBUG_TRACE,
+                               "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
+                               rw ? "w" : "r", id, refcnt );
                }
 
        } else {
+               /* free cache mutex */
+               ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+
                Debug( LDAP_DEBUG_TRACE,
-                       "====> cache_return_entry_%s( %lu ): returned (%d)\n",
-                       rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
+                       "====> cache_return_entry_%s( %ld ): returned (%d)\n",
+                       rw ? "w" : "r", id, refcnt);
        }
-
-
-       /* free cache mutex */
-       ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
 }
 
-#define LRU_DELETE( cache, e ) { \
+#define LRU_DELETE( cache, e ) do { \
        if ( LEI(e)->lei_lruprev != NULL ) { \
                LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
        } else { \
-               cache->c_lruhead = LEI(e)->lei_lrunext; \
+               (cache)->c_lruhead = LEI(e)->lei_lrunext; \
        } \
        if ( LEI(e)->lei_lrunext != NULL ) { \
                LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
        } else { \
-               cache->c_lrutail = LEI(e)->lei_lruprev; \
+               (cache)->c_lrutail = LEI(e)->lei_lruprev; \
        } \
-}
+} while(0)
 
-#define LRU_ADD( cache, e ) { \
-       LEI(e)->lei_lrunext = cache->c_lruhead; \
+#define LRU_ADD( cache, e ) do { \
+       LEI(e)->lei_lrunext = (cache)->c_lruhead; \
        if ( LEI(e)->lei_lrunext != NULL ) { \
-               LEI(LEI(e)->lei_lrunext)->lei_lruprev = e; \
+               LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
        } \
-       cache->c_lruhead = e; \
+       (cache)->c_lruhead = (e); \
        LEI(e)->lei_lruprev = NULL; \
-       if ( cache->c_lrutail == NULL ) { \
-               cache->c_lrutail = e; \
+       if ( (cache)->c_lrutail == NULL ) { \
+               (cache)->c_lrutail = (e); \
        } \
-}
+} while(0)
 
 /*
  * cache_add_entry_rw - create and lock an entry in the cache
@@ -232,43 +220,46 @@ cache_add_entry_rw(
        /* set cache mutex */
        ldap_pvt_thread_mutex_lock( &cache->c_mutex );
 
-#ifdef LDAP_DEBUG
        assert( e->e_private == NULL );
-#endif
 
        if( cache_entry_private_init(e) != 0 ) {
+               /* free cache mutex */
+               ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+
                Debug( LDAP_DEBUG_ANY,
-                       "====> cache_add_entry( %lu ): \"%s\": private init failed!\n",
+                       "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
                    e->e_id, e->e_dn, 0 );
+
                return( -1 );
        }
 
        if ( avl_insert( &cache->c_dntree, (caddr_t) e,
-               cache_entrydn_cmp, avl_dup_error ) != 0 )
+               (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
        {
+               /* free cache mutex */
+               ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+
                Debug( LDAP_DEBUG_TRACE,
-                       "====> cache_add_entry( %lu ): \"%s\": already in dn cache\n",
+                       "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
                    e->e_id, e->e_dn, 0 );
 
                cache_entry_private_destroy(e);
 
-               /* free cache mutex */
-               ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
                return( 1 );
        }
 
        /* id tree */
        if ( avl_insert( &cache->c_idtree, (caddr_t) e,
-               cache_entryid_cmp, avl_dup_error ) != 0 )
+               (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
        {
                Debug( LDAP_DEBUG_ANY,
-                       "====> cache_add_entry( %lu ): \"%s\": already in id cache\n",
+                       "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
                    e->e_id, e->e_dn, 0 );
 
 
                /* delete from dn tree inserted above */
                if ( avl_delete( &cache->c_dntree, (caddr_t) e,
-                       cache_entrydn_cmp ) == NULL )
+                       (AVL_CMP) entry_dn_cmp ) == NULL )
                {
                        Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
                            0, 0, 0 );
@@ -319,6 +310,7 @@ cache_add_entry_rw(
                        e = cache->c_lrutail;
 
                        /* delete from cache and lru q */
+                       /* XXX do we need rc ? */
                        rc = cache_delete_entry_internal( cache, e );
                        cache_entry_private_destroy( e );
                        entry_free( e );
@@ -348,15 +340,13 @@ cache_update_entry(
        /* set cache mutex */
        ldap_pvt_thread_mutex_lock( &cache->c_mutex );
 
-#ifdef LDAP_DEBUG
        assert( e->e_private );
-#endif
 
        if ( avl_insert( &cache->c_dntree, (caddr_t) e,
-               cache_entrydn_cmp, avl_dup_error ) != 0 )
+               (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
        {
                Debug( LDAP_DEBUG_TRACE,
-                       "====> cache_update_entry( %lu ): \"%s\": already in dn cache\n",
+                       "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
                    e->e_id, e->e_dn, 0 );
 
                /* free cache mutex */
@@ -366,15 +356,15 @@ cache_update_entry(
 
        /* id tree */
        if ( avl_insert( &cache->c_idtree, (caddr_t) e,
-               cache_entryid_cmp, avl_dup_error ) != 0 )
+               (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
        {
                Debug( LDAP_DEBUG_ANY,
-                       "====> cache_update_entry( %lu ): \"%s\": already in id cache\n",
+                       "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
                    e->e_id, e->e_dn, 0 );
 
                /* delete from dn tree inserted above */
                if ( avl_delete( &cache->c_dntree, (caddr_t) e,
-                       cache_entrydn_cmp ) == NULL )
+                       (AVL_CMP) entry_dn_cmp ) == NULL )
                {
                        Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
                            0, 0, 0 );
@@ -421,6 +411,7 @@ cache_update_entry(
                        e = cache->c_lrutail;
 
                        /* delete from cache and lru q */
+                       /* XXX do we need rc ? */
                        rc = cache_delete_entry_internal( cache, e );
                        cache_entry_private_destroy( e );
                        entry_free( e );
@@ -443,68 +434,73 @@ cache_find_entry_dn2id(
     char               *dn
 )
 {
-       struct ldbminfo *li = (struct ldbminfo *) be->be_private;
        Entry           e, *ep;
        ID                      id;
-
-       /* set cache mutex */
-       ldap_pvt_thread_mutex_lock( &cache->c_mutex );
+       int count = 0;
 
        e.e_dn = dn;
        e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
 
+try_again:
+       /* set cache mutex */
+       ldap_pvt_thread_mutex_lock( &cache->c_mutex );
+
        if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
-               cache_entrydn_cmp )) != NULL )
+               (AVL_CMP) entry_dn_cmp )) != NULL )
        {
+               int state;
+               count++;
+
                /*
                 * ep now points to an unlocked entry
                 * we do not need to lock the entry if we only
                 * check the state, refcnt, LRU, and id.
                 */
-               free(e.e_ndn);
 
-#ifdef LDAP_DEBUG
                assert( ep->e_private );
-#endif
+
+               /* save id */
+               id = ep->e_id;
+               state = LEI(ep)->lei_state;
+
                /*
                 * entry is deleted or not fully created yet
                 */
-               if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
-#ifdef LDAP_DEBUG
-                       assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
-#endif
-                       Debug(LDAP_DEBUG_TRACE,
-                               "====> cache_find_entry_dn2id(\"%s\"): %lu (not ready) %d\n",
-                               dn, ep->e_id, LEI(ep)->lei_state);
+               if ( state != CACHE_ENTRY_READY ) {
+                       assert(state != CACHE_ENTRY_UNDEFINED);
 
                        /* free cache mutex */
                        ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
-                       return( NOID );
-               }
 
-               Debug(LDAP_DEBUG_TRACE,
-                       "====> cache_find_entry_dn2id(\"%s\"): %lu\n",
-                       dn, ep->e_id, 0);
+                       Debug(LDAP_DEBUG_TRACE,
+                               "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
+                               dn, id, state);
+
+                       ldap_pvt_thread_yield();
+                       goto try_again;
+               }
 
                /* lru */
                LRU_DELETE( cache, ep );
                LRU_ADD( cache, ep );
                 
-               /* save id */
-               id = ep->e_id;
+               /* free cache mutex */
+               ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
 
+               Debug(LDAP_DEBUG_TRACE,
+                       "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
+                       dn, id, count);
+
+       } else {
                /* free cache mutex */
                ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
 
-               return( id );
+               id = NOID;
        }
 
        free(e.e_ndn);
 
-       /* free cache mutex */
-       ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
-
-       return( NOID );
+       return( id );
 }
 
 /*
@@ -520,6 +516,7 @@ cache_find_entry_id(
 {
        Entry   e;
        Entry   *ep;
+       int     count = 0;
 
        e.e_id = id;
 
@@ -528,30 +525,35 @@ try_again:
        ldap_pvt_thread_mutex_lock( &cache->c_mutex );
 
        if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
-               cache_entryid_cmp )) != NULL )
+               (AVL_CMP) entry_id_cmp )) != NULL )
        {
-#ifdef LDAP_DEBUG
+               int state;
+               ID      ep_id;
+
+               count++;
+
                assert( ep->e_private );
-#endif
+
+               ep_id = ep->e_id; 
+               state = LEI(ep)->lei_state;
+
                /*
                 * entry is deleted or not fully created yet
                 */
-               if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
-#ifdef LDAP_DEBUG
-                       assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
-#endif
-                       Debug(LDAP_DEBUG_TRACE,
-                               "====> cache_find_entry_id( %lu ): %lu (not ready) %d\n",
-                               id, ep->e_id, LEI(ep)->lei_state);
+               if ( state != CACHE_ENTRY_READY ) {
+
+                       assert(state != CACHE_ENTRY_UNDEFINED);
 
                        /* free cache mutex */
                        ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
-                       return( NULL );
-               }
 
-               Debug(LDAP_DEBUG_TRACE,
-                       "====> cache_find_entry_id( %lu, %s ) \"%s\" (found)\n",
-                       id, rw ? "w" : "r", ep->e_dn);
+                       Debug(LDAP_DEBUG_TRACE,
+                               "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
+                               id, ep_id, state);
+
+                       ldap_pvt_thread_yield();
+                       goto try_again;
+               }
 
                /* acquire reader lock */
                if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
@@ -562,6 +564,11 @@ try_again:
 
                        /* free cache mutex */
                        ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+
+                       Debug(LDAP_DEBUG_TRACE,
+                               "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
+                               id, ep_id, state);
+
                        ldap_pvt_thread_yield();
                        goto try_again;
                }
@@ -575,6 +582,10 @@ try_again:
                /* free cache mutex */
                ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
 
+               Debug(LDAP_DEBUG_TRACE,
+                       "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
+                       ep_id, ep->e_dn, count);
+
                return( ep );
        }
 
@@ -606,11 +617,9 @@ cache_delete_entry(
        /* set cache mutex */
        ldap_pvt_thread_mutex_lock( &cache->c_mutex );
 
-#ifdef LDAP_DEBUG
        assert( e->e_private );
-#endif
 
-       Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %lu )\n",
+       Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
                e->e_id, 0, 0 );
 
        rc = cache_delete_entry_internal( cache, e );
@@ -629,14 +638,14 @@ cache_delete_entry_internal(
        int rc = 0;     /* return code */
 
        /* dn tree */
-       if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
+       if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
                == NULL )
        {
                rc = -1;
        }
 
        /* id tree */
-       if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
+       if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
                == NULL )
        {
                rc = -1;
@@ -658,6 +667,39 @@ cache_delete_entry_internal(
        return( 0 );
 }
 
+#ifdef SLAP_CLEANUP
+
+void
+cache_release_all( struct cache *cache )
+{
+       Entry *e;
+       int rc;
+
+       /* set cache mutex */
+       ldap_pvt_thread_mutex_lock( &cache->c_mutex );
+
+       Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
+
+       while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
+               assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
+
+               /* delete from cache and lru q */
+               /* XXX do we need rc ? */
+               rc = cache_delete_entry_internal( cache, e );
+               cache_entry_private_destroy( e );
+               entry_free( e );
+       }
+
+       if ( cache->c_cursize ) {
+               Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
+       }
+
+       /* free cache mutex */
+       ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+}
+
+#endif /* SLAP_CLEANUP */
+
 #ifdef LDAP_DEBUG
 
 static void
@@ -667,12 +709,12 @@ lru_print( struct cache *cache )
 
        fprintf( stderr, "LRU queue (head to tail):\n" );
        for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
-               fprintf( stderr, "\tdn \"%20s\" id %lu refcnt %d\n",
+               fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
                        e->e_dn, e->e_id, LEI(e)->lei_refcnt );
        }
        fprintf( stderr, "LRU queue (tail to head):\n" );
        for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
-               fprintf( stderr, "\tdn \"%20s\" id %lu refcnt %d\n",
+               fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
                        e->e_dn, e->e_id, LEI(e)->lei_refcnt );
        }
 }