1 /* id2entry.c - routines to deal with the id2entry index */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
15 #include "back-ldbm.h"
18 * This routine adds (or updates) an entry on disk.
19 * The cache should already be updated.
23 id2entry_add( Backend *be, Entry *e )
29 ldbm_datum_init( key );
30 ldbm_datum_init( data );
33 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
34 "id2entry_add: (%s)%ld\n", e->e_dn, e->e_id ));
36 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
41 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
44 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
45 "id2entry_add: could not open/create id2entry%s\n",
48 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
55 key.dptr = (char *) &e->e_id;
56 key.dsize = sizeof(ID);
58 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
59 data.dptr = entry2str( e, &len );
64 rc = ldbm_cache_store( db, key, data, flags );
66 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
68 ldbm_cache_close( be, db );
71 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
72 "id2entry_add: return %d\n", rc ));
74 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
82 id2entry_delete( Backend *be, Entry *e )
84 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
90 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
91 "id2entry_delete: (%s)%ld\n", e->e_dn, e->e_id ));
93 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
99 #ifdef LDAP_RDWR_DEBUG
100 /* check for writer lock */
101 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
105 ldbm_datum_init( key );
107 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
110 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
111 "id2entry_delete: could not open/create id2entry%s\n",
114 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
121 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
123 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
124 "id2entry_delete: Could not delete (%s)%ld from cache\n",
127 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
128 e->e_id, e->e_dn, 0 );
133 key.dptr = (char *) &e->e_id;
134 key.dsize = sizeof(ID);
136 rc = ldbm_cache_delete( db, key );
138 ldbm_cache_close( be, db );
141 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
142 "id2entry_delete: return %d\n", rc ));
144 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
150 /* returns entry with reader/writer lock */
152 id2entry_rw( Backend *be, ID id, int rw )
154 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
159 ldbm_datum_init( key );
160 ldbm_datum_init( data );
163 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
164 "id2entry_rw: %s (%ld)\n",
165 rw ? "write" : "read", id ));
167 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
168 rw ? "w" : "r", id, 0 );
172 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
174 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
175 "id2entry_rw: %s (%ld) 0x%lx (cache).\n",
176 rw ? "write" : "read", id, (unsigned long)e ));
178 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
179 rw ? "w" : "r", id, (unsigned long) e );
185 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
188 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
189 "id2entry_rw: could not open id2entry%s\n", LDBM_SUFFIX ));
191 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
198 key.dptr = (char *) &id;
199 key.dsize = sizeof(ID);
201 data = ldbm_cache_fetch( db, key );
203 if ( data.dptr == NULL ) {
205 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
206 "id2entry_rw: (%ld) not found\n", id ));
208 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
209 rw ? "w" : "r", id, 0 );
212 ldbm_cache_close( be, db );
216 e = str2entry( data.dptr );
217 ldbm_datum_free( db->dbc_db, data );
218 ldbm_cache_close( be, db );
222 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
223 "id2entry_rw: %s of %ld failed\n",
224 rw ? "write" : "read", id ));
226 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
227 rw ? "w" : "r", id, 0 );
235 if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
238 /* XXX this is a kludge.
239 * maybe the entry got added underneath us
240 * There are many underlying race condtions in the cache/disk code.
242 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
244 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
245 "id2entry_rw: %s of %ld 0x%lx (cache)\n",
246 rw ? "write" : "read", id, (unsigned long)e ));
248 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
249 rw ? "w" : "r", id, (unsigned long) e );
256 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
257 "id2entry_rw: %s of %ld (cache add failed)\n",
258 rw ? "write" : "read", id ));
260 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
261 rw ? "w" : "r", id, 0 );
268 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
269 "id2entry_rw: %s of %ld 0x%lx (disk)\n",
270 rw ? "write" : "read", id, (unsigned long)e ));
272 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
273 rw ? "w" : "r", id, (unsigned long) e );
276 /* marks the entry as committed, so it will get added to the cache
277 * when the lock is released */
278 cache_entry_commit( e );