]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-monitor/cache.c
Fix LBER_ERROR vs. -1 confusion.
[openldap] / servers / slapd / back-monitor / cache.c
index f5d70f59e12e63a13aa81d9ed4a67755952cad08..d1455a182063a960fb35ffb1003d5f9da5afaccd 100644 (file)
@@ -1,12 +1,9 @@
 /* cache.c - routines to maintain an in-core cache of entries */
 /*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /*
- * Copyright 2001 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- * 
  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
  * 
  * This work has beed deveolped for the OpenLDAP Foundation 
@@ -51,13 +48,13 @@ monitor_cache_cmp(
                const void *c2
 )
 {
-       struct monitorcache *cc1 = ( struct monitorcache * )c1;
-       struct monitorcache *cc2 = ( struct monitorcache * )c2;
-       
+       struct monitorcache     *cc1 = ( struct monitorcache * )c1;
+       struct monitorcache     *cc2 = ( struct monitorcache * )c2;
+
        /*
         * case sensitive, because the dn MUST be normalized
         */
-       return strcmp( cc1->mc_ndn, cc2->mc_ndn );
+       return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn );
 }
 
 /*
@@ -71,11 +68,11 @@ monitor_cache_dup(
 {
        struct monitorcache *cc1 = ( struct monitorcache * )c1;
        struct monitorcache *cc2 = ( struct monitorcache * )c2;
-       
+
        /*
         * case sensitive, because the dn MUST be normalized
         */
-       return ( strcmp( cc1->mc_ndn, cc2->mc_ndn ) == 0 ) ? -1 : 0;
+       return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn ) == 0 ? -1 : 0;
 }
 
 /*
@@ -98,7 +95,7 @@ monitor_cache_add(
        ldap_pvt_thread_mutex_init( &mp->mp_mutex );
 
        mc = ( struct monitorcache * )ch_malloc( sizeof( struct monitorcache ) );
-       mc->mc_ndn = e->e_ndn;
+       mc->mc_ndn = e->e_nname;
        mc->mc_e = e;
        ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
        rc = avl_insert( &mi->mi_cache, ( caddr_t )mc,
@@ -134,7 +131,7 @@ monitor_cache_lock(
 int
 monitor_cache_get(
                struct monitorinfo      *mi,
-               const char              *ndn,
+               struct berval           *ndn,
                Entry                   **ep
 )
 {
@@ -144,7 +141,7 @@ monitor_cache_get(
        assert( ndn != NULL );
        assert( ep != NULL );
 
-       tmp_mc.mc_ndn = ( char * )ndn;
+       tmp_mc.mc_ndn = *ndn;
        ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
        mc = ( struct monitorcache * )avl_find( mi->mi_cache,
                        ( caddr_t )&tmp_mc, monitor_cache_cmp );
@@ -173,19 +170,23 @@ monitor_cache_get(
 int
 monitor_cache_dn2entry(
                struct monitorinfo      *mi,
-               const char              *ndn,
-               Entry                   **ep
+               struct berval           *ndn,
+               Entry                   **ep,
+               Entry                   **matched
 )
 {
-       int rc;
+       int             rc;
 
-       char *p_ndn;
-       Entry *e_parent;
+       struct berval           p_ndn = { 0L, NULL };
+       Entry                   *e_parent;
        struct monitorentrypriv *mp;
                
        assert( mi != NULL );
        assert( ndn != NULL );
        assert( ep != NULL );
+       assert( matched != NULL );
+
+       *matched = NULL;
 
        rc = monitor_cache_get( mi, ndn, ep );
                if ( !rc && *ep != NULL ) {
@@ -193,9 +194,20 @@ monitor_cache_dn2entry(
        }
 
        /* try with parent/ancestors */
-       p_ndn = dn_parent( NULL, ndn );
-       rc = monitor_cache_dn2entry( mi, p_ndn, &e_parent );
-       free( p_ndn );
+       if ( ndn->bv_len ) {
+               dnParent( ndn, &p_ndn );
+       }
+
+       if ( p_ndn.bv_val == NULL ) {
+               p_ndn.bv_val = "";
+               p_ndn.bv_len = 0;
+               
+       } else {
+               p_ndn.bv_len = ndn->bv_len 
+                       - ( ber_len_t ) ( p_ndn.bv_val - ndn->bv_val );
+       }
+
+       rc = monitor_cache_dn2entry( mi, &p_ndn, &e_parent, matched );
        if ( rc || e_parent == NULL) {
                return( -1 );
        }
@@ -206,7 +218,12 @@ monitor_cache_dn2entry(
                /* parent entry generates volatile children */
                rc = monitor_entry_create( mi, ndn, e_parent, ep );
        }
-       monitor_cache_release( mi, e_parent );
+
+       if ( !rc ) {
+               monitor_cache_release( mi, e_parent );
+       } else {
+               *matched = e_parent;
+       }
        
        return( rc );
 }
@@ -230,15 +247,27 @@ monitor_cache_release(
        mp = ( struct monitorentrypriv * )e->e_private;
 
        if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
+               struct monitorcache     *mc, tmp_mc;
+
                /* volatile entries do not return to cache */
+               ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
+               tmp_mc.mc_ndn = e->e_nname;
+               mc = avl_delete( &mi->mi_cache,
+                               ( caddr_t )&tmp_mc, monitor_cache_cmp );
+               ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
+               ch_free( mc );
+               
+               ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
                ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
                ch_free( mp );
                e->e_private = NULL;
                entry_free( e );
+
                return( 0 );
        }
        
        ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
+
        return( 0 );
 }