1 /* id2entry.c - routines to deal with the id2entry database */
4 * Copyright 1998-2003 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);
87 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
88 DB *db = bdb->bi_id2entry->bdi_db;
96 key.data = (char *) &id;
97 key.size = sizeof(ID);
100 data.flags = DB_DBT_MALLOC;
102 if ((*e = bdb_cache_find_entry_id(bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock)) != NULL) {
107 rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags | ( rw ? DB_RMW : 0 ));
113 DBT2bv( &data, &bv );
115 rc = entry_decode( &bv, e );
120 /* only free on error. On success, the entry was
123 ch_free( data.data );
128 bdb_fix_dn(be, id, *e);
130 ret = bdb_cache_add_entry_rw( bdb->bi_dbenv,
131 &bdb->bi_cache, *e, rw, locker, lock);
132 while ( ret == 1 || ret == -1 ) {
134 int add_loop_cnt = 0;
135 if ( (*e)->e_private != NULL ) {
136 free ((*e)->e_private);
138 (*e)->e_private = NULL;
139 if ( (ee = bdb_cache_find_entry_id
140 (bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock) ) != NULL) {
141 bdb_entry_return ( *e );
145 if ( ++add_loop_cnt == BDB_MAX_ADD_LOOP ) {
146 bdb_entry_return ( *e );
152 if ( (*e)->e_private != NULL )
153 free ( (*e)->e_private );
154 bdb_entry_return( *e );
161 bdb_cache_entry_commit(*e);
167 int bdb_id2entry_delete(
172 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
173 DB *db = bdb->bi_id2entry->bdi_db;
177 bdb_cache_delete_entry(&bdb->bi_cache, e);
180 key.data = (char *) &e->e_id;
181 key.size = sizeof(ID);
183 /* delete from database */
184 rc = db->del( db, tid, &key, 0 );
189 int bdb_entry_return(
192 /* Our entries are allocated in two blocks; the data comes from
193 * the db itself and the Entry structure and associated pointers
194 * are allocated in entry_decode. The db data pointer is saved
195 * in e_bv. Since the Entry structure is allocated as a single
196 * block, e_attrs is always a fixed offset from e. The exception
197 * is when an entry has been modified, in which case we also need
200 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
204 if( (void *) e->e_attrs != (void *) (e+1)) {
205 attrs_free( e->e_attrs );
209 /* nvals are not contiguous with the rest. oh well. */
211 for (a = e->e_attrs; a; a=a->a_next) {
213 ber_bvarray_free( a->a_nvals );
221 /* See if the DNs were changed by modrdn */
222 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
223 e->e_bv.bv_val + e->e_bv.bv_len ) {
224 ch_free(e->e_name.bv_val);
225 ch_free(e->e_nname.bv_val);
226 e->e_name.bv_val = NULL;
227 e->e_nname.bv_val = NULL;
230 /* We had to construct the dn and ndn as well, in a single block */
231 if( e->e_name.bv_val ) {
232 free( e->e_name.bv_val );
235 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
236 if( !(slapMode & SLAP_TOOL_MODE) ) {
237 free( e->e_bv.bv_val );
245 int bdb_entry_release(
252 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
254 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
255 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
257 if ( slapMode == SLAP_SERVER_MODE ) {
258 /* free entry and reader or writer lock */
259 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
261 if (e->e_private != NULL)
264 bdb_entry_return ( e );