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 while (rc == 0 && bdb_cache_add_entry_rw(&bdb->bi_cache, *e, rw) != 0) {
126 int add_loop_cnt = 0;
127 if ( (*e)->e_private != NULL ) {
128 free ((*e)->e_private);
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 );
137 if ( ++add_loop_cnt == BDB_MAX_ADD_LOOP ) {
138 bdb_entry_return ( *e );
145 bdb_fix_dn(be, id, *e);
149 bdb_cache_entry_commit(*e);
155 int bdb_id2entry_delete(
160 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
161 DB *db = bdb->bi_id2entry->bdi_db;
165 bdb_cache_delete_entry(&bdb->bi_cache, e);
168 key.data = (char *) &e->e_id;
169 key.size = sizeof(ID);
171 /* delete from database */
172 rc = db->del( db, tid, &key, 0 );
177 int bdb_entry_return(
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
188 if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
192 if( (void *) e->e_attrs != (void *) (e+1)) {
193 attrs_free( e->e_attrs );
197 /* See if the DNs were changed by modrdn */
198 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
199 e->e_bv.bv_val + e->e_bv.bv_len ) {
200 ch_free(e->e_name.bv_val);
201 ch_free(e->e_nname.bv_val);
202 e->e_name.bv_val = NULL;
203 e->e_nname.bv_val = NULL;
206 /* We had to construct the dn and ndn as well, in a single block */
207 if( e->e_name.bv_val ) {
208 free( e->e_name.bv_val );
211 /* In tool mode the e_bv buffer is realloc'd, leave it alone */
212 if( !(slapMode & SLAP_TOOL_MODE) ) {
213 free( e->e_bv.bv_val );
221 int bdb_entry_release(
228 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
230 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
231 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
233 if ( slapMode == SLAP_SERVER_MODE ) {
234 /* free entry and reader or writer lock */
235 bdb_cache_return_entry_rw( &bdb->bi_cache, e, rw );
237 if (e->e_private != NULL)
240 bdb_entry_return ( e );