1 /* id2entry.c - routines to deal with the id2entry index */
5 #include <sys/socket.h>
9 extern struct dbcache *ldbm_cache_open();
10 extern Datum ldbm_cache_fetch();
11 extern char *dn_parent();
12 extern Entry *str2entry();
13 extern char *entry2str();
14 extern pthread_mutex_t entry2str_mutex;
17 id2entry_add( Backend *be, Entry *e )
19 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25 memset( &key, 0, sizeof( key ) );
26 memset( &data, 0, sizeof( data ) );
29 Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %d, \"%s\" )\n", e->e_id,
32 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
34 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
39 key.dptr = (char *) &e->e_id;
40 key.dsize = sizeof(ID);
42 pthread_mutex_lock( &entry2str_mutex );
43 data.dptr = entry2str( e, &len, 1 );
48 if ( li->li_flush_wrt ) flags |= LDBM_SYNC;
49 rc = ldbm_cache_store( db, key, data, flags );
51 pthread_mutex_unlock( &entry2str_mutex );
53 ldbm_cache_close( be, db );
54 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
56 Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
58 /* XXX should entries be born locked, i.e. apply writer lock here? */
63 id2entry_delete( Backend *be, Entry *e )
65 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
70 Debug( LDAP_DEBUG_TRACE, "=> id2entry_delete( %d, \"%s\" )\n", e->e_id,
73 /* XXX - check for writer lock - should also check no reader pending */
74 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
77 memset( &key, 0, sizeof( key ) );
80 /* XXX - check for writer lock - should also check no reader pending */
81 Debug (LDAP_DEBUG_TRACE,
82 "rdwr_Xchk: readers_reading: %d writer_writing: %d\n",
83 e->e_rdwr.readers_reading, e->e_rdwr.writer_writing, 0);
85 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
87 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
92 if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
93 Debug( LDAP_DEBUG_ANY, "could not delete %d (%s) from cache\n",
94 e->e_id, e->e_dn, 0 );
97 key.dptr = (char *) &e->e_id;
98 key.dsize = sizeof(ID);
100 rc = ldbm_cache_delete( db, key );
102 ldbm_cache_close( be, db );
104 Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
108 /* XXX returns entry with reader/writer lock */
110 id2entry( Backend *be, ID id, int rw )
112 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
118 memset( &key, 0, sizeof( key ) );
119 memset( &data, 0, sizeof( data ) );
122 Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
123 rw ? "w" : "r", id, 0 );
125 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
126 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s 0x%x (cache)\n",
127 rw ? "w" : "r", e, 0 );
131 if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
133 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
138 key.dptr = (char *) &id;
139 key.dsize = sizeof(ID);
141 data = ldbm_cache_fetch( db, key );
143 if ( data.dptr == NULL ) {
144 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
145 rw ? "w" : "r", id, 0 );
146 ldbm_cache_close( be, db );
150 e = str2entry( data.dptr );
152 ldbm_datum_free( db->dbc_db, data );
153 ldbm_cache_close( be, db );
156 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
157 rw ? "w" : "r", id, 0 );
161 /* acquire required reader/writer lock */
162 if (entry_rdwr_lock(e, rw)) {
163 /* XXX set DELETE flag?? */
169 (void) cache_add_entry_lock( &li->li_cache, e, 0 );
171 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
172 rw ? "w" : "r", id, 0 );
177 id2entry_r( Backend *be, ID id )
179 return( id2entry( be, id, 0 ) );
183 id2entry_2( Backend *be, ID id )
185 return( id2entry( be, id, 1 ) );