1998-08-09 08:43:13 +08:00
|
|
|
/* ldbmcache.c - maintain a cache of open ldbm files */
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <sys/stat.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
#include "ldapconfig.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "slap.h"
|
|
|
|
#include "back-ldbm.h"
|
|
|
|
|
|
|
|
struct dbcache *
|
|
|
|
ldbm_cache_open(
|
|
|
|
Backend *be,
|
|
|
|
char *name,
|
|
|
|
char *suffix,
|
|
|
|
int flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
int i, lru;
|
|
|
|
time_t oldtime, curtime;
|
|
|
|
char buf[MAXPATHLEN];
|
1999-03-02 06:37:05 +08:00
|
|
|
#ifdef HAVE_ST_BLKSIZE
|
1998-08-09 08:43:13 +08:00
|
|
|
struct stat st;
|
1999-03-02 06:37:05 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-02-08 19:42:14 +08:00
|
|
|
sprintf( buf, "%s%s%s%s", li->li_directory, DEFAULT_DIRSEP, name, suffix );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
|
|
|
|
flags, li->li_mode );
|
|
|
|
|
|
|
|
lru = 0;
|
1999-03-22 15:14:54 +08:00
|
|
|
curtime = slap_get_time();
|
1998-08-09 08:43:13 +08:00
|
|
|
oldtime = curtime;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
|
|
|
|
i++ ) {
|
|
|
|
/* already open - return it */
|
|
|
|
if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
|
|
|
|
li->li_dbcache[i].dbc_refcnt++;
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( &li->li_dbcache[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* keep track of lru db */
|
|
|
|
if ( li->li_dbcache[i].dbc_lastref < oldtime &&
|
|
|
|
li->li_dbcache[i].dbc_refcnt == 0 ) {
|
|
|
|
lru = i;
|
|
|
|
oldtime = li->li_dbcache[i].dbc_lastref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no empty slots, not already open - close lru and use that slot */
|
|
|
|
if ( i == MAXDBCACHE ) {
|
|
|
|
i = lru;
|
|
|
|
if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"ldbm_cache_open no unused db to close - waiting\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
lru = -1;
|
|
|
|
while ( lru == -1 ) {
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
|
1998-08-09 08:43:13 +08:00
|
|
|
&li->li_dbcache_mutex );
|
|
|
|
for ( i = 0; i < MAXDBCACHE; i++ ) {
|
|
|
|
if ( li->li_dbcache[i].dbc_refcnt
|
|
|
|
== 0 ) {
|
|
|
|
lru = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = lru;
|
|
|
|
}
|
|
|
|
ldbm_close( li->li_dbcache[i].dbc_db );
|
|
|
|
free( li->li_dbcache[i].dbc_name );
|
|
|
|
li->li_dbcache[i].dbc_name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
|
|
|
|
li->li_dbcachesize )) == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
|
|
|
|
buf, errno, errno > -1 && errno < sys_nerr ?
|
|
|
|
sys_errlist[errno] : "unknown" );
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
1998-11-28 04:21:54 +08:00
|
|
|
li->li_dbcache[i].dbc_name = ch_strdup( buf );
|
1998-08-09 08:43:13 +08:00
|
|
|
li->li_dbcache[i].dbc_refcnt = 1;
|
|
|
|
li->li_dbcache[i].dbc_lastref = curtime;
|
1999-03-02 06:37:05 +08:00
|
|
|
#ifdef HAVE_ST_BLKSIZE
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( stat( buf, &st ) == 0 ) {
|
|
|
|
li->li_dbcache[i].dbc_blksize = st.st_blksize;
|
1999-03-02 06:37:05 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
|
|
|
|
}
|
|
|
|
li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
|
1999-01-31 15:55:53 +08:00
|
|
|
sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
|
1998-08-09 08:43:13 +08:00
|
|
|
li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
|
|
|
|
li->li_dbcache[i].dbc_maxids) + 1;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
"ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
|
1998-08-09 08:43:13 +08:00
|
|
|
li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
|
|
|
|
li->li_dbcache[i].dbc_maxindirect );
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( &li->li_dbcache[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_cache_close( Backend *be, struct dbcache *db )
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( --db->dbc_refcnt == 0 ) {
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_cache_really_close( Backend *be, struct dbcache *db )
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( --db->dbc_refcnt == 0 ) {
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
|
1998-08-09 08:43:13 +08:00
|
|
|
ldbm_close( db->dbc_db );
|
|
|
|
free( db->dbc_name );
|
|
|
|
db->dbc_name = NULL;
|
|
|
|
}
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ldbm_cache_flush_all( Backend *be )
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
int i;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( i = 0; i < MAXDBCACHE; i++ ) {
|
|
|
|
if ( li->li_dbcache[i].dbc_name != NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
|
|
|
|
li->li_dbcache[i].dbc_name, 0, 0 );
|
|
|
|
ldbm_sync( li->li_dbcache[i].dbc_db );
|
|
|
|
}
|
|
|
|
}
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ldbm_cache_fetch(
|
|
|
|
struct dbcache *db,
|
|
|
|
Datum key
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Datum data;
|
1998-12-30 01:28:45 +08:00
|
|
|
|
|
|
|
ldbm_datum_init( data );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
data = ldbm_fetch( db->dbc_db, key );
|
|
|
|
|
|
|
|
return( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_cache_store(
|
|
|
|
struct dbcache *db,
|
|
|
|
Datum key,
|
|
|
|
Datum data,
|
|
|
|
int flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
1998-09-03 08:50:13 +08:00
|
|
|
#ifdef LDBM_DEBUG
|
|
|
|
Statslog( LDAP_DEBUG_STATS,
|
|
|
|
"=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
|
|
|
|
key.dptr, key.dsize, 0, 0, 0 );
|
|
|
|
|
|
|
|
Statslog( LDAP_DEBUG_STATS,
|
|
|
|
"=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
|
|
|
|
key.dptr, data.dptr, 0, 0, 0 );
|
|
|
|
|
|
|
|
Statslog( LDAP_DEBUG_STATS,
|
|
|
|
"=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
|
|
|
|
data.dptr, data.dsize, 0, 0, 0 );
|
|
|
|
|
|
|
|
Statslog( LDAP_DEBUG_STATS,
|
|
|
|
"=> ldbm_cache_store(): flags=0x%08x\n",
|
|
|
|
flags, 0, 0, 0, 0 );
|
|
|
|
#endif /* LDBM_DEBUG */
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = ldbm_store( db->dbc_db, key, data, flags );
|
|
|
|
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldbm_cache_delete(
|
|
|
|
struct dbcache *db,
|
|
|
|
Datum key
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ldbm_delete( db->dbc_db, key );
|
|
|
|
|
|
|
|
return( rc );
|
|
|
|
}
|