]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/dbcache.c
Added support for newSuperior.
[openldap] / servers / slapd / back-ldbm / dbcache.c
1 /* ldbmcache.c - maintain a cache of open ldbm files */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/errno.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/time.h>
11
12 #include <sys/stat.h>
13
14 #ifdef HAVE_SYS_PARAM_H
15 #include <sys/param.h>
16 #endif
17
18 #include "ldapconfig.h"
19 #include "slap.h"
20 #include "back-ldbm.h"
21
22 struct dbcache *
23 ldbm_cache_open(
24     Backend     *be,
25     char        *name,
26     char        *suffix,
27     int         flags
28 )
29 {
30         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
31         int             i, lru;
32         time_t          oldtime, curtime;
33         char            buf[MAXPATHLEN];
34 #ifdef HAVE_ST_BLKSIZE
35         struct stat     st;
36 #endif
37
38         sprintf( buf, "%s%s%s%s", li->li_directory, DEFAULT_DIRSEP, name, suffix );
39
40         Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
41             flags, li->li_mode );
42
43         lru = 0;
44         curtime = slap_get_time();
45         oldtime = curtime;
46
47         ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
48         for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
49             i++ ) {
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] );
57                 }
58
59                 /* keep track of lru db */
60                 if ( li->li_dbcache[i].dbc_lastref < oldtime &&
61                     li->li_dbcache[i].dbc_refcnt == 0 ) {
62                         lru = i;
63                         oldtime = li->li_dbcache[i].dbc_lastref;
64                 }
65         }
66
67         /* no empty slots, not already open - close lru and use that slot */
68         if ( i == MAXDBCACHE ) {
69                 i = lru;
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",
73                             0, 0, 0 );
74                         lru = -1;
75                         while ( lru == -1 ) {
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
80                                             == 0 ) {
81                                                 lru = i;
82                                                 break;
83                                         }
84                                 }
85                         }
86                         i = lru;
87                 }
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;
91         }
92
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 );
100                 return( NULL );
101         }
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;
108         } else
109 #endif
110         {
111                 li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
112         }
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;
117
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] );
125 }
126
127 void
128 ldbm_cache_close( Backend *be, struct dbcache *db )
129 {
130         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
131
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 );
135         }
136         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
137 }
138
139 void
140 ldbm_cache_really_close( Backend *be, struct dbcache *db )
141 {
142         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
143
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 );
149                 db->dbc_name = NULL;
150         }
151         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
152 }
153
154 void
155 ldbm_cache_flush_all( Backend *be )
156 {
157         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
158         int             i;
159
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 );
166                 }
167         }
168         ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
169 }
170
171 Datum
172 ldbm_cache_fetch(
173     struct dbcache      *db,
174     Datum               key
175 )
176 {
177         Datum   data;
178
179         ldbm_datum_init( data );
180
181         data = ldbm_fetch( db->dbc_db, key );
182
183         return( data );
184 }
185
186 int
187 ldbm_cache_store(
188     struct dbcache      *db,
189     Datum               key,
190     Datum               data,
191     int                 flags
192 )
193 {
194         int     rc;
195
196 #ifdef LDBM_DEBUG
197         Statslog( LDAP_DEBUG_STATS,
198                 "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
199                 key.dptr, key.dsize, 0, 0, 0 );
200
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 );
204
205         Statslog( LDAP_DEBUG_STATS,
206                 "=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
207                 data.dptr, data.dsize, 0, 0, 0 );
208
209         Statslog( LDAP_DEBUG_STATS,
210                 "=> ldbm_cache_store(): flags=0x%08x\n",
211                 flags, 0, 0, 0, 0 );
212 #endif /* LDBM_DEBUG */
213
214         rc = ldbm_store( db->dbc_db, key, data, flags );
215
216         return( rc );
217 }
218
219 int
220 ldbm_cache_delete(
221     struct dbcache      *db,
222     Datum               key
223 )
224 {
225         int     rc;
226
227         rc = ldbm_delete( db->dbc_db, key );
228
229         return( rc );
230 }