]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
Update for Alpha3 from -devel as of OPENLDAP_DEVEL_981116.
[openldap] / servers / slapd / back-ldbm / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8
9 #include "slap.h"
10 #include "back-ldbm.h"
11
12 int
13 id2entry_add( Backend *be, Entry *e )
14 {
15         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
16         struct dbcache  *db;
17         Datum           key, data;
18         int             len, rc, flags;
19
20 #ifdef HAVE_BERKELEY_DB2
21         memset( &key, 0, sizeof( key ) );
22         memset( &data, 0, sizeof( data ) );
23 #endif
24
25         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %lu, \"%s\" )\n", e->e_id,
26             e->e_dn, 0 );
27
28         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
29             == NULL ) {
30                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
31                     LDBM_SUFFIX, 0, 0 );
32                 return( -1 );
33         }
34
35         key.dptr = (char *) &e->e_id;
36         key.dsize = sizeof(ID);
37
38         pthread_mutex_lock( &entry2str_mutex );
39         data.dptr = entry2str( e, &len, 1 );
40         data.dsize = len + 1;
41
42         /* store it */
43         flags = LDBM_REPLACE;
44         if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
45         rc = ldbm_cache_store( db, key, data, flags );
46
47         pthread_mutex_unlock( &entry2str_mutex );
48
49         ldbm_cache_close( be, db );
50         (void) cache_add_entry_lock( &li->li_cache, e, 0 );
51
52         Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
53
54         /* XXX should entries be born locked, i.e. apply writer lock here? */
55         return( rc );
56 }
57
58 int
59 id2entry_delete( Backend *be, Entry *e )
60 {
61         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
62         struct dbcache  *db;
63         Datum           key;
64         int             rc;
65
66         Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %lu, \"%s\" )\n", e->e_id,
67             e->e_dn, 0 );
68
69         /* XXX - check for writer lock - should also check no reader pending */
70 #ifdef LDAP_DEBUG
71         assert(pthread_rdwr_wchk_np(&e->e_rdwr));
72 #endif
73
74 #ifdef HAVE_BERKELEY_DB2
75         memset( &key, 0, sizeof( key ) );
76 #endif
77
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);
82  
83         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
84                 == NULL ) {
85                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
86                     LDBM_SUFFIX, 0, 0 );
87                 return( -1 );
88         }
89
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 );
93         }
94
95         key.dptr = (char *) &e->e_id;
96         key.dsize = sizeof(ID);
97
98         rc = ldbm_cache_delete( db, key );
99
100         ldbm_cache_close( be, db );
101
102         Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
103         return( rc );
104 }
105
106 /* XXX returns entry with reader/writer lock */
107 Entry *
108 id2entry( Backend *be, ID id, int rw )
109 {
110         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
111         struct dbcache  *db;
112         Datum           key, data;
113         Entry           *e;
114
115 #ifdef HAVE_BERKELEY_DB2
116         memset( &key, 0, sizeof( key ) );
117         memset( &data, 0, sizeof( data ) );
118 #endif
119
120         Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
121                 rw ? "w" : "r", id, 0 );
122
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 );
126                 return( e );
127         }
128
129         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
130                 == NULL ) {
131                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
132                     LDBM_SUFFIX, 0, 0 );
133                 return( NULL );
134         }
135
136         key.dptr = (char *) &id;
137         key.dsize = sizeof(ID);
138
139         data = ldbm_cache_fetch( db, key );
140
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 );
145                 return( NULL );
146         }
147
148         e = str2entry( data.dptr );
149
150         ldbm_datum_free( db->dbc_db, data );
151         ldbm_cache_close( be, db );
152
153         if ( e == NULL ) {
154                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld )  (failed)\n",
155                         rw ? "w" : "r", id, 0 );
156                 return( NULL );
157         }
158
159         /* acquire required reader/writer lock */
160         if (entry_rdwr_lock(e, rw)) {
161                 /* XXX set DELETE flag?? */
162                 entry_free(e);
163                 return(NULL);
164         }
165
166         e->e_id = id;
167         (void) cache_add_entry_lock( &li->li_cache, e, 0 );
168
169         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
170                 rw ? "w" : "r", id, 0 );
171         return( e );
172 }
173
174 Entry *
175 id2entry_r( Backend *be, ID id )
176 {
177         return( id2entry( be, id, 0 ) );
178 }
179
180 Entry *
181 id2entry_w( Backend *be, ID id )
182 {
183         return( id2entry( be, id, 1 ) );
184 }
185