1 /* ldbmcache.c - maintain a cache of open ldbm files */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
19 #include "back-ldbm.h"
29 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
31 time_t oldtime, curtime;
33 #ifdef HAVE_ST_BLKSIZE
37 sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
38 li->li_directory, name, suffix );
40 if( li->li_dblocking ) {
41 flags |= LDBM_LOCKING;
43 flags |= LDBM_NOLOCKING;
46 if( li->li_dbwritesync ) {
52 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
56 curtime = slap_get_time();
59 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
60 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
62 /* already open - return it */
63 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
64 li->li_dbcache[i].dbc_refcnt++;
65 Debug( LDAP_DEBUG_TRACE,
66 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
67 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
68 return( &li->li_dbcache[i] );
71 /* keep track of lru db */
72 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
73 li->li_dbcache[i].dbc_refcnt == 0 ) {
75 oldtime = li->li_dbcache[i].dbc_lastref;
79 /* no empty slots, not already open - close lru and use that slot */
80 if ( i == MAXDBCACHE ) {
82 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
83 Debug( LDAP_DEBUG_ANY,
84 "ldbm_cache_open no unused db to close - waiting\n",
88 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
89 &li->li_dbcache_mutex );
90 for ( i = 0; i < MAXDBCACHE; i++ ) {
91 if ( li->li_dbcache[i].dbc_refcnt
100 ldbm_close( li->li_dbcache[i].dbc_db );
101 free( li->li_dbcache[i].dbc_name );
102 li->li_dbcache[i].dbc_name = NULL;
105 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
106 li->li_dbcachesize )) == NULL ) {
108 Debug( LDAP_DEBUG_TRACE,
109 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
110 buf, err, err > -1 && err < sys_nerr ?
111 sys_errlist[err] : "unknown" );
112 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
115 li->li_dbcache[i].dbc_name = ch_strdup( buf );
116 li->li_dbcache[i].dbc_refcnt = 1;
117 li->li_dbcache[i].dbc_lastref = curtime;
118 #ifdef HAVE_ST_BLKSIZE
119 if ( stat( buf, &st ) == 0 ) {
120 li->li_dbcache[i].dbc_blksize = st.st_blksize;
124 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
126 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
127 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
128 li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
129 li->li_dbcache[i].dbc_maxids ) + 1;
131 assert( li->li_dbcache[i].dbc_maxindirect < 256 );
133 Debug( LDAP_DEBUG_ARGS,
134 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
135 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
136 li->li_dbcache[i].dbc_maxindirect );
137 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
138 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
139 return( &li->li_dbcache[i] );
143 ldbm_cache_close( Backend *be, DBCache *db )
145 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
147 if( li->li_dbwritesync ) {
148 ldbm_sync( db->dbc_db );
151 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
152 if ( --db->dbc_refcnt == 0 ) {
153 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
155 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
159 ldbm_cache_really_close( Backend *be, DBCache *db )
161 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
163 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
164 if ( --db->dbc_refcnt == 0 ) {
165 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
166 ldbm_close( db->dbc_db );
167 free( db->dbc_name );
170 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
174 ldbm_cache_flush_all( Backend *be )
176 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
179 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
180 for ( i = 0; i < MAXDBCACHE; i++ ) {
181 if ( li->li_dbcache[i].dbc_name != NULL ) {
182 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
183 li->li_dbcache[i].dbc_name, 0, 0 );
184 ldbm_sync( li->li_dbcache[i].dbc_db );
185 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
186 Debug( LDAP_DEBUG_TRACE,
187 "refcnt = %d, couldn't close db (%s)\n",
188 li->li_dbcache[i].dbc_refcnt,
189 li->li_dbcache[i].dbc_name, 0 );
191 Debug( LDAP_DEBUG_TRACE,
192 "ldbm closing db (%s)\n",
193 li->li_dbcache[i].dbc_name, 0, 0 );
194 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
195 ldbm_close( li->li_dbcache[i].dbc_db );
196 free( li->li_dbcache[i].dbc_name );
197 li->li_dbcache[i].dbc_name = NULL;
201 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
212 return ldbm_fetch( db->dbc_db, key );
228 Statslog( LDAP_DEBUG_STATS,
229 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
230 key.dptr, key.dsize, 0, 0, 0 );
232 Statslog( LDAP_DEBUG_STATS,
233 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
234 key.dptr, data.dptr, 0, 0, 0 );
236 Statslog( LDAP_DEBUG_STATS,
237 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
238 data.dptr, data.dsize, 0, 0, 0 );
240 Statslog( LDAP_DEBUG_STATS,
241 "=> ldbm_cache_store(): flags=0x%08x\n",
243 #endif /* LDBM_DEBUG */
245 rc = ldbm_store( db->dbc_db, key, data, flags );
258 rc = ldbm_delete( db->dbc_db, key );