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