1 /* ldbmcache.c - maintain a cache of open ldbm files */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
19 #ifdef HAVE_SYS_PARAM_H
20 #include <sys/param.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 if( li->li_dblocking ) {
46 flags |= LDBM_LOCKING;
48 flags |= LDBM_NOLOCKING;
51 if( li->li_dbwritesync ) {
57 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
61 curtime = slap_get_time();
64 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
65 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
67 /* already open - return it */
68 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
69 li->li_dbcache[i].dbc_refcnt++;
70 Debug( LDAP_DEBUG_TRACE,
71 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
72 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
73 return( &li->li_dbcache[i] );
76 /* keep track of lru db */
77 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
78 li->li_dbcache[i].dbc_refcnt == 0 ) {
80 oldtime = li->li_dbcache[i].dbc_lastref;
84 /* no empty slots, not already open - close lru and use that slot */
85 if ( i == MAXDBCACHE ) {
87 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
88 Debug( LDAP_DEBUG_ANY,
89 "ldbm_cache_open no unused db to close - waiting\n",
93 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
94 &li->li_dbcache_mutex );
95 for ( i = 0; i < MAXDBCACHE; i++ ) {
96 if ( li->li_dbcache[i].dbc_refcnt
105 ldbm_close( li->li_dbcache[i].dbc_db );
106 free( li->li_dbcache[i].dbc_name );
107 li->li_dbcache[i].dbc_name = NULL;
110 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
111 li->li_dbcachesize )) == NULL ) {
113 Debug( LDAP_DEBUG_TRACE,
114 "<= ldbm_cache_open NULL \"%s\" errno=%d reason=\"%s\")\n",
115 buf, err, err > -1 && err < sys_nerr ?
116 sys_errlist[err] : "unknown" );
117 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
120 li->li_dbcache[i].dbc_name = ch_strdup( buf );
121 li->li_dbcache[i].dbc_refcnt = 1;
122 li->li_dbcache[i].dbc_lastref = curtime;
123 #ifdef HAVE_ST_BLKSIZE
124 if ( stat( buf, &st ) == 0 ) {
125 li->li_dbcache[i].dbc_blksize = st.st_blksize;
129 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
131 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
132 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
133 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
134 li->li_dbcache[i].dbc_maxids) + 1;
136 Debug( LDAP_DEBUG_ARGS,
137 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
138 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
139 li->li_dbcache[i].dbc_maxindirect );
140 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
141 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
142 return( &li->li_dbcache[i] );
146 ldbm_cache_close( Backend *be, DBCache *db )
148 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
150 if( li->li_dbwritesync ) {
151 ldbm_sync( db->dbc_db );
154 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
155 if ( --db->dbc_refcnt == 0 ) {
156 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
158 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
162 ldbm_cache_really_close( Backend *be, DBCache *db )
164 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
166 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
167 if ( --db->dbc_refcnt == 0 ) {
168 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
169 ldbm_close( db->dbc_db );
170 free( db->dbc_name );
173 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
177 ldbm_cache_flush_all( Backend *be )
179 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
182 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
183 for ( i = 0; i < MAXDBCACHE; i++ ) {
184 if ( li->li_dbcache[i].dbc_name != NULL ) {
185 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
186 li->li_dbcache[i].dbc_name, 0, 0 );
187 ldbm_sync( li->li_dbcache[i].dbc_db );
188 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
189 Debug( LDAP_DEBUG_TRACE,
190 "refcnt = %d, couldn't close db (%s)\n",
191 li->li_dbcache[i].dbc_refcnt,
192 li->li_dbcache[i].dbc_name, 0 );
194 Debug( LDAP_DEBUG_TRACE,
195 "ldbm closing db (%s)\n",
196 li->li_dbcache[i].dbc_name, 0, 0 );
197 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
198 ldbm_close( li->li_dbcache[i].dbc_db );
199 free( li->li_dbcache[i].dbc_name );
200 li->li_dbcache[i].dbc_name = NULL;
204 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
215 return ldbm_fetch( db->dbc_db, key );
231 Statslog( LDAP_DEBUG_STATS,
232 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
233 key.dptr, key.dsize, 0, 0, 0 );
235 Statslog( LDAP_DEBUG_STATS,
236 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
237 key.dptr, data.dptr, 0, 0, 0 );
239 Statslog( LDAP_DEBUG_STATS,
240 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
241 data.dptr, data.dsize, 0, 0, 0 );
243 Statslog( LDAP_DEBUG_STATS,
244 "=> ldbm_cache_store(): flags=0x%08x\n",
246 #endif /* LDBM_DEBUG */
248 rc = ldbm_store( db->dbc_db, key, data, flags );
261 rc = ldbm_delete( db->dbc_db, key );