1 /* cache.c - routines to maintain an in-core cache of entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
25 #include "ac/string.h"
29 #include "back-monitor.h"
32 * The cache maps DNs to Entries.
33 * Each entry, on turn, holds the list of its children in the e_private field.
34 * This is used by search operation to perform onelevel and subtree candidate
37 typedef struct monitor_cache_t {
43 * compares entries based on the dn
51 monitor_cache_t *cc1 = ( monitor_cache_t * )c1;
52 monitor_cache_t *cc2 = ( monitor_cache_t * )c2;
55 * case sensitive, because the dn MUST be normalized
57 return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn );
61 * checks for duplicate entries
69 monitor_cache_t *cc1 = ( monitor_cache_t * )c1;
70 monitor_cache_t *cc2 = ( monitor_cache_t * )c2;
73 * case sensitive, because the dn MUST be normalized
75 return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn ) == 0 ? -1 : 0;
79 * adds an entry to the cache and inits the mutex
94 mp = ( monitor_entry_t *)e->e_private;
95 ldap_pvt_thread_mutex_init( &mp->mp_mutex );
97 mc = ( monitor_cache_t * )ch_malloc( sizeof( monitor_cache_t ) );
98 mc->mc_ndn = e->e_nname;
100 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
101 rc = avl_insert( &mi->mi_cache, ( caddr_t )mc,
102 monitor_cache_cmp, monitor_cache_dup );
103 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
109 * locks the entry (no r/w)
119 assert( e->e_private != NULL );
121 mp = ( monitor_entry_t * )e->e_private;
122 ldap_pvt_thread_mutex_lock( &mp->mp_mutex );
128 * gets an entry from the cache based on the normalized dn
138 monitor_cache_t tmp_mc, *mc;
140 assert( mi != NULL );
141 assert( ndn != NULL );
142 assert( ep != NULL );
144 tmp_mc.mc_ndn = *ndn;
145 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
146 mc = ( monitor_cache_t * )avl_find( mi->mi_cache,
147 ( caddr_t )&tmp_mc, monitor_cache_cmp );
150 /* entry is returned with mutex locked */
151 monitor_cache_lock( mc->mc_e );
152 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
158 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
165 * If the entry exists in cache, it is returned in locked status;
166 * otherwise, if the parent exists, if it may generate volatile
167 * descendants an attempt to generate the required entry is
168 * performed and, if successful, the entry is returned
171 monitor_cache_dn2entry(
178 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
180 struct berval p_ndn = BER_BVNULL;
184 assert( mi != NULL );
185 assert( ndn != NULL );
186 assert( ep != NULL );
187 assert( matched != NULL );
191 if ( !dnIsSuffix( ndn, &op->o_bd->be_nsuffix[ 0 ] ) ) {
195 rc = monitor_cache_get( mi, ndn, ep );
196 if ( !rc && *ep != NULL ) {
200 /* try with parent/ancestors */
201 if ( BER_BVISNULL( ndn ) ) {
202 BER_BVSTR( &p_ndn, "" );
205 dnParent( ndn, &p_ndn );
208 rc = monitor_cache_dn2entry( op, &p_ndn, &e_parent, matched );
209 if ( rc || e_parent == NULL ) {
213 mp = ( monitor_entry_t * )e_parent->e_private;
215 if ( mp->mp_flags & MONITOR_F_VOLATILE_CH ) {
216 /* parent entry generates volatile children */
217 rc = monitor_entry_create( op, ndn, e_parent, ep );
221 monitor_cache_release( mi, e_parent );
230 * releases the lock of the entry; if it is marked as volatile, it is
234 monitor_cache_release(
241 assert( mi != NULL );
243 assert( e->e_private != NULL );
245 mp = ( monitor_entry_t * )e->e_private;
247 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
248 monitor_cache_t *mc, tmp_mc;
250 /* volatile entries do not return to cache */
251 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
252 tmp_mc.mc_ndn = e->e_nname;
253 mc = avl_delete( &mi->mi_cache,
254 ( caddr_t )&tmp_mc, monitor_cache_cmp );
255 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
258 ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
259 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
267 ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );