]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
Sync with HEAD
[openldap] / servers / slapd / back-ldbm / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/socket.h>
22
23 #include "slap.h"
24 #include "back-ldbm.h"
25
26 /*
27  * This routine adds (or updates) an entry on disk.
28  * The cache should already be updated. 
29  */
30
31 int
32 id2entry_add( Backend *be, Entry *e )
33 {
34         DBCache *db;
35         Datum           key, data;
36         int             len, rc, flags;
37 #ifndef WORDS_BIGENDIAN
38         ID              id;
39 #endif
40
41         ldbm_datum_init( key );
42         ldbm_datum_init( data );
43
44         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
45             e->e_dn, 0 );
46
47
48         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
49             == NULL ) {
50                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
51                     LDBM_SUFFIX, 0, 0 );
52
53                 return( -1 );
54         }
55
56 #ifdef WORDS_BIGENDIAN
57         key.dptr = (char *) &e->e_id;
58 #else
59         id = htonl(e->e_id);
60         key.dptr = (char *) &id;
61 #endif
62         key.dsize = sizeof(ID);
63
64         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
65         data.dptr = entry2str( e, &len );
66         data.dsize = len + 1;
67
68         /* store it */
69         flags = LDBM_REPLACE;
70         rc = ldbm_cache_store( db, key, data, flags );
71
72         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
73
74         ldbm_cache_close( be, db );
75
76         Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
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 #ifndef WORDS_BIGENDIAN
90         ID              id;
91 #endif
92
93         Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
94             e->e_dn, 0 );
95
96
97 #ifdef notdef
98 #ifdef LDAP_RDWR_DEBUG
99         /* check for writer lock */
100         assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
101 #endif
102 #endif
103
104         ldbm_datum_init( key );
105
106         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
107                 == NULL ) {
108                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
109                     LDBM_SUFFIX, 0, 0 );
110
111                 return( -1 );
112         }
113
114         if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
115                 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
116                     e->e_id, e->e_dn, 0 );
117
118         }
119
120 #ifdef WORDS_BIGENDIAN
121         key.dptr = (char *) &e->e_id;
122 #else
123         id = htonl(e->e_id);
124         key.dptr = (char *) &id;
125 #endif
126         key.dsize = sizeof(ID);
127
128         rc = ldbm_cache_delete( db, key );
129
130         ldbm_cache_close( be, db );
131
132         Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
133
134         return( rc );
135 }
136
137 /* returns entry with reader/writer lock */
138 Entry *
139 id2entry_rw( Backend *be, ID id, int rw )
140 {
141         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
142         DBCache *db;
143         Datum           key, data;
144         Entry           *e;
145 #ifndef WORDS_BIGENDIAN
146         ID              id2;
147 #endif
148
149         ldbm_datum_init( key );
150         ldbm_datum_init( data );
151
152         Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
153                 rw ? "w" : "r", id, 0 );
154
155
156         if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
157                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
158                         rw ? "w" : "r", id, (unsigned long) e );
159
160                 return( e );
161         }
162
163         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
164                 == NULL ) {
165                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
166                     LDBM_SUFFIX, 0, 0 );
167
168                 return( NULL );
169         }
170
171 #ifdef WORDS_BIGENDIAN
172         key.dptr = (char *) &id;
173 #else
174         id2 = htonl(id);
175         key.dptr = (char *) &id2;
176 #endif
177         key.dsize = sizeof(ID);
178
179         data = ldbm_cache_fetch( db, key );
180
181         if ( data.dptr == NULL ) {
182                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
183                         rw ? "w" : "r", id, 0 );
184
185                 ldbm_cache_close( be, db );
186                 return( NULL );
187         }
188
189         e = str2entry2( data.dptr, 0 );
190         ldbm_datum_free( db->dbc_db, data );
191         ldbm_cache_close( be, db );
192
193         if ( e == NULL ) {
194                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
195                         rw ? "w" : "r", id, 0 );
196
197                 return( NULL );
198         }
199
200         e->e_id = id;
201
202         if ( slapMode == SLAP_SERVER_MODE
203                         && cache_add_entry_rw( &li->li_cache, e, rw ) != 0 )
204         {
205                 entry_free( e );
206
207                 /* XXX this is a kludge.
208                  * maybe the entry got added underneath us
209                  * There are many underlying race condtions in the cache/disk code.
210                  */
211                 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
212                         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
213                                 rw ? "w" : "r", id, (unsigned long) e );
214
215                         return( e );
216                 }
217
218                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
219                         rw ? "w" : "r", id, 0 );
220
221                 return NULL;
222         }
223
224         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
225                 rw ? "w" : "r", id, (unsigned long) e );
226
227         if ( slapMode == SLAP_SERVER_MODE ) {
228                 /* marks the entry as committed, so it will get added to the cache
229                  * when the lock is released */
230                 cache_entry_commit( e );
231         }
232
233         return( e );
234 }