1 /* id2entry.c - routines to deal with the id2entry index */
10 #include "back-ldbm.h"
12 extern struct dbcache *ldbm_cache_open();
13 extern Datum ldbm_cache_fetch();
14 extern char *dn_parent();
15 extern Entry *str2entry();
16 extern char *entry2str();
17 extern pthread_mutex_t entry2str_mutex;
20 id2entry_add( Backend *be, Entry *e )
22 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
27 #ifdef HAVE_BERKELEY_DB2
28 memset( &key, 0, sizeof( key ) );
29 memset( &data, 0, sizeof( data ) );
32 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %d, \"%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 pthread_mutex_lock( &entry2str_mutex );
46 data.dptr = entry2str( e, &len, 1 );
51 if ( li->li_flush_wrt ) flags |= LDBM_SYNC;
52 rc = ldbm_cache_store( db, key, data, flags );
54 pthread_mutex_unlock( &entry2str_mutex );
56 ldbm_cache_close( be, db );
57 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
59 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
61 /* XXX should entries be born locked, i.e. apply writer lock here? */
66 id2entry_delete( Backend *be, Entry *e )
68 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
73 Debug( LDAP_DEBUG_TRACE, "=> id2entry_delete( %d, \"%s\" )\n", e->e_id,
76 /* XXX - check for writer lock - should also check no reader pending */
77 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
79 #ifdef HAVE_BERKELEY_DB2
80 memset( &key, 0, sizeof( key ) );
83 /* XXX - check for writer lock - should also check no reader pending */
84 Debug (LDAP_DEBUG_TRACE,
85 "rdwr_Xchk: readers_reading: %d writer_writing: %d\n",
86 e->e_rdwr.readers_reading, e->e_rdwr.writer_writing, 0);
88 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
90 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
95 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
96 Debug( LDAP_DEBUG_ANY, "could not delete %d (%s) from cache\n",
97 e->e_id, e->e_dn, 0 );
100 key.dptr = (char *) &e->e_id;
101 key.dsize = sizeof(ID);
103 rc = ldbm_cache_delete( db, key );
105 ldbm_cache_close( be, db );
107 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
111 /* XXX returns entry with reader/writer lock */
113 id2entry( Backend *be, ID id, int rw )
115 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
120 #ifdef HAVE_BERKELEY_DB2
121 memset( &key, 0, sizeof( key ) );
122 memset( &data, 0, sizeof( data ) );
125 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
126 rw ? "w" : "r", id, 0 );
128 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
129 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s 0x%x (cache)\n",
130 rw ? "w" : "r", e, 0 );
134 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
136 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
141 key.dptr = (char *) &id;
142 key.dsize = sizeof(ID);
144 data = ldbm_cache_fetch( db, key );
146 if ( data.dptr == NULL ) {
147 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
148 rw ? "w" : "r", id, 0 );
149 ldbm_cache_close( be, db );
153 e = str2entry( data.dptr );
155 ldbm_datum_free( db->dbc_db, data );
156 ldbm_cache_close( be, db );
159 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
160 rw ? "w" : "r", id, 0 );
164 /* acquire required reader/writer lock */
165 if (entry_rdwr_lock(e, rw)) {
166 /* XXX set DELETE flag?? */
172 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
174 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
175 rw ? "w" : "r", id, 0 );
180 id2entry_r( Backend *be, ID id )
182 return( id2entry( be, id, 0 ) );
186 id2entry_2( Backend *be, ID id )
188 return( id2entry( be, id, 1 ) );