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;
34 #ifdef HAVE_ST_BLKSIZE
38 sprintf( buf, "%s%s%s%s", li->li_directory, DEFAULT_DIRSEP, name, suffix );
40 Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
44 curtime = slap_get_time();
47 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
48 for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
50 /* already open - return it */
51 if ( strcmp( li->li_dbcache[i].dbc_name, buf ) == 0 ) {
52 li->li_dbcache[i].dbc_refcnt++;
53 Debug( LDAP_DEBUG_TRACE,
54 "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
55 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
56 return( &li->li_dbcache[i] );
59 /* keep track of lru db */
60 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
61 li->li_dbcache[i].dbc_refcnt == 0 ) {
63 oldtime = li->li_dbcache[i].dbc_lastref;
67 /* no empty slots, not already open - close lru and use that slot */
68 if ( i == MAXDBCACHE ) {
70 if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
71 Debug( LDAP_DEBUG_ANY,
72 "ldbm_cache_open no unused db to close - waiting\n",
76 ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
77 &li->li_dbcache_mutex );
78 for ( i = 0; i < MAXDBCACHE; i++ ) {
79 if ( li->li_dbcache[i].dbc_refcnt
88 ldbm_close( li->li_dbcache[i].dbc_db );
89 free( li->li_dbcache[i].dbc_name );
90 li->li_dbcache[i].dbc_name = NULL;
93 if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
94 li->li_dbcachesize )) == NULL ) {
95 Debug( LDAP_DEBUG_TRACE,
96 "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
97 buf, errno, errno > -1 && errno < sys_nerr ?
98 sys_errlist[errno] : "unknown" );
99 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
102 li->li_dbcache[i].dbc_name = ch_strdup( buf );
103 li->li_dbcache[i].dbc_refcnt = 1;
104 li->li_dbcache[i].dbc_lastref = curtime;
105 #ifdef HAVE_ST_BLKSIZE
106 if ( stat( buf, &st ) == 0 ) {
107 li->li_dbcache[i].dbc_blksize = st.st_blksize;
111 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
113 li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
114 sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
115 li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
116 li->li_dbcache[i].dbc_maxids) + 1;
118 Debug( LDAP_DEBUG_ARGS,
119 "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
120 li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
121 li->li_dbcache[i].dbc_maxindirect );
122 Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
123 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
124 return( &li->li_dbcache[i] );
128 ldbm_cache_close( Backend *be, struct dbcache *db )
130 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
132 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
133 if ( --db->dbc_refcnt == 0 ) {
134 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
136 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
140 ldbm_cache_really_close( Backend *be, struct dbcache *db )
142 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
144 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
145 if ( --db->dbc_refcnt == 0 ) {
146 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
147 ldbm_close( db->dbc_db );
148 free( db->dbc_name );
151 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
155 ldbm_cache_flush_all( Backend *be )
157 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
160 ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
161 for ( i = 0; i < MAXDBCACHE; i++ ) {
162 if ( li->li_dbcache[i].dbc_name != NULL ) {
163 Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
164 li->li_dbcache[i].dbc_name, 0, 0 );
165 ldbm_sync( li->li_dbcache[i].dbc_db );
168 ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
179 ldbm_datum_init( data );
181 data = ldbm_fetch( db->dbc_db, key );
197 Statslog( LDAP_DEBUG_STATS,
198 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
199 key.dptr, key.dsize, 0, 0, 0 );
201 Statslog( LDAP_DEBUG_STATS,
202 "=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
203 key.dptr, data.dptr, 0, 0, 0 );
205 Statslog( LDAP_DEBUG_STATS,
206 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
207 data.dptr, data.dsize, 0, 0, 0 );
209 Statslog( LDAP_DEBUG_STATS,
210 "=> ldbm_cache_store(): flags=0x%08x\n",
212 #endif /* LDBM_DEBUG */
214 rc = ldbm_store( db->dbc_db, key, data, flags );
227 rc = ldbm_delete( db->dbc_db, key );