]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/tools.c
Fix ITS#3255, boi_bdb comparisons
[openldap] / servers / slapd / back-bdb / tools.c
index 69aeea22aea59b9ae632faf1da58688638f50679..8898017156db93e9bbf0216b64b087be369fbaf3 100644 (file)
@@ -1,8 +1,17 @@
 /* tools.c - tools for slap tools */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2000-2004 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
@@ -10,6 +19,7 @@
 #include <stdio.h>
 #include <ac/string.h>
 
+#define AVL_INTERNAL
 #include "back-bdb.h"
 #include "external.h"
 
@@ -26,6 +36,8 @@ static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
 static unsigned nhmax = HOLE_SIZE;
 static unsigned nholes;
 
+Avlnode *index_attrs, index_dummy;
+
 int bdb_tool_entry_open(
        BackendDB *be, int mode )
 {
@@ -70,6 +82,8 @@ int bdb_tool_entry_close(
        return 0;
 }
 
+static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
+
 ID bdb_tool_entry_next(
        BackendDB *be )
 {
@@ -81,6 +95,7 @@ ID bdb_tool_entry_next(
        assert( slapMode & SLAP_TOOL_MODE );
        assert( bdb != NULL );
        
+       /* Initialization */
        if (cursor == NULL) {
                rc = bdb->bi_id2entry->bdi_db->cursor(
                        bdb->bi_id2entry->bdi_db, NULL, &cursor,
@@ -93,7 +108,22 @@ ID bdb_tool_entry_next(
        rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
 
        if( rc != 0 ) {
-               return NOID;
+               /* If we're doing linear indexing and there are more attrs to
+                * index, and we're at the end of the database, start over.
+                */
+               if ( bdb->bi_attrs == &index_dummy ) {
+                       if ( index_attrs && rc == DB_NOTFOUND ) {
+                               /* optional - do a checkpoint here? */
+                               index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
+                               rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
+                       }
+                       if ( rc ) {
+                               bdb->bi_attrs = NULL;
+                               return NOID;
+                       }
+               } else {
+                       return NOID;
+               }
        }
 
        if( data.data == NULL ) {
@@ -104,6 +134,54 @@ ID bdb_tool_entry_next(
        return id;
 }
 
+ID bdb_tool_dn2id_get(
+       Backend *be,
+       struct berval *dn
+)
+{
+       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+       DB *db = bdb->bi_dn2id->bdi_db;
+       int rc;
+       DBT     key, data;
+       ID      id;
+
+       DBTzero( &key );
+       key.size = dn->bv_len + 2;
+       key.data = ch_malloc( key.size );
+       ((char*)key.data)[0] = DN_BASE_PREFIX;
+       AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
+
+       DBTzero( &data );
+       data.data = &id;
+       data.ulen = sizeof(ID);
+       data.flags = DB_DBT_USERMEM;
+
+       rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
+
+    if( rc != 0 ) {
+#ifdef NEW_LOGGING
+               LDAP_LOG ( INDEX, ERR, "bdb_tool_dn2id_get: get failed %s (%d)\n",
+                               db_strerror(rc), rc, 0 );
+#else
+               Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
+                               db_strerror( rc ), rc, 0 );
+#endif
+               id = NOID;
+       }
+
+       ch_free( key.data );
+       return id;
+}
+
+int bdb_tool_id2entry_get(
+       Backend *be,
+       ID id,
+       Entry **e
+)
+{
+       return bdb_id2entry( be, NULL, id, e );
+}
+
 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
 {
        int rc;
@@ -144,24 +222,28 @@ static int bdb_tool_next_id(
        DB_TXN *tid,
        Entry *e,
        struct berval *text,
-       int hole,
-       u_int32_t locker )
+       int hole )
 {
        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
-       struct berval dn = e->e_nname;
-       struct berval pdn;
+       struct berval dn = e->e_name;
+       struct berval ndn = e->e_nname;
+       struct berval pdn, npdn;
        EntryInfo *ei = NULL;
        int rc;
 
-       rc = bdb_cache_find_ndn( op, tid, &dn, &ei, locker );
+       if (ndn.bv_len == 0) return 0;
+
+       rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
        if ( ei ) bdb_cache_entryinfo_unlock( ei );
        if ( rc == DB_NOTFOUND ) {
-               if ( be_issuffix( op->o_bd, &dn ) ) {
-                       pdn = slap_empty_bv;
-               } else {
+               if ( !be_issuffix( op->o_bd, &ndn ) ) {
                        dnParent( &dn, &pdn );
-                       e->e_nname = pdn;
-                       rc = bdb_tool_next_id( op, tid, e, text, 1, locker );
+                       dnParent( &ndn, &npdn );
+                       e->e_name = pdn;
+                       e->e_nname = npdn;
+                       rc = bdb_tool_next_id( op, tid, e, text, 1 );
+                       e->e_name = dn;
+                       e->e_nname = ndn;
                        if ( rc ) {
                                return rc;
                        }
@@ -173,14 +255,13 @@ static int bdb_tool_next_id(
                                db_strerror(rc), rc );
 #ifdef NEW_LOGGING
                LDAP_LOG ( TOOLS, ERR, 
-                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+                       "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
 #else
                Debug( LDAP_DEBUG_ANY,
-                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+                       "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
 #endif
                        return rc;
                }
-               e->e_nname = dn;
                rc = bdb_dn2id_add( op, tid, ei, e );
                if ( rc ) {
                        snprintf( text->bv_val, text->bv_len, 
@@ -188,27 +269,29 @@ static int bdb_tool_next_id(
                                db_strerror(rc), rc );
 #ifdef NEW_LOGGING
                LDAP_LOG ( TOOLS, ERR, 
-                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+                       "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
 #else
                Debug( LDAP_DEBUG_ANY,
-                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+                       "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
 #endif
                } else if ( hole ) {
                        if ( nholes == nhmax - 1 ) {
                                if ( holes == hbuf ) {
-                                       holes = ch_malloc( nhmax * sizeof(ID) * 2 );
+                                       holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
                                        AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
                                } else {
-                                       holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
+                                       holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
                                }
                                nhmax *= 2;
                        }
-                       ber_dupbv( &holes[nholes].dn, &dn );
+                       ber_dupbv( &holes[nholes].dn, &ndn );
                        holes[nholes++].id = e->e_id;
                }
        } else if ( !hole ) {
                unsigned i;
 
+               e->e_id = ei->bei_id;
+
                for ( i=0; i<nholes; i++) {
                        if ( holes[i].id == e->e_id ) {
                                int j;
@@ -234,7 +317,6 @@ ID bdb_tool_entry_put(
        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
        DB_TXN *tid = NULL;
        Operation op = {0};
-       u_int32_t locker;
 
        assert( be != NULL );
        assert( slapMode & SLAP_TOOL_MODE );
@@ -271,9 +353,8 @@ ID bdb_tool_entry_put(
        op.o_tmpmemctx = NULL;
        op.o_tmpmfuncs = &ch_mfuncs;
 
-       locker = TXN_ID( tid );
        /* add dn2id indices */
-       rc = bdb_tool_next_id( &op, tid, e, text, 0, locker );
+       rc = bdb_tool_next_id( &op, tid, e, text, 0 );
        if( rc != 0 ) {
                goto done;
        }
@@ -294,7 +375,8 @@ ID bdb_tool_entry_put(
                goto done;
        }
 
-       rc = bdb_index_entry_add( &op, tid, e );
+       if ( !bdb->bi_linear_index )
+               rc = bdb_index_entry_add( &op, tid, e );
        if( rc != 0 ) {
                snprintf( text->bv_val, text->bv_len,
                                "index_entry_add failed: %s (%d)",
@@ -364,6 +446,20 @@ int bdb_tool_entry_reindex(
                (long) id, 0, 0 );
 #endif
 
+       /* No indexes configured, nothing to do. Could return an
+        * error here to shortcut things.
+        */
+       if (!bi->bi_attrs) {
+               return 0;
+       }
+
+       /* Get the first attribute to index */
+       if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
+               index_attrs = bi->bi_attrs;
+               bi->bi_attrs = &index_dummy;
+               index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
+       }
+
        e = bdb_tool_entry_get( be, id );
 
        if( e == NULL ) {
@@ -412,7 +508,7 @@ int bdb_tool_entry_reindex(
        op.o_tmpmemctx = NULL;
        op.o_tmpmfuncs = &ch_mfuncs;
 
-#ifndef BDB_HIER
+#if 0 /* ndef BDB_HIER */
        /* add dn2id indices */
        rc = bdb_dn2id_add( &op, tid, NULL, e );
        if( rc != 0 && rc != DB_KEYEXIST ) {
@@ -464,3 +560,134 @@ done:
 
        return rc;
 }
+
+ID bdb_tool_entry_modify(
+       BackendDB *be,
+       Entry *e,
+       struct berval *text )
+{
+       int rc;
+       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+       DB_TXN *tid = NULL;
+       Operation op = {0};
+
+       assert( be != NULL );
+       assert( slapMode & SLAP_TOOL_MODE );
+
+       assert( text );
+       assert( text->bv_val );
+       assert( text->bv_val[0] == '\0' );      /* overconservative? */
+
+       assert ( e->e_id != NOID );
+       assert ( e->e_id != 0 );
+
+#ifdef NEW_LOGGING
+       LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
+               (long) e->e_id, e->e_dn, 0 );
+#else
+       Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
+               (long) e->e_id, e->e_dn, 0 );
+#endif
+
+       rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
+               bdb->bi_db_opflags );
+       if( rc != 0 ) {
+               snprintf( text->bv_val, text->bv_len,
+                       "txn_begin failed: %s (%d)",
+                       db_strerror(rc), rc );
+#ifdef NEW_LOGGING
+       LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#else
+               Debug( LDAP_DEBUG_ANY,
+                       "=> bdb_tool_entry_put: %s\n",
+                        text->bv_val, 0, 0 );
+#endif
+               return NOID;
+       }
+
+       op.o_bd = be;
+       op.o_tmpmemctx = NULL;
+       op.o_tmpmfuncs = &ch_mfuncs;
+
+       /* id2entry index */
+       rc = bdb_id2entry_update( be, tid, e );
+       if( rc != 0 ) {
+               snprintf( text->bv_val, text->bv_len,
+                               "id2entry_add failed: %s (%d)",
+                               db_strerror(rc), rc );
+#ifdef NEW_LOGGING
+               LDAP_LOG ( TOOLS, ERR, 
+                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#else
+               Debug( LDAP_DEBUG_ANY,
+                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#endif
+               goto done;
+       }
+
+       rc = bdb_index_entry_del( &op, tid, e );
+       if( rc != 0 ) {
+               snprintf( text->bv_val, text->bv_len,
+                               "index_entry_del failed: %s (%d)",
+                               db_strerror(rc), rc );
+#ifdef NEW_LOGGING
+               LDAP_LOG ( TOOLS, ERR, 
+                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#else
+               Debug( LDAP_DEBUG_ANY,
+                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#endif
+               goto done;
+       }
+
+       rc = bdb_index_entry_add( &op, tid, e );
+       if( rc != 0 ) {
+               snprintf( text->bv_val, text->bv_len,
+                               "index_entry_add failed: %s (%d)",
+                               db_strerror(rc), rc );
+#ifdef NEW_LOGGING
+               LDAP_LOG ( TOOLS, ERR, 
+                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#else
+               Debug( LDAP_DEBUG_ANY,
+                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#endif
+               goto done;
+       }
+
+done:
+       if( rc == 0 ) {
+               rc = TXN_COMMIT( tid, 0 );
+               if( rc != 0 ) {
+                       snprintf( text->bv_val, text->bv_len,
+                                       "txn_commit failed: %s (%d)",
+                                       db_strerror(rc), rc );
+#ifdef NEW_LOGGING
+                       LDAP_LOG ( TOOLS, ERR, 
+                               "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#else
+                       Debug( LDAP_DEBUG_ANY,
+                               "=> bdb_tool_entry_put: %s\n",
+                               text->bv_val, 0, 0 );
+#endif
+                       e->e_id = NOID;
+               }
+
+       } else {
+               TXN_ABORT( tid );
+               snprintf( text->bv_val, text->bv_len,
+                       "txn_aborted! %s (%d)",
+                       db_strerror(rc), rc );
+#ifdef NEW_LOGGING
+               LDAP_LOG ( TOOLS, ERR, 
+                       "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
+#else
+               Debug( LDAP_DEBUG_ANY,
+                       "=> bdb_tool_entry_put: %s\n",
+                       text->bv_val, 0, 0 );
+#endif
+               e->e_id = NOID;
+       }
+
+       return e->e_id;
+}