]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
Remove lint
[openldap] / servers / slapd / back-ldbm / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
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/socket.h>
13
14 #include "slap.h"
15 #include "back-ldbm.h"
16
17 /*
18  * This routine adds (or updates) an entry on disk.
19  * The cache should already be updated. 
20  */
21
22 int
23 id2entry_add( Backend *be, Entry *e )
24 {
25         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
26         DBCache *db;
27         Datum           key, data;
28         int             len, rc, flags;
29
30         ldbm_datum_init( key );
31         ldbm_datum_init( data );
32
33 #ifdef NEW_LOGGING
34         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
35                    "id2entry_add: (%s)%ld\n", e->e_dn, e->e_id ));
36 #else
37         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
38             e->e_dn, 0 );
39 #endif
40
41
42         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
43             == NULL ) {
44 #ifdef NEW_LOGGING
45                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
46                            "id2entry_add: could not open/create id2entry%s\n",
47                            LDBM_SUFFIX ));
48 #else
49                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
50                     LDBM_SUFFIX, 0, 0 );
51 #endif
52
53                 return( -1 );
54         }
55
56         key.dptr = (char *) &e->e_id;
57         key.dsize = sizeof(ID);
58
59         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
60         data.dptr = entry2str( e, &len );
61         data.dsize = len + 1;
62
63         /* store it */
64         flags = LDBM_REPLACE;
65         rc = ldbm_cache_store( db, key, data, flags );
66
67         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
68
69         ldbm_cache_close( be, db );
70
71 #ifdef NEW_LOGGING
72         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
73                    "id2entry_add: return %d\n", rc ));
74 #else
75         Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
76 #endif
77
78
79         return( rc );
80 }
81
82 int
83 id2entry_delete( Backend *be, Entry *e )
84 {
85         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
86         DBCache *db;
87         Datum           key;
88         int             rc;
89
90 #ifdef NEW_LOGGING
91         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
92                    "id2entry_delete: (%s)%ld\n", e->e_dn, e->e_id ));
93 #else
94         Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
95             e->e_dn, 0 );
96 #endif
97
98
99 #ifdef notdef
100 #ifdef LDAP_RDWR_DEBUG
101         /* check for writer lock */
102         assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
103 #endif
104 #endif
105
106         ldbm_datum_init( key );
107
108         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
109                 == NULL ) {
110 #ifdef NEW_LOGGING
111                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
112                            "id2entry_delete: could not open/create id2entry%s\n",
113                            LDBM_SUFFIX ));
114 #else
115                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
116                     LDBM_SUFFIX, 0, 0 );
117 #endif
118
119                 return( -1 );
120         }
121
122         if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
123 #ifdef NEW_LOGGING
124                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
125                            "id2entry_delete: Could not delete (%s)%ld from cache\n",
126                            e->e_dn, e->e_id ));
127 #else
128                 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
129                     e->e_id, e->e_dn, 0 );
130 #endif
131
132         }
133
134         key.dptr = (char *) &e->e_id;
135         key.dsize = sizeof(ID);
136
137         rc = ldbm_cache_delete( db, key );
138
139         ldbm_cache_close( be, db );
140
141 #ifdef NEW_LOGGING
142         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
143                    "id2entry_delete: return %d\n", rc ));
144 #else
145         Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
146 #endif
147
148         return( rc );
149 }
150
151 /* returns entry with reader/writer lock */
152 Entry *
153 id2entry_rw( Backend *be, ID id, int rw )
154 {
155         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
156         DBCache *db;
157         Datum           key, data;
158         Entry           *e;
159
160         ldbm_datum_init( key );
161         ldbm_datum_init( data );
162
163 #ifdef NEW_LOGGING
164         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
165                    "id2entry_rw: %s (%ld)\n",
166                    rw ? "write" : "read", id ));
167 #else
168         Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
169                 rw ? "w" : "r", id, 0 );
170 #endif
171
172
173         if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
176                            "id2entry_rw: %s (%ld) 0x%lx (cache).\n",
177                            rw ? "write" : "read", id, (unsigned long)e ));
178 #else
179                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
180                         rw ? "w" : "r", id, (unsigned long) e );
181 #endif
182
183                 return( e );
184         }
185
186         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
187                 == NULL ) {
188 #ifdef NEW_LOGGING
189                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
190                            "id2entry_rw: could not open id2entry%s\n", LDBM_SUFFIX ));
191 #else
192                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
193                     LDBM_SUFFIX, 0, 0 );
194 #endif
195
196                 return( NULL );
197         }
198
199         key.dptr = (char *) &id;
200         key.dsize = sizeof(ID);
201
202         data = ldbm_cache_fetch( db, key );
203
204         if ( data.dptr == NULL ) {
205 #ifdef NEW_LOGGING
206                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
207                            "id2entry_rw: (%ld) not found\n", id ));
208 #else
209                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
210                         rw ? "w" : "r", id, 0 );
211 #endif
212
213                 ldbm_cache_close( be, db );
214                 return( NULL );
215         }
216
217         e = str2entry( data.dptr );
218         ldbm_datum_free( db->dbc_db, data );
219         ldbm_cache_close( be, db );
220
221         if ( e == NULL ) {
222 #ifdef NEW_LOGGING
223                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
224                            "id2entry_rw: %s of %ld failed\n",
225                            rw ? "write" : "read", id ));
226 #else
227                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
228                         rw ? "w" : "r", id, 0 );
229 #endif
230
231                 return( NULL );
232         }
233
234         e->e_id = id;
235
236         if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
237                 entry_free( e );
238
239                 /* XXX this is a kludge.
240                  * maybe the entry got added underneath us
241                  * There are many underlying race condtions in the cache/disk code.
242                  */
243                 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
244 #ifdef NEW_LOGGING
245                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
246                                    "id2entry_rw: %s of %ld 0x%lx (cache)\n",
247                                    rw ? "write" : "read", id, (unsigned long)e ));
248 #else
249                         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
250                                 rw ? "w" : "r", id, (unsigned long) e );
251 #endif
252
253                         return( e );
254                 }
255
256 #ifdef NEW_LOGGING
257                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
258                            "id2entry_rw: %s of %ld (cache add failed)\n",
259                            rw ? "write" : "read", id ));
260 #else
261                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
262                         rw ? "w" : "r", id, 0 );
263 #endif
264
265                 return NULL;
266         }
267
268 #ifdef NEW_LOGGING
269         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
270                    "id2entry_rw: %s of %ld 0x%lx (disk)\n",
271                    rw ? "write" : "read", id, (unsigned long)e ));
272 #else
273         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
274                 rw ? "w" : "r", id, (unsigned long) e );
275 #endif
276
277         /* marks the entry as committed, so it will get added to the cache
278          * when the lock is released */
279         cache_entry_commit( e );
280
281         return( e );
282 }