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-2004 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 );
45 LDAP_LOG( INDEX, ENTRY, "id2entry_add: (%s)%ld\n", e->e_dn, e->e_id, 0 );
47 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
52 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
56 "id2entry_add: could not open/create id2entry%s\n",
59 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
66 #ifdef WORDS_BIGENDIAN
67 key.dptr = (char *) &e->e_id;
70 key.dptr = (char *) &id;
72 key.dsize = sizeof(ID);
74 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
75 data.dptr = entry2str( e, &len );
80 rc = ldbm_cache_store( db, key, data, flags );
82 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
84 ldbm_cache_close( be, db );
87 LDAP_LOG( INDEX, ENTRY, "id2entry_add: return %d\n", rc, 0, 0 );
89 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
97 id2entry_delete( Backend *be, Entry *e )
99 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
103 #ifndef WORDS_BIGENDIAN
108 LDAP_LOG( INDEX, ENTRY, "id2entry_delete: (%s)%ld\n", e->e_dn, e->e_id, 0 );
110 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
116 #ifdef LDAP_RDWR_DEBUG
117 /* check for writer lock */
118 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
122 ldbm_datum_init( key );
124 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
127 LDAP_LOG( INDEX, ERR,
128 "id2entry_delete: could not open/create id2entry%s\n",
131 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
138 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
140 LDAP_LOG( INDEX, ERR,
141 "id2entry_delete: Could not delete (%s)%ld from cache\n",
142 e->e_dn, e->e_id, 0 );
144 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
145 e->e_id, e->e_dn, 0 );
150 #ifdef WORDS_BIGENDIAN
151 key.dptr = (char *) &e->e_id;
154 key.dptr = (char *) &id;
156 key.dsize = sizeof(ID);
158 rc = ldbm_cache_delete( db, key );
160 ldbm_cache_close( be, db );
163 LDAP_LOG( INDEX, ENTRY, "id2entry_delete: return %d\n", rc, 0, 0 );
165 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
171 /* returns entry with reader/writer lock */
173 id2entry_rw( Backend *be, ID id, int rw )
175 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
179 #ifndef WORDS_BIGENDIAN
183 ldbm_datum_init( key );
184 ldbm_datum_init( data );
187 LDAP_LOG( INDEX, ENTRY,
188 "id2entry_rw: %s (%ld)\n", rw ? "write" : "read", id, 0 );
190 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
191 rw ? "w" : "r", id, 0 );
195 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
197 LDAP_LOG( INDEX, DETAIL1,
198 "id2entry_rw: %s (%ld) 0x%lx (cache).\n",
199 rw ? "write" : "read", id, (unsigned long)e );
201 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
202 rw ? "w" : "r", id, (unsigned long) e );
208 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
211 LDAP_LOG( INDEX, ERR,
212 "id2entry_rw: could not open id2entry%s\n", LDBM_SUFFIX, 0, 0 );
214 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
221 #ifdef WORDS_BIGENDIAN
222 key.dptr = (char *) &id;
225 key.dptr = (char *) &id2;
227 key.dsize = sizeof(ID);
229 data = ldbm_cache_fetch( db, key );
231 if ( data.dptr == NULL ) {
233 LDAP_LOG( INDEX, ERR,
234 "id2entry_rw: (%ld) not found\n", id, 0, 0 );
236 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
237 rw ? "w" : "r", id, 0 );
240 ldbm_cache_close( be, db );
244 e = str2entry( data.dptr );
245 ldbm_datum_free( db->dbc_db, data );
246 ldbm_cache_close( be, db );
250 LDAP_LOG( INDEX, ERR,
251 "id2entry_rw: %s of %ld failed\n", rw ? "write" : "read", id, 0);
253 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
254 rw ? "w" : "r", id, 0 );
262 if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
265 /* XXX this is a kludge.
266 * maybe the entry got added underneath us
267 * There are many underlying race condtions in the cache/disk code.
269 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
271 LDAP_LOG( INDEX, DETAIL1,
272 "id2entry_rw: %s of %ld 0x%lx (cache)\n",
273 rw ? "write" : "read", id, (unsigned long)e );
275 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
276 rw ? "w" : "r", id, (unsigned long) e );
283 LDAP_LOG( INDEX, ERR,
284 "id2entry_rw: %s of %ld (cache add failed)\n",
285 rw ? "write" : "read", id, 0 );
287 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
288 rw ? "w" : "r", id, 0 );
295 LDAP_LOG( INDEX, ENTRY,
296 "id2entry_rw: %s of %ld 0x%lx (disk)\n",
297 rw ? "write" : "read", id, (unsigned long)e );
299 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
300 rw ? "w" : "r", id, (unsigned long) e );
303 /* marks the entry as committed, so it will get added to the cache
304 * when the lock is released */
305 cache_entry_commit( e );