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%s", li->li_directory, DEFAULT_DIRSEP, 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 #ifdef HAVE_BERKELEY_DB2
95 if ( (li->li_dbcache[i].dbc_db = ldbm_open_env( buf, flags, li->li_mode,
96 li->li_dbcachesize, &li->li_db_env )) == 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, errno, errno > -1 && errno < sys_nerr ?
104 sys_errlist[errno] : "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 if ( stat( buf, &st ) == 0 ) {
112 li->li_dbcache[i].dbc_blksize = st.st_blksize;
114 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
116 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
117 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
118 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
119 li->li_dbcache[i].dbc_maxids) + 1;
121 Debug( LDAP_DEBUG_ARGS,
122 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
123 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
124 li->li_dbcache[i].dbc_maxindirect );
125 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
126 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
127 return( &li->li_dbcache[i] );
131 ldbm_cache_close( Backend *be, struct dbcache *db )
133 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
135 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
136 if ( --db->dbc_refcnt == 0 ) {
137 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
139 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
143 ldbm_cache_really_close( Backend *be, struct dbcache *db )
145 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
147 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
148 if ( --db->dbc_refcnt == 0 ) {
149 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
150 ldbm_close( db->dbc_db );
151 free( db->dbc_name );
154 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
158 ldbm_cache_flush_all( Backend *be )
160 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
163 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
164 for ( i = 0; i < MAXDBCACHE; i++ ) {
165 if ( li->li_dbcache[i].dbc_name != NULL ) {
166 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
167 li->li_dbcache[i].dbc_name, 0, 0 );
168 ldbm_sync( li->li_dbcache[i].dbc_db );
171 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
182 ldbm_datum_init( data );
184 data = ldbm_fetch( db->dbc_db, key );
200 Statslog( LDAP_DEBUG_STATS,
201 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
202 key.dptr, key.dsize, 0, 0, 0 );
204 Statslog( LDAP_DEBUG_STATS,
205 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
206 key.dptr, data.dptr, 0, 0, 0 );
208 Statslog( LDAP_DEBUG_STATS,
209 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
210 data.dptr, data.dsize, 0, 0, 0 );
212 Statslog( LDAP_DEBUG_STATS,
213 "=> ldbm_cache_store(): flags=0x%08x\n",
215 #endif /* LDBM_DEBUG */
217 rc = ldbm_store( db->dbc_db, key, data, flags );
230 rc = ldbm_delete( db->dbc_db, key );