]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
b7789a647c971cc194d0ec12578fee8d69477539
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 int bdb_id2entry_put(
16         BackendDB *be,
17         DB_TXN *tid,
18         Entry *e,
19         int flag )
20 {
21         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
22         DB *db = bdb->bi_id2entry->bdi_db;
23         DBT key, data;
24         struct berval bv;
25         int rc;
26 #ifdef BDB_HIER
27         char *odn, *ondn;
28
29         /* We only store rdns, and they go in the id2parent database. */
30
31         odn = e->e_dn; ondn = e->e_ndn;
32
33         e->e_dn = ""; e->e_ndn = "";
34 #endif
35         DBTzero( &key );
36         key.data = (char *) &e->e_id;
37         key.size = sizeof(ID);
38
39         rc = entry_encode( e, &bv );
40 #ifdef BDB_HIER
41         e->e_dn = odn; e->e_ndn = ondn;
42 #endif
43         if( rc != LDAP_SUCCESS ) {
44                 return -1;
45         }
46
47         DBTzero( &data );
48         bv2DBT( &bv, &data );
49
50         rc = db->put( db, tid, &key, &data, flag );
51
52         free( bv.bv_val );
53         return rc;
54 }
55
56 /*
57  * This routine adds (or updates) an entry on disk.
58  * The cache should be already be updated.
59  */
60
61
62 int bdb_id2entry_add(
63         BackendDB *be,
64         DB_TXN *tid,
65         Entry *e )
66 {
67         return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
68 }
69
70 int bdb_id2entry_update(
71         BackendDB *be,
72         DB_TXN *tid,
73         Entry *e )
74 {
75         return bdb_id2entry_put(be, tid, e, 0);
76 }
77
78 int bdb_id2entry_rw(
79         BackendDB *be,
80         DB_TXN *tid,
81         ID id,
82         Entry **e,
83         int rw )
84 {
85         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
86         DB *db = bdb->bi_id2entry->bdi_db;
87         DBT key, data;
88         struct berval bv;
89         int rc = 0;
90
91         *e = NULL;
92
93         DBTzero( &key );
94         key.data = (char *) &id;
95         key.size = sizeof(ID);
96
97         DBTzero( &data );
98         data.flags = DB_DBT_MALLOC;
99
100         if ((*e = bdb_cache_find_entry_id(&bdb->bi_cache, id, rw)) != NULL) {
101                 return 0;
102         }
103
104         /* fetch it */
105         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
106
107         if( rc != 0 ) {
108                 return rc;
109         }
110
111         DBT2bv( &data, &bv );
112
113         rc = entry_decode( &bv, e );
114
115         if( rc == 0 ) {
116                 (*e)->e_id = id;
117         } else {
118                 /* only free on error. On success, the entry was
119                  * decoded in place.
120                  */
121                 ch_free( data.data );
122         }
123
124         while (rc == 0 && bdb_cache_add_entry_rw(&bdb->bi_cache, *e, rw) != 0) {
125                 Entry *ee;
126                 int add_loop_cnt = 0;
127                 if ( (*e)->e_private != NULL ) {
128                         free ((*e)->e_private);
129                 }
130                 (*e)->e_private = NULL;
131                 if ( (ee = bdb_cache_find_entry_id
132                                 (&bdb->bi_cache, id, rw) ) != NULL) {
133                         bdb_entry_return ( *e );
134                         *e = ee;
135                         return 0;
136                 }
137                 if ( ++add_loop_cnt == BDB_MAX_ADD_LOOP ) {
138                         bdb_entry_return ( *e );
139                         *e = NULL;
140                         return LDAP_BUSY;
141                 }
142         }
143
144 #ifdef BDB_HIER
145         bdb_fix_dn(be, id, *e);
146 #endif
147
148         if (rc == 0) {
149                 bdb_cache_entry_commit(*e);
150         }
151
152         return rc;
153 }
154
155 int bdb_id2entry_delete(
156         BackendDB *be,
157         DB_TXN *tid,
158         Entry *e )
159 {
160         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
161         DB *db = bdb->bi_id2entry->bdi_db;
162         DBT key;
163         int rc;
164
165         bdb_cache_delete_entry(&bdb->bi_cache, e);
166
167         DBTzero( &key );
168         key.data = (char *) &e->e_id;
169         key.size = sizeof(ID);
170
171         /* delete from database */
172         rc = db->del( db, tid, &key, 0 );
173
174         return rc;
175 }
176
177 int bdb_entry_return(
178         Entry *e )
179 {
180         /* Our entries are allocated in two blocks; the data comes from
181          * the db itself and the Entry structure and associated pointers
182          * are allocated in entry_decode. The db data pointer is saved
183          * in e_bv. Since the Entry structure is allocated as a single
184          * block, e_attrs is always a fixed offset from e. The exception
185          * is when an entry has been modified, in which case we also need
186          * to free e_attrs.
187          */
188         if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
189                 entry_free( e );
190                 return 0;
191         }
192         if( (void *) e->e_attrs != (void *) (e+1)) {
193                 attrs_free( e->e_attrs );
194         }
195
196         /* See if the DNs were changed by modrdn */
197         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
198                 e->e_bv.bv_val + e->e_bv.bv_len ) {
199                 ch_free(e->e_name.bv_val);
200                 ch_free(e->e_nname.bv_val);
201                 e->e_name.bv_val = NULL;
202                 e->e_nname.bv_val = NULL;
203         }
204 #ifdef BDB_HIER
205         /* We had to construct the dn and ndn as well, in a single block */
206         if( e->e_name.bv_val ) {
207                 free( e->e_name.bv_val );
208         }
209 #endif
210         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
211         if( !(slapMode & SLAP_TOOL_MODE) ) {
212                 free( e->e_bv.bv_val );
213         }
214
215         free( e );
216
217         return 0;
218 }
219
220 int bdb_entry_release(
221         BackendDB *be,
222         Connection *c,
223         Operation *o,
224         Entry *e,
225         int rw )
226 {
227         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
228  
229         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
230                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
231  
232         if ( slapMode == SLAP_SERVER_MODE ) {
233                 /* free entry and reader or writer lock */
234                 bdb_cache_return_entry_rw( &bdb->bi_cache, e, rw );
235         } else {
236                 if (e->e_private != NULL)
237                         free (e->e_private);
238                 e->e_private = NULL;
239                 bdb_entry_return ( e );
240         }
241  
242         return 0;
243 }