1 /* id2entry.c - routines to deal with the id2entry index */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 #include <ac/socket.h>
24 #include "back-ldbm.h"
27 * This routine adds (or updates) an entry on disk.
28 * The cache should already be updated.
32 id2entry_add( Backend *be, Entry *e )
37 #ifndef WORDS_BIGENDIAN
41 ldbm_datum_init( key );
42 ldbm_datum_init( data );
44 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
48 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
50 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
56 #ifdef WORDS_BIGENDIAN
57 key.dptr = (char *) &e->e_id;
60 key.dptr = (char *) &id;
62 key.dsize = sizeof(ID);
64 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
65 data.dptr = entry2str( e, &len );
70 rc = ldbm_cache_store( db, key, data, flags );
72 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
74 ldbm_cache_close( be, db );
76 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;
89 #ifndef WORDS_BIGENDIAN
93 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
98 #ifdef LDAP_RDWR_DEBUG
99 /* check for writer lock */
100 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
104 ldbm_datum_init( key );
106 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
108 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
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 );
120 #ifdef WORDS_BIGENDIAN
121 key.dptr = (char *) &e->e_id;
124 key.dptr = (char *) &id;
126 key.dsize = sizeof(ID);
128 rc = ldbm_cache_delete( db, key );
130 ldbm_cache_close( be, db );
132 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
137 /* returns entry with reader/writer lock */
139 id2entry_rw( Backend *be, ID id, int rw )
141 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
145 #ifndef WORDS_BIGENDIAN
149 ldbm_datum_init( key );
150 ldbm_datum_init( data );
152 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
153 rw ? "w" : "r", id, 0 );
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 );
163 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
165 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
171 #ifdef WORDS_BIGENDIAN
172 key.dptr = (char *) &id;
175 key.dptr = (char *) &id2;
177 key.dsize = sizeof(ID);
179 data = ldbm_cache_fetch( db, key );
181 if ( data.dptr == NULL ) {
182 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
183 rw ? "w" : "r", id, 0 );
185 ldbm_cache_close( be, db );
189 e = str2entry2( data.dptr, 0 );
190 ldbm_datum_free( db->dbc_db, data );
191 ldbm_cache_close( be, db );
194 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
195 rw ? "w" : "r", id, 0 );
202 if ( slapMode == SLAP_SERVER_MODE
203 && cache_add_entry_rw( &li->li_cache, e, rw ) != 0 )
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.
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 );
218 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
219 rw ? "w" : "r", id, 0 );
224 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
225 rw ? "w" : "r", id, (unsigned long) e );
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 );