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);
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 );
156 ch_free( data.data );
162 bdb_cache_entry_commit(*e);
168 int bdb_id2entry_delete(
173 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
174 DB *db = bdb->bi_id2entry->bdi_db;
178 bdb_cache_delete_entry(&bdb->bi_cache, e);
181 key.data = (char *) &e->e_id;
182 key.size = sizeof(ID);
184 /* delete from database */
185 rc = db->del( db, tid, &key, 0 );
190 int bdb_entry_return(
193 /* Our entries are allocated in two blocks; the data comes from
194 * the db itself and the Entry structure and associated pointers
195 * are allocated in entry_decode. The db data pointer is saved
196 * in e_bv. Since the Entry structure is allocated as a single
197 * block, e_attrs is always a fixed offset from e. The exception
198 * is when an entry has been modified, in which case we also need
201 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
205 if( (void *) e->e_attrs != (void *) (e+1)) {
206 attrs_free( e->e_attrs );
210 /* See if the DNs were changed by modrdn */
211 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
212 e->e_bv.bv_val + e->e_bv.bv_len ) {
213 ch_free(e->e_name.bv_val);
214 ch_free(e->e_nname.bv_val);
215 e->e_name.bv_val = NULL;
216 e->e_nname.bv_val = NULL;
219 /* We had to construct the dn and ndn as well, in a single block */
220 if( e->e_name.bv_val ) {
221 free( e->e_name.bv_val );
224 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
225 if( !(slapMode & SLAP_TOOL_MODE) ) {
226 free( e->e_bv.bv_val );
234 int bdb_entry_release(
241 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
243 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
244 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
246 if ( slapMode == SLAP_SERVER_MODE ) {
247 /* free entry and reader or writer lock */
248 bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
250 if (e->e_private != NULL)
253 bdb_entry_return ( e );