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 )
25 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
30 ldbm_datum_init( key );
31 ldbm_datum_init( data );
34 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
35 "id2entry_add: (%s)%ld\n", e->e_dn, e->e_id ));
37 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
42 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
45 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
46 "id2entry_add: could not open/create id2entry%s\n",
49 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
56 key.dptr = (char *) &e->e_id;
57 key.dsize = sizeof(ID);
59 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
60 data.dptr = entry2str( e, &len );
65 rc = ldbm_cache_store( db, key, data, flags );
67 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
69 ldbm_cache_close( be, db );
72 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
73 "id2entry_add: return %d\n", rc ));
75 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
83 id2entry_delete( Backend *be, Entry *e )
85 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
91 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
92 "id2entry_delete: (%s)%ld\n", e->e_dn, e->e_id ));
94 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
100 #ifdef LDAP_RDWR_DEBUG
101 /* check for writer lock */
102 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
106 ldbm_datum_init( key );
108 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
111 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
112 "id2entry_delete: could not open/create id2entry%s\n",
115 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
122 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
124 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
125 "id2entry_delete: Could not delete (%s)%ld from cache\n",
128 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
129 e->e_id, e->e_dn, 0 );
134 key.dptr = (char *) &e->e_id;
135 key.dsize = sizeof(ID);
137 rc = ldbm_cache_delete( db, key );
139 ldbm_cache_close( be, db );
142 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
143 "id2entry_delete: return %d\n", rc ));
145 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
151 /* returns entry with reader/writer lock */
153 id2entry_rw( Backend *be, ID id, int rw )
155 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
160 ldbm_datum_init( key );
161 ldbm_datum_init( data );
164 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
165 "id2entry_rw: %s (%ld)\n",
166 rw ? "write" : "read", id ));
168 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
169 rw ? "w" : "r", id, 0 );
173 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
175 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
176 "id2entry_rw: %s (%ld) 0x%lx (cache).\n",
177 rw ? "write" : "read", id, (unsigned long)e ));
179 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
180 rw ? "w" : "r", id, (unsigned long) e );
186 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
189 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
190 "id2entry_rw: could not open id2entry%s\n", LDBM_SUFFIX ));
192 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
199 key.dptr = (char *) &id;
200 key.dsize = sizeof(ID);
202 data = ldbm_cache_fetch( db, key );
204 if ( data.dptr == NULL ) {
206 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
207 "id2entry_rw: (%ld) not found\n", id ));
209 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
210 rw ? "w" : "r", id, 0 );
213 ldbm_cache_close( be, db );
217 e = str2entry( data.dptr );
218 ldbm_datum_free( db->dbc_db, data );
219 ldbm_cache_close( be, db );
223 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
224 "id2entry_rw: %s of %ld failed\n",
225 rw ? "write" : "read", id ));
227 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
228 rw ? "w" : "r", id, 0 );
236 if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
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.
243 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
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 ));
249 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
250 rw ? "w" : "r", id, (unsigned long) e );
257 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
258 "id2entry_rw: %s of %ld (cache add failed)\n",
259 rw ? "write" : "read", id ));
261 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
262 rw ? "w" : "r", id, 0 );
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 ));
273 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
274 rw ? "w" : "r", id, (unsigned long) e );
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 );