1 /* id2entry.c - routines to deal with the id2entry index */
10 #include "back-bdb2.h"
13 bdb2i_id2entry_add( BackendDB *be, Entry *e )
15 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
20 ldbm_datum_init( key );
21 ldbm_datum_init( data );
23 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_id2entry_add( %lu, \"%s\" )\n", e->e_id,
26 if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
28 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
33 key.dptr = (char *) &e->e_id;
34 key.dsize = sizeof(ID);
36 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
37 data.dptr = entry2str( e, &len, 1 );
42 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
43 rc = bdb2i_cache_store( db, key, data, flags );
45 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
47 bdb2i_cache_close( be, db );
48 (void) bdb2i_cache_add_entry_lock( &li->li_cache, e, 0 );
50 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_add %d\n", rc, 0, 0 );
52 /* XXX should entries be born locked, i.e. apply writer lock here? */
57 bdb2i_id2entry_delete( BackendDB *be, Entry *e )
59 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
64 Debug(LDAP_DEBUG_TRACE, "=> bdb2i_id2entry_delete( %lu, \"%s\" )\n", e->e_id,
68 /* check for writer lock */
69 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr));
72 ldbm_datum_init( key );
74 if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
76 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
81 if ( bdb2i_cache_delete_entry( &li->li_cache, e ) != 0 ) {
82 Debug(LDAP_DEBUG_ANY, "could not delete %lu (%s) from cache\n",
83 e->e_id, e->e_dn, 0 );
86 key.dptr = (char *) &e->e_id;
87 key.dsize = sizeof(ID);
89 rc = bdb2i_cache_delete( db, key );
91 bdb2i_cache_close( be, db );
93 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_delete %d\n", rc, 0, 0 );
97 /* XXX returns entry with reader/writer lock */
99 bdb2i_id2entry( BackendDB *be, ID id, int rw )
101 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
106 ldbm_datum_init( key );
107 ldbm_datum_init( data );
109 Debug( LDAP_DEBUG_TRACE, "=> bdb2i_id2entry_%s( %ld )\n",
110 rw ? "w" : "r", id, 0 );
112 if ( (e = bdb2i_cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
113 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s 0x%lx (cache)\n",
114 rw ? "w" : "r", (unsigned long)e, 0 );
118 if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
120 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
125 key.dptr = (char *) &id;
126 key.dsize = sizeof(ID);
128 data = bdb2i_cache_fetch( db, key );
130 if ( data.dptr == NULL ) {
131 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) not found\n",
132 rw ? "w" : "r", id, 0 );
133 bdb2i_cache_close( be, db );
137 e = str2entry( data.dptr );
139 ldbm_datum_free( db->dbc_db, data );
140 bdb2i_cache_close( be, db );
143 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) (failed)\n",
144 rw ? "w" : "r", id, 0 );
148 /* acquire required reader/writer lock */
149 if (entry_rdwr_lock(e, rw)) {
150 /* XXX set DELETE flag?? */
156 (void) bdb2i_cache_add_entry_lock( &li->li_cache, e, 0 );
158 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) (disk)\n",
159 rw ? "w" : "r", id, 0 );
164 bdb2i_id2entry_r( BackendDB *be, ID id )
166 return( bdb2i_id2entry( be, id, 0 ) );
170 bdb2i_id2entry_w( BackendDB *be, ID id )
172 return( bdb2i_id2entry( be, id, 1 ) );