1 /* ldbmcache.c - maintain a cache of open ldbm files */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
18 #ifdef HAVE_SYS_PARAM_H
19 #include <sys/param.h>
22 #include "ldap_defaults.h"
24 #include "back-ldbm.h"
34 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
36 time_t oldtime, curtime;
38 #ifdef HAVE_ST_BLKSIZE
42 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
43 li->li_directory, name, suffix );
45 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
49 curtime = slap_get_time();
52 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
53 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
55 /* already open - return it */
56 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
57 li->li_dbcache[i].dbc_refcnt++;
58 Debug( LDAP_DEBUG_TRACE,
59 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
60 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
61 return( &li->li_dbcache[i] );
64 /* keep track of lru db */
65 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
66 li->li_dbcache[i].dbc_refcnt == 0 ) {
68 oldtime = li->li_dbcache[i].dbc_lastref;
72 /* no empty slots, not already open - close lru and use that slot */
73 if ( i == MAXDBCACHE ) {
75 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
76 Debug( LDAP_DEBUG_ANY,
77 "ldbm_cache_open no unused db to close - waiting\n",
81 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
82 &li->li_dbcache_mutex );
83 for ( i = 0; i < MAXDBCACHE; i++ ) {
84 if ( li->li_dbcache[i].dbc_refcnt
93 ldbm_close( li->li_dbcache[i].dbc_db );
94 free( li->li_dbcache[i].dbc_name );
95 li->li_dbcache[i].dbc_name = NULL;
98 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
99 li->li_dbcachesize )) == NULL ) {
101 Debug( LDAP_DEBUG_TRACE,
102 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
103 buf, err, err > -1 && err < sys_nerr ?
104 sys_errlist[err] : "unknown" );
105 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
108 li->li_dbcache[i].dbc_name = ch_strdup( buf );
109 li->li_dbcache[i].dbc_refcnt = 1;
110 li->li_dbcache[i].dbc_lastref = curtime;
111 #ifdef HAVE_ST_BLKSIZE
112 if ( stat( buf, &st ) == 0 ) {
113 li->li_dbcache[i].dbc_blksize = st.st_blksize;
117 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
119 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
120 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
121 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
122 li->li_dbcache[i].dbc_maxids) + 1;
124 Debug( LDAP_DEBUG_ARGS,
125 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
126 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
127 li->li_dbcache[i].dbc_maxindirect );
128 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
129 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
130 return( &li->li_dbcache[i] );
134 ldbm_cache_close( Backend *be, DBCache *db )
136 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
138 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
139 if ( --db->dbc_refcnt == 0 ) {
140 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
142 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
146 ldbm_cache_really_close( Backend *be, DBCache *db )
148 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
150 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
151 if ( --db->dbc_refcnt == 0 ) {
152 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
153 ldbm_close( db->dbc_db );
154 free( db->dbc_name );
157 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
161 ldbm_cache_flush_all( Backend *be )
163 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
166 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
167 for ( i = 0; i < MAXDBCACHE; i++ ) {
168 if ( li->li_dbcache[i].dbc_name != NULL ) {
169 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
170 li->li_dbcache[i].dbc_name, 0, 0 );
171 ldbm_sync( li->li_dbcache[i].dbc_db );
173 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
174 Debug( LDAP_DEBUG_TRACE,
175 "refcnt = %d, couldn't close db (%s)\n",
176 li->li_dbcache[i].dbc_refcnt,
177 li->li_dbcache[i].dbc_name, 0 );
179 Debug( LDAP_DEBUG_TRACE,
180 "ldbm closing db (%s)\n",
181 li->li_dbcache[i].dbc_name, 0, 0 );
182 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
183 ldbm_close( li->li_dbcache[i].dbc_db );
184 free( li->li_dbcache[i].dbc_name );
185 li->li_dbcache[i].dbc_name = NULL;
187 #endif /* SLAP_CLEANUP */
190 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
201 return ldbm_fetch( db->dbc_db, key );
217 Statslog( LDAP_DEBUG_STATS,
218 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
219 key.dptr, key.dsize, 0, 0, 0 );
221 Statslog( LDAP_DEBUG_STATS,
222 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
223 key.dptr, data.dptr, 0, 0, 0 );
225 Statslog( LDAP_DEBUG_STATS,
226 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
227 data.dptr, data.dsize, 0, 0, 0 );
229 Statslog( LDAP_DEBUG_STATS,
230 "=> ldbm_cache_store(): flags=0x%08x\n",
232 #endif /* LDBM_DEBUG */
234 rc = ldbm_store( db->dbc_db, key, data, flags );
247 rc = ldbm_delete( db->dbc_db, key );