1 /* cache.c - routines to maintain an in-core cache of entries */
3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright 2001 The OpenLDAP Foundation, All Rights Reserved.
8 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
12 * This work has beed deveolped for the OpenLDAP Foundation
13 * in the hope that it may be useful to the Open Source community,
14 * but WITHOUT ANY WARRANTY.
16 * Permission is granted to anyone to use this software for any purpose
17 * on any computer system, and to alter it and redistribute it, subject
18 * to the following restrictions:
20 * 1. The author and SysNet s.n.c. are not responsible for the consequences
21 * of use of this software, no matter how awful, even if they arise from
24 * 2. The origin of this software must not be misrepresented, either by
25 * explicit claim or by omission. Since few users ever read sources,
26 * credits should appear in the documentation.
28 * 3. Altered versions must be plainly marked as such, and must not be
29 * misrepresented as being the original software. Since few users
30 * ever read sources, credits should appear in the documentation.
31 * SysNet s.n.c. cannot be responsible for the consequences of the
34 * 4. This notice may not be removed or altered.
43 #include "back-monitor.h"
46 * compares entries based on the dn
54 struct monitorcache *cc1 = ( struct monitorcache * )c1;
55 struct monitorcache *cc2 = ( struct monitorcache * )c2;
57 int d = cc1->mc_ndn->bv_len - cc2->mc_ndn->bv_len;
60 * case sensitive, because the dn MUST be normalized
62 return d != 0 ? d : strcmp( cc1->mc_ndn->bv_val, cc2->mc_ndn->bv_val );
66 * checks for duplicate entries
74 struct monitorcache *cc1 = ( struct monitorcache * )c1;
75 struct monitorcache *cc2 = ( struct monitorcache * )c2;
77 int d = cc1->mc_ndn->bv_len - cc2->mc_ndn->bv_len;
81 * case sensitive, because the dn MUST be normalized
83 cmp = d != 0 ? d : strcmp( cc1->mc_ndn->bv_val, cc2->mc_ndn->bv_val );
84 return cmp == 0 ? -1 : 0;
88 * adds an entry to the cache and inits the mutex
92 struct monitorinfo *mi,
96 struct monitorcache *mc;
97 struct monitorentrypriv *mp;
100 assert( mi != NULL );
103 mp = ( struct monitorentrypriv *)e->e_private;
104 ldap_pvt_thread_mutex_init( &mp->mp_mutex );
106 mc = ( struct monitorcache * )ch_malloc( sizeof( struct monitorcache ) );
107 mc->mc_ndn = &e->e_nname;
109 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
110 rc = avl_insert( &mi->mi_cache, ( caddr_t )mc,
111 monitor_cache_cmp, monitor_cache_dup );
112 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
118 * locks the entry (no r/w)
125 struct monitorentrypriv *mp;
128 assert( e->e_private != NULL );
130 mp = ( struct monitorentrypriv * )e->e_private;
131 ldap_pvt_thread_mutex_lock( &mp->mp_mutex );
137 * gets an entry from the cache based on the normalized dn
142 struct monitorinfo *mi,
147 struct monitorcache tmp_mc, *mc;
149 assert( mi != NULL );
150 assert( ndn != NULL );
151 assert( ep != NULL );
154 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
155 mc = ( struct monitorcache * )avl_find( mi->mi_cache,
156 ( caddr_t )&tmp_mc, monitor_cache_cmp );
159 /* entry is returned with mutex locked */
160 monitor_cache_lock( mc->mc_e );
161 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
167 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
174 * If the entry exists in cache, it is returned in locked status;
175 * otherwise, if the parent exists, if it may generate volatile
176 * descendants an attempt to generate the required entry is
177 * performed and, if successful, the entry is returned
180 monitor_cache_dn2entry(
181 struct monitorinfo *mi,
189 struct berval p_ndn = { 0L, NULL };
191 struct monitorentrypriv *mp;
193 assert( mi != NULL );
194 assert( ndn != NULL );
195 assert( ep != NULL );
196 assert( matched != NULL );
200 rc = monitor_cache_get( mi, ndn, ep );
201 if ( !rc && *ep != NULL ) {
205 /* try with parent/ancestors */
206 if ( ndn && ndn->bv_len ) {
207 p_ndn.bv_val = dn_parent( NULL, ndn->bv_val );
209 if ( p_ndn.bv_val == NULL ) {
213 p_ndn.bv_len = ndn->bv_len
214 - ( ber_len_t ) ( p_ndn.bv_val - ndn->bv_val );
216 rc = monitor_cache_dn2entry( mi, &p_ndn, &e_parent, matched );
217 if ( rc || e_parent == NULL) {
221 mp = ( struct monitorentrypriv * )e_parent->e_private;
223 if ( mp->mp_flags & MONITOR_F_VOLATILE_CH ) {
224 /* parent entry generates volatile children */
225 rc = monitor_entry_create( mi, ndn, e_parent, ep );
229 monitor_cache_release( mi, e_parent );
238 * releases the lock of the entry; if it is marked as volatile, it is
242 monitor_cache_release(
243 struct monitorinfo *mi,
247 struct monitorentrypriv *mp;
249 assert( mi != NULL );
251 assert( e->e_private != NULL );
253 mp = ( struct monitorentrypriv * )e->e_private;
255 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
256 /* volatile entries do not return to cache */
257 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
264 ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );