1 /* id2entry.c - routines to deal with the id2entry index */
11 #include "back-bdb2.h"
14 * This routine adds (or updates) an entry on disk.
15 * The cache should already be updated.
19 bdb2i_id2entry_add( BackendDB *be, Entry *e )
21 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
26 ldbm_datum_init( key );
27 ldbm_datum_init( data );
29 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_id2entry_add( %ld, \"%s\" )\n", e->e_id,
32 if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
34 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
39 key.dptr = (char *) &e->e_id;
40 key.dsize = sizeof(ID);
42 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
43 data.dptr = entry2str( e, &len );
48 rc = bdb2i_cache_store( db, key, data, flags );
50 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
52 bdb2i_cache_close( be, db );
54 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_add %d\n", rc, 0, 0 );
60 bdb2i_id2entry_delete( BackendDB *be, Entry *e )
62 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
67 Debug(LDAP_DEBUG_TRACE, "=> bdb2i_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 = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
81 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
86 if ( bdb2i_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 = bdb2i_cache_delete( db, key );
96 bdb2i_cache_close( be, db );
98 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_delete %d\n", rc, 0, 0 );
102 /* returns entry with reader/writer lock */
104 bdb2i_id2entry_rw( BackendDB *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, "=> bdb2i_id2entry_%s( %ld )\n",
115 rw ? "w" : "r", id, 0 );
117 if ( (e = bdb2i_cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
118 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) 0x%lx (cache)\n",
119 rw ? "w" : "r", id, (unsigned long)e );
123 if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_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 = bdb2i_cache_fetch( db, key );
135 if ( data.dptr == NULL ) {
136 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) not found\n",
137 rw ? "w" : "r", id, 0 );
138 bdb2i_cache_close( be, db );
142 e = str2entry( data.dptr );
144 ldbm_datum_free( db->dbc_db, data );
145 bdb2i_cache_close( be, db );
148 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) (failed)\n",
149 rw ? "w" : "r", id, 0 );
155 if ( bdb2i_cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
158 /* see if it got added underneath us */
159 if((e = bdb2i_cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
160 Debug( LDAP_DEBUG_TRACE,
161 "<= bdb2i_id2entry_%s( %ld ) 0x%lx (cache)\n",
162 rw ? "w" : "r", id, (unsigned long)e );
166 Debug( LDAP_DEBUG_TRACE,
167 "<= bdb2i_id2entry_%s( %ld ) (cache add failed)\n",
168 rw ? "w" : "r", id, 0 );
172 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) 0x%lx (disk)\n",
173 rw ? "w" : "r", id, (unsigned long) e );