1 /* dncache.c - dn caching for back-asyncmeta */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2016-2017 The OpenLDAP Foundation.
6 * Portions Copyright 2016 Symas Corporation.
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>.
19 * This work was developed by Symas Corporation
20 * based on back-meta module for inclusion in OpenLDAP Software.
21 * This work was sponsored by Ericsson. */
26 #include <ac/string.h>
29 #include "../back-ldap/back-ldap.h"
30 #include "back-asyncmeta.h"
33 * The dncache, at present, maps an entry to the target that holds it.
36 typedef struct metadncacheentry_t {
44 * asyncmeta_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?)
51 asyncmeta_dncache_cmp(
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 * asyncmeta_dncache_dup
67 * returns -1 in case a duplicate struct metadncacheentry has been inserted;
71 asyncmeta_dncache_dup(
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 * asyncmeta_dncache_get_target
87 * returns the target a dn belongs to, or -1 in case the dn is not
91 asyncmeta_dncache_get_target(
92 a_metadncache_t *cache,
95 metadncacheentry_t tmp_entry,
97 int target = META_TARGET_NONE;
99 assert( cache != NULL );
100 assert( ndn != NULL );
103 ldap_pvt_thread_mutex_lock( &cache->mutex );
104 entry = ( metadncacheentry_t * )avl_find( cache->tree,
105 ( caddr_t )&tmp_entry, asyncmeta_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;
118 if ( entry->lastupdated+cache->ttl > slap_get_time() ) {
119 target = entry->target;
123 ldap_pvt_thread_mutex_unlock( &cache->mutex );
129 * asyncmeta_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 asyncmeta_dncache_update_entry(
136 a_metadncache_t *cache,
140 metadncacheentry_t *entry,
142 time_t curr_time = 0L;
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();
159 ldap_pvt_thread_mutex_lock( &cache->mutex );
160 entry = ( metadncacheentry_t * )avl_find( cache->tree,
161 ( caddr_t )&tmp_entry, asyncmeta_dncache_cmp );
163 if ( entry != NULL ) {
164 entry->target = target;
165 entry->lastupdated = curr_time;
168 entry = ch_malloc( sizeof( metadncacheentry_t ) + ndn->bv_len + 1 );
169 if ( entry == NULL ) {
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 asyncmeta_dncache_cmp, asyncmeta_dncache_dup );
187 ldap_pvt_thread_mutex_unlock( &cache->mutex );
193 asyncmeta_dncache_delete_entry(
194 a_metadncache_t *cache,
197 metadncacheentry_t *entry,
200 assert( cache != NULL );
201 assert( ndn != NULL );
205 ldap_pvt_thread_mutex_lock( &cache->mutex );
206 entry = avl_delete( &cache->tree, ( caddr_t )&tmp_entry,
207 asyncmeta_dncache_cmp );
208 ldap_pvt_thread_mutex_unlock( &cache->mutex );
210 if ( entry != NULL ) {
211 asyncmeta_dncache_free( ( void * )entry );
224 asyncmeta_dncache_free(