#include "portable.h"
#include <stdio.h>
#include "slap.h"
#include "back-monitor.h"
int
monitor_cache_cmp(
const void *c1,
const void *c2
)
{
struct monitorcache *cc1 = ( struct monitorcache * )c1;
struct monitorcache *cc2 = ( struct monitorcache * )c2;
return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn );
}
int
monitor_cache_dup(
void *c1,
void *c2
)
{
struct monitorcache *cc1 = ( struct monitorcache * )c1;
struct monitorcache *cc2 = ( struct monitorcache * )c2;
return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn ) == 0 ? -1 : 0;
}
int
monitor_cache_add(
struct monitorinfo *mi,
Entry *e
)
{
struct monitorcache *mc;
struct monitorentrypriv *mp;
int rc;
assert( mi != NULL );
assert( e != NULL );
mp = ( struct monitorentrypriv *)e->e_private;
ldap_pvt_thread_mutex_init( &mp->mp_mutex );
mc = ( struct monitorcache * )ch_malloc( sizeof( struct monitorcache ) );
mc->mc_ndn = e->e_nname;
mc->mc_e = e;
ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
rc = avl_insert( &mi->mi_cache, ( caddr_t )mc,
monitor_cache_cmp, monitor_cache_dup );
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
return rc;
}
int
monitor_cache_lock(
Entry *e
)
{
struct monitorentrypriv *mp;
assert( e != NULL );
assert( e->e_private != NULL );
mp = ( struct monitorentrypriv * )e->e_private;
ldap_pvt_thread_mutex_lock( &mp->mp_mutex );
return( 0 );
}
int
monitor_cache_get(
struct monitorinfo *mi,
struct berval *ndn,
Entry **ep
)
{
struct monitorcache tmp_mc, *mc;
assert( mi != NULL );
assert( ndn != NULL );
assert( ep != NULL );
tmp_mc.mc_ndn = *ndn;
ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
mc = ( struct monitorcache * )avl_find( mi->mi_cache,
( caddr_t )&tmp_mc, monitor_cache_cmp );
if ( mc != NULL ) {
monitor_cache_lock( mc->mc_e );
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
*ep = mc->mc_e;
return( 0 );
}
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
*ep = NULL;
return( -1 );
}
int
monitor_cache_dn2entry(
struct monitorinfo *mi,
struct berval *ndn,
Entry **ep,
Entry **matched
)
{
int rc;
struct berval p_ndn = { 0L, NULL };
Entry *e_parent;
struct monitorentrypriv *mp;
assert( mi != NULL );
assert( ndn != NULL );
assert( ep != NULL );
assert( matched != NULL );
*matched = NULL;
rc = monitor_cache_get( mi, ndn, ep );
if ( !rc && *ep != NULL ) {
return( 0 );
}
if ( ndn->bv_len ) {
dnParent( ndn, &p_ndn );
}
if ( p_ndn.bv_val == NULL ) {
p_ndn.bv_val = "";
p_ndn.bv_len = 0;
} else {
p_ndn.bv_len = ndn->bv_len
- ( ber_len_t ) ( p_ndn.bv_val - ndn->bv_val );
}
rc = monitor_cache_dn2entry( mi, &p_ndn, &e_parent, matched );
if ( rc || e_parent == NULL) {
return( -1 );
}
mp = ( struct monitorentrypriv * )e_parent->e_private;
rc = -1;
if ( mp->mp_flags & MONITOR_F_VOLATILE_CH ) {
rc = monitor_entry_create( mi, ndn, e_parent, ep );
}
if ( !rc ) {
monitor_cache_release( mi, e_parent );
} else {
*matched = e_parent;
}
return( rc );
}
int
monitor_cache_release(
struct monitorinfo *mi,
Entry *e
)
{
struct monitorentrypriv *mp;
assert( mi != NULL );
assert( e != NULL );
assert( e->e_private != NULL );
mp = ( struct monitorentrypriv * )e->e_private;
if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
struct monitorcache *mc, tmp_mc;
ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
tmp_mc.mc_ndn = e->e_nname;
mc = avl_delete( &mi->mi_cache,
( caddr_t )&tmp_mc, monitor_cache_cmp );
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
ch_free( mc );
ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
ch_free( mp );
e->e_private = NULL;
entry_free( e );
return( 0 );
}
ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
return( 0 );
}