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 ldbm_datum_init( key );
21 ldbm_datum_init( data );
23 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %lu, \"%s\" )\n", e->e_id,
26 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_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 pthread_mutex_lock( &entry2str_mutex );
37 data.dptr = entry2str( e, &len, 1 );
42 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
43 rc = ldbm_cache_store( db, key, data, flags );
45 pthread_mutex_unlock( &entry2str_mutex );
47 ldbm_cache_close( be, db );
48 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
50 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
52 /* XXX should entries be born locked, i.e. apply writer lock here? */
57 id2entry_delete( Backend *be, Entry *e )
59 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
64 Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %lu, \"%s\" )\n", e->e_id,
67 /* XXX - check for writer lock - should also check no reader pending */
69 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
72 ldbm_datum_init( key );
74 /* XXX - check for writer lock - should also check no reader pending */
75 Debug (LDAP_DEBUG_TRACE,
76 "rdwr_Xchk: readers_reading: %d writer_writing: %d\n",
77 e->e_rdwr.readers_reading, e->e_rdwr.writer_writing, 0);
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 %lu (%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 /* XXX returns entry with reader/writer lock */
104 id2entry( 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 0x%lx (cache)\n",
119 rw ? "w" : "r", (unsigned long)e, 0 );
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 /* acquire required reader/writer lock */
154 if (entry_rdwr_lock(e, rw)) {
155 /* XXX set DELETE flag?? */
161 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
163 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
164 rw ? "w" : "r", id, 0 );
169 id2entry_r( Backend *be, ID id )
171 return( id2entry( be, id, 0 ) );
175 id2entry_w( Backend *be, ID id )
177 return( id2entry( be, id, 1 ) );