1 /* id2entry.c - routines to deal with the id2entry index */
4 * Copyright 1998-2002 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 )
28 #ifndef WORDS_BIGENDIAN
32 ldbm_datum_init( key );
33 ldbm_datum_init( data );
36 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
37 "id2entry_add: (%s)%ld\n", e->e_dn, e->e_id ));
39 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
44 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
47 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
48 "id2entry_add: could not open/create id2entry%s\n",
51 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
58 #ifdef WORDS_BIGENDIAN
59 key.dptr = (char *) &e->e_id;
62 key.dptr = (char *) &id;
64 key.dsize = sizeof(ID);
66 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
67 data.dptr = entry2str( e, &len );
72 rc = ldbm_cache_store( db, key, data, flags );
74 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
76 ldbm_cache_close( be, db );
79 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
80 "id2entry_add: return %d\n", rc ));
82 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
90 id2entry_delete( Backend *be, Entry *e )
92 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
96 #ifndef WORDS_BIGENDIAN
101 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
102 "id2entry_delete: (%s)%ld\n", e->e_dn, e->e_id ));
104 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
110 #ifdef LDAP_RDWR_DEBUG
111 /* check for writer lock */
112 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
116 ldbm_datum_init( key );
118 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
121 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
122 "id2entry_delete: could not open/create id2entry%s\n",
125 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
132 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
134 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
135 "id2entry_delete: Could not delete (%s)%ld from cache\n",
138 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
139 e->e_id, e->e_dn, 0 );
144 #ifdef WORDS_BIGENDIAN
145 key.dptr = (char *) &e->e_id;
148 key.dptr = (char *) &id;
150 key.dsize = sizeof(ID);
152 rc = ldbm_cache_delete( db, key );
154 ldbm_cache_close( be, db );
157 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
158 "id2entry_delete: return %d\n", rc ));
160 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
166 /* returns entry with reader/writer lock */
168 id2entry_rw( Backend *be, ID id, int rw )
170 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
174 #ifndef WORDS_BIGENDIAN
178 ldbm_datum_init( key );
179 ldbm_datum_init( data );
182 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
183 "id2entry_rw: %s (%ld)\n",
184 rw ? "write" : "read", id ));
186 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
187 rw ? "w" : "r", id, 0 );
191 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
193 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
194 "id2entry_rw: %s (%ld) 0x%lx (cache).\n",
195 rw ? "write" : "read", id, (unsigned long)e ));
197 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
198 rw ? "w" : "r", id, (unsigned long) e );
204 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
207 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
208 "id2entry_rw: could not open id2entry%s\n", LDBM_SUFFIX ));
210 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
217 #ifdef WORDS_BIGENDIAN
218 key.dptr = (char *) &id;
221 key.dptr = (char *) &id2;
223 key.dsize = sizeof(ID);
225 data = ldbm_cache_fetch( db, key );
227 if ( data.dptr == NULL ) {
229 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
230 "id2entry_rw: (%ld) not found\n", id ));
232 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
233 rw ? "w" : "r", id, 0 );
236 ldbm_cache_close( be, db );
240 e = str2entry( data.dptr );
241 ldbm_datum_free( db->dbc_db, data );
242 ldbm_cache_close( be, db );
246 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
247 "id2entry_rw: %s of %ld failed\n",
248 rw ? "write" : "read", id ));
250 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
251 rw ? "w" : "r", id, 0 );
259 if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
262 /* XXX this is a kludge.
263 * maybe the entry got added underneath us
264 * There are many underlying race condtions in the cache/disk code.
266 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
268 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
269 "id2entry_rw: %s of %ld 0x%lx (cache)\n",
270 rw ? "write" : "read", id, (unsigned long)e ));
272 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
273 rw ? "w" : "r", id, (unsigned long) e );
280 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
281 "id2entry_rw: %s of %ld (cache add failed)\n",
282 rw ? "write" : "read", id ));
284 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
285 rw ? "w" : "r", id, 0 );
292 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
293 "id2entry_rw: %s of %ld 0x%lx (disk)\n",
294 rw ? "write" : "read", id, (unsigned long)e ));
296 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
297 rw ? "w" : "r", id, (unsigned long) e );
300 /* marks the entry as committed, so it will get added to the cache
301 * when the lock is released */
302 cache_entry_commit( e );