1 /* id2entry.c - routines to deal with the id2entry database */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
21 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
22 DB *db = bdb->bi_id2entry->bdi_db;
29 /* We only store rdns, and they go in the id2parent database. */
31 odn = e->e_dn; ondn = e->e_ndn;
33 e->e_dn = ""; e->e_ndn = "";
36 key.data = (char *) &e->e_id;
37 key.size = sizeof(ID);
39 rc = entry_encode( e, &bv );
41 e->e_dn = odn; e->e_ndn = ondn;
43 if( rc != LDAP_SUCCESS ) {
50 rc = db->put( db, tid, &key, &data, flag );
57 * This routine adds (or updates) an entry on disk.
58 * The cache should be already be updated.
67 return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
70 int bdb_id2entry_update(
75 return bdb_id2entry_put(be, tid, e, 0);
85 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
86 DB *db = bdb->bi_id2entry->bdi_db;
94 key.data = (char *) &id;
95 key.size = sizeof(ID);
98 data.flags = DB_DBT_MALLOC;
100 if ((*e = bdb_cache_find_entry_id(&bdb->bi_cache, id, rw)) != NULL) {
105 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
111 DBT2bv( &data, &bv );
113 rc = entry_decode( &bv, e );
118 /* only free on error. On success, the entry was
121 ch_free( data.data );
124 if (rc == 0 && bdb_cache_add_entry_rw(&bdb->bi_cache, *e, rw) != 0) {
125 if ((*e)->e_private != NULL)
126 free ((*e)->e_private);
127 (*e)->e_private = NULL;
128 bdb_entry_return (*e);
129 if ((*e=bdb_cache_find_entry_id(&bdb->bi_cache,id,rw)) != NULL) {
135 bdb_fix_dn(be, id, *e);
139 bdb_cache_entry_commit(*e);
144 int bdb_id2entry_delete(
149 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
150 DB *db = bdb->bi_id2entry->bdi_db;
154 bdb_cache_delete_entry(&bdb->bi_cache, e);
157 key.data = (char *) &e->e_id;
158 key.size = sizeof(ID);
160 /* delete from database */
161 rc = db->del( db, tid, &key, 0 );
166 int bdb_entry_return(
169 /* Our entries are allocated in two blocks; the data comes from
170 * the db itself and the Entry structure and associated pointers
171 * are allocated in entry_decode. The db data pointer is saved
172 * in e_bv. Since the Entry structure is allocated as a single
173 * block, e_attrs is always a fixed offset from e. The exception
174 * is when an entry has been modified, in which case we also need
177 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
181 if( (void *) e->e_attrs != (void *) (e+1)) {
182 attrs_free( e->e_attrs );
185 /* See if the DNs were changed by modrdn */
186 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
187 e->e_bv.bv_val + e->e_bv.bv_len ) {
188 ch_free(e->e_name.bv_val);
189 ch_free(e->e_nname.bv_val);
190 e->e_name.bv_val = NULL;
191 e->e_nname.bv_val = NULL;
194 /* We had to construct the dn and ndn as well, in a single block */
195 if( e->e_name.bv_val ) {
196 free( e->e_name.bv_val );
199 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
200 if( !(slapMode & SLAP_TOOL_MODE) ) {
201 free( e->e_bv.bv_val );
209 int bdb_entry_release(
216 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
218 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
219 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
221 if ( slapMode == SLAP_SERVER_MODE ) {
222 /* free entry and reader or writer lock */
223 bdb_cache_return_entry_rw( &bdb->bi_cache, e, rw );
225 if (e->e_private != NULL)
228 bdb_entry_return ( e );