2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
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 the 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 the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include "../back-ldap/back-ldap.h"
29 #include "back-meta.h"
32 * The dncache, at present, maps an entry to the target that holds it.
35 struct metadncacheentry {
45 * compares two struct metadncacheentry; used by avl stuff
46 * FIXME: modify avl stuff to delete an entry based on cmp
47 * (e.g. when ttl expired?)
55 struct metadncacheentry *cc1 = ( struct metadncacheentry * )c1;
56 struct metadncacheentry *cc2 = ( struct metadncacheentry * )c2;
59 * case sensitive, because the dn MUST be normalized
61 return ber_bvcmp( &cc1->dn, &cc2->dn);
67 * returns -1 in case a duplicate struct metadncacheentry has been inserted;
76 struct metadncacheentry *cc1 = ( struct metadncacheentry * )c1;
77 struct metadncacheentry *cc2 = ( struct metadncacheentry * )c2;
80 * case sensitive, because the dn MUST be normalized
82 return ( ber_bvcmp( &cc1->dn, &cc2->dn ) == 0 ) ? -1 : 0;
86 * meta_dncache_get_target
88 * returns the target a dn belongs to, or -1 in case the dn is not
92 meta_dncache_get_target(
93 struct metadncache *cache,
97 struct metadncacheentry tmp_entry, *entry;
105 ldap_pvt_thread_mutex_lock( &cache->mutex );
106 entry = ( struct metadncacheentry * )avl_find( cache->tree,
107 ( caddr_t )&tmp_entry, meta_dncache_cmp );
109 if ( entry != NULL ) {
112 * if cache->ttl < 0, cache never expires;
113 * if cache->ttl = 0 no cache is used; shouldn't get here
114 * else, cache is used with ttl
116 if ( cache->ttl < 0 ) {
117 target = entry->target;
123 curr_time = time( NULL );
125 if ( entry->lastupdated+cache->ttl > curr_time ) {
126 target = entry->target;
130 ldap_pvt_thread_mutex_unlock( &cache->mutex );
136 * meta_dncache_update_entry
138 * updates target and lastupdated of a struct metadncacheentry if exists,
139 * otherwise it gets created; returns -1 in case of error
142 meta_dncache_update_entry(
143 struct metadncache *cache,
148 struct metadncacheentry *entry, tmp_entry;
149 time_t curr_time = 0L;
156 * if cache->ttl < 0, cache never expires;
157 * if cache->ttl = 0 no cache is used; shouldn't get here
158 * else, cache is used with ttl
160 if ( cache->ttl > 0 ) {
165 curr_time = time( NULL );
170 ldap_pvt_thread_mutex_lock( &cache->mutex );
171 entry = ( struct metadncacheentry * )avl_find( cache->tree,
172 ( caddr_t )&tmp_entry, meta_dncache_cmp );
174 if ( entry != NULL ) {
175 entry->target = target;
176 entry->lastupdated = curr_time;
178 entry = ch_calloc( sizeof( struct metadncacheentry ), 1 );
179 if ( entry == NULL ) {
180 ldap_pvt_thread_mutex_unlock( &cache->mutex );
184 ber_dupbv( &entry->dn, ndn );
185 if ( entry->dn.bv_val == NULL ) {
186 ldap_pvt_thread_mutex_unlock( &cache->mutex );
189 entry->target = target;
190 entry->lastupdated = curr_time;
192 err = avl_insert( &cache->tree, ( caddr_t )entry,
193 meta_dncache_cmp, meta_dncache_dup );
195 ldap_pvt_thread_mutex_unlock( &cache->mutex );
201 * meta_dncache_update_entry
203 * updates target and lastupdated of a struct metadncacheentry if exists,
204 * otherwise it gets created; returns -1 in case of error
207 meta_dncache_delete_entry(
208 struct metadncache *cache,
212 struct metadncacheentry *entry, tmp_entry;
219 ldap_pvt_thread_mutex_lock( &cache->mutex );
220 entry = avl_delete( &cache->tree, ( caddr_t )&tmp_entry,
222 ldap_pvt_thread_mutex_unlock( &cache->mutex );
224 if ( entry != NULL ) {
225 meta_dncache_free( ( void * )entry );
242 struct metadncacheentry *entry = ( struct metadncacheentry * )e;
244 free( entry->dn.bv_val );