]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
ad65ddc73e2c945dcce3be9f623294a3ee48469b
[openldap] / servers / slapd / back-ldbm / dbcache.c
1 /* ldbmcache.c - maintain a cache of open ldbm files */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16 #include <sys/stat.h>
17
18 #include "slap.h"
19 #include "back-ldbm.h"
20
21 DBCache *
22 ldbm_cache_open(
23     Backend     *be,
24     const char  *name,
25     const char  *suffix,
26     int         flags
27 )
28 {
29         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
30         int             i, lru;
31         time_t          oldtime, curtime;
32         char            buf[MAXPATHLEN];
33 #ifdef HAVE_ST_BLKSIZE
34         struct stat     st;
35 #endif
36
37         sprintf( buf, "%s" LDAP_DIRSEP "%s%s",
38                 li->li_directory, name, suffix );
39
40         if( li->li_dblocking ) {
41                 flags |= LDBM_LOCKING;
42         } else {
43                 flags |= LDBM_NOLOCKING;
44         }
45         
46         if( li->li_dbwritesync ) {
47                 flags |= LDBM_SYNC;
48         } else {
49                 flags |= LDBM_NOSYNC;
50         }
51         
52         Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
53             flags, li->li_mode );
54
55         lru = 0;
56         curtime = slap_get_time();
57         oldtime = curtime;
58
59         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
60         for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
61             i++ ) {
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] );
69                 }
70
71                 /* keep track of lru db */
72                 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
73                     li->li_dbcache[i].dbc_refcnt == 0 ) {
74                         lru = i;
75                         oldtime = li->li_dbcache[i].dbc_lastref;
76                 }
77         }
78
79         /* no empty slots, not already open - close lru and use that slot */
80         if ( i == MAXDBCACHE ) {
81                 i = lru;
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",
85                             0, 0, 0 );
86                         lru = -1;
87                         while ( lru == -1 ) {
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
92                                             == 0 ) {
93                                                 lru = i;
94                                                 break;
95                                         }
96                                 }
97                         }
98                         i = lru;
99                 }
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;
103         }
104
105         if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
106             li->li_dbcachesize )) == NULL ) {
107                 int err = errno;
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 );
113                 return( NULL );
114         }
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         li->li_dbcache[i].dbc_dirty = 0;
119 #ifdef HAVE_ST_BLKSIZE
120         if ( stat( buf, &st ) == 0 ) {
121                 li->li_dbcache[i].dbc_blksize = st.st_blksize;
122         } else
123 #endif
124         {
125                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
126         }
127         li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
128             sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
129         li->li_dbcache[i].dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
130             li->li_dbcache[i].dbc_maxids ) + 1;
131
132         assert( li->li_dbcache[i].dbc_maxindirect < 256 );
133
134         Debug( LDAP_DEBUG_ARGS,
135             "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
136             li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
137             li->li_dbcache[i].dbc_maxindirect );
138         Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
139         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
140         return( &li->li_dbcache[i] );
141 }
142
143 void
144 ldbm_cache_close( Backend *be, DBCache *db )
145 {
146         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
147
148         if( li->li_dbwritesync && db->dbc_dirty ) {
149                 ldbm_sync( db->dbc_db );
150                 db->dbc_dirty = 0;
151         }
152
153         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
154         if ( --db->dbc_refcnt == 0 ) {
155                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
156         }
157         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
158 }
159
160 void
161 ldbm_cache_really_close( Backend *be, DBCache *db )
162 {
163         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
164
165         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
166         if ( --db->dbc_refcnt == 0 ) {
167                 ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
168                 ldbm_close( db->dbc_db );
169                 free( db->dbc_name );
170                 db->dbc_name = NULL;
171         }
172         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
173 }
174
175 void
176 ldbm_cache_flush_all( Backend *be )
177 {
178         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
179         int             i;
180
181         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
182         for ( i = 0; i < MAXDBCACHE; i++ ) {
183                 if ( li->li_dbcache[i].dbc_name != NULL ) {
184                         Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
185                             li->li_dbcache[i].dbc_name, 0, 0 );
186                         ldbm_sync( li->li_dbcache[i].dbc_db );
187                         li->li_dbcache[i].dbc_dirty = 0;
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 );
193                         } else {
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;
201                         }
202                 }
203         }
204         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
205 }
206
207 Datum
208 ldbm_cache_fetch(
209     DBCache     *db,
210     Datum               key
211 )
212 {
213         Datum   data;
214
215         return ldbm_fetch( db->dbc_db, key );
216
217         return( data );
218 }
219
220 int
221 ldbm_cache_store(
222     DBCache     *db,
223     Datum               key,
224     Datum               data,
225     int                 flags
226 )
227 {
228         int     rc;
229
230 #ifdef LDBM_DEBUG
231         Statslog( LDAP_DEBUG_STATS,
232                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
233                 key.dptr, key.dsize, 0, 0, 0 );
234
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 );
238
239         Statslog( LDAP_DEBUG_STATS,
240                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
241                 data.dptr, data.dsize, 0, 0, 0 );
242
243         Statslog( LDAP_DEBUG_STATS,
244                 "=> ldbm_cache_store(): flags=0x%08x\n",
245                 flags, 0, 0, 0, 0 );
246 #endif /* LDBM_DEBUG */
247
248         db->dbc_dirty = 1;
249         rc = ldbm_store( db->dbc_db, key, data, flags );
250
251         return( rc );
252 }
253
254 int
255 ldbm_cache_delete(
256     DBCache     *db,
257     Datum               key
258 )
259 {
260         int     rc;
261
262         db->dbc_dirty = 1;
263         rc = ldbm_delete( db->dbc_db, key );
264
265         return( rc );
266 }