1 /* id2entry.c - routines to deal with the id2entry index */
4 * Copyright 1998-2003 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( INDEX, ENTRY, "id2entry_add: (%s)%ld\n", e->e_dn, e->e_id, 0 );
38 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
43 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
47 "id2entry_add: could not open/create id2entry%s\n",
50 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
57 #ifdef WORDS_BIGENDIAN
58 key.dptr = (char *) &e->e_id;
61 key.dptr = (char *) &id;
63 key.dsize = sizeof(ID);
65 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
66 data.dptr = entry2str( e, &len );
71 rc = ldbm_cache_store( db, key, data, flags );
73 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
75 ldbm_cache_close( be, db );
78 LDAP_LOG( INDEX, ENTRY, "id2entry_add: return %d\n", rc, 0, 0 );
80 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
88 id2entry_delete( Backend *be, Entry *e )
90 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
94 #ifndef WORDS_BIGENDIAN
99 LDAP_LOG( INDEX, ENTRY, "id2entry_delete: (%s)%ld\n", e->e_dn, e->e_id, 0 );
101 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
107 #ifdef LDAP_RDWR_DEBUG
108 /* check for writer lock */
109 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
113 ldbm_datum_init( key );
115 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
118 LDAP_LOG( INDEX, ERR,
119 "id2entry_delete: could not open/create id2entry%s\n",
122 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
129 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
131 LDAP_LOG( INDEX, ERR,
132 "id2entry_delete: Could not delete (%s)%ld from cache\n",
133 e->e_dn, e->e_id, 0 );
135 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
136 e->e_id, e->e_dn, 0 );
141 #ifdef WORDS_BIGENDIAN
142 key.dptr = (char *) &e->e_id;
145 key.dptr = (char *) &id;
147 key.dsize = sizeof(ID);
149 rc = ldbm_cache_delete( db, key );
151 ldbm_cache_close( be, db );
154 LDAP_LOG( INDEX, ENTRY, "id2entry_delete: return %d\n", rc, 0, 0 );
156 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
162 /* returns entry with reader/writer lock */
164 id2entry_rw( Backend *be, ID id, int rw )
166 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
170 #ifndef WORDS_BIGENDIAN
174 ldbm_datum_init( key );
175 ldbm_datum_init( data );
178 LDAP_LOG( INDEX, ENTRY,
179 "id2entry_rw: %s (%ld)\n", rw ? "write" : "read", id, 0 );
181 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
182 rw ? "w" : "r", id, 0 );
186 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
188 LDAP_LOG( INDEX, DETAIL1,
189 "id2entry_rw: %s (%ld) 0x%lx (cache).\n",
190 rw ? "write" : "read", id, (unsigned long)e );
192 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
193 rw ? "w" : "r", id, (unsigned long) e );
199 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
202 LDAP_LOG( INDEX, ERR,
203 "id2entry_rw: could not open id2entry%s\n", LDBM_SUFFIX, 0, 0 );
205 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
212 #ifdef WORDS_BIGENDIAN
213 key.dptr = (char *) &id;
216 key.dptr = (char *) &id2;
218 key.dsize = sizeof(ID);
220 data = ldbm_cache_fetch( db, key );
222 if ( data.dptr == NULL ) {
224 LDAP_LOG( INDEX, ERR,
225 "id2entry_rw: (%ld) not found\n", id, 0, 0 );
227 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
228 rw ? "w" : "r", id, 0 );
231 ldbm_cache_close( be, db );
235 e = str2entry( data.dptr );
236 ldbm_datum_free( db->dbc_db, data );
237 ldbm_cache_close( be, db );
241 LDAP_LOG( INDEX, ERR,
242 "id2entry_rw: %s of %ld failed\n", rw ? "write" : "read", id, 0);
244 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
245 rw ? "w" : "r", id, 0 );
253 if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
256 /* XXX this is a kludge.
257 * maybe the entry got added underneath us
258 * There are many underlying race condtions in the cache/disk code.
260 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
262 LDAP_LOG( INDEX, DETAIL1,
263 "id2entry_rw: %s of %ld 0x%lx (cache)\n",
264 rw ? "write" : "read", id, (unsigned long)e );
266 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
267 rw ? "w" : "r", id, (unsigned long) e );
274 LDAP_LOG( INDEX, ERR,
275 "id2entry_rw: %s of %ld (cache add failed)\n",
276 rw ? "write" : "read", id, 0 );
278 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
279 rw ? "w" : "r", id, 0 );
286 LDAP_LOG( INDEX, ENTRY,
287 "id2entry_rw: %s of %ld 0x%lx (disk)\n",
288 rw ? "write" : "read", id, (unsigned long)e );
290 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
291 rw ? "w" : "r", id, (unsigned long) e );
294 /* marks the entry as committed, so it will get added to the cache
295 * when the lock is released */
296 cache_entry_commit( e );