]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/id2entry.c
Use #ifdef, not #if
[openldap] / servers / slapd / back-bdb2 / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
2 /* $OpenLDAP$ */
3
4 #include "portable.h"
5
6 #include <stdio.h>
7
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-bdb2.h"
12
13 /*
14  * This routine adds (or updates) an entry on disk.
15  * The cache should already be updated.
16  */
17
18 int
19 bdb2i_id2entry_add( BackendDB *be, Entry *e )
20 {
21         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
22         struct dbcache  *db;
23         Datum           key, data;
24         int             len, rc, flags;
25
26         ldbm_datum_init( key );
27         ldbm_datum_init( data );
28
29         Debug( LDAP_DEBUG_TRACE, "=> bdb2i_id2entry_add( %ld, \"%s\" )\n", e->e_id,
30             e->e_dn, 0 );
31
32         if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
33             == NULL ) {
34                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
35                     BDB2_SUFFIX, 0, 0 );
36                 return( -1 );
37         }
38
39         key.dptr = (char *) &e->e_id;
40         key.dsize = sizeof(ID);
41
42         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
43         data.dptr = entry2str( e, &len );
44         data.dsize = len + 1;
45
46         /* store it */
47         flags = LDBM_REPLACE;
48         rc = bdb2i_cache_store( db, key, data, flags );
49
50         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
51
52         bdb2i_cache_close( be, db );
53
54         Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_add %d\n", rc, 0, 0 );
55
56         return( rc );
57 }
58
59 int
60 bdb2i_id2entry_delete( BackendDB *be, Entry *e )
61 {
62         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
63         struct dbcache  *db;
64         Datum           key;
65         int             rc;
66
67         Debug(LDAP_DEBUG_TRACE, "=> bdb2i_id2entry_delete( %ld, \"%s\" )\n", e->e_id,
68             e->e_dn, 0 );
69
70 #ifdef notdef
71 #ifdef LDAP_DEBUG
72         /* check for writer lock */
73         assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
74 #endif
75 #endif
76
77         ldbm_datum_init( key );
78
79         if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
80                 == NULL ) {
81                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
82                     BDB2_SUFFIX, 0, 0 );
83                 return( -1 );
84         }
85
86         if ( bdb2i_cache_delete_entry( &li->li_cache, e ) != 0 ) {
87                 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
88                     e->e_id, e->e_dn, 0 );
89         }
90
91         key.dptr = (char *) &e->e_id;
92         key.dsize = sizeof(ID);
93
94         rc = bdb2i_cache_delete( db, key );
95
96         bdb2i_cache_close( be, db );
97
98         Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_delete %d\n", rc, 0, 0 );
99         return( rc );
100 }
101
102 /* returns entry with reader/writer lock */
103 Entry *
104 bdb2i_id2entry_rw( BackendDB *be, ID id, int rw )
105 {
106         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
107         struct dbcache  *db;
108         Datum           key, data;
109         Entry           *e;
110
111         ldbm_datum_init( key );
112         ldbm_datum_init( data );
113
114         Debug( LDAP_DEBUG_TRACE, "=> bdb2i_id2entry_%s( %ld )\n",
115                 rw ? "w" : "r", id, 0 );
116
117         if ( (e = bdb2i_cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
118                 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) 0x%lx (cache)\n",
119                         rw ? "w" : "r", id, (unsigned long)e );
120                 return( e );
121         }
122
123         if ( (db = bdb2i_cache_open( be, "id2entry", BDB2_SUFFIX, LDBM_WRCREAT ))
124                 == NULL ) {
125                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
126                     BDB2_SUFFIX, 0, 0 );
127                 return( NULL );
128         }
129
130         key.dptr = (char *) &id;
131         key.dsize = sizeof(ID);
132
133         data = bdb2i_cache_fetch( db, key );
134
135         if ( data.dptr == NULL ) {
136                 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) not found\n",
137                         rw ? "w" : "r", id, 0 );
138                 bdb2i_cache_close( be, db );
139                 return( NULL );
140         }
141
142         e = str2entry( data.dptr );
143
144         ldbm_datum_free( db->dbc_db, data );
145         bdb2i_cache_close( be, db );
146
147         if ( e == NULL ) {
148                 Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld )  (failed)\n",
149                         rw ? "w" : "r", id, 0 );
150                 return( NULL );
151         }
152
153         e->e_id = id; 
154
155         if ( bdb2i_cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
156                 entry_free( e );
157
158                 /* see if it got added underneath us */
159                 if((e = bdb2i_cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
160                         Debug( LDAP_DEBUG_TRACE,
161                                 "<= bdb2i_id2entry_%s( %ld ) 0x%lx (cache)\n",
162                                 rw ? "w" : "r", id, (unsigned long)e );
163                         return( e );
164                 }
165
166                 Debug( LDAP_DEBUG_TRACE,
167                         "<= bdb2i_id2entry_%s( %ld ) (cache add failed)\n",
168                         rw ? "w" : "r", id, 0 );
169                 return( NULL );
170         }
171
172         Debug( LDAP_DEBUG_TRACE, "<= bdb2i_id2entry_%s( %ld ) 0x%lx (disk)\n",
173                 rw ? "w" : "r", id, (unsigned long) e );
174
175         return( e );
176 }
177
178