1 /* id2entry.c - routines to deal with the id2entry index */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
14 #include "back-ldbm.h"
17 * This routine adds (or updates) an entry on disk.
18 * The cache should already be updated.
22 id2entry_add( Backend *be, Entry *e )
24 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
29 ldbm_datum_init( key );
30 ldbm_datum_init( data );
32 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
35 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
37 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
42 key.dptr = (char *) &e->e_id;
43 key.dsize = sizeof(ID);
45 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
46 data.dptr = entry2str( e, &len );
51 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
52 rc = ldbm_cache_store( db, key, data, flags );
54 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
56 ldbm_cache_close( be, db );
58 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
64 id2entry_delete( Backend *be, Entry *e )
66 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
71 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
76 /* check for writer lock */
77 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
81 ldbm_datum_init( key );
83 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
85 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
90 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
91 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
92 e->e_id, e->e_dn, 0 );
95 key.dptr = (char *) &e->e_id;
96 key.dsize = sizeof(ID);
98 rc = ldbm_cache_delete( db, key );
100 ldbm_cache_close( be, db );
102 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
106 /* returns entry with reader/writer lock */
108 id2entry_rw( Backend *be, ID id, int rw )
110 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
115 ldbm_datum_init( key );
116 ldbm_datum_init( data );
118 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
119 rw ? "w" : "r", id, 0 );
121 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
122 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
123 rw ? "w" : "r", id, (unsigned long) e );
127 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
129 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
134 key.dptr = (char *) &id;
135 key.dsize = sizeof(ID);
137 data = ldbm_cache_fetch( db, key );
139 if ( data.dptr == NULL ) {
140 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
141 rw ? "w" : "r", id, 0 );
142 ldbm_cache_close( be, db );
146 e = str2entry( data.dptr );
147 ldbm_datum_free( db->dbc_db, data );
148 ldbm_cache_close( be, db );
151 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
152 rw ? "w" : "r", id, 0 );
158 if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
161 /* XXX this is a kludge.
162 * maybe the entry got added underneath us
163 * There are many underlying race condtions in the cache/disk code.
165 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
166 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
167 rw ? "w" : "r", id, (unsigned long) e );
171 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
172 rw ? "w" : "r", id, 0 );
176 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
177 rw ? "w" : "r", id, (unsigned long) e );