1 /* ldbmcache.c - maintain a cache of open ldbm files */
14 #ifdef HAVE_SYS_PARAM_H
15 #include <sys/param.h>
18 #include "ldapconfig.h"
20 #include "back-ldbm.h"
30 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
32 time_t oldtime, curtime;
37 sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
39 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
43 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
44 curtime = currenttime;
45 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
48 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
49 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
51 /* already open - return it */
52 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
53 li->li_dbcache[i].dbc_refcnt++;
54 Debug( LDAP_DEBUG_TRACE,
55 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
56 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
57 return( &li->li_dbcache[i] );
60 /* keep track of lru db */
61 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
62 li->li_dbcache[i].dbc_refcnt == 0 ) {
64 oldtime = li->li_dbcache[i].dbc_lastref;
68 /* no empty slots, not already open - close lru and use that slot */
69 if ( i == MAXDBCACHE ) {
71 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
72 Debug( LDAP_DEBUG_ANY,
73 "ldbm_cache_open no unused db to close - waiting\n",
77 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
78 &li->li_dbcache_mutex );
79 for ( i = 0; i < MAXDBCACHE; i++ ) {
80 if ( li->li_dbcache[i].dbc_refcnt
89 ldbm_close( li->li_dbcache[i].dbc_db );
90 free( li->li_dbcache[i].dbc_name );
91 li->li_dbcache[i].dbc_name = NULL;
94 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
95 li->li_dbcachesize )) == NULL ) {
97 Debug( LDAP_DEBUG_TRACE,
98 "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
99 buf, err, err > -1 && err < sys_nerr ?
100 sys_errlist[err] : "unknown" );
101 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
104 li->li_dbcache[i].dbc_name = ch_strdup( buf );
105 li->li_dbcache[i].dbc_refcnt = 1;
106 li->li_dbcache[i].dbc_lastref = curtime;
107 if ( stat( buf, &st ) == 0 ) {
108 li->li_dbcache[i].dbc_blksize = st.st_blksize;
110 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
112 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
113 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
114 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
115 li->li_dbcache[i].dbc_maxids) + 1;
117 Debug( LDAP_DEBUG_ARGS,
118 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
119 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
120 li->li_dbcache[i].dbc_maxindirect );
121 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
122 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
123 return( &li->li_dbcache[i] );
127 ldbm_cache_close( Backend *be, struct dbcache *db )
129 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
131 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
132 if ( --db->dbc_refcnt == 0 ) {
133 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
135 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
139 ldbm_cache_really_close( Backend *be, struct dbcache *db )
141 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
143 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
144 if ( --db->dbc_refcnt == 0 ) {
145 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
146 ldbm_close( db->dbc_db );
147 free( db->dbc_name );
150 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
154 ldbm_cache_flush_all( Backend *be )
156 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
159 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
160 for ( i = 0; i < MAXDBCACHE; i++ ) {
161 if ( li->li_dbcache[i].dbc_name != NULL ) {
162 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
163 li->li_dbcache[i].dbc_name, 0, 0 );
164 ldbm_sync( li->li_dbcache[i].dbc_db );
167 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
178 ldbm_datum_init( data );
180 data = ldbm_fetch( db->dbc_db, key );
196 Statslog( LDAP_DEBUG_STATS,
197 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
198 key.dptr, key.dsize, 0, 0, 0 );
200 Statslog( LDAP_DEBUG_STATS,
201 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
202 key.dptr, data.dptr, 0, 0, 0 );
204 Statslog( LDAP_DEBUG_STATS,
205 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
206 data.dptr, data.dsize, 0, 0, 0 );
208 Statslog( LDAP_DEBUG_STATS,
209 "=> ldbm_cache_store(): flags=0x%08x\n",
211 #endif /* LDBM_DEBUG */
213 rc = ldbm_store( db->dbc_db, key, data, flags );
226 rc = ldbm_delete( db->dbc_db, key );