1 /* id2entry.c - routines to deal with the id2entry index */
10 #include "back-ldbm.h"
13 * This routine adds (or updates) an entry on disk.
14 * The cache should already be updated.
18 id2entry_add( Backend *be, Entry *e )
20 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25 ldbm_datum_init( key );
26 ldbm_datum_init( data );
28 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
31 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
33 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
38 key.dptr = (char *) &e->e_id;
39 key.dsize = sizeof(ID);
41 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
42 data.dptr = entry2str( e, &len, 1 );
47 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
48 rc = ldbm_cache_store( db, key, data, flags );
50 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
52 ldbm_cache_close( be, db );
54 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
60 id2entry_delete( Backend *be, Entry *e )
62 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
67 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
72 /* check for writer lock */
73 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
77 ldbm_datum_init( key );
79 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
81 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
86 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
87 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
88 e->e_id, e->e_dn, 0 );
91 key.dptr = (char *) &e->e_id;
92 key.dsize = sizeof(ID);
94 rc = ldbm_cache_delete( db, key );
96 ldbm_cache_close( be, db );
98 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
102 /* returns entry with reader/writer lock */
104 id2entry_rw( Backend *be, ID id, int rw )
106 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
111 ldbm_datum_init( key );
112 ldbm_datum_init( data );
114 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
115 rw ? "w" : "r", id, 0 );
117 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
118 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
119 rw ? "w" : "r", id, (unsigned long) e );
123 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
125 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
130 key.dptr = (char *) &id;
131 key.dsize = sizeof(ID);
133 data = ldbm_cache_fetch( db, key );
135 if ( data.dptr == NULL ) {
136 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
137 rw ? "w" : "r", id, 0 );
138 ldbm_cache_close( be, db );
142 e = str2entry( data.dptr );
144 ldbm_datum_free( db->dbc_db, data );
145 ldbm_cache_close( be, db );
148 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
149 rw ? "w" : "r", id, 0 );
153 if ( e->e_id != id ) {
154 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (wrong id %ld on disk)\n",
155 rw ? "w" : "r", id, e->e_id );
160 if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
163 /* XXX this is a kludge.
164 * maybe the entry got added underneath us
165 * There are many underlying race condtions in the cache/disk code.
167 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
168 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
169 rw ? "w" : "r", id, (unsigned long) e );
173 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
174 rw ? "w" : "r", id, 0 );
178 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
179 rw ? "w" : "r", id, (unsigned long) e );