Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-meta / dncache.c
blob8b4d24b1f799c8f78358f9ce853a21a100aa25d9
1 /* $OpenLDAP: pkg/ldap/servers/slapd/back-meta/dncache.c,v 1.16.2.3 2008/02/11 23:26:47 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
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>.
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 * Masarati.
23 #include "portable.h"
25 #include <stdio.h>
26 #include <ac/string.h>
28 #include "slap.h"
29 #include "../back-ldap/back-ldap.h"
30 #include "back-meta.h"
33 * The dncache, at present, maps an entry to the target that holds it.
36 typedef struct metadncacheentry_t {
37 struct berval dn;
38 int target;
40 time_t lastupdated;
41 } metadncacheentry_t;
44 * meta_dncache_cmp
46 * compares two struct metadncacheentry; used by avl stuff
47 * FIXME: modify avl stuff to delete an entry based on cmp
48 * (e.g. when ttl expired?)
50 int
51 meta_dncache_cmp(
52 const void *c1,
53 const void *c2 )
55 metadncacheentry_t *cc1 = ( metadncacheentry_t * )c1;
56 metadncacheentry_t *cc2 = ( metadncacheentry_t * )c2;
59 * case sensitive, because the dn MUST be normalized
61 return ber_bvcmp( &cc1->dn, &cc2->dn);
65 * meta_dncache_dup
67 * returns -1 in case a duplicate struct metadncacheentry has been inserted;
68 * used by avl stuff
70 int
71 meta_dncache_dup(
72 void *c1,
73 void *c2 )
75 metadncacheentry_t *cc1 = ( metadncacheentry_t * )c1;
76 metadncacheentry_t *cc2 = ( metadncacheentry_t * )c2;
79 * case sensitive, because the dn MUST be normalized
81 return ( ber_bvcmp( &cc1->dn, &cc2->dn ) == 0 ) ? -1 : 0;
85 * meta_dncache_get_target
87 * returns the target a dn belongs to, or -1 in case the dn is not
88 * in the cache
90 int
91 meta_dncache_get_target(
92 metadncache_t *cache,
93 struct berval *ndn )
95 metadncacheentry_t tmp_entry,
96 *entry;
97 int target = META_TARGET_NONE;
99 assert( cache != NULL );
100 assert( ndn != NULL );
102 tmp_entry.dn = *ndn;
103 ldap_pvt_thread_mutex_lock( &cache->mutex );
104 entry = ( metadncacheentry_t * )avl_find( cache->tree,
105 ( caddr_t )&tmp_entry, meta_dncache_cmp );
107 if ( entry != NULL ) {
110 * if cache->ttl < 0, cache never expires;
111 * if cache->ttl = 0 no cache is used; shouldn't get here
112 * else, cache is used with ttl
114 if ( cache->ttl < 0 ) {
115 target = entry->target;
117 } else {
118 if ( entry->lastupdated+cache->ttl > slap_get_time() ) {
119 target = entry->target;
123 ldap_pvt_thread_mutex_unlock( &cache->mutex );
125 return target;
129 * meta_dncache_update_entry
131 * updates target and lastupdated of a struct metadncacheentry if exists,
132 * otherwise it gets created; returns -1 in case of error
135 meta_dncache_update_entry(
136 metadncache_t *cache,
137 struct berval *ndn,
138 int target )
140 metadncacheentry_t *entry,
141 tmp_entry;
142 time_t curr_time = 0L;
143 int err = 0;
145 assert( cache != NULL );
146 assert( ndn != NULL );
149 * if cache->ttl < 0, cache never expires;
150 * if cache->ttl = 0 no cache is used; shouldn't get here
151 * else, cache is used with ttl
153 if ( cache->ttl > 0 ) {
154 curr_time = slap_get_time();
157 tmp_entry.dn = *ndn;
159 ldap_pvt_thread_mutex_lock( &cache->mutex );
160 entry = ( metadncacheentry_t * )avl_find( cache->tree,
161 ( caddr_t )&tmp_entry, meta_dncache_cmp );
163 if ( entry != NULL ) {
164 entry->target = target;
165 entry->lastupdated = curr_time;
167 } else {
168 entry = ch_malloc( sizeof( metadncacheentry_t ) + ndn->bv_len + 1 );
169 if ( entry == NULL ) {
170 err = -1;
171 goto error_return;
174 entry->dn.bv_len = ndn->bv_len;
175 entry->dn.bv_val = (char *)&entry[ 1 ];
176 AC_MEMCPY( entry->dn.bv_val, ndn->bv_val, ndn->bv_len );
177 entry->dn.bv_val[ ndn->bv_len ] = '\0';
179 entry->target = target;
180 entry->lastupdated = curr_time;
182 err = avl_insert( &cache->tree, ( caddr_t )entry,
183 meta_dncache_cmp, meta_dncache_dup );
186 error_return:;
187 ldap_pvt_thread_mutex_unlock( &cache->mutex );
189 return err;
193 * meta_dncache_update_entry
195 * updates target and lastupdated of a struct metadncacheentry if exists,
196 * otherwise it gets created; returns -1 in case of error
199 meta_dncache_delete_entry(
200 metadncache_t *cache,
201 struct berval *ndn )
203 metadncacheentry_t *entry,
204 tmp_entry;
206 assert( cache != NULL );
207 assert( ndn != NULL );
209 tmp_entry.dn = *ndn;
211 ldap_pvt_thread_mutex_lock( &cache->mutex );
212 entry = avl_delete( &cache->tree, ( caddr_t )&tmp_entry,
213 meta_dncache_cmp );
214 ldap_pvt_thread_mutex_unlock( &cache->mutex );
216 if ( entry != NULL ) {
217 meta_dncache_free( ( void * )entry );
220 return 0;
224 * meta_dncache_free
226 * frees an entry
229 void
230 meta_dncache_free(
231 void *e )
233 free( e );