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 ldap_pvt_thread_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 ldap_pvt_thread_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,
68 /* check for writer lock */
69 assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
72 ldbm_datum_init( key );
74 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
76 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
81 if ( 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 = ldbm_cache_delete( db, key );
91 ldbm_cache_close( be, db );
93 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
97 /* XXX returns entry with reader/writer lock */
99 id2entry( Backend *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, "=> id2entry_%s( %ld )\n",
110 rw ? "w" : "r", id, 0 );
112 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
113 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s 0x%lx (cache)\n",
114 rw ? "w" : "r", (unsigned long)e, 0 );
118 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_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 = ldbm_cache_fetch( db, key );
130 if ( data.dptr == NULL ) {
131 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
132 rw ? "w" : "r", id, 0 );
133 ldbm_cache_close( be, db );
137 e = str2entry( data.dptr );
139 ldbm_datum_free( db->dbc_db, data );
140 ldbm_cache_close( be, db );
143 Debug( LDAP_DEBUG_TRACE, "<= 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) cache_add_entry_lock( &li->li_cache, e, 0 );
158 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
159 rw ? "w" : "r", id, 0 );
164 id2entry_r( Backend *be, ID id )
166 return( id2entry( be, id, 0 ) );
170 id2entry_w( Backend *be, ID id )
172 return( id2entry( be, id, 1 ) );