/* id2entry.c - routines to deal with the id2entry database */
/* $OpenLDAP$ */
/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "back-bdb.h"
-#ifdef BDB_USE_BINARY_RW
-
-/* A cache of every AttributeDescription we ever see. We don't want
- * to churn thru malloc/free on them all the time since most will be
- * encountered repeatedly.
- */
-static Avlnode *adcache;
-
-static int
-ad_type_cmp(
- char *desc,
- AttributeDescription *ad
-)
-{
- return strcasecmp(desc, ad->ad_cname.bv_val);
-}
-
-static int
-ad_info_cmp(
- AttributeDescription *a1,
- AttributeDescription *a2
-)
-{
- return strcasecmp(a1->ad_cname.bv_val, a2->ad_cname.bv_val);
-}
-
-AttributeDescription *
-bdb_str2ad(const char *desc)
-{
- AttributeDescription *a;
-
- a = (AttributeDescription *) avl_find(adcache, desc,
- (AVL_CMP) ad_type_cmp);
- if (!a) {
- int rc;
- const char *text;
-
- rc = slap_str2ad(desc, &a, &text);
- if (rc != LDAP_SUCCESS) {
- return NULL;
- }
- rc = avl_insert(&adcache, (caddr_t)a, (AVL_CMP) ad_info_cmp,
- (AVL_DUP) avl_dup_error);
- }
- return a;
-}
-
-/* Flatten an Entry into a buffer. The buffer contents become a direct
- * copy of the entry, with all pointers converted to offsets from the
- * beginning of the buffer. We do this by first walking through all
- * the fields of the Entry, adding up their sizes. Then a single chunk
- * of memory is malloc'd and the entry is copied. We differentiate between
- * fixed size fields and variable-length content when tallying up the
- * entry size, so that we can stick all of the variable-length stuff
- * into the back half of the buffer.
- */
-int bdb_encode(Entry *e, struct berval **bv)
-{
- int siz = sizeof(Entry);
- int len, dnlen;
- int i, j;
- Entry *f;
- Attribute *a, *b;
- struct berval **bvl, *bz;
- char *ptr, *base, *data;
-
- *bv = ch_malloc(sizeof(struct berval));
- /* Compress any white space in the DN */
- dn_validate(e->e_dn);
- dnlen = strlen(e->e_dn);
- /* The dn and ndn are always the same length */
- len = dnlen + dnlen + 2; /* two trailing NUL bytes */
- for (a=e->e_attrs; a; a=a->a_next) {
- /* For AttributeDesc, we only store the attr name */
- siz += sizeof(Attribute);
- len += a->a_desc->ad_cname.bv_len+1;
- for (i=0; a->a_vals[i]; i++) {
- siz += sizeof(struct berval *);
- siz += sizeof(struct berval);
- len += a->a_vals[i]->bv_len + 1;
- }
- siz += sizeof(struct berval *); /* NULL pointer at end */
- }
- (*bv)->bv_len = siz + len;
- (*bv)->bv_val = ch_malloc(siz+len);
- base = (*bv)->bv_val;
- ptr = base + siz;
- f = (Entry *)base;
- data = (char *)(f+1);
- f->e_id = e->e_id;
- f->e_dn = (char *)(ptr-base);
- memcpy(ptr, e->e_dn, dnlen);
- ptr += dnlen;
- *ptr++ = '\0';
- f->e_ndn = (char *)(ptr-base);
- memcpy(ptr, e->e_ndn, dnlen);
- ptr += dnlen;
- *ptr++ = '\0';
- f->e_attrs = e->e_attrs ? (Attribute *)sizeof(Entry) : NULL;
- f->e_private = NULL;
- for (a=e->e_attrs; a; a=a->a_next) {
- b = (Attribute *)data;
- data = (char *)(b+1);
- b->a_desc = (AttributeDescription *)(ptr-base);
- memcpy(ptr, a->a_desc->ad_cname.bv_val,
- a->a_desc->ad_cname.bv_len);
- ptr += a->a_desc->ad_cname.bv_len;
- *ptr++ = '\0';
- if (a->a_vals) {
- bvl = (struct berval **)data;
- b->a_vals = (struct berval **)(data-base);
- for (i=0; a->a_vals[i]; i++);
- data = (char *)(bvl+i+1);
- bz = (struct berval *)data;
- for (j=0; j<i; j++) {
- bz->bv_len = a->a_vals[j]->bv_len;
- if (a->a_vals[j]->bv_val) {
- bz->bv_val = (char *)(ptr-base);
- memcpy(ptr, a->a_vals[j]->bv_val, bz->bv_len);
- } else {
- bz->bv_val = NULL;
- }
- ptr += bz->bv_len;
- *ptr++ = '\0';
- bvl[j] = (struct berval *)(data-base);
- bz++;
- data = (char *)bz;
- }
- bvl[j] = NULL;
- } else {
- b->a_vals = NULL;
- }
-
- if (a->a_next)
- b->a_next = (Attribute *)(data-base);
- else
- b->a_next = NULL;
- }
- return 0;
-}
-
-/* Retrieve an Entry that was stored using bdb_encode above.
- * All we have to do is add the buffer address to all of the
- * stored offsets. We also use the stored attribute names to
- * pull AttributeDescriptions from our ad_cache. To detect if
- * the attributes of an Entry are later modified, we also store
- * the address of the end of this block in e_private. Since
- * modify_internal always allocs a new list of attrs to work
- * with, we need to free that separately.
- */
-int bdb_decode(struct berval *bv, Entry **e)
-{
- int i;
- long base;
- Attribute *a;
- Entry *x = (Entry *)bv->bv_val;
-
- base = (long)bv->bv_val;
- x->e_dn += base;
- x->e_ndn += base;
- x->e_private = bv->bv_val + bv->bv_len;
- if (x->e_attrs)
- x->e_attrs = (Attribute *)((long)x->e_attrs+base);
- for (a=x->e_attrs; a; a=a->a_next) {
- if (a->a_next)
- a->a_next = (Attribute *)((long)a->a_next+base);
- a->a_desc=bdb_str2ad((char *)a->a_desc+base);
- if (!a->a_desc) return -1;
- if (a->a_vals) {
- a->a_vals = (struct berval **)((long)a->a_vals+base);
- for (i=0; a->a_vals[i]; i++) {
- a->a_vals[i] = (struct berval *)
- ((long)a->a_vals[i]+base);
- if (a->a_vals[i]->bv_val)
- a->a_vals[i]->bv_val += base;
- }
- }
- }
- *e = x;
- return 0;
-}
-
-#define entry_encode(a, b) bdb_encode(a,b)
-#define entry_decode(a, b) bdb_decode(a,b)
-
-#endif /* BDB_USE_BINARY_RW */
-
-int bdb_id2entry_add(
+int bdb_id2entry_put(
BackendDB *be,
DB_TXN *tid,
- Entry *e )
+ Entry *e,
+ int flag )
{
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_id2entry->bdi_db;
DBT key, data;
- struct berval *bv;
+ struct berval bv;
int rc;
+#ifdef BDB_HIER
+ char *odn, *ondn;
+
+ /* We only store rdns, and they go in the id2parent database. */
+ odn = e->e_dn; ondn = e->e_ndn;
+
+ e->e_dn = ""; e->e_ndn = "";
+#endif
DBTzero( &key );
key.data = (char *) &e->e_id;
key.size = sizeof(ID);
rc = entry_encode( e, &bv );
+#ifdef BDB_HIER
+ e->e_dn = odn; e->e_ndn = ondn;
+#endif
if( rc != LDAP_SUCCESS ) {
return -1;
}
DBTzero( &data );
- bv2DBT( bv, &data );
+ bv2DBT( &bv, &data );
- rc = db->put( db, tid, &key, &data, DB_NOOVERWRITE );
+ rc = db->put( db, tid, &key, &data, flag );
- ber_bvfree( bv );
+ free( bv.bv_val );
return rc;
}
-int bdb_id2entry_update(
+/*
+ * This routine adds (or updates) an entry on disk.
+ * The cache should be already be updated.
+ */
+
+
+int bdb_id2entry_add(
BackendDB *be,
DB_TXN *tid,
Entry *e )
{
- struct bdb_info *bdb = (struct bdb_info *) be->be_private;
- DB *db = bdb->bi_id2entry->bdi_db;
- DBT key, data;
- struct berval *bv;
- int rc;
-
- DBTzero( &key );
- key.data = (char *) &e->e_id;
- key.size = sizeof(ID);
-
- rc = entry_encode( e, &bv );
- if( rc != LDAP_SUCCESS ) {
- return -1;
- }
-
- DBTzero( &data );
- bv2DBT( bv, &data );
-
- rc = db->put( db, tid, &key, &data, 0 );
+ return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
+}
- ber_bvfree( bv );
- return rc;
+int bdb_id2entry_update(
+ BackendDB *be,
+ DB_TXN *tid,
+ Entry *e )
+{
+ return bdb_id2entry_put(be, tid, e, 0);
}
-int bdb_id2entry(
+int bdb_id2entry_rw(
BackendDB *be,
DB_TXN *tid,
ID id,
- Entry **e )
+ Entry **e,
+ int rw,
+ u_int32_t locker,
+ DB_LOCK *lock )
{
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_id2entry->bdi_db;
DBT key, data;
struct berval bv;
- int rc;
+ int rc = 0, ret = 0;
*e = NULL;
DBTzero( &data );
data.flags = DB_DBT_MALLOC;
+ if ((*e = bdb_cache_find_entry_id(bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock)) != NULL) {
+ return 0;
+ }
+
/* fetch it */
- rc = db->get( db, tid, &key, &data, 0 );
+ rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags | ( rw ? DB_RMW : 0 ));
if( rc != 0 ) {
return rc;
if( rc == 0 ) {
(*e)->e_id = id;
+ } else {
+ /* only free on error. On success, the entry was
+ * decoded in place.
+ */
+ ch_free( data.data );
+ }
+
+ if ( rc == 0 ) {
+ ret = bdb_cache_add_entry_rw( bdb->bi_dbenv,
+ &bdb->bi_cache, *e, rw, locker, lock);
+ while ( ret == 1 || ret == -1 ) {
+ Entry *ee;
+ int add_loop_cnt = 0;
+ if ( (*e)->e_private != NULL ) {
+ free ((*e)->e_private);
+ }
+ (*e)->e_private = NULL;
+ if ( (ee = bdb_cache_find_entry_id
+ (bdb->bi_dbenv, &bdb->bi_cache, id, rw, locker, lock) ) != NULL) {
+ bdb_entry_return ( *e );
+ *e = ee;
+ return 0;
+ }
+ if ( ++add_loop_cnt == BDB_MAX_ADD_LOOP ) {
+ bdb_entry_return ( *e );
+ *e = NULL;
+ return LDAP_BUSY;
+ }
+ }
+ if ( ret != 0 ) {
+ if ( (*e)->e_private != NULL )
+ free ( (*e)->e_private );
+ bdb_entry_return( *e );
+ *e = NULL;
+ ch_free( data.data );
+ }
+ rc = ret;
}
-#ifndef BDB_USE_BINARY_RW
- ch_free( data.data );
+#ifdef BDB_HIER
+ bdb_fix_dn(be, id, *e);
#endif
+
+ if (rc == 0) {
+ bdb_cache_entry_commit(*e);
+ }
+
return rc;
}
int bdb_id2entry_delete(
BackendDB *be,
DB_TXN *tid,
- ID id )
+ Entry *e )
{
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_id2entry->bdi_db;
DBT key;
- struct berval *bv;
int rc;
+ bdb_cache_delete_entry(&bdb->bi_cache, e);
+
DBTzero( &key );
- key.data = (char *) &id;
+ key.data = (char *) &e->e_id;
key.size = sizeof(ID);
+ /* delete from database */
rc = db->del( db, tid, &key, 0 );
- ber_bvfree( bv );
return rc;
}
int bdb_entry_return(
- BackendDB *be,
Entry *e )
{
-#ifdef BDB_USE_BINARY_RW
- /* bdb_modify_internal always operates on a dup'd set of attrs. */
- if ((void *)e->e_attrs < (void *)e ||
- (void *)e->e_attrs > e->e_private)
- attrs_free(e->e_attrs);
- ch_free(e);
+ /* Our entries are allocated in two blocks; the data comes from
+ * the db itself and the Entry structure and associated pointers
+ * are allocated in entry_decode. The db data pointer is saved
+ * in e_bv. Since the Entry structure is allocated as a single
+ * block, e_attrs is always a fixed offset from e. The exception
+ * is when an entry has been modified, in which case we also need
+ * to free e_attrs.
+ */
+ if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
+ entry_free( e );
+ return 0;
+ }
+ if( (void *) e->e_attrs != (void *) (e+1)) {
+ attrs_free( e->e_attrs );
+ }
+
+#ifndef BDB_HIER
+ /* See if the DNs were changed by modrdn */
+ if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
+ e->e_bv.bv_val + e->e_bv.bv_len ) {
+ ch_free(e->e_name.bv_val);
+ ch_free(e->e_nname.bv_val);
+ e->e_name.bv_val = NULL;
+ e->e_nname.bv_val = NULL;
+ }
#else
- entry_free( e );
+ /* We had to construct the dn and ndn as well, in a single block */
+ if( e->e_name.bv_val ) {
+ free( e->e_name.bv_val );
+ }
#endif
+ /* In tool mode the e_bv buffer is realloc'd, leave it alone */
+ if( !(slapMode & SLAP_TOOL_MODE) ) {
+ free( e->e_bv.bv_val );
+ }
+
+ free( e );
+
+ return 0;
+}
+
+int bdb_entry_release(
+ BackendDB *be,
+ Connection *c,
+ Operation *o,
+ Entry *e,
+ int rw )
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+
+ /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
+ SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
+
+ if ( slapMode == SLAP_SERVER_MODE ) {
+ /* free entry and reader or writer lock */
+ bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
+ } else {
+ if (e->e_private != NULL)
+ free (e->e_private);
+ e->e_private = NULL;
+ bdb_entry_return ( e );
+ }
+
return 0;
}