1 /* id2entry.c - routines to deal with the id2entry index */
10 #include "back-ldbm.h"
13 id2entry_add( Backend *be, Entry *e )
15 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
20 #ifdef HAVE_BERKELEY_DB2
21 memset( &key, 0, sizeof( key ) );
22 memset( &data, 0, sizeof( data ) );
25 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %lu, \"%s\" )\n", e->e_id,
28 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
30 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
35 key.dptr = (char *) &e->e_id;
36 key.dsize = sizeof(ID);
38 pthread_mutex_lock( &entry2str_mutex );
39 data.dptr = entry2str( e, &len, 1 );
44 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
45 rc = ldbm_cache_store( db, key, data, flags );
47 pthread_mutex_unlock( &entry2str_mutex );
49 ldbm_cache_close( be, db );
50 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
52 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
54 /* XXX should entries be born locked, i.e. apply writer lock here? */
59 id2entry_delete( Backend *be, Entry *e )
61 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
66 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %lu, \"%s\" )\n", e->e_id,
69 /* XXX - check for writer lock - should also check no reader pending */
71 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
74 #ifdef HAVE_BERKELEY_DB2
75 memset( &key, 0, sizeof( key ) );
78 /* XXX - check for writer lock - should also check no reader pending */
79 Debug (LDAP_DEBUG_TRACE,
80 "rdwr_Xchk: readers_reading: %d writer_writing: %d\n",
81 e->e_rdwr.readers_reading, e->e_rdwr.writer_writing, 0);
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 %lu (%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 /* XXX returns entry with reader/writer lock */
108 id2entry( Backend *be, ID id, int rw )
110 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
115 #ifdef HAVE_BERKELEY_DB2
116 memset( &key, 0, sizeof( key ) );
117 memset( &data, 0, sizeof( data ) );
120 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
121 rw ? "w" : "r", id, 0 );
123 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
124 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s 0x%lx (cache)\n",
125 rw ? "w" : "r", (unsigned long)e, 0 );
129 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
131 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
136 key.dptr = (char *) &id;
137 key.dsize = sizeof(ID);
139 data = ldbm_cache_fetch( db, key );
141 if ( data.dptr == NULL ) {
142 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
143 rw ? "w" : "r", id, 0 );
144 ldbm_cache_close( be, db );
148 e = str2entry( data.dptr );
150 ldbm_datum_free( db->dbc_db, data );
151 ldbm_cache_close( be, db );
154 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
155 rw ? "w" : "r", id, 0 );
159 /* acquire required reader/writer lock */
160 if (entry_rdwr_lock(e, rw)) {
161 /* XXX set DELETE flag?? */
167 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
169 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
170 rw ? "w" : "r", id, 0 );
175 id2entry_r( Backend *be, ID id )
177 return( id2entry( be, id, 0 ) );
181 id2entry_w( Backend *be, ID id )
183 return( id2entry( be, id, 1 ) );